@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.
Files changed (44) hide show
  1. package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLite.java +118 -156
  2. package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLitePlugin.java +81 -249
  3. package/android/src/main/java/com/getcapacitor/community/database/sqlite/NotificationCenter.java +1 -1
  4. package/android/src/main/java/com/getcapacitor/community/database/sqlite/RetHandler.java +0 -12
  5. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java +184 -40
  6. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ExportToJson.java +141 -135
  7. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ImportFromJson.java +2 -1
  8. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/UtilsEncryption.java +111 -0
  9. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsBiometric.java +0 -4
  10. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsFile.java +30 -18
  11. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsMigrate.java +12 -4
  12. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsNCDatabase.java +4 -1
  13. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsSQLCipher.java +8 -6
  14. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsSQLite.java +14 -28
  15. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsSecret.java +3 -4
  16. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsUpgrade.java +0 -1
  17. package/dist/esm/definitions.d.ts +91 -4
  18. package/dist/esm/definitions.js +79 -19
  19. package/dist/esm/definitions.js.map +1 -1
  20. package/dist/esm/web.d.ts +3 -1
  21. package/dist/esm/web.js +8 -0
  22. package/dist/esm/web.js.map +1 -1
  23. package/dist/plugin.cjs.js +87 -19
  24. package/dist/plugin.cjs.js.map +1 -1
  25. package/dist/plugin.js +87 -19
  26. package/dist/plugin.js.map +1 -1
  27. package/electron/dist/plugin.js +333 -148
  28. package/electron/dist/plugin.js.map +1 -1
  29. package/electron/rollup.config.js +2 -0
  30. package/ios/Plugin/CapacitorSQLite.swift +125 -92
  31. package/ios/Plugin/CapacitorSQLitePlugin.swift +6 -3
  32. package/ios/Plugin/Database.swift +45 -19
  33. package/ios/Plugin/ImportExportJson/ExportToJson.swift +10 -5
  34. package/ios/Plugin/ImportExportJson/ImportData.swift +434 -0
  35. package/ios/Plugin/ImportExportJson/ImportFromJson.swift +47 -59
  36. package/ios/Plugin/ImportExportJson/JsonSQLite.swift +7 -0
  37. package/ios/Plugin/Utils/UtilsDownloadFromHTTP.swift +61 -61
  38. package/ios/Plugin/Utils/UtilsDrop.swift +2 -1
  39. package/ios/Plugin/Utils/UtilsJson.swift +123 -1
  40. package/ios/Plugin/Utils/UtilsSQLCipher.swift +254 -23
  41. package/ios/Plugin/Utils/UtilsUpgrade.swift +0 -1
  42. package/package.json +2 -2
  43. package/src/definitions.ts +171 -18
  44. package/src/web.ts +10 -0
@@ -19,6 +19,7 @@ import com.getcapacitor.community.database.sqlite.SQLite.BiometricListener;
19
19
  import com.getcapacitor.community.database.sqlite.SQLite.Database;
20
20
  import com.getcapacitor.community.database.sqlite.SQLite.GlobalSQLite;
21
21
  import com.getcapacitor.community.database.sqlite.SQLite.ImportExportJson.JsonSQLite;
22
+ import com.getcapacitor.community.database.sqlite.SQLite.ImportExportJson.UtilsEncryption;
22
23
  import com.getcapacitor.community.database.sqlite.SQLite.ImportExportJson.UtilsJson;
23
24
  import com.getcapacitor.community.database.sqlite.SQLite.SqliteConfig;
24
25
  import com.getcapacitor.community.database.sqlite.SQLite.UtilsBiometric;
@@ -30,7 +31,6 @@ import com.getcapacitor.community.database.sqlite.SQLite.UtilsSQLCipher;
30
31
  import com.getcapacitor.community.database.sqlite.SQLite.UtilsSQLite;
31
32
  import com.getcapacitor.community.database.sqlite.SQLite.UtilsSecret;
32
33
  import java.io.File;
33
- import java.nio.ByteBuffer;
34
34
  import java.security.KeyStore;
35
35
  import java.util.ArrayList;
36
36
  import java.util.Collections;
@@ -40,8 +40,8 @@ import java.util.HashMap;
40
40
  import java.util.HashSet;
41
41
  import java.util.Hashtable;
42
42
  import java.util.Map;
43
+ import java.util.Objects;
43
44
  import java.util.Set;
44
- import org.json.JSONException;
45
45
  import org.json.JSONObject;
46
46
 
