Skip to content

Commit be972ed

Browse files
dryganetsandpor
authored andcommitted
I don't think it makes any sence to support pre-honeycomb code while react-native minimum API level is 16. It causes side effects for us in production (#147)
1 parent e558bcd commit be972ed

File tree

2 files changed

+46
-329
lines changed

2 files changed

+46
-329
lines changed

src/android-native/src/main/java/io/liteglue/SQLiteAndroidDatabase.java

Lines changed: 22 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import android.annotation.SuppressLint;
1111

1212
import android.database.Cursor;
13-
import android.database.CursorWindow;
14-
import android.database.sqlite.SQLiteCursor;
1513
import android.database.sqlite.SQLiteDatabase;
1614
import android.database.sqlite.SQLiteException;
1715
import android.database.sqlite.SQLiteStatement;
@@ -38,14 +36,6 @@ class SQLiteAndroidDatabase {
3836
private static final Pattern FIRST_WORD = Pattern.compile("^\\s*(\\S+)",
3937
Pattern.CASE_INSENSITIVE);
4038

41-
private static final Pattern WHERE_CLAUSE = Pattern.compile("\\s+WHERE\\s+(.+)$",
42-
Pattern.CASE_INSENSITIVE);
43-
44-
private static final Pattern UPDATE_TABLE_NAME = Pattern.compile("^\\s*UPDATE\\s+(\\S+)",
45-
Pattern.CASE_INSENSITIVE);
46-
47-
private static final Pattern DELETE_TABLE_NAME = Pattern.compile("^\\s*DELETE\\s+FROM\\s+(\\S+)",
48-
Pattern.CASE_INSENSITIVE);
4939

5040
File dbFile;
5141
int openFlags;
@@ -129,37 +119,29 @@ void executeSqlBatch(String[] queryarr, JSONArray[] jsonparams,
129119
QueryType queryType = getQueryType(query);
130120

131121
if (queryType == QueryType.update || queryType == QueryType.delete) {
132-
if (android.os.Build.VERSION.SDK_INT >= 11) {
133-
SQLiteStatement myStatement = mydb.compileStatement(query);
134122

135-
if (jsonparams != null) {
136-
bindArgsToStatement(myStatement, jsonparams[i]);
137-
}
123+
SQLiteStatement myStatement = mydb.compileStatement(query);
138124

139-
int rowsAffected = -1; // (assuming invalid)
140-
141-
// Use try & catch just in case android.os.Build.VERSION.SDK_INT >= 11 is lying:
142-
try {
143-
rowsAffected = myStatement.executeUpdateDelete();
144-
// Indicate valid results:
145-
needRawQuery = false;
146-
} catch (SQLiteException ex) {
147-
// Indicate problem & stop this query:
148-
errorMessage = ex.getMessage();
149-
FLog.e(SQLitePlugin.TAG, "SQLiteStatement.executeUpdateDelete() failed", ex);
150-
needRawQuery = false;
151-
} catch (Exception ex) {
152-
// Assuming SDK_INT was lying & method not found:
153-
// do nothing here & try again with raw query.
154-
}
125+
if (jsonparams != null) {
126+
bindArgsToStatement(myStatement, jsonparams[i]);
127+
}
155128

156-
if (rowsAffected != -1) {
157-
queryResult = new JSONObject();
158-
queryResult.put("rowsAffected", rowsAffected);
159-
}
160-
} else { // pre-honeycomb
161-
rowsAffectedCompat = countRowsAffectedCompat(queryType, query, jsonparams, mydb, i);
162-
needRowsAffectedCompat = true;
129+
int rowsAffected = -1; // (assuming invalid)
130+
131+
try {
132+
rowsAffected = myStatement.executeUpdateDelete();
133+
// Indicate valid results:
134+
needRawQuery = false;
135+
} catch (SQLiteException ex) {
136+
// Indicate problem & stop this query:
137+
errorMessage = ex.getMessage();
138+
FLog.e(SQLitePlugin.TAG, "SQLiteStatement.executeUpdateDelete() failed", ex);
139+
needRawQuery = false;
140+
}
141+
142+
if (rowsAffected != -1) {
143+
queryResult = new JSONObject();
144+
queryResult.put("rowsAffected", rowsAffected);
163145
}
164146
}
165147

@@ -273,81 +255,6 @@ void executeSqlBatch(String[] queryarr, JSONArray[] jsonparams,
273255
cbc.success(batchResults);
274256
}
275257

276-
private int countRowsAffectedCompat(QueryType queryType, String query, JSONArray[] jsonparams,
277-
SQLiteDatabase mydb, int i) throws JSONException {
278-
// quick and dirty way to calculate the rowsAffected in pre-Honeycomb. just do a SELECT
279-
// beforehand using the same WHERE clause. might not be perfect, but it's better than nothing
280-
Matcher whereMatcher = WHERE_CLAUSE.matcher(query);
281-
282-
String where = "";
283-
284-
int pos = 0;
285-
while (whereMatcher.find(pos)) {
286-
where = " WHERE " + whereMatcher.group(1);
287-
pos = whereMatcher.start(1);
288-
}
289-
// WHERE clause may be omitted, and also be sure to find the last one,
290-
// e.g. for cases where there's a subquery
291-
292-
// bindings may be in the update clause, so only take the last n
293-
int numQuestionMarks = 0;
294-
for (int j = 0; j < where.length(); j++) {
295-
if (where.charAt(j) == '?') {
296-
numQuestionMarks++;
297-
}
298-
}
299-
300-
JSONArray subParams = null;
301-
302-
if (jsonparams != null) {
303-
// only take the last n of every array of sqlArgs
304-
JSONArray origArray = jsonparams[i];
305-
subParams = new JSONArray();
306-
int startPos = origArray.length() - numQuestionMarks;
307-
for (int j = startPos; j < origArray.length(); j++) {
308-
subParams.put(j - startPos, origArray.get(j));
309-
}
310-
}
311-
312-
if (queryType == QueryType.update) {
313-
Matcher tableMatcher = UPDATE_TABLE_NAME.matcher(query);
314-
if (tableMatcher.find()) {
315-
String table = tableMatcher.group(1);
316-
try {
317-
SQLiteStatement statement = mydb.compileStatement(
318-
"SELECT count(*) FROM " + table + where);
319-
320-
if (subParams != null) {
321-
bindArgsToStatement(statement, subParams);
322-
}
323-
324-
return (int)statement.simpleQueryForLong();
325-
} catch (Exception e) {
326-
// assume we couldn't count for whatever reason, keep going
327-
FLog.e(SQLitePlugin.TAG, "update query failed", e);
328-
}
329-
}
330-
} else { // delete
331-
Matcher tableMatcher = DELETE_TABLE_NAME.matcher(query);
332-
if (tableMatcher.find()) {
333-
String table = tableMatcher.group(1);
334-
try {
335-
SQLiteStatement statement = mydb.compileStatement(
336-
"SELECT count(*) FROM " + table + where);
337-
bindArgsToStatement(statement, subParams);
338-
339-
return (int)statement.simpleQueryForLong();
340-
} catch (Exception e) {
341-
// assume we couldn't count for whatever reason, keep going
342-
FLog.e(SQLitePlugin.TAG, "delete table query failed", e);
343-
344-
}
345-
}
346-
}
347-
348-
return 0;
349-
}
350-
351258
private void bindArgsToStatement(SQLiteStatement myStatement, JSONArray sqlArgs) throws JSONException {
352259
for (int i = 0; i < sqlArgs.length(); i++) {
353260
if (sqlArgs.get(i) instanceof Float || sqlArgs.get(i) instanceof Double) {
@@ -403,18 +310,7 @@ private JSONObject executeSqlStatementQuery(SQLiteDatabase mydb,
403310
try {
404311
for (int i = 0; i < colCount; ++i) {
405312
key = cur.getColumnName(i);
406-
407-
if (android.os.Build.VERSION.SDK_INT >= 11) {
408-
409-
// Use try & catch just in case android.os.Build.VERSION.SDK_INT >= 11 is lying:
410-
try {
411-
bindPostHoneycomb(row, key, cur, i);
412-
} catch (Exception ex) {
413-
bindPreHoneycomb(row, key, cur, i);
414-
}
415-
} else {
416-
bindPreHoneycomb(row, key, cur, i);
417-
}
313+
bindRow(row, key, cur, i);
418314
}
419315

420316
rowsArrayResult.put(row);
@@ -439,7 +335,7 @@ private JSONObject executeSqlStatementQuery(SQLiteDatabase mydb,
439335
}
440336

441337
@SuppressLint("NewApi")
442-
private void bindPostHoneycomb(JSONObject row, String key, Cursor cur, int i) throws JSONException {
338+
private void bindRow(JSONObject row, String key, Cursor cur, int i) throws JSONException {
443339
int curType = cur.getType(i);
444340

445341
switch (curType) {
@@ -462,26 +358,6 @@ private void bindPostHoneycomb(JSONObject row, String key, Cursor cur, int i) th
462358
}
463359
}
464360

465-
private void bindPreHoneycomb(JSONObject row, String key, Cursor cursor, int i) throws JSONException {
466-
// Since cursor.getType() is not available pre-honeycomb, this is
467-
// a workaround so we don't have to bind everything as a string
468-
// Details here: http://stackoverflow.com/q/11658239
469-
SQLiteCursor sqLiteCursor = (SQLiteCursor) cursor;
470-
CursorWindow cursorWindow = sqLiteCursor.getWindow();
471-
int pos = cursor.getPosition();
472-
if (cursorWindow.isNull(pos, i)) {
473-
row.put(key, JSONObject.NULL);
474-
} else if (cursorWindow.isLong(pos, i)) {
475-
row.put(key, cursor.getLong(i));
476-
} else if (cursorWindow.isFloat(pos, i)) {
477-
row.put(key, cursor.getDouble(i));
478-
} else if (cursorWindow.isBlob(pos, i)) {
479-
row.put(key, new String(Base64.encode(cursor.getBlob(i), Base64.DEFAULT)));
480-
} else { // string
481-
row.put(key, cursor.getString(i));
482-
}
483-
}
484-
485361
static QueryType getQueryType(String query) {
486362
Matcher matcher = FIRST_WORD.matcher(query);
487363
if (matcher.find()) {

0 commit comments

Comments
 (0)