@capacitor-community/sqlite 5.0.7-1 → 5.0.7

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.
Files changed (33) hide show
  1. package/README.md +6 -1
  2. package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLite.java +123 -1
  3. package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLitePlugin.java +112 -0
  4. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java +140 -78
  5. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ImportFromJson.java +9 -9
  6. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsDelete.java +484 -0
  7. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsDrop.java +3 -3
  8. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsSQLStatement.java +169 -0
  9. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsUpgrade.java +4 -4
  10. package/dist/esm/definitions.d.ts +96 -11
  11. package/dist/esm/definitions.js +99 -50
  12. package/dist/esm/definitions.js.map +1 -1
  13. package/dist/esm/web.d.ts +4 -0
  14. package/dist/esm/web.js +44 -0
  15. package/dist/esm/web.js.map +1 -1
  16. package/dist/plugin.cjs.js +143 -50
  17. package/dist/plugin.cjs.js.map +1 -1
  18. package/dist/plugin.js +143 -50
  19. package/dist/plugin.js.map +1 -1
  20. package/electron/dist/plugin.js +1102 -260
  21. package/electron/dist/plugin.js.map +1 -1
  22. package/ios/Plugin/CapacitorSQLite.swift +119 -0
  23. package/ios/Plugin/CapacitorSQLitePlugin.m +4 -0
  24. package/ios/Plugin/CapacitorSQLitePlugin.swift +128 -0
  25. package/ios/Plugin/Database.swift +76 -0
  26. package/ios/Plugin/ImportExportJson/ImportFromJson.swift +13 -2
  27. package/ios/Plugin/Utils/UtilsDelete.swift +116 -114
  28. package/ios/Plugin/Utils/UtilsSQLCipher.swift +10 -3
  29. package/ios/Plugin/Utils/UtilsSQLStatement.swift +84 -84
  30. package/ios/Plugin/Utils/UtilsUpgrade.swift +3 -0
  31. package/package.json +2 -2
  32. package/src/definitions.ts +187 -53
  33. package/src/web.ts +48 -0
package/README.md CHANGED
@@ -18,7 +18,7 @@
18
18
  <a href="https://www.npmjs.com/package/@capacitor-community/sqlite"><img src="https://img.shields.io/npm/dw/@capacitor-community/sqlite?style=flat-square" /></a>
19
19
  <a href="https://www.npmjs.com/package/@capacitor-community/sqlite"><img src="https://img.shields.io/npm/v/@capacitor-community/sqlite?style=flat-square" /></a>
20
20
  <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
21
- <a href="#contributors-"><img src="https://img.shields.io/badge/all%20contributors-36-orange?style=flat-square" /></a>
21
+ <a href="#contributors-"><img src="https://img.shields.io/badge/all%20contributors-37-orange?style=flat-square" /></a>
22
22
  <!-- ALL-CONTRIBUTORS-BADGE:END -->
23
23
  </p>
24
24
 
@@ -242,6 +242,10 @@ npm install --save-dev @types/crypto-js
242
242
  | isInConfigBiometricAuth | ✅ | ✅ | ❌ | ❌ | since 4.6.2-2
243
243
  | getFromLocalDiskToStore | ❌ | ❌ | ❌ | ✅ | since 4.6.3
244
244
  | saveToLocalDisk | ❌ | ❌ | ❌ | ✅ | since 4.6.3
245
+ | beginTransaction | ✅ | ✅ | ✅ | ✅ | since 5.0.7
246
+ | commitTransaction | ✅ | ✅ | ✅ | ✅ | since 5.0.7
247
+ | rollbackTransaction | ✅ | ✅ | ✅ | ✅ | since 5.0.7
248
+ | isTransactionActive | ✅ | ✅ | ✅ | ✅ | since 5.0.7
245
249
 
246
250
 
247
251
  ## Documentation & APIs
@@ -374,6 +378,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
374
378
  <a href="https://github.com/pranav-singhal" title="pranav-singhal"><img src="https://github.com/pranav-singhal.png?size=100" width="50" height="50" /></a>
375
379
  <a href="https://github.com/beligatclement" title="beligatclement"><img src="https://github.com/beligatclement.png?size=100" width="50" height="50" /></a>