47
47
  public class CapacitorSQLite {
@@ -61,9 +61,8 @@ public class CapacitorSQLite {
61
61
  private SharedPreferences sharedPreferences = null;
62
62
  private MasterKey masterKeyAlias;
63
63
  private BiometricManager biometricManager;
64
- private final SqliteConfig config;
65
- private Boolean isEncryption = true;
66
- private Boolean biometricAuth = false;
64
+ private final Boolean isEncryption;
65
+ private final Boolean biometricAuth;
67
66
  private final String biometricTitle;
68
67
  private final String biometricSubTitle;
69
68
  private final int VALIDITY_DURATION = 5;
@@ -74,11 +73,10 @@ public class CapacitorSQLite {
74
73
  public CapacitorSQLite(Context context, SqliteConfig config) throws Exception {
75
74
  this.context = context;
76
75
  this.call = call;
77
- this.config = config;
78
- this.isEncryption = this.config.getIsEncryption();
79
- this.biometricAuth = this.config.getBiometricAuth();
80
- this.biometricTitle = this.config.getBiometricTitle();
81
- this.biometricSubTitle = this.config.getBiometricSubTitle();
76
+ this.isEncryption = config.getIsEncryption();
77
+ this.biometricAuth = config.getBiometricAuth();
78
+ this.biometricTitle = config.getBiometricTitle();
79
+ this.biometricSubTitle = config.getBiometricSubTitle();
82
80
  try {
83
81
  if (isEncryption) {
84
82
  // create or retrieve masterkey from Android keystore
@@ -104,7 +102,6 @@ public class CapacitorSQLite {
104
102
  }
105
103
  setSharedPreferences();
106
104
  notifyBiometricEvent(true, null);
107
- return;
108
105
  } catch (Exception e) {
109
106
  String input = e.getMessage();
110
107
  Log.e("MY_APP_TAG", input);
@@ -139,7 +136,7 @@ public class CapacitorSQLite {
139
136
  }
140
137
 
141
138
  private void notifyBiometricEvent(Boolean ret, String msg) {
142
- Map<String, Object> info = new HashMap<String, Object>() {
139
+ Map<String, Object> info = new HashMap<>() {
143
140
  {
144
141
  put("result", ret);
145
142
  put("message", msg);
@@ -169,15 +166,15 @@ public class CapacitorSQLite {
169
166
  /**
170
167
  * Echo
171
168
  *
172
- * @param value
173
- * @return
169
+ * @param value string to echo
170
+ * @return string to echo
174
171
  */
175
172
  public String echo(String value) {
176
173
  return value;
177
174
  }
178
175
 
179
176
  public Boolean isSecretStored() throws Exception {
180
- Boolean ret = false;
177
+ boolean ret = false;
181
178
  if (isEncryption) {
182
179
  try {
183
180
  String secret = uSecret.getPassphrase();
@@ -194,8 +191,8 @@ public class CapacitorSQLite {
194
191
  /**
195
192
  * SetEncryptionSecret
196
193
  *
197
- * @param passphrase
198
- * @throws Exception
194
+ * @param passphrase passphrase
195
+ * @throws Exception message
199
196
  */
200
197
  public void setEncryptionSecret(String passphrase) throws Exception {
201
198
  if (isEncryption) {
@@ -215,9 +212,9 @@ public class CapacitorSQLite {
215
212
  /**
216
213
  * ChangeEncryptionSecret
217
214
  *
218
- * @param passphrase
219
- * @param oldPassphrase
220
- * @throws Exception
215
+ * @param passphrase new passphrase
216
+ * @param oldPassphrase old passphrase
217
+ * @throws Exception message
221
218
  */
222
219
  public void changeEncryptionSecret(PluginCall call, String passphrase, String oldPassphrase) throws Exception {
223
220
  this.call = call;
@@ -233,7 +230,6 @@ public class CapacitorSQLite {
233
230
  // change encryption secret
234
231
  uSecret.changeEncryptionSecret(passphrase, oldPassphrase);
235
232
  rHandler.retResult(call, null, null);
236
- return;
237
233
  } catch (Exception e) {
238
234
  String input = e.getMessage();
239
235
  Log.e("MY_APP_TAG", input);
@@ -271,7 +267,7 @@ public class CapacitorSQLite {
271
267
 
272
268
  /**
273
269
  * ClearEncryptionSecret
274
- * @throws Exception
270
+ * @throws Exception message
275
271
  */
276
272
  public void clearEncryptionSecret() throws Exception {
277
273
  if (isEncryption) {
@@ -291,8 +287,8 @@ public class CapacitorSQLite {
291
287
  /**
292
288
  * CheckEncryptionSecret
293
289
  *
294
- * @param passphrase
295
- * @throws Exception
290
+ * @param passphrase secret phrase for encryption
291
+ * @throws Exception message
296
292
  */
297
293
  public Boolean checkEncryptionSecret(String passphrase) throws Exception {
298
294
  if (isEncryption) {
@@ -300,8 +296,7 @@ public class CapacitorSQLite {
300
296
  // close all connections
301
297
  closeAllConnections();
302
298
  // set encryption secret
303
- Boolean res = uSecret.checkEncryptionSecret(passphrase);
304
- return res;
299
+ return uSecret.checkEncryptionSecret(passphrase);
305
300
  } catch (Exception e) {
306
301
  throw new Exception(e.getMessage());
307
302
  }
@@ -312,8 +307,7 @@ public class CapacitorSQLite {
312
307
 
313
308
  public String getNCDatabasePath(String folderPath, String database) throws Exception {
314
309
  try {
315
- String databasePath = uNCDatabase.getNCDatabasePath(context, folderPath, database);
316
- return databasePath;
310
+ return uNCDatabase.getNCDatabasePath(context, folderPath, database);
317
311
  } catch (Exception e) {
318
312
  throw new Exception(e.getMessage());
319
313
  }
@@ -322,12 +316,12 @@ public class CapacitorSQLite {
322
316
  /**
323
317
  * CreateConnection
324
318
  *
325
- * @param dbName
326
- * @param encrypted
327
- * @param mode
328
- * @param version
329
- * @param vUpgObject
330
- * @throws Exception
319
+ * @param dbName database name
320
+ * @param encrypted boolean
321
+ * @param mode "no-encryption", "secret", "encryption"
322
+ * @param version database version
323
+ * @param vUpgObject upgrade Object
324
+ * @throws Exception message
331
325
  */
332
326
  public void createConnection(
333
327
  String dbName,
@@ -360,13 +354,7 @@ public class CapacitorSQLite {
360
354
  sharedPreferences,
361
355
  readonly
362
356
  );
363
- if (db != null) {
364
- dbDict.put(connName, db);
365
- return;
366
- } else {
367
- String msg = "db is null";
368
- throw new Exception(msg);
369
- }
357
+ dbDict.put(connName, db);
370
358
  } catch (Exception e) {
371
359
  throw new Exception(e.getMessage());
372
360
  }
@@ -375,9 +363,9 @@ public class CapacitorSQLite {
375
363
  /**
376
364
  * CreateNCConnection
377
365
  *
378
- * @param dbPath
379
- * @param version
380
- * @throws Exception
366
+ * @param dbPath database path
367
+ * @param version database version
368
+ * @throws Exception message
381
369
  */
382
370
  public void createNCConnection(String dbPath, int version) throws Exception {
383
371
  // check if connection already exists
@@ -404,13 +392,7 @@ public class CapacitorSQLite {
404
392
  sharedPreferences,
405
393
  true
406
394
  );
407
- if (db != null) {
408
- dbDict.put(connName, db);
409
- return;
410
- } else {
411
- String msg = "db is null";
412
- throw new Exception(msg);
413
- }
395
+ dbDict.put(connName, db);
414
396
  } catch (Exception e) {
415
397
  throw new Exception(e.getMessage());
416
398
  }
@@ -419,8 +401,8 @@ public class CapacitorSQLite {
419
401
  /**
420
402
  * Open
421
403
  *
422
- * @param dbName
423
- * @throws Exception
404
+ * @param dbName database name
405
+ * @throws Exception message
424
406
  */
425
407
  public void open(String dbName, Boolean readonly) throws Exception {
426
408
  dbName = getDatabaseName(dbName);
@@ -429,7 +411,6 @@ public class CapacitorSQLite {
429
411
  if (db != null) {
430
412
  try {
431
413
  db.open();
432
- return;
433
414
  } catch (Exception e) {
434
415
  throw new Exception(e.getMessage());
435
416
  }
@@ -442,8 +423,8 @@ public class CapacitorSQLite {
442
423
  /**
443
424
  * Close
444
425
  *
445
- * @param dbName
446
- * @throws Exception
426
+ * @param dbName database name
427
+ * @throws Exception message
447
428
  */
448
429
  public void close(String dbName, Boolean readonly) throws Exception {
449
430
  dbName = getDatabaseName(dbName);
@@ -454,7 +435,6 @@ public class CapacitorSQLite {
454
435
  if (!db.inTransaction()) {
455
436
  try {
456
437
  db.close();
457
- return;
458
438
  } catch (Exception e) {
459
439
  throw new Exception(e.getMessage());
460
440
  }
@@ -475,9 +455,9 @@ public class CapacitorSQLite {
475
455
  /**
476
456
  * GetUrl
477
457
  *
478
- * @param dbName
458
+ * @param dbName database name
479
459
  * @return String
480
- * @throws Exception
460
+ * @throws Exception message
481
461
  */
482
462
  public String getUrl(String dbName, Boolean readonly) throws Exception {
483
463
  dbName = getDatabaseName(dbName);
@@ -485,8 +465,7 @@ public class CapacitorSQLite {
485
465
  Database db = dbDict.get(connName);
486
466
  if (db != null) {
487
467
  try {
488
- String url = db.getUrl();
489
- return url;
468
+ return db.getUrl();
490
469
  } catch (Exception e) {
491
470
  throw new Exception(e.getMessage());
492
471
  }
@@ -499,9 +478,9 @@ public class CapacitorSQLite {
499
478
  /**
500
479
  * GetVersion
501
480
  *
502
- * @param dbName
481
+ * @param dbName database name
503
482
  * @return Integer
504
- * @throws Exception
483
+ * @throws Exception message
505
484
  */
506
485
  public Integer getVersion(String dbName, Boolean readonly) throws Exception {
507
486
  dbName = getDatabaseName(dbName);
@@ -509,8 +488,7 @@ public class CapacitorSQLite {
509
488
  Database db = dbDict.get(connName);
510
489
  if (db != null) {
511
490
  try {
512
- Integer version = db.getVersion();
513
- return version;
491
+ return db.getVersion();
514
492
  } catch (Exception e) {
515
493
  throw new Exception(e.getMessage());
516
494
  }
@@ -523,8 +501,8 @@ public class CapacitorSQLite {
523
501
  /**
524
502
  * CloseNCConnection
525
503
  *
526
- * @param dbPath
527
- * @throws Exception
504
+ * @param dbPath database path
505
+ * @throws Exception message
528
506
  */
529
507
  public void closeNCConnection(String dbPath) throws Exception {
530
508
  String connName = "RO_" + dbPath;
@@ -538,7 +516,6 @@ public class CapacitorSQLite {
538
516
  }
539
517
  }
540
518
  dbDict.remove(connName);
541
- return;
542
519
  } else {
543
520
  String msg = "No available connection for database " + dbPath;
544
521
  throw new Exception(msg);
@@ -548,8 +525,8 @@ public class CapacitorSQLite {
548
525
  /**
549
526
  * CloseConnection
550
527
  *
551
- * @param dbName
552
- * @throws Exception
528
+ * @param dbName database name
529
+ * @throws Exception message
553
530
  */
554
531
  public void closeConnection(String dbName, Boolean readonly) throws Exception {
555
532
  dbName = getDatabaseName(dbName);
@@ -564,7 +541,6 @@ public class CapacitorSQLite {
564
541
  }
565
542
  }
566
543
  dbDict.remove(connName);
567
- return;
568
544
  } else {
569
545
  String msg = "No available connection for database " + dbName;
570
546
  throw new Exception(msg);
@@ -580,14 +556,13 @@ public class CapacitorSQLite {
580
556
  }
581
557
 
582
558
  public Boolean checkConnectionsConsistency(JSArray dbNames, JSArray openModes) throws Exception {
583
- Set<String> keys = new HashSet<String>(Collections.list(dbDict.keys()));
584
- String msg = "All Native Connections released";
559
+ Set<String> keys = new HashSet<>(Collections.list(dbDict.keys()));
585
560
  JSArray nameDBs = new JSArray();
586
- for (Integer i = 0; i < dbNames.length(); i++) {
561
+ for (int i = 0; i < dbNames.length(); i++) {
587
562
  String name = openModes.getString(i) + "_" + dbNames.getString(i);
588
563
  nameDBs.put(name);
589
564
  }
590
- Set<String> conns = new HashSet<String>(uSqlite.stringJSArrayToArrayList(nameDBs));
565
+ Set<String> conns = new HashSet<>(uSqlite.stringJSArrayToArrayList(nameDBs));
591
566
  try {
592
567
  if (conns.size() == 0) {
593
568
  closeAllConnections();
@@ -605,11 +580,11 @@ public class CapacitorSQLite {
605
580
  }
606
581
  }
607
582
  }
608
- keys = new HashSet<String>(Collections.list(dbDict.keys()));
583
+ keys = new HashSet<>(Collections.list(dbDict.keys()));
609
584
  if (keys.size() == conns.size()) {
610
- Set<String> symmetricDiff = new HashSet<String>(keys);
585
+ Set<String> symmetricDiff = new HashSet<>(keys);
611
586
  symmetricDiff.addAll(conns);
612
- Set<String> tmp = new HashSet<String>(keys);
587
+ Set<String> tmp = new HashSet<>(keys);
613
588
  tmp.retainAll(conns);
614
589
  symmetricDiff.removeAll(tmp);
615
590
  if (symmetricDiff.size() == 0) {
@@ -631,9 +606,8 @@ public class CapacitorSQLite {
631
606
  /**
632
607
  * IsDatabase
633
608
  *
634
- * @param dbName
609
+ * @param dbName database name
635
610
  * @return Boolean
636
- * @throws Exception
637
611
  */
638
612
  public Boolean isDatabase(String dbName) {
639
613
  dbName = getDatabaseName(dbName);
@@ -643,9 +617,9 @@ public class CapacitorSQLite {
643
617
  /**
644
618
  * IsDatabaseEncrypted
645
619
  *
646
- * @param dbName
620
+ * @param dbName database name
647
621
  * @return Boolean
648
- * @throws Exception
622
+ * @throws Exception message
649
623
  */
650
624
  public Boolean isDatabaseEncrypted(String dbName) throws Exception {
651
625
  dbName = getDatabaseName(dbName);
@@ -667,9 +641,8 @@ public class CapacitorSQLite {
667
641
  /**
668
642
  * IsNCDatabase
669
643
  *
670
- * @param dbPath
644
+ * @param dbPath database path
671
645
  * @return Boolean
672
- * @throws Exception
673
646
  */
674
647
  public Boolean isNCDatabase(String dbPath) {
675
648
  return uFile.isPathExists(dbPath);
@@ -678,17 +651,16 @@ public class CapacitorSQLite {
678
651
  /**
679
652
  * IsTableExists
680
653
  *
681
- * @param dbName
682
- * @param tableName
683
- * @throws Exception
654
+ * @param dbName database name
655
+ * @param tableName table name
656
+ * @throws Exception message
684
657
  */
685
658
  public Boolean isTableExists(String dbName, String tableName, Boolean readonly) throws Exception {
686
659
  dbName = getDatabaseName(dbName);
687
660
  String connName = readonly ? "RO_" + dbName : "RW_" + dbName;
688
661
  Database db = dbDict.get(connName);
689
662
  if (db != null) {
690
- boolean res = uJson.isTableExists(db, tableName);
691
- return res;
663
+ return uJson.isTableExists(db, tableName);
692
664
  } else {
693
665
  String msg = "No available connection for database " + dbName;
694
666
  throw new Exception(msg);
@@ -698,8 +670,8 @@ public class CapacitorSQLite {
698
670
  /**
699
671
  * GetDatabaseList
700
672
  *
701
- * @return JSArray
702
- * @throws Exception
673
+ * @return JSArray database list
674
+ * @throws Exception message
703
675
  */
704
676
  public JSArray getDatabaseList() throws Exception {
705
677
  String[] listFiles = uFile.getListOfFiles(context);
@@ -718,8 +690,8 @@ public class CapacitorSQLite {
718
690
  /**
719
691
  * GetMigratableDbList
720
692
  *
721
- * @return JSArray
722
- * @throws Exception
693
+ * @return JSArray database list
694
+ * @throws Exception message
723
695
  */
724
696
  public JSArray getMigratableDbList(String folderPath) throws Exception {
725
697
  String[] listFiles = uMigrate.getMigratableList(context, folderPath);
@@ -738,28 +710,26 @@ public class CapacitorSQLite {
738
710
  /**
739
711
  * AddSQLiteSuffix
740
712
  *
741
- * @param folderPath
742
- * @throws Exception
713
+ * @param folderPath folder path
714
+ * @throws Exception message
743
715
  */
744
716
  public void addSQLiteSuffix(String folderPath, JSArray dbList) throws Exception {
745
717
  try {
746
718
  ArrayList<String> mDbList = uSqlite.stringJSArrayToArrayList(dbList);
747
719
  uMigrate.addSQLiteSuffix(context, folderPath, mDbList);
748
- return;
749
720
  } catch (Exception e) {
750
721
  throw new Exception(e.getMessage());
751
722
  }
752
723
  }
753
724
 
754
725
  /**
755
- * @param folderPath
756
- * @throws Exception
726
+ * @param folderPath folder path
727
+ * @throws Exception message
757
728
  */
758
729
  public void deleteOldDatabases(String folderPath, JSArray dbList) throws Exception {
759
730
  try {
760
731
  ArrayList<String> mDbList = uSqlite.stringJSArrayToArrayList(dbList);
761
732
  uMigrate.deleteOldDatabases(context, folderPath, mDbList);
762
- return;
763
733
  } catch (Exception e) {
764
734
  throw new Exception(e.getMessage());
765
735
  }
@@ -767,14 +737,13 @@ public class CapacitorSQLite {
767
737
 
768
738
  /**
769
739
  *
770
- * @param folderPath
771
- * @throws Exception
740
+ * @param folderPath folder path
741
+ * @throws Exception message
772
742
  */
773
743
  public void moveDatabasesAndAddSuffix(String folderPath, JSArray dbList) throws Exception {
774
744
  try {
775
745
  ArrayList<String> mDbList = uSqlite.stringJSArrayToArrayList(dbList);
776
746
  uMigrate.moveDatabasesAndAddSuffix(context, folderPath, mDbList);
777
- return;
778
747
  } catch (Exception e) {
779
748
  throw new Exception(e.getMessage());
780
749
  }
@@ -783,10 +752,10 @@ public class CapacitorSQLite {
783
752
  /**
784
753
  * Execute
785
754
  *
786
- * @param dbName
787
- * @param statements
788
- * @return
789
- * @throws Exception
755
+ * @param dbName Database name
756
+ * @param statements a bench of statement
757
+ * @return JSObject changes
758
+ * @throws Exception message
790
759
  */
791
760
  public JSObject execute(String dbName, String statements, Boolean transaction, Boolean readonly) throws Exception {
792
761
  dbName = getDatabaseName(dbName);
@@ -800,8 +769,7 @@ public class CapacitorSQLite {
800
769
  // convert string in string[]
801
770
  String[] sqlCmdArray = uSqlite.getStatementsArray(statements);
802
771
  try {
803
- JSObject res = db.execute(sqlCmdArray, transaction);
804
- return res;
772
+ return db.execute(sqlCmdArray, transaction);
805
773
  } catch (Exception e) {
806
774
  throw new Exception(e.getMessage());
807
775
  }
@@ -818,12 +786,12 @@ public class CapacitorSQLite {
818
786
  /**
819
787
  * ExecuteSet
820
788
  *
821
- * @param dbName
822
- * @param set
823
- * @return
824
- * @throws Exception
789
+ * @param dbName database name
790
+ * @param set Set containing statements and values
791
+ * @return JSObject changes, lastId, values when RETURNING
792
+ * @throws Exception message
825
793
  */
826
- public JSObject executeSet(String dbName, JSArray set, Boolean transaction, Boolean readonly) throws Exception {
794
+ public JSObject executeSet(String dbName, JSArray set, Boolean transaction, Boolean readonly, String returnMode) throws Exception {
827
795
  dbName = getDatabaseName(dbName);
828
796
  String connName = "RW_" + dbName;
829
797
  Database db = dbDict.get(connName);
@@ -833,8 +801,7 @@ public class CapacitorSQLite {
833
801
  }
834
802
  if (!db.isNCDB() && db.isOpen()) {
835
803
  try {
836
- JSObject res = db.executeSet(set, transaction);
837
- return res;
804
+ return db.executeSet(set, transaction, returnMode);
838
805
  } catch (Exception e) {
839
806
  throw new Exception(e.getMessage());
840
807
  }
@@ -851,13 +818,14 @@ public class CapacitorSQLite {
851
818
  /**
852
819
  * Run
853
820
  *
854
- * @param dbName
855
- * @param statement
856
- * @param values
857
- * @return
858
- * @throws Exception
821
+ * @param dbName Database name
822
+ * @param statement SQLite statement
823
+ * @param values SQLite values if any
824
+ * @return JSObject changes, lastId, values when RETURNING
825
+ * @throws Exception message
859
826
  */
860
- public JSObject run(String dbName, String statement, JSArray values, Boolean transaction, Boolean readonly) throws Exception {
827
+ public JSObject run(String dbName, String statement, JSArray values, Boolean transaction, Boolean readonly, String returnMode)
828
+ throws Exception {
861
829
  JSObject res;
862
830
  dbName = getDatabaseName(dbName);
863
831
  String connName = "RW_" + dbName;
@@ -869,17 +837,16 @@ public class CapacitorSQLite {
869
837
  if (!db.isNCDB() && db.isOpen()) {
870
838
  if (values.length() > 0) {
871
839
  try {
840
+ // returnMode = "no";
872
841
  ArrayList<Object> arrValues = uSqlite.objectJSArrayToArrayList(values);
873
- res = db.runSQL(statement, arrValues, transaction);
842
+ res = db.runSQL(statement, arrValues, transaction, returnMode);
874
843
  return res;
875
- } catch (JSONException e) {
876
- throw new Exception(e.getMessage());
877
844
  } catch (Exception e) {
878
845
  throw new Exception(e.getMessage());
879
846
  }
880
847
  } else {
881
848
  try {
882
- res = db.runSQL(statement, null, transaction);
849
+ res = db.runSQL(statement, null, transaction, returnMode);
883
850
  return res;
884
851
  } catch (Exception e) {
885
852
  throw new Exception(e.getMessage());
@@ -898,11 +865,11 @@ public class CapacitorSQLite {
898
865
  /**
899
866
  * Query
900
867
  *
901
- * @param dbName
902
- * @param statement
903
- * @param values
904
- * @return
905
- * @throws Exception
868
+ * @param dbName Database name
869
+ * @param statement SQLite statement
870
+ * @param values SQLite values if any
871
+ * @return JSArray
872
+ * @throws Exception message
906
873
  */
907
874
  public JSArray query(String dbName, String statement, JSArray values, Boolean readonly) throws Exception {
908
875
  JSArray res;
@@ -916,14 +883,12 @@ public class CapacitorSQLite {
916
883
  ArrayList<Object> arrValues = uSqlite.objectJSArrayToArrayList(values);
917
884
  res = db.selectSQL(statement, arrValues);
918
885
  return res;
919
- } catch (JSONException e) {
920
- throw new Exception(e.getMessage());
921
886
  } catch (Exception e) {
922
887
  throw new Exception(e.getMessage());
923
888
  }
924
889
  } else {
925
890
  try {
926
- res = db.selectSQL(statement, new ArrayList<Object>());
891
+ res = db.selectSQL(statement, new ArrayList<>());
927
892
  return res;
928
893
  } catch (Exception e) {
929
894
  throw new Exception(e.getMessage());
@@ -975,8 +940,7 @@ public class CapacitorSQLite {
975
940
  String connName = readonly ? "RO_" + dbName : "RW_" + dbName;
976
941
  Database db = dbDict.get(connName);
977
942
  if (db != null) {
978
- Boolean isOpen = db.isOpen();
979
- return isOpen;
943
+ return db.isOpen();
980
944
  } else {
981
945
  String msg = "No available connection for database " + dbName;
982
946
  throw new Exception(msg);
@@ -993,7 +957,6 @@ public class CapacitorSQLite {
993
957
  }
994
958
  try {
995
959
  db.deleteDB(dbName + "SQLite.db");
996
- return;
997
960
  } catch (Exception e) {
998
961
  throw new Exception(e.getMessage());
999
962
  }
@@ -1016,8 +979,7 @@ public class CapacitorSQLite {
1016
979
  String msg = "CreateSyncTable: db not opened";
1017
980
  throw new Exception(msg);
1018
981
  }
1019
- JSObject res = db.createSyncTable();
1020
- return res;
982
+ return db.createSyncTable();
1021
983
  } catch (Exception e) {
1022
984
  throw new Exception(e.getMessage());
1023
985
  }
@@ -1041,7 +1003,6 @@ public class CapacitorSQLite {
1041
1003
  throw new Exception(msg);
1042
1004
  }
1043
1005
  db.setSyncDate(syncDate);
1044
- return;
1045
1006
  } catch (Exception e) {
1046
1007
  throw new Exception(e.getMessage());
1047
1008
  }
@@ -1061,8 +1022,7 @@ public class CapacitorSQLite {
1061
1022
  String msg = "GetSyncDate: db not opened";
1062
1023
  throw new Exception(msg);
1063
1024
  }
1064
- long syncDate = db.getSyncDate();
1065
- return syncDate;
1025
+ return db.getSyncDate();
1066
1026
  } catch (Exception e) {
1067
1027
  throw new Exception(e.getMessage());
1068
1028
  }
@@ -1076,7 +1036,7 @@ public class CapacitorSQLite {
1076
1036
  Dictionary<Integer, JSONObject> upgDict = new Hashtable<>();
1077
1037
 
1078
1038
  for (int i = 0; i < upgrade.length(); i++) {
1079
- JSONObject upgObj = null;
1039
+ JSONObject upgObj;
1080
1040
  try {
1081
1041
  upgObj = (JSONObject) upgrade.get(i);
1082
1042
  if (upgObj == null || !upgObj.has("toVersion") || !upgObj.has("statements")) {
@@ -1103,8 +1063,7 @@ public class CapacitorSQLite {
1103
1063
  try {
1104
1064
  JSObject jsonObject = new JSObject(parsingData);
1105
1065
  JsonSQLite jsonSQL = new JsonSQLite();
1106
- Boolean isValid = jsonSQL.isJsonSQLite(jsonObject, isEncryption);
1107
- return isValid;
1066
+ return jsonSQL.isJsonSQLite(jsonObject, isEncryption);
1108
1067
  } catch (Exception e) {
1109
1068
  throw new Exception(e.getMessage());
1110
1069
  }
@@ -1113,14 +1072,20 @@ public class CapacitorSQLite {
1113
1072
  public JSObject importFromJson(String parsingData) throws Exception {
1114
1073
  try {
1115
1074
  JSObject jsonObject = new JSObject(parsingData);
1075
+ if (jsonObject.has("expData")) {
1076
+ // Decrypt the data
1077
+ // test decrypt to be removed
1078
+ JSObject decryptJson = UtilsEncryption.decryptJSONObject(this.context, jsonObject.getString("expData"));
1079
+ jsonObject = decryptJson;
1080
+ }
1116
1081
  JsonSQLite jsonSQL = new JsonSQLite();
1117
- Boolean isValid = jsonSQL.isJsonSQLite(jsonObject, isEncryption);
1082
+ boolean isValid = jsonSQL.isJsonSQLite(jsonObject, isEncryption);
1118
1083
  if (!isValid) {
1119
1084
  String msg = "Stringify Json Object not Valid";
1120
1085
  throw new Exception(msg);
1121
1086
  }
1122
1087
  String dbName = getDatabaseName(jsonSQL.getDatabase());
1123
- dbName = new StringBuilder(dbName).append("SQLite.db").toString();
1088
+ dbName = dbName + "SQLite.db";
1124
1089
  Integer dbVersion = jsonSQL.getVersion();
1125
1090
  String mode = jsonSQL.getMode();
1126
1091
  Boolean overwrite = jsonSQL.getOverwrite();
@@ -1161,7 +1126,7 @@ public class CapacitorSQLite {
1161
1126
  msg += "version lower than" + curVersion;
1162
1127
  throw new Exception(msg);
1163
1128
  }
1164
- if (curVersion == dbVersion) {
1129
+ if (curVersion.equals(dbVersion)) {
1165
1130
  JSObject result = new JSObject();
1166
1131
  result.put("changes", Integer.valueOf(0));
1167
1132
  return result;
@@ -1169,11 +1134,11 @@ public class CapacitorSQLite {
1169
1134
  }
1170
1135
  JSObject res = db.importFromJson(jsonSQL);
1171
1136
  db.close();
1172
- if (res.getInteger("changes") == Integer.valueOf(-1)) {
1137
+ if (!Objects.equals(res.getInteger("changes"), -1)) {
1138
+ return res;
1139
+ } else {
1173
1140
  String msg = "importFromJson: import JsonObject not successful";
1174
1141
  throw new Exception(msg);
1175
- } else {
1176
- return res;
1177
1142
  }
1178
1143
  }
1179
1144
  } catch (Exception e) {
@@ -1196,6 +1161,8 @@ public class CapacitorSQLite {
1196
1161
  if (ret.length() == 0) {
1197
1162
  String msg = "ExportToJson: : return Object is empty " + "No data to synchronize";
1198
1163
  throw new Exception(msg);
1164
+ } else if (ret.length() == 1 && ret.has("expData")) {
1165
+ return ret;
1199
1166
  } else if (ret.length() == 5 || ret.length() == 6 || ret.length() == 7) {
1200
1167
  return ret;
1201
1168
  } else {
@@ -1226,7 +1193,6 @@ public class CapacitorSQLite {
1226
1193
  throw new Exception(msg);
1227
1194
  }
1228
1195
  db.deleteExportedRows();
1229
- return;
1230
1196
  } catch (Exception e) {
1231
1197
  String msg = "DeleteExportedRows " + e.getMessage();
1232
1198
  throw new Exception(msg);
@@ -1241,7 +1207,6 @@ public class CapacitorSQLite {
1241
1207
  String msg = "copy failed : ";
1242
1208
  try {
1243
1209
  uFile.copyFromAssetsToDatabase(context, overwrite);
1244
- return;
1245
1210
  } catch (Exception e) {
1246
1211
  msg += e.getMessage();
1247
1212
  throw new Exception(msg);
@@ -1264,10 +1229,7 @@ public class CapacitorSQLite {
1264
1229
  Enumeration<String> connections = dbDict.keys();
1265
1230
  while (connections.hasMoreElements()) {
1266
1231
  String dbName = connections.nextElement();
1267
- Boolean readonly = false;
1268
- if (dbName.substring(0, 3).equals("RO_")) {
1269
- readonly = true;
1270
- }
1232
+ boolean readonly = dbName.startsWith("RO_");
1271
1233
  dbName = dbName.substring(3);
1272
1234
  closeConnection(dbName, readonly);
1273
1235
  }