@capacitor-community/sqlite 4.1.0-6 → 4.1.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.
package/README.md CHANGED
@@ -90,9 +90,12 @@ npm install --save-dev @types/sqlite3
90
90
 
91
91
  | Name | Android | iOS | Electron | Web |
92
92
  | :-------------------------- | :------ | :-- | :------- | :-- |
93
- | createConnection | ✅ | ✅ | ✅ | ✅ |
94
- | closeConnection | ✅ | ✅ | | |
95
- | isConnection | ✅ | ✅ | ✅ | ✅ |
93
+ | createConnection (ReadWrite)| ✅ | ✅ | ✅ | ✅ |
94
+ | createConnection (ReadOnly) | ✅ | ✅ | | | since 4.1.0-7
95
+ | closeConnection (ReadWrite) | ✅ | ✅ | ✅ | ✅ |
96
+ | closeConnection (ReadOnly) | ✅ | ✅ | ❌ | ❌ | since 4.1.0-7
97
+ | isConnection (ReadWrite) | ✅ | ✅ | ✅ | ✅ |
98
+ | isConnection (ReadOnly) | ✅ | ✅ | ❌ | ❌ | since 4.1.0-7
96
99
  | open (non-encrypted DB) | ✅ | ✅ | ✅ | ✅ |
97
100
  | open (encrypted DB) | ✅ | ✅ | ❌ | ❌ |
98
101
  | close | ✅ | ✅ | ✅ | ✅ |
@@ -4,12 +4,10 @@ import android.content.Context;
4
4
  import android.content.SharedPreferences;
5
5
  import android.util.Log;
6
6
  import android.widget.Toast;
7
-
8
7
  import androidx.biometric.BiometricManager;
9
8
  import androidx.biometric.BiometricPrompt;
10
9
  import androidx.security.crypto.EncryptedSharedPreferences;
11
10
  import androidx.security.crypto.MasterKey;
12
-
13
11
  import com.getcapacitor.JSArray;
14
12
  import com.getcapacitor.JSObject;
15
13
  import com.getcapacitor.PluginCall;
@@ -24,10 +22,6 @@ import com.getcapacitor.community.database.sqlite.SQLite.UtilsMigrate;
24
22
  import com.getcapacitor.community.database.sqlite.SQLite.UtilsNCDatabase;
25
23
  import com.getcapacitor.community.database.sqlite.SQLite.UtilsSQLite;
26
24
  import com.getcapacitor.community.database.sqlite.SQLite.UtilsSecret;
27
-
28
- import org.json.JSONException;
29
- import org.json.JSONObject;
30
-
31
25
  import java.io.File;
32
26
  import java.security.KeyStore;
33
27
  import java.util.ArrayList;
@@ -39,6 +33,8 @@ import java.util.HashSet;
39
33
  import java.util.Hashtable;
40
34
  import java.util.Map;
41
35
  import java.util.Set;
36
+ import org.json.JSONException;
37
+ import org.json.JSONObject;
42
38
 