376
380
  <a href="https://github.com/cosentino" title="cosentino"><img src="https://avatars.githubusercontent.com/u/376903?s=48&v=4" width="50" height="50" /></a>
381
+ <a href="https://github.com/Guiqft" title="cosentino"><img src="https://avatars.githubusercontent.com/u/9392803?v=4" width="50" height="50" /></a>
377
382
  </p>
378
383
 
379
384
 
@@ -451,6 +451,128 @@ public class CapacitorSQLite {
451
451
  throw new Exception(msg);
452
452
  }
453
453
  }
454
+ /**
455
+ * BeginTransaction
456
+ *
457
+ * @param dbName Database name
458
+ * @return JSObject changes
459
+ * @throws Exception message
460
+ */
461
+ public JSObject beginTransaction(String dbName) throws Exception {
462
+ JSObject retObj = new JSObject();
463
+ dbName = getDatabaseName(dbName);
464
+ String connName = "RW_" + dbName;
465
+ Database db = dbDict.get(connName);
466
+ if (db != null) {
467
+ if (!db.isNCDB() && db.isOpen()) {
468
+ try {
469
+ Integer res = db.beginTransaction();
470
+ retObj.put("changes", res);
471
+ return retObj;
472
+ } catch (Exception e) {
473
+ throw new Exception(e.getMessage());
474
+ }
475
+ } else {
476
+ String msg = "database " + dbName + " not opened";
477
+ throw new Exception(msg);
478
+ }
479
+ } else {
480
+ String msg = "No available connection for database " + dbName;
481
+ throw new Exception(msg);
482
+ }
483
+ }
484
+
485
+ /**
486
+ * CommitTransaction
487
+ *
488
+ * @param dbName Database name
489
+ * @return JSObject changes
490
+ * @throws Exception message
491
+ */
492
+ public JSObject commitTransaction(String dbName) throws Exception {
493
+ JSObject retObj = new JSObject();
494
+ dbName = getDatabaseName(dbName);
495
+ String connName = "RW_" + dbName;
496
+ Database db = dbDict.get(connName);
497
+ if (db != null) {
498
+ if (!db.isNCDB() && db.isOpen()) {
499
+ try {
500
+ Integer res = db.commitTransaction();
501
+ retObj.put("changes", res);
502
+ return retObj;
503
+ } catch (Exception e) {
504
+ throw new Exception(e.getMessage());
505
+ }
506
+ } else {
507
+ String msg = "database " + dbName + " not opened";
508
+ throw new Exception(msg);
509
+ }
510
+ } else {
511
+ String msg = "No available connection for database " + dbName;
512
+ throw new Exception(msg);
513
+ }
514
+ }
515
+
516
+ /**
517
+ * Rollback Transaction
518
+ *
519
+ * @param dbName Database name
520
+ * @return JSObject changes
521
+ * @throws Exception message
522
+ */
523
+ public JSObject rollbackTransaction(String dbName) throws Exception {
524
+ JSObject retObj = new JSObject();
525
+ dbName = getDatabaseName(dbName);
526
+ String connName = "RW_" + dbName;
527
+ Database db = dbDict.get(connName);
528
+ if (db != null) {
529
+ if (!db.isNCDB() && db.isOpen()) {
530
+ try {
531
+ Integer res = db.rollbackTransaction();
532
+ retObj.put("changes", res);
533
+ return retObj;
534
+ } catch (Exception e) {
535
+ throw new Exception(e.getMessage());
536
+ }
537
+ } else {
538
+ String msg = "database " + dbName + " not opened";
539
+ throw new Exception(msg);
540
+ }
541
+ } else {
542
+ String msg = "No available connection for database " + dbName;
543
+ throw new Exception(msg);
544
+ }
545
+ }
546
+
547
+ /**
548
+ * IsTransactionActive
549
+ *
550
+ * @param dbName database name
551
+ * @return Boolean
552
+ * @throws Exception message
553
+ */
554
+ public Boolean isTransactionActive(String dbName) throws Exception {
555
+ dbName = getDatabaseName(dbName);
556
+ String connName = "RW_" + dbName;
557
+ Database db = dbDict.get(connName);
558
+ if (db != null) {
559
+ if (!db.isNCDB() && db.isOpen()) {
560
+ try {
561
+ boolean res = db.isAvailTrans();
562
+ return res;
563
+ } catch (Exception e) {
564
+ throw new Exception(e.getMessage());
565
+ }
566
+
567
+ } else {
568
+ String msg = "database " + dbName + " not opened";
569
+ throw new Exception(msg);
570
+ }
571
+ } else {
572
+ String msg = "No available connection for database " + dbName;
573
+ throw new Exception(msg);
574
+ }
575
+ }
454
576
 
