10
10
import android .annotation .SuppressLint ;
11
11
12
12
import android .database .Cursor ;
13
- import android .database .CursorWindow ;
14
- import android .database .sqlite .SQLiteCursor ;
15
13
import android .database .sqlite .SQLiteDatabase ;
16
14
import android .database .sqlite .SQLiteException ;
17
15
import android .database .sqlite .SQLiteStatement ;
@@ -38,14 +36,6 @@ class SQLiteAndroidDatabase {
38
36
private static final Pattern FIRST_WORD = Pattern .compile ("^\\ s*(\\ S+)" ,
39
37
Pattern .CASE_INSENSITIVE );
40
38
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 );
49
39
50
40
File dbFile ;
51
41
int openFlags ;
@@ -129,37 +119,29 @@ void executeSqlBatch(String[] queryarr, JSONArray[] jsonparams,
129
119
QueryType queryType = getQueryType (query );
130
120
131
121
if (queryType == QueryType .update || queryType == QueryType .delete ) {
132
- if (android .os .Build .VERSION .SDK_INT >= 11 ) {
133
- SQLiteStatement myStatement = mydb .compileStatement (query );
134
122
135
- if (jsonparams != null ) {
136
- bindArgsToStatement (myStatement , jsonparams [i ]);
137
- }
123
+ SQLiteStatement myStatement = mydb .compileStatement (query );
138
124
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
+ }
155
128
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 );
163
145
}
164
146
}
165
147
@@ -273,81 +255,6 @@ void executeSqlBatch(String[] queryarr, JSONArray[] jsonparams,
273
255
cbc .success (batchResults );
274
256
}
275
257
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
-
351
258
private void bindArgsToStatement (SQLiteStatement myStatement , JSONArray sqlArgs ) throws JSONException {
352
259
for (int i = 0 ; i < sqlArgs .length (); i ++) {
353
260
if (sqlArgs .get (i ) instanceof Float || sqlArgs .get (i ) instanceof Double ) {
@@ -403,18 +310,7 @@ private JSONObject executeSqlStatementQuery(SQLiteDatabase mydb,
403
310
try {
404
311
for (int i = 0 ; i < colCount ; ++i ) {
405
312
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 );
418
314
}
419
315
420
316
rowsArrayResult .put (row );
@@ -439,7 +335,7 @@ private JSONObject executeSqlStatementQuery(SQLiteDatabase mydb,
439
335
}
440
336
441
337
@ 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 {
443
339
int curType = cur .getType (i );
444
340
445
341
switch (curType ) {
@@ -462,26 +358,6 @@ private void bindPostHoneycomb(JSONObject row, String key, Cursor cur, int i) th
462
358
}
463
359
}
464
360
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
-
485
361
static QueryType getQueryType (String query ) {
486
362
Matcher matcher = FIRST_WORD .matcher (query );
487
363
if (matcher .find ()) {
0 commit comments