@capacitor-community/sqlite 5.0.5-2 → 5.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLite.java +118 -156
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLitePlugin.java +81 -249
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/NotificationCenter.java +1 -1
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/RetHandler.java +0 -12
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java +184 -40
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ExportToJson.java +141 -135
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ImportFromJson.java +2 -1
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/UtilsEncryption.java +111 -0
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsBiometric.java +0 -4
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsFile.java +30 -18
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsMigrate.java +12 -4
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsNCDatabase.java +4 -1
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsSQLCipher.java +8 -6
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsSQLite.java +14 -28
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsSecret.java +3 -4
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsUpgrade.java +0 -1
- package/dist/esm/definitions.d.ts +91 -4
- package/dist/esm/definitions.js +79 -19
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/web.d.ts +3 -1
- package/dist/esm/web.js +8 -0
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +87 -19
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +87 -19
- package/dist/plugin.js.map +1 -1
- package/electron/dist/plugin.js +333 -148
- package/electron/dist/plugin.js.map +1 -1
- package/electron/rollup.config.js +2 -0
- package/ios/Plugin/CapacitorSQLite.swift +125 -92
- package/ios/Plugin/CapacitorSQLitePlugin.swift +6 -3
- package/ios/Plugin/Database.swift +45 -19
- package/ios/Plugin/ImportExportJson/ExportToJson.swift +10 -5
- package/ios/Plugin/ImportExportJson/ImportData.swift +434 -0
- package/ios/Plugin/ImportExportJson/ImportFromJson.swift +47 -59
- package/ios/Plugin/ImportExportJson/JsonSQLite.swift +7 -0
- package/ios/Plugin/Utils/UtilsDownloadFromHTTP.swift +61 -61
- package/ios/Plugin/Utils/UtilsDrop.swift +2 -1
- package/ios/Plugin/Utils/UtilsJson.swift +123 -1
- package/ios/Plugin/Utils/UtilsSQLCipher.swift +254 -23
- package/ios/Plugin/Utils/UtilsUpgrade.swift +0 -1
- package/package.json +2 -2
- package/src/definitions.ts +171 -18
- package/src/web.ts +10 -0
package/android/src/main/java/com/getcapacitor/community/database/sqlite/NotificationCenter.java
CHANGED
|
@@ -26,7 +26,7 @@ public class NotificationCenter {
|
|
|
26
26
|
public synchronized void addMethodForNotification(String notificationName, MyRunnable r) {
|
|
27
27
|
ArrayList<MyRunnable> list = registredObjects.get(notificationName);
|
|
28
28
|
if (list == null) {
|
|
29
|
-
list = new ArrayList
|
|
29
|
+
list = new ArrayList<>();
|
|
30
30
|
registredObjects.put(notificationName, list);
|
|
31
31
|
}
|
|
32
32
|
list.add(r);
|
|
@@ -27,7 +27,6 @@ public class RetHandler {
|
|
|
27
27
|
if (res != null) {
|
|
28
28
|
ret.put("result", res);
|
|
29
29
|
call.resolve(ret);
|
|
30
|
-
return;
|
|
31
30
|
} else {
|
|
32
31
|
call.resolve();
|
|
33
32
|
}
|
|
@@ -51,7 +50,6 @@ public class RetHandler {
|
|
|
51
50
|
if (res != null) {
|
|
52
51
|
ret.put("version", res);
|
|
53
52
|
call.resolve(ret);
|
|
54
|
-
return;
|
|
55
53
|
} else {
|
|
56
54
|
call.resolve();
|
|
57
55
|
}
|
|
@@ -70,11 +68,9 @@ public class RetHandler {
|
|
|
70
68
|
ret.put("message", message);
|
|
71
69
|
Log.v(TAG, "*** ERROR " + message);
|
|
72
70
|
call.reject(message);
|
|
73
|
-
return;
|
|
74
71
|
} else {
|
|
75
72
|
ret.put("changes", res);
|
|
76
73
|
call.resolve(ret);
|
|
77
|
-
return;
|
|
78
74
|
}
|
|
79
75
|
}
|
|
80
76
|
|
|
@@ -91,11 +87,9 @@ public class RetHandler {
|
|
|
91
87
|
ret.put("message", message);
|
|
92
88
|
Log.v(TAG, "*** ERROR " + message);
|
|
93
89
|
call.reject(message);
|
|
94
|
-
return;
|
|
95
90
|
} else {
|
|
96
91
|
ret.put("values", res);
|
|
97
92
|
call.resolve(ret);
|
|
98
|
-
return;
|
|
99
93
|
}
|
|
100
94
|
}
|
|
101
95
|
|
|
@@ -112,11 +106,9 @@ public class RetHandler {
|
|
|
112
106
|
ret.put("message", message);
|
|
113
107
|
Log.v(TAG, "*** ERROR " + message);
|
|
114
108
|
call.reject(message);
|
|
115
|
-
return;
|
|
116
109
|
} else {
|
|
117
110
|
ret.put("syncDate", res);
|
|
118
111
|
call.resolve(ret);
|
|
119
|
-
return;
|
|
120
112
|
}
|
|
121
113
|
}
|
|
122
114
|
|
|
@@ -133,11 +125,9 @@ public class RetHandler {
|
|
|
133
125
|
ret.put("message", message);
|
|
134
126
|
Log.v(TAG, "*** ERROR " + message);
|
|
135
127
|
call.reject(message);
|
|
136
|
-
return;
|
|
137
128
|
} else {
|
|
138
129
|
ret.put("export", res);
|
|
139
130
|
call.resolve(ret);
|
|
140
|
-
return;
|
|
141
131
|
}
|
|
142
132
|
}
|
|
143
133
|
|
|
@@ -159,7 +149,6 @@ public class RetHandler {
|
|
|
159
149
|
if (res != null) {
|
|
160
150
|
ret.put("path", res);
|
|
161
151
|
call.resolve(ret);
|
|
162
|
-
return;
|
|
163
152
|
} else {
|
|
164
153
|
call.resolve();
|
|
165
154
|
}
|
|
@@ -183,7 +172,6 @@ public class RetHandler {
|
|
|
183
172
|
if (res != null) {
|
|
184
173
|
ret.put("url", res);
|
|
185
174
|
call.resolve(ret);
|
|
186
|
-
return;
|
|
187
175
|
} else {
|
|
188
176
|
call.resolve();
|
|
189
177
|
}
|
package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java
CHANGED
|
@@ -18,17 +18,18 @@ import com.getcapacitor.JSObject;
|
|
|
18
18
|
import com.getcapacitor.community.database.sqlite.SQLite.ImportExportJson.ExportToJson;
|
|
19
19
|
import com.getcapacitor.community.database.sqlite.SQLite.ImportExportJson.ImportFromJson;
|
|
20
20
|
import com.getcapacitor.community.database.sqlite.SQLite.ImportExportJson.JsonSQLite;
|
|
21
|
+
import com.getcapacitor.community.database.sqlite.SQLite.ImportExportJson.UtilsEncryption;
|
|
21
22
|
import com.getcapacitor.community.database.sqlite.SQLite.ImportExportJson.UtilsJson;
|
|
22
23
|
import java.io.File;
|
|
23
24
|
import java.text.SimpleDateFormat;
|
|
24
25
|
import java.util.ArrayList;
|
|
25
26
|
import java.util.Arrays;
|
|
26
|
-
import java.util.Base64;
|
|
27
|
-
import java.util.Collections;
|
|
28
27
|
import java.util.Date;
|
|
29
28
|
import java.util.Dictionary;
|
|
30
|
-
import java.util.Hashtable;
|
|
31
29
|
import java.util.List;
|
|
30
|
+
import java.util.Objects;
|
|
31
|
+
import java.util.regex.Matcher;
|
|
32
|
+
import java.util.regex.Pattern;
|
|
32
33
|
import net.sqlcipher.Cursor;
|
|
33
34
|
import net.sqlcipher.database.SQLiteDatabase;
|
|
34
35
|
import net.sqlcipher.database.SQLiteException;
|
|
@@ -43,14 +44,11 @@ public class Database {
|
|
|
43
44
|
private final String _dbName;
|
|
44
45
|
private final Context _context;
|
|
45
46
|
private final String _mode;
|
|
46
|
-
private String _secret;
|
|
47
47
|
private final Boolean _encrypted;
|
|
48
48
|
private final Boolean _isEncryption;
|
|
49
|
-
private final SharedPreferences _sharedPreferences;
|
|
50
49
|
private final Boolean _readOnly;
|
|
51
50
|
private final File _file;
|
|
52
51
|
private final int _version;
|
|
53
|
-
private final GlobalSQLite _globVar;
|
|
54
52
|
private SupportSQLiteDatabase _db = null;
|
|
55
53
|
private final UtilsSQLite _uSqlite;
|
|
56
54
|
private final UtilsSQLCipher _uCipher;
|
|
@@ -59,7 +57,7 @@ public class Database {
|
|
|
59
57
|
private final UtilsUpgrade _uUpg;
|
|
60
58
|
private final UtilsDrop _uDrop;
|
|
61
59
|
private final UtilsSecret _uSecret;
|
|
62
|
-
private Dictionary<Integer, JSONObject> _vUpgObject
|
|
60
|
+
private final Dictionary<Integer, JSONObject> _vUpgObject;
|
|
63
61
|
private final ImportFromJson fromJson = new ImportFromJson();
|
|
64
62
|
private final ExportToJson toJson = new ExportToJson();
|
|
65
63
|
private Boolean ncDB = false;
|
|
@@ -82,7 +80,6 @@ public class Database {
|
|
|
82
80
|
this._isEncryption = isEncryption;
|
|
83
81
|
this._version = version;
|
|
84
82
|
this._vUpgObject = vUpgObject;
|
|
85
|
-
this._sharedPreferences = sharedPreferences;
|
|
86
83
|
this._readOnly = readonly;
|
|
87
84
|
if (dbName.contains("/") && dbName.endsWith("SQLite.db")) {
|
|
88
85
|
this.ncDB = true;
|
|
@@ -90,7 +87,6 @@ public class Database {
|
|
|
90
87
|
} else {
|
|
91
88
|
this._file = this._context.getDatabasePath(dbName);
|
|
92
89
|
}
|
|
93
|
-
this._globVar = new GlobalSQLite();
|
|
94
90
|
this._uSqlite = new UtilsSQLite();
|
|
95
91
|
this._uCipher = new UtilsSQLCipher();
|
|
96
92
|
this._uFile = new UtilsFile();
|
|
@@ -99,8 +95,11 @@ public class Database {
|
|
|
99
95
|
this._uDrop = new UtilsDrop();
|
|
100
96
|
this._uSecret = isEncryption ? new UtilsSecret(context, sharedPreferences) : null;
|
|
101
97
|
InitializeSQLCipher();
|
|
102
|
-
if (!this._file.getParentFile().exists()) {
|
|
103
|
-
this._file.getParentFile().mkdirs();
|
|
98
|
+
if (!Objects.requireNonNull(this._file.getParentFile()).exists()) {
|
|
99
|
+
boolean dirCreated = this._file.getParentFile().mkdirs();
|
|
100
|
+
if (!dirCreated) {
|
|
101
|
+
System.out.println("Failed to create parent directories.");
|
|
102
|
+
}
|
|
104
103
|
}
|
|
105
104
|
Log.v(TAG, "&&& file path " + this._file.getAbsolutePath());
|
|
106
105
|
}
|
|
@@ -350,14 +349,14 @@ public class Database {
|
|
|
350
349
|
* @param set JSArray of statements
|
|
351
350
|
* @return
|
|
352
351
|
*/
|
|
353
|
-
public JSObject executeSet(JSArray set, Boolean
|
|
354
|
-
Boolean transaction = others.length == 1 ? others[0] : true;
|
|
352
|
+
public JSObject executeSet(JSArray set, Boolean transaction, String returnMode) throws Exception {
|
|
355
353
|
JSObject retObj = new JSObject();
|
|
356
|
-
Long lastId =
|
|
357
|
-
Integer changes =
|
|
354
|
+
Long lastId = (long) -1;
|
|
355
|
+
Integer changes = -1;
|
|
356
|
+
JSObject response = new JSObject();
|
|
358
357
|
try {
|
|
359
358
|
if (_db != null && _db.isOpen()) {
|
|
360
|
-
|
|
359
|
+
int initChanges = _uSqlite.dbChanges(_db);
|
|
361
360
|
if (transaction) _db.beginTransaction();
|
|
362
361
|
for (int i = 0; i < set.length(); i++) {
|
|
363
362
|
JSONObject row = set.getJSONObject(i);
|
|
@@ -375,13 +374,17 @@ public class Database {
|
|
|
375
374
|
for (int k = 0; k < valsJson.length(); k++) {
|
|
376
375
|
vals.add(valsJson.get(k));
|
|
377
376
|
}
|
|
378
|
-
|
|
377
|
+
JSObject respSet = prepareSQL(statement, vals, false, returnMode);
|
|
378
|
+
lastId = respSet.getLong("lastId");
|
|
379
379
|
if (lastId == -1) break;
|
|
380
|
+
response = addToResponse(response, respSet);
|
|
380
381
|
}
|
|
381
382
|
} else {
|
|
382
|
-
|
|
383
|
+
JSObject respSet = prepareSQL(statement, values, false, returnMode);
|
|
384
|
+
lastId = respSet.getLong("lastId");
|
|
385
|
+
if (lastId == -1) break;
|
|
386
|
+
response = addToResponse(response, respSet);
|
|
383
387
|
}
|
|
384
|
-
if (lastId == -1) break;
|
|
385
388
|
}
|
|
386
389
|
changes = _uSqlite.dbChanges(_db) - initChanges;
|
|
387
390
|
if (changes >= 0) {
|
|
@@ -389,6 +392,7 @@ public class Database {
|
|
|
389
392
|
changes = _uSqlite.dbChanges(_db) - initChanges;
|
|
390
393
|
retObj.put("changes", changes);
|
|
391
394
|
retObj.put("lastId", lastId);
|
|
395
|
+
retObj.put("values", response.getJSONArray("values"));
|
|
392
396
|
return retObj;
|
|
393
397
|
} else {
|
|
394
398
|
throw new Exception("lastId equals -1");
|
|
@@ -403,6 +407,27 @@ public class Database {
|
|
|
403
407
|
}
|
|
404
408
|
}
|
|
405
409
|
|
|
410
|
+
public JSObject addToResponse(JSObject response, JSObject respSet) throws JSONException {
|
|
411
|
+
JSObject retResp = response;
|
|
412
|
+
long lastId = respSet.getLong("lastId");
|
|
413
|
+
JSONArray respVals = respSet.getJSONArray("values");
|
|
414
|
+
if (retResp.keys().hasNext()) {
|
|
415
|
+
JSONArray retVals = respSet.getJSONArray("values");
|
|
416
|
+
respVals = response.getJSONArray("values");
|
|
417
|
+
mergeJSONArrays(respVals, retVals);
|
|
418
|
+
}
|
|
419
|
+
retResp.put("lastId", lastId);
|
|
420
|
+
retResp.put("values", respVals);
|
|
421
|
+
return retResp;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
public static void mergeJSONArrays(JSONArray target, JSONArray source) throws JSONException {
|
|
425
|
+
for (int i = 0; i < source.length(); i++) {
|
|
426
|
+
Object element = source.get(i);
|
|
427
|
+
target.put(element);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
406
431
|
/**
|
|
407
432
|
* InTransaction Method
|
|
408
433
|
* Check if a transaction is still running
|
|
@@ -420,20 +445,21 @@ public class Database {
|
|
|
420
445
|
* @param values Array of Strings to bind to the statement
|
|
421
446
|
* @return
|
|
422
447
|
*/
|
|
423
|
-
public JSObject runSQL(String statement, ArrayList<Object> values, Boolean
|
|
424
|
-
Boolean transaction = others.length == 1 ? others[0] : true;
|
|
448
|
+
public JSObject runSQL(String statement, ArrayList<Object> values, Boolean transaction, String returnMode) throws Exception {
|
|
425
449
|
JSObject retObj = new JSObject();
|
|
426
|
-
long lastId =
|
|
427
|
-
int changes =
|
|
450
|
+
long lastId = (long) -1;
|
|
451
|
+
int changes = -1;
|
|
428
452
|
try {
|
|
429
453
|
if (_db != null && _db.isOpen() && statement.length() > 0) {
|
|
430
|
-
|
|
454
|
+
int initChanges = _uSqlite.dbChanges(_db);
|
|
431
455
|
if (transaction) _db.beginTransaction();
|
|
432
|
-
|
|
456
|
+
JSObject response = prepareSQL(statement, values, false, returnMode);
|
|
457
|
+
lastId = response.getLong("lastId");
|
|
433
458
|
if (lastId != -1 && transaction) _db.setTransactionSuccessful();
|
|
434
459
|
changes = _uSqlite.dbChanges(_db) - initChanges;
|
|
435
460
|
retObj.put("changes", changes);
|
|
436
461
|
retObj.put("lastId", lastId);
|
|
462
|
+
retObj.put("values", response.getJSONArray("values"));
|
|
437
463
|
return retObj;
|
|
438
464
|
} else {
|
|
439
465
|
throw new Exception("Database not opened");
|
|
@@ -448,26 +474,49 @@ public class Database {
|
|
|
448
474
|
/**
|
|
449
475
|
* PrepareSQL Method
|
|
450
476
|
*
|
|
451
|
-
* @param statement
|
|
452
|
-
* @param values
|
|
453
|
-
* @
|
|
477
|
+
* @param statement SQL statement
|
|
478
|
+
* @param values SQL Values if any
|
|
479
|
+
* @param fromJson is the statement from importFromJson
|
|
480
|
+
* @param returnMode return mode to handle RETURNING
|
|
481
|
+
* @return JSObject
|
|
482
|
+
* @throws Exception message
|
|
454
483
|
*/
|
|
455
|
-
public
|
|
484
|
+
public JSObject prepareSQL(String statement, ArrayList<Object> values, Boolean fromJson, String returnMode) throws Exception {
|
|
456
485
|
String stmtType = statement.replaceAll("\n", "").trim().substring(0, 6).toUpperCase();
|
|
457
486
|
SupportSQLiteStatement stmt = null;
|
|
458
487
|
String sqlStmt = statement;
|
|
488
|
+
String retMode = returnMode;
|
|
489
|
+
JSArray retValues = new JSArray();
|
|
490
|
+
JSObject retObject = new JSObject();
|
|
491
|
+
String colNames = "";
|
|
492
|
+
long initLastId = (long) -1;
|
|
493
|
+
boolean isReturning = sqlStmt.toUpperCase().contains("RETURNING");
|
|
459
494
|
try {
|
|
460
495
|
if (!fromJson && stmtType.equals("DELETE")) {
|
|
461
496
|
sqlStmt = deleteSQL(this, statement, values);
|
|
462
497
|
}
|
|
463
|
-
|
|
498
|
+
if (isReturning && Build.VERSION.SDK_INT > Build.VERSION_CODES.TIRAMISU) {
|
|
499
|
+
throw new Exception("Not implemented for above TIRAMISU");
|
|
500
|
+
}
|
|
501
|
+
if (isReturning) {
|
|
502
|
+
// get the lastID
|
|
503
|
+
initLastId = _uSqlite.dbLastId(_db);
|
|
504
|
+
// get the statement and the returning column names
|
|
505
|
+
JSObject stmtObj = getStmtAndRetColNames(sqlStmt);
|
|
506
|
+
sqlStmt = stmtObj.getString("stmt");
|
|
507
|
+
colNames = stmtObj.getString("names");
|
|
508
|
+
}
|
|
509
|
+
if (sqlStmt != null) {
|
|
510
|
+
stmt = _db.compileStatement(sqlStmt);
|
|
511
|
+
} else {
|
|
512
|
+
throw new Exception("sqlStmt is null");
|
|
513
|
+
}
|
|
464
514
|
if (values != null && values.size() > 0) {
|
|
515
|
+
// retMode = "no";
|
|
465
516
|
Object[] valObj = new Object[values.size()];
|
|
466
517
|
for (int i = 0; i < values.size(); i++) {
|
|
467
518
|
if (values.get(i) == null) {
|
|
468
519
|
valObj[i] = null;
|
|
469
|
-
// } else if (values.get(i).equals("NULL")) {
|
|
470
|
-
// valObj[i] = null;
|
|
471
520
|
} else if (JSONObject.NULL == values.get(i)) {
|
|
472
521
|
valObj[i] = null;
|
|
473
522
|
} else {
|
|
@@ -479,13 +528,28 @@ public class Database {
|
|
|
479
528
|
if (stmtType.equals("INSERT")) {
|
|
480
529
|
stmt.executeInsert();
|
|
481
530
|
} else {
|
|
531
|
+
if (isReturning && stmtType.equals("DELETE")) {
|
|
532
|
+
isReturning = false;
|
|
533
|
+
retValues = getUpdDelReturnedValues(this, sqlStmt, colNames);
|
|
534
|
+
}
|
|
482
535
|
stmt.executeUpdateDelete();
|
|
483
536
|
}
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
537
|
+
Long lastId = _uSqlite.dbLastId(_db);
|
|
538
|
+
if (isReturning) {
|
|
539
|
+
if (stmtType.equals("INSERT")) {
|
|
540
|
+
String tableName = extractTableName(sqlStmt);
|
|
541
|
+
if (tableName != null) {
|
|
542
|
+
if (retMode.equals("one") || retMode.equals("all")) {
|
|
543
|
+
retValues = getInsertReturnedValues(this, colNames, tableName, initLastId, lastId, retMode);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
} else if (stmtType.equals("UPDATE")) {
|
|
547
|
+
retValues = getUpdDelReturnedValues(this, sqlStmt, colNames);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
retObject.put("lastId", lastId);
|
|
551
|
+
retObject.put("values", retValues);
|
|
552
|
+
return retObject;
|
|
489
553
|
} catch (Exception e) {
|
|
490
554
|
throw new Exception(e.getMessage());
|
|
491
555
|
} finally {
|
|
@@ -495,6 +559,76 @@ public class Database {
|
|
|
495
559
|
}
|
|
496
560
|
}
|
|
497
561
|
|
|
562
|
+
private JSObject getStmtAndRetColNames(String sqlStmt) {
|
|
563
|
+
JSObject retObj = new JSObject();
|
|
564
|
+
int idx = sqlStmt.toUpperCase().indexOf("RETURNING");
|
|
565
|
+
String retStmt = sqlStmt.substring(0, idx - 1) + ";";
|
|
566
|
+
String names = sqlStmt.substring(idx + 9);
|
|
567
|
+
String retNames = names;
|
|
568
|
+
if (names.contains(";")) retNames = names.substring(0, names.length() - 1).trim();
|
|
569
|
+
retObj.put("stmt", retStmt);
|
|
570
|
+
retObj.put("names", retNames);
|
|
571
|
+
return retObj;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
private JSArray getInsertReturnedValues(Database mDB, String colNames, String tableName, Long iLastId, Long lastId, String rMode)
|
|
575
|
+
throws Exception {
|
|
576
|
+
JSArray retVals = new JSArray();
|
|
577
|
+
if (iLastId < 0 || colNames.length() == 0) return retVals;
|
|
578
|
+
Long sLastId = iLastId + 1;
|
|
579
|
+
StringBuilder sbQuery = new StringBuilder("SELECT ").append(colNames).append(" FROM ");
|
|
580
|
+
|
|
581
|
+
sbQuery.append(tableName).append(" WHERE ").append("rowid ");
|
|
582
|
+
if (rMode.equals("one")) {
|
|
583
|
+
sbQuery.append("= ").append(sLastId);
|
|
584
|
+
}
|
|
585
|
+
if (rMode.equals("all")) {
|
|
586
|
+
sbQuery.append("BETWEEN ").append(sLastId).append(" AND ").append(lastId);
|
|
587
|
+
}
|
|
588
|
+
sbQuery.append(";");
|
|
589
|
+
retVals = mDB.selectSQL(sbQuery.toString(), new ArrayList<>());
|
|
590
|
+
|
|
591
|
+
return retVals;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
private JSArray getUpdDelReturnedValues(Database mDB, String stmt, String colNames) throws Exception {
|
|
595
|
+
JSArray retVals = new JSArray();
|
|
596
|
+
String tableName = extractTableName(stmt);
|
|
597
|
+
String whereClause = extractWhereClause(stmt);
|
|
598
|
+
if (whereClause != null && tableName != null) {
|
|
599
|
+
StringBuilder sbQuery = new StringBuilder("SELECT ").append(colNames).append(" FROM ");
|
|
600
|
+
sbQuery.append(tableName).append(" WHERE ").append(whereClause).append(";");
|
|
601
|
+
retVals = mDB.selectSQL(sbQuery.toString(), new ArrayList<>());
|
|
602
|
+
}
|
|
603
|
+
return retVals;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
private static String extractTableName(String statement) {
|
|
607
|
+
Pattern pattern = Pattern.compile("(?i)(?:INSERT\\s+INTO|UPDATE|DELETE\\s+FROM)\\s+(\\w+)");
|
|
608
|
+
Matcher matcher = pattern.matcher(statement);
|
|
609
|
+
if (matcher.find()) {
|
|
610
|
+
return matcher.group(1);
|
|
611
|
+
}
|
|
612
|
+
return null;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
private static String extractWhereClause(String sqlStatement) {
|
|
616
|
+
// Regular expression pattern to match the WHERE clause and removing the
|
|
617
|
+
// ORDER BY and LIMIT if any
|
|
618
|
+
Pattern pattern = Pattern.compile("(?i)\\bWHERE\\b\\s*(.*?)(?:\\s*\\b(?:ORDER\\s+BY|LIMIT)\\b|$)");
|
|
619
|
+
Matcher matcher = pattern.matcher(sqlStatement);
|
|
620
|
+
|
|
621
|
+
if (matcher.find()) {
|
|
622
|
+
String whereClause = matcher.group(1);
|
|
623
|
+
if (whereClause != null && whereClause.endsWith(";")) {
|
|
624
|
+
whereClause = whereClause.substring(0, whereClause.length() - 1);
|
|
625
|
+
}
|
|
626
|
+
return whereClause.trim();
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
return null;
|
|
630
|
+
}
|
|
631
|
+
|
|
498
632
|
/**
|
|
499
633
|
* DeleteSQL method
|
|
500
634
|
*
|
|
@@ -575,10 +709,11 @@ public class Database {
|
|
|
575
709
|
ArrayList<Object> selValues = new ArrayList<Object>();
|
|
576
710
|
if (values != null && values.size() > 0) {
|
|
577
711
|
String[] arrVal = whereStmt.split("\\?");
|
|
712
|
+
String[] modArr = arrVal;
|
|
578
713
|
if (arrVal[arrVal.length - 1].equals(";")) {
|
|
579
|
-
Arrays.copyOf(arrVal, arrVal.length - 1);
|
|
714
|
+
modArr = Arrays.copyOf(arrVal, arrVal.length - 1);
|
|
580
715
|
}
|
|
581
|
-
for (int j = 0; j <
|
|
716
|
+
for (int j = 0; j < modArr.length; j++) {
|
|
582
717
|
for (String updVal : updColNames) {
|
|
583
718
|
int idxVal = arrVal[j].indexOf(updVal);
|
|
584
719
|
if (idxVal > -1) {
|
|
@@ -588,7 +723,8 @@ public class Database {
|
|
|
588
723
|
}
|
|
589
724
|
}
|
|
590
725
|
|
|
591
|
-
|
|
726
|
+
JSObject retObj = prepareSQL(stmt, selValues, false, "no");
|
|
727
|
+
long lastId = retObj.getLong("lastId");
|
|
592
728
|
if (lastId == -1) {
|
|
593
729
|
String msg = "UPDATE sql_deleted failed for references " + "table: " + refTable + ";";
|
|
594
730
|
throw new Exception(msg);
|
|
@@ -1032,6 +1168,14 @@ public class Database {
|
|
|
1032
1168
|
}
|
|
1033
1169
|
}
|
|
1034
1170
|
}
|
|
1171
|
+
if (this._encrypted && this._isEncryption) {
|
|
1172
|
+
retObj.put("encrypted", true);
|
|
1173
|
+
retObj.put("overwrite", true);
|
|
1174
|
+
String base64Str = UtilsEncryption.encryptJSONObject(this._context, retObj);
|
|
1175
|
+
retObj = new JSObject();
|
|
1176
|
+
retObj.put("expData", base64Str);
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1035
1179
|
return retObj;
|
|
1036
1180
|
} catch (Exception e) {
|
|
1037
1181
|
Log.e(TAG, "Error: exportToJson " + e.getMessage());
|