455
577
  /**
456
578
  * GetUrl
@@ -846,7 +968,7 @@ public class CapacitorSQLite {
846
968
  }
847
969
  } else {
848
970
  try {
849
- res = db.runSQL(statement, null, transaction, returnMode);
971
+ res = db.runSQL(statement, new ArrayList<>(), transaction, returnMode);
850
972
  return res;
851
973
  } catch (Exception e) {
852
974
  throw new Exception(e.getMessage());
@@ -369,7 +369,119 @@ public class CapacitorSQLitePlugin extends Plugin {
369
369
  rHandler.retResult(call, null, loadMessage);
370
370
  }
371
371
  }
372
+ /**
373
+ * BeginTransaction Method
374
+ * Begin a Database Transaction
375
+ *
376
+ * @param call PluginCall
377
+ */
378
+ @PluginMethod
379
+ public void beginTransaction(PluginCall call) {
380
+ JSObject retRes = new JSObject();
381
+ retRes.put("changes", Integer.valueOf(-1));
382
+ if (!call.getData().has("database")) {
383
+ String msg = "BeginTransaction: Must provide a database name";
384
+ rHandler.retChanges(call, retRes, msg);
385
+ return;
386
+ }
387
+ String dbName = call.getString("database");
372
388
 
389
+ if (implementation != null) {
390
+ try {
391
+ JSObject res = implementation.beginTransaction(dbName);
392
+ rHandler.retChanges(call, res, null);
393
+ } catch (Exception e) {
394
+ String msg = "BeginTransaction: " + e.getMessage();
395
+ rHandler.retChanges(call, retRes, msg);
396
+ }
397
+ } else {
398
+ rHandler.retChanges(call, retRes, loadMessage);
399
+ }
400
+ }
401
+ /**
402
+ * CommitTransaction Method
403
+ * Commit a Database Transaction
404
+ *
405
+ * @param call PluginCall
406
+ */
407
+ @PluginMethod
408
+ public void commitTransaction(PluginCall call) {
409
+ JSObject retRes = new JSObject();
410
+ retRes.put("changes", Integer.valueOf(-1));
411
+ if (!call.getData().has("database")) {
412
+ String msg = "CommitTransaction: Must provide a database name";
413
+ rHandler.retChanges(call, retRes, msg);
414
+ return;
415
+ }
416
+ String dbName = call.getString("database");
417
+
418
+ if (implementation != null) {
419
+ try {
420
+ JSObject res = implementation.commitTransaction(dbName);
421
+ rHandler.retChanges(call, res, null);
422
+ } catch (Exception e) {
423
+ String msg = "CommitTransaction: " + e.getMessage();
424
+ rHandler.retChanges(call, retRes, msg);
425
+ }
426
+ } else {
427
+ rHandler.retChanges(call, retRes, loadMessage);
428
+ }
429
+ }
430
+
431
+ /**
432
+ * RollbackTransaction Method
433
+ * Rollbact a Database Transaction
434
+ *
435
+ * @param call PluginCall
436
+ */
437
+ @PluginMethod
438
+ public void rollbackTransaction(PluginCall call) {
439
+ JSObject retRes = new JSObject();
440
+ retRes.put("changes", Integer.valueOf(-1));
441
+ if (!call.getData().has("database")) {
442
+ String msg = "RollbackTransaction: Must provide a database name";
443
+ rHandler.retChanges(call, retRes, msg);
444
+ return;
445
+ }
446
+ String dbName = call.getString("database");
447
+
448
+ if (implementation != null) {
449
+ try {
450
+ JSObject res = implementation.rollbackTransaction(dbName);
451
+ rHandler.retChanges(call, res, null);
452
+ } catch (Exception e) {
453
+ String msg = "RollbackTransaction: " + e.getMessage();
454
+ rHandler.retChanges(call, retRes, msg);
455
+ }
456
+ } else {
457
+ rHandler.retChanges(call, retRes, loadMessage);
458
+ }
459
+ }
460
+ /**
461
+ * IsTransactionActive Method
462
+ * Check if a Database Transaction is Active
463
+ *
464
+ * @param call PluginCall
465
+ */
466
+ @PluginMethod
467
+ public void isTransactionActive(PluginCall call) {
468
+ if (!call.getData().has("database")) {
469
+ rHandler.retResult(call, null, "Must provide a database name");
470
+ return;
471
+ }
472
+ String dbName = call.getString("database");
473
+ if (implementation != null) {
474
+ try {
475
+ Boolean res = implementation.isTransactionActive(dbName);
476
+ rHandler.retResult(call, res, null);
477
+ } catch (Exception e) {
478
+ String msg = "IsTransactionActive: " + e.getMessage();
479
+ rHandler.retResult(call, null, msg);
480
+ }
481
+ } else {
482
+ rHandler.retResult(call, null, loadMessage);
483
+ }
484
+ }
373
485
  /**
374
486
  * GetUrl Method
375
487
  * Get a database Url
@@ -5,6 +5,10 @@ import static android.database.Cursor.FIELD_TYPE_FLOAT;
5
5
  import static android.database.Cursor.FIELD_TYPE_INTEGER;
6
6
  import static android.database.Cursor.FIELD_TYPE_NULL;
7
7
  import static android.database.Cursor.FIELD_TYPE_STRING;
8
+ import static com.getcapacitor.community.database.sqlite.SQLite.UtilsDelete.findReferencesAndUpdate;
9
+ import static com.getcapacitor.community.database.sqlite.SQLite.UtilsSQLStatement.extractColumnNames;
10
+ import static com.getcapacitor.community.database.sqlite.SQLite.UtilsSQLStatement.extractTableName;
11
+ import static com.getcapacitor.community.database.sqlite.SQLite.UtilsSQLStatement.extractWhereClause;
8
12
 
9
13
  import android.content.Context;
10
14
  import android.content.SharedPreferences;
@@ -23,13 +27,10 @@ import com.getcapacitor.community.database.sqlite.SQLite.ImportExportJson.UtilsJ
23
27
  import java.io.File;
24
28
  import java.text.SimpleDateFormat;
25
29
  import java.util.ArrayList;
26
- import java.util.Arrays;
27
30
  import java.util.Date;
28
31
  import java.util.Dictionary;
29
32
  import java.util.List;
30
33
  import java.util.Objects;
31
- import java.util.regex.Matcher;
32
- import java.util.regex.Pattern;
33
34
  import net.sqlcipher.Cursor;
34
35
  import net.sqlcipher.database.SQLiteDatabase;
35
36
  import net.sqlcipher.database.SQLiteException;
@@ -61,6 +62,7 @@ public class Database {
61
62
  private final ImportFromJson fromJson = new ImportFromJson();
62
63
  private final ExportToJson toJson = new ExportToJson();
63
64
  private Boolean ncDB = false;
65
+ private boolean isAvailableTransaction = false;
64
66
 
65
67
  public Database(
66
68
  Context context,
@@ -134,6 +136,84 @@ public class Database {
134
136
  return ncDB;
135
137
  }
136
138
 
139
+ /**
140
+ * IsAvailTrans method
141
+ *
142
+ * @return database transaction is active
143
+ */
144
+ public boolean isAvailTrans() {
145
+ return _db.inTransaction();
146
+ }
147
+ /**
148
+ * BeginTransaction method
149
+ *
150
+ * @return begin a database transaction
151
+ */
152
+ public Integer beginTransaction() throws Exception {
153
+ if (_db.isOpen()) {
154
+ try {
155
+ if( isAvailTrans()) {
156
+ throw new Exception("Already in transaction");
157
+ }
158
+ _db.beginTransaction();
159
+ return 0;
160
+ } catch (Exception e) {
161
+ String msg = "Failed in beginTransaction" + e.getMessage();
162
+ Log.v(TAG, msg);
163
+ throw new Exception(msg);
164
+ }
165
+ } else {
166
+ throw new Exception("Database not opened");
167
+ }
168
+
169
+ }
170
+ /**
171
+ * CommitTransaction method
172
+ *
173
+ * @return commit a database transaction
174
+ */
175
+ public Integer commitTransaction() throws Exception {
176
+ if (_db.isOpen()) {
177
+ try {
178
+ if(!isAvailTrans()) {
179
+ throw new Exception("No transaction active");
180
+ }
181
+ _db.setTransactionSuccessful();
182
+ return 0;
183
+ } catch (Exception e) {
184
+ String msg = "Failed in commitTransaction" + e.getMessage();
185
+ Log.v(TAG, msg);
186
+ throw new Exception(msg);
187
+ }
188
+ } else {
189
+ throw new Exception("Database not opened");
190
+ }
191
+
192
+ }
193
+
194
+ /**
195
+ * Rollback Transaction method
196
+ *
197
+ * @return rollback a database transaction
198
+ */
199
+ public Integer rollbackTransaction() throws Exception {
200
+ if (_db.isOpen()) {
201
+ try {
202
+ if( isAvailTrans()) {
203
+ _db.endTransaction();
204
+ }
205
+ return 0;
206
+ } catch (Exception e) {
207
+ String msg = "Failed in rollbackTransaction" + e.getMessage();
208
+ Log.v(TAG, msg);
209
+ throw new Exception(msg);
210
+ }
211
+ } else {
212
+ throw new Exception("Database not opened");
213
+ }
214
+
215
+ }
216
+
137
217
  /**
138
218
  * GetUrl method
139
219
  *
@@ -315,7 +395,7 @@ public class Database {
315
395
  try {
316
396
  if (_db != null && _db.isOpen()) {
317
397
  Integer initChanges = _uSqlite.dbChanges(_db);
318
- if (transaction) _db.beginTransaction();
398
+ if (transaction) beginTransaction();
319
399
  for (String cmd : statements) {
320
400
  if (!cmd.endsWith(";")) cmd += ";";
321
401
  String nCmd = cmd;
@@ -328,7 +408,7 @@ public class Database {
328
408
  }
329
409
  changes = _uSqlite.dbChanges(_db) - initChanges;
330
410
  if (changes != -1) {
331
- if (transaction) _db.setTransactionSuccessful();
411
+ if (transaction) commitTransaction();
332
412
  retObj.put("changes", changes);
333
413
  }
334
414
  return retObj;
@@ -338,7 +418,7 @@ public class Database {
338
418
  } catch (Exception e) {
339
419
  throw new Exception(e.getMessage());
340
420
  } finally {
341
- if (_db != null && transaction && _db.inTransaction()) _db.endTransaction();
421
+ if (_db != null && transaction && _db.inTransaction()) rollbackTransaction();
342
422
  }
343
423
  }
344
424
 
@@ -357,7 +437,7 @@ public class Database {
357
437
  try {
358
438
  if (_db != null && _db.isOpen()) {
359
439
  int initChanges = _uSqlite.dbChanges(_db);
360
- if (transaction) _db.beginTransaction();
440
+ if (transaction) beginTransaction();
361
441
  for (int i = 0; i < set.length(); i++) {
362
442
  JSONObject row = set.getJSONObject(i);
363
443
  String statement = row.getString("statement");
@@ -388,7 +468,7 @@ public class Database {
388
468
  }
389
469
  changes = _uSqlite.dbChanges(_db) - initChanges;
390
470
  if (changes >= 0) {
391
- if (transaction) _db.setTransactionSuccessful();
471
+ if (transaction) commitTransaction();
392
472
  changes = _uSqlite.dbChanges(_db) - initChanges;
393
473
  retObj.put("changes", changes);
394
474
  retObj.put("lastId", lastId);
@@ -403,7 +483,7 @@ public class Database {
403
483
  } catch (Exception e) {
404
484
  throw new Exception(e.getMessage());
405
485
  } finally {
406
- if (_db != null && transaction && _db.inTransaction()) _db.endTransaction();
486
+ if (_db != null && transaction && _db.inTransaction()) rollbackTransaction();
407
487
  }
408
488
  }
409
489
 
@@ -452,10 +532,10 @@ public class Database {
452
532
  try {
453
533
  if (_db != null && _db.isOpen() && statement.length() > 0) {
454
534
  int initChanges = _uSqlite.dbChanges(_db);
455
- if (transaction) _db.beginTransaction();
535
+ if (transaction) beginTransaction();
456
536
  JSObject response = prepareSQL(statement, values, false, returnMode);
457
537
  lastId = response.getLong("lastId");
458
- if (lastId != -1 && transaction) _db.setTransactionSuccessful();
538
+ if (lastId != -1 && transaction) commitTransaction();
459
539
  changes = _uSqlite.dbChanges(_db) - initChanges;
460
540
  retObj.put("changes", changes);
461
541
  retObj.put("lastId", lastId);
@@ -467,7 +547,7 @@ public class Database {
467
547
  } catch (Exception e) {
468
548
  throw new Exception(e.getMessage());
469
549
  } finally {
470
- if (_db != null && transaction && _db.inTransaction()) _db.endTransaction();
550
+ if (_db != null && transaction && _db.inTransaction()) rollbackTransaction();
471
551
  }
472
552
  }
473
553
 
@@ -603,6 +683,7 @@ public class Database {
603
683
  return retVals;
604
684
  }
605
685
 
686
+ /*
606
687
  private static String extractTableName(String statement) {
607
688
  Pattern pattern = Pattern.compile("(?i)(?:INSERT\\s+INTO|UPDATE|DELETE\\s+FROM)\\s+(\\w+)");
608
689
  Matcher matcher = pattern.matcher(statement);
@@ -628,7 +709,7 @@ public class Database {
628
709
 
629
710
  return null;
630
711
  }
631
-
712
+ */
632
713
  /**
633
714
  * DeleteSQL method
634
715
  *
@@ -640,34 +721,55 @@ public class Database {
640
721
  */