43
39
  public class CapacitorSQLite {
44
40
 
@@ -87,10 +83,10 @@ public class CapacitorSQLite {
87
83
  Enumeration<String> aliases = ks.aliases();
88
84
  if (aliases.hasMoreElements()) {
89
85
  masterKeyAlias =
90
- new MasterKey.Builder(context)
91
- .setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
92
- .setUserAuthenticationRequired(true, VALIDITY_DURATION)
93
- .build();
86
+ new MasterKey.Builder(context)
87
+ .setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
88
+ .setUserAuthenticationRequired(true, VALIDITY_DURATION)
89
+ .build();
94
90
  } else {
95
91
  masterKeyAlias = new MasterKey.Builder(context).setKeyScheme(MasterKey.KeyScheme.AES256_GCM).build();
96
92
  }
@@ -145,13 +141,13 @@ public class CapacitorSQLite {
145
141
  try {
146
142
  // get instance of the EncryptedSharedPreferences class
147
143
  this.sharedPreferences =
148
- EncryptedSharedPreferences.create(
149
- context,
150
- "sqlite_encrypted_shared_prefs",
151
- masterKeyAlias,
152
- EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
153
- EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
154
- );
144
+ EncryptedSharedPreferences.create(
145
+ context,
146
+ "sqlite_encrypted_shared_prefs",
147
+ masterKeyAlias,
148
+ EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
149
+ EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
150
+ );
155
151
  this.uSecret = new UtilsSecret(this.context, this.sharedPreferences);
156
152
  } catch (Exception e) {
157
153
  throw new Exception(e.getMessage());
@@ -299,11 +295,18 @@ public class CapacitorSQLite {
299
295
  * @param vUpgObject
300
296
  * @throws Exception
301
297
  */
302
- public void createConnection(String dbName, boolean encrypted, String mode, int version, Dictionary<Integer, JSONObject> vUpgObject)
303
- throws Exception {
298
+ public void createConnection(
299
+ String dbName,
300
+ boolean encrypted,
301
+ String mode,
302
+ int version,
303
+ Dictionary<Integer, JSONObject> vUpgObject,
304
+ Boolean readonly
305
+ ) throws Exception {
304
306
  dbName = getDatabaseName(dbName);
307
+ String connName = readonly ? "RO_" + dbName : "RW_" + dbName;
305
308
  // check if connection already exists
306
- Database conn = dbDict.get(dbName);
309
+ Database conn = dbDict.get(connName);
307
310
  if (conn != null) {
308
311
  String msg = "Connection " + dbName + " already exists";
309
312
  throw new Exception(msg);
@@ -313,17 +316,18 @@ public class CapacitorSQLite {
313
316
  }
314
317
  try {
315
318
  Database db = new Database(
316
- context,
317
- dbName + "SQLite.db",
318
- encrypted,
319
- mode,
320
- version,
321
- isEncryption,
322
- vUpgObject,
323
- sharedPreferences
319
+ context,
320
+ dbName + "SQLite.db",
321
+ encrypted,
322
+ mode,
323
+ version,
324
+ isEncryption,
325
+ vUpgObject,
326
+ sharedPreferences,
327
+ readonly
324
328
  );
325
329
  if (db != null) {
326
- dbDict.put(dbName, db);
330
+ dbDict.put(connName, db);
327
331
  return;
328
332
  } else {
329
333
  String msg = "db is null";
@@ -343,7 +347,8 @@ public class CapacitorSQLite {
343
347
  */
344
348
  public void createNCConnection(String dbPath, int version) throws Exception {
345
349
  // check if connection already exists
346
- Database conn = dbDict.get(dbPath);
350
+ String connName = "RO_" + dbPath;
351
+ Database conn = dbDict.get(connName);
347
352
  if (conn != null) {
348
353
  String msg = "Connection " + dbPath + " already exists";
349
354
  throw new Exception(msg);
@@ -355,14 +360,15 @@ public class CapacitorSQLite {
355
360
  throw new Exception(msg);
356
361
  }
357
362
  Database db = new Database(
358
- context,
359
- dbPath,
360
- false,
361
- "no-encryption",
362
- version,
363
- isEncryption,
364
- new Hashtable<>(),
365
- sharedPreferences
363
+ context,
364
+ dbPath,
365
+ false,
366
+ "no-encryption",
367
+ version,
368
+ isEncryption,
369
+ new Hashtable<>(),
370
+ sharedPreferences,
371
+ true
366
372
  );
367
373
  if (db != null) {
368
374
  dbDict.put(dbPath, db);
@@ -382,9 +388,10 @@ public class CapacitorSQLite {
382
388
  * @param dbName
383
389
  * @throws Exception
384
390
  */
385
- public void open(String dbName) throws Exception {
391
+ public void open(String dbName, Boolean readonly) throws Exception {
386
392
  dbName = getDatabaseName(dbName);
387
- Database db = dbDict.get(dbName);
393
+ String connName = readonly ? "RO_" + dbName : "RW_" + dbName;
394
+ Database db = dbDict.get(connName);
388
395
  if (db != null) {
389
396
  try {
390
397
  db.open();
@@ -404,9 +411,10 @@ public class CapacitorSQLite {
404
411
  * @param dbName
405
412
  * @throws Exception
406
413
  */
407
- public void close(String dbName) throws Exception {
414
+ public void close(String dbName, Boolean readonly) throws Exception {
408
415
  dbName = getDatabaseName(dbName);
409
- Database db = dbDict.get(dbName);
416
+ String connName = readonly ? "RO_" + dbName : "RW_" + dbName;
417
+ Database db = dbDict.get(connName);
410
418
  if (db != null) {
411
419
  if (db.isOpen()) {
412
420
  if (!db.inTransaction()) {
@@ -437,9 +445,10 @@ public class CapacitorSQLite {
437
445
  * @return String
438
446
  * @throws Exception
439
447
  */
440
- public String getUrl(String dbName) throws Exception {
448
+ public String getUrl(String dbName, Boolean readonly) throws Exception {
441
449
  dbName = getDatabaseName(dbName);
442
- Database db = dbDict.get(dbName);
450
+ String connName = readonly ? "RO_" + dbName : "RW_" + dbName;
451
+ Database db = dbDict.get(connName);
443
452
  if (db != null) {
444
453
  try {
445
454
  String url = db.getUrl();
@@ -460,9 +469,10 @@ public class CapacitorSQLite {
460
469
  * @return Integer
461
470
  * @throws Exception
462
471
  */
463
- public Integer getVersion(String dbName) throws Exception {
472
+ public Integer getVersion(String dbName, Boolean readonly) throws Exception {
464
473
  dbName = getDatabaseName(dbName);
465
- Database db = dbDict.get(dbName);
474
+ String connName = readonly ? "RO_" + dbName : "RW_" + dbName;
475
+ Database db = dbDict.get(connName);
466
476
  if (db != null) {
467
477
  try {
468
478
  Integer version = db.getVersion();
@@ -483,16 +493,17 @@ public class CapacitorSQLite {
483
493
  * @throws Exception
484
494
  */
485
495
  public void closeNCConnection(String dbPath) throws Exception {
496
+ String connName = "RO_" + dbPath;
486
497
  Database db = dbDict.get(dbPath);
487
498
  if (db != null) {
488
499
  if (db.isOpen()) {
489
500
  try {
490
- close(dbPath);
501
+ db.close();
491
502
  } catch (Exception e) {
492
503
  throw new Exception(e.getMessage());
493
504
  }
494
505
  }
495
- dbDict.remove(dbPath);
506
+ dbDict.remove(connName);
496
507
  return;
497
508
  } else {
498
509
  String msg = "No available connection for database " + dbPath;
@@ -506,18 +517,19 @@ public class CapacitorSQLite {
506
517
  * @param dbName
507
518
  * @throws Exception
508
519
  */
509
- public void closeConnection(String dbName) throws Exception {
520
+ public void closeConnection(String dbName, Boolean readonly) throws Exception {
510
521
  dbName = getDatabaseName(dbName);
511
- Database db = dbDict.get(dbName);
522
+ String connName = readonly ? "RO_" + dbName : "RW_" + dbName;
523
+ Database db = dbDict.get(connName);
512
524
  if (db != null) {
513
525
  if (db.isOpen()) {
514
526
  try {
515
- close(dbName);
527
+ db.close();
516
528
  } catch (Exception e) {
517
529
  throw new Exception(e.getMessage());
518
530
  }
519
531
  }
520
- dbDict.remove(dbName);
532
+ dbDict.remove(connName);
521
533
  return;
522
534
  } else {
523
535
  String msg = "No available connection for database " + dbName;
@@ -599,9 +611,10 @@ public class CapacitorSQLite {
599
611
  * @param tableName
600
612
  * @throws Exception
601
613
  */
602
- public Boolean isTableExists(String dbName, String tableName) throws Exception {
614
+ public Boolean isTableExists(String dbName, String tableName, Boolean readonly) throws Exception {
603
615
  dbName = getDatabaseName(dbName);
604
- Database db = dbDict.get(dbName);
616
+ String connName = readonly ? "RO_" + dbName : "RW_" + dbName;
617
+ Database db = dbDict.get(connName);
605
618
  if (db != null) {
606
619
  boolean res = uJson.isTableExists(db, tableName);
607
620
  return res;
@@ -704,10 +717,14 @@ public class CapacitorSQLite {
704
717
  * @return
705
718
  * @throws Exception
706
719
  */
707
- public JSObject execute(String dbName, String statements, Boolean transaction) throws Exception {
720
+ public JSObject execute(String dbName, String statements, Boolean transaction, Boolean readonly) throws Exception {
708
721
  dbName = getDatabaseName(dbName);
709
- Database db = dbDict.get(dbName);
722
+ String connName = "RW_" + dbName;
723
+ Database db = dbDict.get(connName);
710
724
  if (db != null) {
725
+ if (readonly) {
726
+ throw new Exception("not allowed in read-only mode");
727
+ }
711
728
  if (!db.isNCDB() && db.isOpen()) {
712
729
  // convert string in string[]
713
730
  String[] sqlCmdArray = uSqlite.getStatementsArray(statements);
@@ -735,10 +752,14 @@ public class CapacitorSQLite {
735
752
  * @return
736
753
  * @throws Exception
737
754
  */
738
- public JSObject executeSet(String dbName, JSArray set, Boolean transaction) throws Exception {
755
+ public JSObject executeSet(String dbName, JSArray set, Boolean transaction, Boolean readonly) throws Exception {
739
756
  dbName = getDatabaseName(dbName);
740
- Database db = dbDict.get(dbName);
757
+ String connName = "RW_" + dbName;
758
+ Database db = dbDict.get(connName);
741
759
  if (db != null) {
760
+ if (readonly) {
761
+ throw new Exception("not allowed in read-only mode");
762
+ }
742
763
  if (!db.isNCDB() && db.isOpen()) {
743
764
  try {
744
765
  JSObject res = db.executeSet(set, transaction);
@@ -765,11 +786,15 @@ public class CapacitorSQLite {
765
786
  * @return
766
787
  * @throws Exception
767
788
  */
768
- public JSObject run(String dbName, String statement, JSArray values, Boolean transaction) throws Exception {
789
+ public JSObject run(String dbName, String statement, JSArray values, Boolean transaction, Boolean readonly) throws Exception {
769
790
  JSObject res;
770
791
  dbName = getDatabaseName(dbName);
771
- Database db = dbDict.get(dbName);
792
+ String connName = "RW_" + dbName;
793
+ Database db = dbDict.get(connName);
772
794
  if (db != null) {
795
+ if (readonly) {
796
+ throw new Exception("not allowed in read-only mode");
797
+ }
773
798
  if (!db.isNCDB() && db.isOpen()) {
774
799
  if (values.length() > 0) {
775
800
  try {
@@ -808,10 +833,11 @@ public class CapacitorSQLite {
808
833
  * @return
809
834
  * @throws Exception
810
835
  */
811
- public JSArray query(String dbName, String statement, JSArray values) throws Exception {
836
+ public JSArray query(String dbName, String statement, JSArray values, Boolean readonly) throws Exception {
812
837
  JSArray res;
813
838
  dbName = getDatabaseName(dbName);
814
- Database db = dbDict.get(dbName);
839
+ String connName = readonly ? "RO_" + dbName : "RW_" + dbName;
840
+ Database db = dbDict.get(connName);
815
841
  if (db != null) {
816
842
  if (db.isOpen()) {
817
843
  if (values.length() > 0) {
@@ -842,10 +868,10 @@ public class CapacitorSQLite {
842
868
  }
843
869
  }
844
870
 
845
- public JSArray getTableList(String dbName) throws Exception {
871
+ public JSArray getTableList(String dbName, Boolean readonly) throws Exception {
846
872
  JSArray res;
847
- dbName = getDatabaseName(dbName);
848
- Database db = dbDict.get(dbName);
873
+ String connName = readonly ? "RO_" + dbName : "RW_" + dbName;
874
+ Database db = dbDict.get(connName);
849
875
  if (db != null) {
850
876
  if (db.isOpen()) {
851
877
  res = db.getTableNames();
@@ -860,9 +886,10 @@ public class CapacitorSQLite {
860
886
  }
861
887
  }
862
888
 
863
- public Boolean isDBExists(String dbName) throws Exception {
889
+ public Boolean isDBExists(String dbName, Boolean readonly) throws Exception {
864
890
  dbName = getDatabaseName(dbName);
865
- Database db = dbDict.get(dbName);
891
+ String connName = readonly ? "RO_" + dbName : "RW_" + dbName;
892
+ Database db = dbDict.get(connName);
866
893
  if (db != null) {
867
894
  File databaseFile = context.getDatabasePath(dbName + "SQLite.db");
868
895
  return databaseFile.exists();
@@ -872,9 +899,10 @@ public class CapacitorSQLite {
872
899
  }
873
900
  }
874
901
 
875
- public Boolean isDBOpen(String dbName) throws Exception {
902
+ public Boolean isDBOpen(String dbName, Boolean readonly) throws Exception {
876
903
  dbName = getDatabaseName(dbName);
877
- Database db = dbDict.get(dbName);
904
+ String connName = readonly ? "RO_" + dbName : "RW_" + dbName;
905
+ Database db = dbDict.get(connName);
878
906
  if (db != null) {
879
907
  Boolean isOpen = db.isOpen();
880
908
  return isOpen;
@@ -884,10 +912,14 @@ public class CapacitorSQLite {
884
912
  }
885
913
  }
886
914
 
887
- public void deleteDatabase(String dbName) throws Exception {
915
+ public void deleteDatabase(String dbName, Boolean readonly) throws Exception {
888
916
  dbName = getDatabaseName(dbName);
889
- Database db = dbDict.get(dbName);
917
+ String connName = "RW_" + dbName;
918
+ Database db = dbDict.get(connName);
890
919
  if (db != null) {
920
+ if (readonly) {
921
+ throw new Exception("not allowed in read-only mode");
922
+ }
891
923
  try {
892
924
  db.deleteDB(dbName + "SQLite.db");
893
925
  return;
@@ -900,10 +932,14 @@ public class CapacitorSQLite {
900
932
  }
901
933
  }
902
934
 
903
- public JSObject createSyncTable(String dbName) throws Exception {
935
+ public JSObject createSyncTable(String dbName, Boolean readonly) throws Exception {
904
936
  dbName = getDatabaseName(dbName);
905
- Database db = dbDict.get(dbName);
937
+ String connName = "RW_" + dbName;
938
+ Database db = dbDict.get(connName);
906
939
  if (db != null) {
940
+ if (readonly) {
941
+ throw new Exception("not allowed in read-only mode");
942
+ }
907
943
  try {
908
944
  if (!db.isOpen()) {
909
945
  String msg = "CreateSyncTable: db not opened";
@@ -920,10 +956,14 @@ public class CapacitorSQLite {
920
956
  }
921
957
  }
922
958
 
923
- public void setSyncDate(String dbName, String syncDate) throws Exception {
959
+ public void setSyncDate(String dbName, String syncDate, Boolean readonly) throws Exception {
924
960
  dbName = getDatabaseName(dbName);
925
- Database db = dbDict.get(dbName);
961
+ String connName = "RW_" + dbName;
962
+ Database db = dbDict.get(connName);
926
963
  if (db != null) {
964
+ if (readonly) {
965
+ throw new Exception("not allowed in read-only mode");
966
+ }
927
967
  try {
928
968
  if (!db.isOpen()) {
929
969
  String msg = "SetSyncDate: db not opened";
@@ -940,9 +980,10 @@ public class CapacitorSQLite {
940
980
  }
941
981
  }
942
982
 
943
- public Long getSyncDate(String dbName) throws Exception {
983
+ public Long getSyncDate(String dbName, Boolean readonly) throws Exception {
944
984
  dbName = getDatabaseName(dbName);
945
- Database db = dbDict.get(dbName);
985
+ String connName = readonly ? "RO_" + dbName : "RW_" + dbName;
986
+ Database db = dbDict.get(connName);
946
987
  if (db != null) {
947
988
  try {
948
989
  if (!db.isOpen()) {
@@ -1017,7 +1058,17 @@ public class CapacitorSQLite {
1017
1058
  if (encrypted) {
1018
1059
  inMode = "secret";
1019
1060
  }
1020
- Database db = new Database(context, dbName, encrypted, inMode, dbVersion, isEncryption, new Hashtable<>(), sharedPreferences);
1061
+ Database db = new Database(
1062
+ context,
1063
+ dbName,
1064
+ encrypted,
1065
+ inMode,
1066
+ dbVersion,
1067
+ isEncryption,
1068
+ new Hashtable<>(),
1069
+ sharedPreferences,
1070
+ false
1071
+ );
1021
1072
  if (overwrite && mode.equals("full")) {
1022
1073
  Boolean isExists = this.uFile.isFileExists(context, dbName);
1023
1074
  if (isExists) {
@@ -1059,9 +1110,10 @@ public class CapacitorSQLite {
1059
1110
  }
1060
1111
  }
1061
1112
 
1062
- public JSObject exportToJson(String dbName, String expMode) throws Exception {
1113
+ public JSObject exportToJson(String dbName, String expMode, Boolean readonly) throws Exception {
1063
1114
  dbName = getDatabaseName(dbName);
1064
- Database db = dbDict.get(dbName);
1115
+ String connName = readonly ? "RO_" + dbName : "RW_" + dbName;
1116
+ Database db = dbDict.get(connName);
1065
1117
  if (db != null) {
1066
1118
  try {
1067
1119
  if (!db.isOpen()) {
@@ -1088,10 +1140,14 @@ public class CapacitorSQLite {
1088
1140
  }
1089
1141
  }
1090
1142
 
1091
- public void deleteExportedRows(String dbName) throws Exception {
1143
+ public void deleteExportedRows(String dbName, Boolean readonly) throws Exception {
1092
1144
  dbName = getDatabaseName(dbName);
1093
- Database db = dbDict.get(dbName);
1145
+ String connName = "RW_" + dbName;
1146
+ Database db = dbDict.get(connName);
1094
1147
  if (db != null) {
1148
+ if (readonly) {
1149
+ throw new Exception("not allowed in read-only mode");
1150
+ }
1095
1151
  try {
1096
1152
  if (!db.isOpen()) {
1097
1153
  String msg = "deleteExportedRows: db not opened";
@@ -1136,7 +1192,12 @@ public class CapacitorSQLite {
1136
1192
  Enumeration<String> connections = dbDict.keys();
1137
1193
  while (connections.hasMoreElements()) {
1138
1194
  String dbName = connections.nextElement();
1139
- closeConnection(dbName);
1195
+ Boolean readonly = false;
1196
+ if (dbName.substring(0, 3).equals("RO_")) {
1197
+ readonly = true;
1198
+ }
1199
+ dbName = dbName.substring(3);
1200
+ closeConnection(dbName, readonly);
1140
1201
  }
1141
1202
  } catch (Exception e) {
1142
1203
  String msg = "close all connections " + e.getMessage();