641
722
  public String deleteSQL(Database mDB, String statement, ArrayList<Object> values) throws Exception {
642
723
  String sqlStmt = statement;
724
+ String msg = "DeleteSQL";
725
+
643
726
  try {
644
- Boolean isLast = _uJson.isLastModified(mDB);
645
- Boolean isDel = _uJson.isSqlDeleted(mDB);
646
- if (isLast && isDel) {
647
- // Replace DELETE by UPDATE and set sql_deleted to 1
648
- Integer wIdx = statement.toUpperCase().indexOf("WHERE");
649
- String preStmt = statement.substring(0, wIdx - 1);
650
- String clauseStmt = statement.substring(wIdx);
651
- String tableName = preStmt.substring(("DELETE FROM").length()).trim();
652
- sqlStmt = "UPDATE " + tableName + " SET sql_deleted = 1 " + clauseStmt;
653
- // Find REFERENCES if any and update the sql_deleted column
654
- findReferencesAndUpdate(mDB, tableName, clauseStmt, values);
727
+ boolean isLast = this._uJson.isLastModified(mDB);
728
+ boolean isDel = this._uJson.isSqlDeleted(mDB);
729
+
730
+ if (!isLast || !isDel) {
731
+ return sqlStmt;
732
+ }
733
+
734
+ // Replace DELETE by UPDATE
735
+ // set sql_deleted to 1
736
+ String whereClause = extractWhereClause(sqlStmt);
737
+
738
+ if (whereClause == null) {
739
+ throw new Exception("deleteSQL: cannot find a WHERE clause");
740
+ }
741
+
742
+ String tableName = extractTableName(sqlStmt);
743
+
744
+ if (tableName == null) {
745
+ throw new Exception("deleteSQL: cannot find a WHERE clause");
746
+ }
747
+ String[] colNames = extractColumnNames(whereClause).toArray(new String[0]);
748
+
749
+ if (colNames.length == 0) {
750
+ throw new Exception("deleteSQL: Did not find column names in the WHERE Statement");
751
+ }
752
+ String setStmt = "sql_deleted = 1";
753
+
754
+ // Find REFERENCES if any and update the sql_deleted column
755
+ boolean hasToUpdate = findReferencesAndUpdate(mDB, tableName, whereClause, colNames, values);
756
+
757
+ if (hasToUpdate) {
758
+ String whereStmt = whereClause.endsWith(";") ? whereClause.substring(0, whereClause.length() - 1) : whereClause;
759
+
760
+ sqlStmt = "UPDATE " + tableName + " SET " + setStmt + " WHERE " + whereStmt + " AND sql_deleted = 0;";
761
+ } else {
762
+ sqlStmt = "";
655
763
  }
764
+
656
765
  return sqlStmt;
657
- } catch (Exception e) {
658
- throw new Exception(e.getMessage());
766
+ } catch (Exception err) {
767
+ String errmsg = err.getMessage() != null ? err.getMessage() : err.toString();
768
+ throw new Exception(msg + " " + errmsg);
659
769
  }
660
770
  }
661
771
 
662
- /**
663
- * FindReferencesAndUpdate method
664
- *
665
- * @param mDB
666
- * @param tableName
667
- * @param whereStmt
668
- * @param values
669
- * @throws Exception
670
- */
772
+ /*
671
773
  public void findReferencesAndUpdate(Database mDB, String tableName, String whereStmt, ArrayList<Object> values) throws Exception {
672
774
  try {
673
775
  ArrayList<String> references = getReferences(mDB, tableName);
@@ -738,12 +840,6 @@ public class Database {
738
840
  }
739
841
  }
740
842
 
741
- /**
742
- * GetReferenceTableName method
743
- *
744
- * @param refValue
745
- * @return
746
- */
747
843
  public String getReferenceTableName(String refValue) {
748
844
  String tableName = "";
749
845
  if (refValue.length() > 0) {
@@ -757,12 +853,6 @@ public class Database {
757
853
  return tableName;
758
854
  }
759
855
 
760
- /**
761
- * GetWithRefsColumnName
762
- *
763
- * @param refValue
764
- * @return
765
- */
766
856
  public String[] getWithRefsColumnName(String refValue) {
767
857
  String[] colNames = new String[0];
768
858
  if (refValue.length() > 0) {
@@ -777,12 +867,6 @@ public class Database {
777
867
  return colNames;
778
868
  }
779
869
 
780
- /**
781
- * GetReferencedColumnName method
782
- *
783
- * @param refValue
784
- * @return
785
- */
786
870
  public String[] getReferencedColumnName(String refValue) {
787
871
  String[] colNames = new String[0];
788
872
  if (refValue.length() > 0) {
@@ -797,14 +881,6 @@ public class Database {
797
881
  return colNames;
798
882
  }
799
883
 
800
- /**
801
- * UpdateWhere method
802
- *
803
- * @param whStmt
804
- * @param withRefsNames
805
- * @param colNames
806
- * @return
807
- */
808
884
  public String updateWhere(String whStmt, String[] withRefsNames, String[] colNames) {
809
885
  String whereStmt = "";
810
886
  if (whStmt.length() > 0) {
@@ -848,14 +924,6 @@ public class Database {
848
924
  return whereStmt;
849
925
  }
850
926
 
851
- /**
852
- * GetReferences method
853
- *
854
- * @param mDB
855
- * @param tableName
856
- * @return
857
- * @throws Exception
858
- */
859
927
  public ArrayList<String> getReferences(Database mDB, String tableName) throws Exception {
860
928
  String sqlStmt =
861
929
  "SELECT sql FROM sqlite_master " +
@@ -876,13 +944,6 @@ public class Database {
876
944
  }
877
945
  }
878
946
 
879
- /**
880
- * GetRefs
881
- *
882
- * @param str
883
- * @return
884
- * @throws Exception
885
- */
886
947
  private ArrayList<String> getRefs(String str) throws Exception {
887
948
  ArrayList<String> retRefs = new ArrayList<String>();
888
949
  String[] arrFor = str.split("(?i)FOREIGN KEY", -1);
@@ -900,6 +961,7 @@ public class Database {
900
961
  return retRefs;
901
962
  }
902
963
 
964
+ */
903
965
  /**
904
966
  * SelectSQL Method
905
967
  * Query a raw sql statement with or without binding values
@@ -1024,8 +1086,8 @@ public class Database {
1024
1086
  // check if the table has already been created
1025
1087
  boolean isExists = _uJson.isTableExists(this, "sync_table");
1026
1088
  if (!isExists) {
1027
- boolean isLastModified = _uJson.isLastModified(this);
1028
- boolean isSqlDeleted = _uJson.isSqlDeleted(this);
1089
+ boolean isLastModified = this._uJson.isLastModified(this);
1090
+ boolean isSqlDeleted = this._uJson.isSqlDeleted(this);
1029
1091
  if (isLastModified && isSqlDeleted) {
1030
1092
  Date date = new Date();
1031
1093
  long syncTime = date.getTime() / 1000L;