@capacitor-community/sqlite 4.1.0-3 → 4.1.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/README.md +73 -160
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLite.java +77 -70
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java +73 -88
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsUpgrade.java +45 -319
- package/dist/esm/definitions.d.ts +3 -6
- package/dist/esm/definitions.js +2 -4
- package/dist/esm/definitions.js.map +1 -1
- package/dist/plugin.cjs.js +2 -4
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +2 -4
- package/dist/plugin.js.map +1 -1
- package/electron/dist/plugin.js +47 -332
- package/electron/dist/plugin.js.map +1 -1
- package/ios/Plugin/CapacitorSQLite.swift +5 -7
- package/ios/Plugin/Database.swift +8 -7
- package/ios/Plugin/Utils/UtilsUpgrade.swift +46 -370
- package/package.json +2 -2
package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLite.java
CHANGED
|
@@ -2,14 +2,14 @@ package com.getcapacitor.community.database.sqlite;
|
|
|
2
2
|
|
|
3
3
|
import android.content.Context;
|
|
4
4
|
import android.content.SharedPreferences;
|
|
5
|
-
import android.text.TextUtils;
|
|
6
5
|
import android.util.Log;
|
|
7
6
|
import android.widget.Toast;
|
|
7
|
+
|
|
8
8
|
import androidx.biometric.BiometricManager;
|
|
9
9
|
import androidx.biometric.BiometricPrompt;
|
|
10
10
|
import androidx.security.crypto.EncryptedSharedPreferences;
|
|
11
11
|
import androidx.security.crypto.MasterKey;
|
|
12
|
-
|
|
12
|
+
|
|
13
13
|
import com.getcapacitor.JSArray;
|
|
14
14
|
import com.getcapacitor.JSObject;
|
|
15
15
|
import com.getcapacitor.PluginCall;
|
|
@@ -24,14 +24,13 @@ import com.getcapacitor.community.database.sqlite.SQLite.UtilsMigrate;
|
|
|
24
24
|
import com.getcapacitor.community.database.sqlite.SQLite.UtilsNCDatabase;
|
|
25
25
|
import com.getcapacitor.community.database.sqlite.SQLite.UtilsSQLite;
|
|
26
26
|
import com.getcapacitor.community.database.sqlite.SQLite.UtilsSecret;
|
|
27
|
+
|
|
28
|
+
import org.json.JSONException;
|
|
29
|
+
import org.json.JSONObject;
|
|
30
|
+
|
|
27
31
|
import java.io.File;
|
|
28
|
-
import java.io.IOException;
|
|
29
|
-
import java.nio.charset.Charset;
|
|
30
|
-
import java.nio.charset.StandardCharsets;
|
|
31
|
-
import java.security.GeneralSecurityException;
|
|
32
32
|
import java.security.KeyStore;
|
|
33
33
|
import java.util.ArrayList;
|
|
34
|
-
import java.util.Arrays;
|
|
35
34
|
import java.util.Collections;
|
|
36
35
|
import java.util.Dictionary;
|
|
37
36
|
import java.util.Enumeration;
|
|
@@ -40,32 +39,28 @@ import java.util.HashSet;
|
|
|
40
39
|
import java.util.Hashtable;
|
|
41
40
|
import java.util.Map;
|
|
42
41
|
import java.util.Set;
|
|
43
|
-
import javax.crypto.BadPaddingException;
|
|
44
|
-
import javax.crypto.IllegalBlockSizeException;
|
|
45
|
-
import org.json.JSONException;
|
|
46
|
-
import org.json.JSONObject;
|
|
47
42
|
|
|
48
43
|
public class CapacitorSQLite {
|
|
49
44
|
|
|
50
45
|
private static final String TAG = CapacitorSQLite.class.getName();
|
|
51
|
-
private Context context;
|
|
52
|
-
private Dictionary<String, Database> dbDict = new Hashtable<>();
|
|
53
|
-
private UtilsSQLite uSqlite = new UtilsSQLite();
|
|
54
|
-
private UtilsFile uFile = new UtilsFile();
|
|
55
|
-
private UtilsJson uJson = new UtilsJson();
|
|
56
|
-
private UtilsMigrate uMigrate = new UtilsMigrate();
|
|
57
|
-
private UtilsNCDatabase uNCDatabase = new UtilsNCDatabase();
|
|
46
|
+
private final Context context;
|
|
47
|
+
private final Dictionary<String, Database> dbDict = new Hashtable<>();
|
|
48
|
+
private final UtilsSQLite uSqlite = new UtilsSQLite();
|
|
49
|
+
private final UtilsFile uFile = new UtilsFile();
|
|
50
|
+
private final UtilsJson uJson = new UtilsJson();
|
|
51
|
+
private final UtilsMigrate uMigrate = new UtilsMigrate();
|
|
52
|
+
private final UtilsNCDatabase uNCDatabase = new UtilsNCDatabase();
|
|
58
53
|
private UtilsSecret uSecret;
|
|
59
54
|
private SharedPreferences sharedPreferences = null;
|
|
60
55
|
private MasterKey masterKeyAlias;
|
|
61
56
|
private BiometricManager biometricManager;
|
|
62
|
-
private SqliteConfig config;
|
|
57
|
+
private final SqliteConfig config;
|
|
63
58
|
private Boolean isEncryption = true;
|
|
64
59
|
private Boolean biometricAuth = false;
|
|
65
|
-
private String biometricTitle;
|
|
66
|
-
private String biometricSubTitle;
|
|
67
|
-
private int VALIDITY_DURATION = 5;
|
|
68
|
-
private RetHandler rHandler = new RetHandler();
|
|
60
|
+
private final String biometricTitle;
|
|
61
|
+
private final String biometricSubTitle;
|
|
62
|
+
private final int VALIDITY_DURATION = 5;
|
|
63
|
+
private final RetHandler rHandler = new RetHandler();
|
|
69
64
|
private PluginCall call;
|
|
70
65
|
|
|
71
66
|
public CapacitorSQLite(Context context, SqliteConfig config) throws Exception {
|
|
@@ -92,10 +87,10 @@ public class CapacitorSQLite {
|
|
|
92
87
|
Enumeration<String> aliases = ks.aliases();
|
|
93
88
|
if (aliases.hasMoreElements()) {
|
|
94
89
|
masterKeyAlias =
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
90
|
+
new MasterKey.Builder(context)
|
|
91
|
+
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
|
|
92
|
+
.setUserAuthenticationRequired(true, VALIDITY_DURATION)
|
|
93
|
+
.build();
|
|
99
94
|
} else {
|
|
100
95
|
masterKeyAlias = new MasterKey.Builder(context).setKeyScheme(MasterKey.KeyScheme.AES256_GCM).build();
|
|
101
96
|
}
|
|
@@ -150,13 +145,13 @@ public class CapacitorSQLite {
|
|
|
150
145
|
try {
|
|
151
146
|
// get instance of the EncryptedSharedPreferences class
|
|
152
147
|
this.sharedPreferences =
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
148
|
+
EncryptedSharedPreferences.create(
|
|
149
|
+
context,
|
|
150
|
+
"sqlite_encrypted_shared_prefs",
|
|
151
|
+
masterKeyAlias,
|
|
152
|
+
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
|
|
153
|
+
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
|
|
154
|
+
);
|
|
160
155
|
this.uSecret = new UtilsSecret(this.context, this.sharedPreferences);
|
|
161
156
|
} catch (Exception e) {
|
|
162
157
|
throw new Exception(e.getMessage());
|
|
@@ -165,6 +160,7 @@ public class CapacitorSQLite {
|
|
|
165
160
|
|
|
166
161
|
/**
|
|
167
162
|
* Echo
|
|
163
|
+
*
|
|
168
164
|
* @param value
|
|
169
165
|
* @return
|
|
170
166
|
*/
|
|
@@ -189,6 +185,7 @@ public class CapacitorSQLite {
|
|
|
189
185
|
|
|
190
186
|
/**
|
|
191
187
|
* SetEncryptionSecret
|
|
188
|
+
*
|
|
192
189
|
* @param passphrase
|
|
193
190
|
* @throws Exception
|
|
194
191
|
*/
|
|
@@ -209,6 +206,7 @@ public class CapacitorSQLite {
|
|
|
209
206
|
|
|
210
207
|
/**
|
|
211
208
|
* ChangeEncryptionSecret
|
|
209
|
+
*
|
|
212
210
|
* @param passphrase
|
|
213
211
|
* @param oldPassphrase
|
|
214
212
|
* @throws Exception
|
|
@@ -293,6 +291,7 @@ public class CapacitorSQLite {
|
|
|
293
291
|
|
|
294
292
|
/**
|
|
295
293
|
* CreateConnection
|
|
294
|
+
*
|
|
296
295
|
* @param dbName
|
|
297
296
|
* @param encrypted
|
|
298
297
|
* @param mode
|
|
@@ -301,7 +300,7 @@ public class CapacitorSQLite {
|
|
|
301
300
|
* @throws Exception
|
|
302
301
|
*/
|
|
303
302
|
public void createConnection(String dbName, boolean encrypted, String mode, int version, Dictionary<Integer, JSONObject> vUpgObject)
|
|
304
|
-
|
|
303
|
+
throws Exception {
|
|
305
304
|
dbName = getDatabaseName(dbName);
|
|
306
305
|
// check if connection already exists
|
|
307
306
|
Database conn = dbDict.get(dbName);
|
|
@@ -314,14 +313,14 @@ public class CapacitorSQLite {
|
|
|
314
313
|
}
|
|
315
314
|
try {
|
|
316
315
|
Database db = new Database(
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
316
|
+
context,
|
|
317
|
+
dbName + "SQLite.db",
|
|
318
|
+
encrypted,
|
|
319
|
+
mode,
|
|
320
|
+
version,
|
|
321
|
+
isEncryption,
|
|
322
|
+
vUpgObject,
|
|
323
|
+
sharedPreferences
|
|
325
324
|
);
|
|
326
325
|
if (db != null) {
|
|
327
326
|
dbDict.put(dbName, db);
|
|
@@ -337,6 +336,7 @@ public class CapacitorSQLite {
|
|
|
337
336
|
|
|
338
337
|
/**
|
|
339
338
|
* CreateNCConnection
|
|
339
|
+
*
|
|
340
340
|
* @param dbPath
|
|
341
341
|
* @param version
|
|
342
342
|
* @throws Exception
|
|
@@ -355,14 +355,14 @@ public class CapacitorSQLite {
|
|
|
355
355
|
throw new Exception(msg);
|
|
356
356
|
}
|
|
357
357
|
Database db = new Database(
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
358
|
+
context,
|
|
359
|
+
dbPath,
|
|
360
|
+
false,
|
|
361
|
+
"no-encryption",
|
|
362
|
+
version,
|
|
363
|
+
isEncryption,
|
|
364
|
+
new Hashtable<>(),
|
|
365
|
+
sharedPreferences
|
|
366
366
|
);
|
|
367
367
|
if (db != null) {
|
|
368
368
|
dbDict.put(dbPath, db);
|
|
@@ -378,6 +378,7 @@ public class CapacitorSQLite {
|
|
|
378
378
|
|
|
379
379
|
/**
|
|
380
380
|
* Open
|
|
381
|
+
*
|
|
381
382
|
* @param dbName
|
|
382
383
|
* @throws Exception
|
|
383
384
|
*/
|
|
@@ -399,6 +400,7 @@ public class CapacitorSQLite {
|
|
|
399
400
|
|
|
400
401
|
/**
|
|
401
402
|
* Close
|
|
403
|
+
*
|
|
402
404
|
* @param dbName
|
|
403
405
|
* @throws Exception
|
|
404
406
|
*/
|
|
@@ -430,9 +432,10 @@ public class CapacitorSQLite {
|
|
|
430
432
|
|
|
431
433
|
/**
|
|
432
434
|
* GetUrl
|
|
435
|
+
*
|
|
433
436
|
* @param dbName
|
|
434
|
-
* @throws Exception
|
|
435
437
|
* @return String
|
|
438
|
+
* @throws Exception
|
|
436
439
|
*/
|
|
437
440
|
public String getUrl(String dbName) throws Exception {
|
|
438
441
|
dbName = getDatabaseName(dbName);
|
|
@@ -452,9 +455,10 @@ public class CapacitorSQLite {
|
|
|
452
455
|
|
|
453
456
|
/**
|
|
454
457
|
* GetVersion
|
|
458
|
+
*
|
|
455
459
|
* @param dbName
|
|
456
|
-
* @throws Exception
|
|
457
460
|
* @return Integer
|
|
461
|
+
* @throws Exception
|
|
458
462
|
*/
|
|
459
463
|
public Integer getVersion(String dbName) throws Exception {
|
|
460
464
|
dbName = getDatabaseName(dbName);
|
|
@@ -474,6 +478,7 @@ public class CapacitorSQLite {
|
|
|
474
478
|
|
|
475
479
|
/**
|
|
476
480
|
* CloseNCConnection
|
|
481
|
+
*
|
|
477
482
|
* @param dbPath
|
|
478
483
|
* @throws Exception
|
|
479
484
|
*/
|
|
@@ -497,6 +502,7 @@ public class CapacitorSQLite {
|
|
|
497
502
|
|
|
498
503
|
/**
|
|
499
504
|
* CloseConnection
|
|
505
|
+
*
|
|
500
506
|
* @param dbName
|
|
501
507
|
* @throws Exception
|
|
502
508
|
*/
|
|
@@ -565,6 +571,7 @@ public class CapacitorSQLite {
|
|
|
565
571
|
|
|
566
572
|
/**
|
|
567
573
|
* IsDatabase
|
|
574
|
+
*
|
|
568
575
|
* @param dbName
|
|
569
576
|
* @return Boolean
|
|
570
577
|
* @throws Exception
|
|
@@ -576,6 +583,7 @@ public class CapacitorSQLite {
|
|
|
576
583
|
|
|
577
584
|
/**
|
|
578
585
|
* IsNCDatabase
|
|
586
|
+
*
|
|
579
587
|
* @param dbPath
|
|
580
588
|
* @return Boolean
|
|
581
589
|
* @throws Exception
|
|
@@ -586,6 +594,7 @@ public class CapacitorSQLite {
|
|
|
586
594
|
|
|
587
595
|
/**
|
|
588
596
|
* IsTableExists
|
|
597
|
+
*
|
|
589
598
|
* @param dbName
|
|
590
599
|
* @param tableName
|
|
591
600
|
* @throws Exception
|
|
@@ -604,6 +613,7 @@ public class CapacitorSQLite {
|
|
|
604
613
|
|
|
605
614
|
/**
|
|
606
615
|
* GetDatabaseList
|
|
616
|
+
*
|
|
607
617
|
* @return JSArray
|
|
608
618
|
* @throws Exception
|
|
609
619
|
*/
|
|
@@ -623,6 +633,7 @@ public class CapacitorSQLite {
|
|
|
623
633
|
|
|
624
634
|
/**
|
|
625
635
|
* GetMigratableDbList
|
|
636
|
+
*
|
|
626
637
|
* @return JSArray
|
|
627
638
|
* @throws Exception
|
|
628
639
|
*/
|
|
@@ -642,6 +653,7 @@ public class CapacitorSQLite {
|
|
|
642
653
|
|
|
643
654
|
/**
|
|
644
655
|
* AddSQLiteSuffix
|
|
656
|
+
*
|
|
645
657
|
* @param folderPath
|
|
646
658
|
* @throws Exception
|
|
647
659
|
*/
|
|
@@ -656,7 +668,6 @@ public class CapacitorSQLite {
|
|
|
656
668
|
}
|
|
657
669
|
|
|
658
670
|
/**
|
|
659
|
-
*
|
|
660
671
|
* @param folderPath
|
|
661
672
|
* @throws Exception
|
|
662
673
|
*/
|
|
@@ -687,6 +698,7 @@ public class CapacitorSQLite {
|
|
|
687
698
|
|
|
688
699
|
/**
|
|
689
700
|
* Execute
|
|
701
|
+
*
|
|
690
702
|
* @param dbName
|
|
691
703
|
* @param statements
|
|
692
704
|
* @return
|
|
@@ -717,6 +729,7 @@ public class CapacitorSQLite {
|
|
|
717
729
|
|
|
718
730
|
/**
|
|
719
731
|
* ExecuteSet
|
|
732
|
+
*
|
|
720
733
|
* @param dbName
|
|
721
734
|
* @param set
|
|
722
735
|
* @return
|
|
@@ -745,6 +758,7 @@ public class CapacitorSQLite {
|
|
|
745
758
|
|
|
746
759
|
/**
|
|
747
760
|
* Run
|
|
761
|
+
*
|
|
748
762
|
* @param dbName
|
|
749
763
|
* @param statement
|
|
750
764
|
* @param values
|
|
@@ -787,6 +801,7 @@ public class CapacitorSQLite {
|
|
|
787
801
|
|
|
788
802
|
/**
|
|
789
803
|
* Query
|
|
804
|
+
*
|
|
790
805
|
* @param dbName
|
|
791
806
|
* @param statement
|
|
792
807
|
* @param values
|
|
@@ -850,11 +865,7 @@ public class CapacitorSQLite {
|
|
|
850
865
|
Database db = dbDict.get(dbName);
|
|
851
866
|
if (db != null) {
|
|
852
867
|
File databaseFile = context.getDatabasePath(dbName + "SQLite.db");
|
|
853
|
-
|
|
854
|
-
return true;
|
|
855
|
-
} else {
|
|
856
|
-
return false;
|
|
857
|
-
}
|
|
868
|
+
return databaseFile.exists();
|
|
858
869
|
} else {
|
|
859
870
|
String msg = "No available connection for database " + dbName;
|
|
860
871
|
throw new Exception(msg);
|
|
@@ -866,11 +877,7 @@ public class CapacitorSQLite {
|
|
|
866
877
|
Database db = dbDict.get(dbName);
|
|
867
878
|
if (db != null) {
|
|
868
879
|
Boolean isOpen = db.isOpen();
|
|
869
|
-
|
|
870
|
-
return true;
|
|
871
|
-
} else {
|
|
872
|
-
return false;
|
|
873
|
-
}
|
|
880
|
+
return isOpen;
|
|
874
881
|
} else {
|
|
875
882
|
String msg = "No available connection for database " + dbName;
|
|
876
883
|
throw new Exception(msg);
|
|
@@ -964,17 +971,17 @@ public class CapacitorSQLite {
|
|
|
964
971
|
throw new Exception(msg);
|
|
965
972
|
}
|
|
966
973
|
|
|
967
|
-
if (upgObj == null || !upgObj.has("
|
|
974
|
+
if (upgObj == null || !upgObj.has("toVersion") || !upgObj.has("statements")) {
|
|
968
975
|
String msg = "Must provide an upgrade statement";
|
|
969
|
-
msg += " {
|
|
976
|
+
msg += " {toVersion,statement}";
|
|
970
977
|
throw new Exception(msg);
|
|
971
978
|
}
|
|
972
979
|
try {
|
|
973
|
-
int
|
|
974
|
-
upgDict.put(
|
|
980
|
+
int toVersion = upgObj.getInt("toVersion");
|
|
981
|
+
upgDict.put(toVersion, upgObj);
|
|
975
982
|
return upgDict;
|
|
976
983
|
} catch (Exception e) {
|
|
977
|
-
String msg = "Must provide
|
|
984
|
+
String msg = "Must provide toVersion as Integer" + e.getMessage();
|
|
978
985
|
throw new Exception(msg);
|
|
979
986
|
}
|
|
980
987
|
}
|
|
@@ -1128,7 +1135,7 @@ public class CapacitorSQLite {
|
|
|
1128
1135
|
try {
|
|
1129
1136
|
Enumeration<String> connections = dbDict.keys();
|
|
1130
1137
|
while (connections.hasMoreElements()) {
|
|
1131
|
-
String dbName =
|
|
1138
|
+
String dbName = connections.nextElement();
|
|
1132
1139
|
closeConnection(dbName);
|
|
1133
1140
|
}
|
|
1134
1141
|
} catch (Exception e) {
|
package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java
CHANGED
|
@@ -8,21 +8,27 @@ import static android.database.Cursor.FIELD_TYPE_STRING;
|
|
|
8
8
|
|
|
9
9
|
import android.content.Context;
|
|
10
10
|
import android.content.SharedPreferences;
|
|
11
|
-
import android.text.TextUtils;
|
|
12
11
|
import android.util.Log;
|
|
12
|
+
|
|
13
13
|
import androidx.sqlite.db.SimpleSQLiteQuery;
|
|
14
14
|
import androidx.sqlite.db.SupportSQLiteDatabase;
|
|
15
15
|
import androidx.sqlite.db.SupportSQLiteStatement;
|
|
16
|
+
|
|
16
17
|
import com.getcapacitor.JSArray;
|
|
17
18
|
import com.getcapacitor.JSObject;
|
|
18
|
-
import com.getcapacitor.community.database.sqlite.SQLite.GlobalSQLite;
|
|
19
19
|
import com.getcapacitor.community.database.sqlite.SQLite.ImportExportJson.ExportToJson;
|
|
20
20
|
import com.getcapacitor.community.database.sqlite.SQLite.ImportExportJson.ImportFromJson;
|
|
21
|
-
import com.getcapacitor.community.database.sqlite.SQLite.ImportExportJson.JsonIndex;
|
|
22
21
|
import com.getcapacitor.community.database.sqlite.SQLite.ImportExportJson.JsonSQLite;
|
|
23
22
|
import com.getcapacitor.community.database.sqlite.SQLite.ImportExportJson.UtilsJson;
|
|
24
|
-
|
|
25
|
-
import
|
|
23
|
+
|
|
24
|
+
import net.sqlcipher.Cursor;
|
|
25
|
+
import net.sqlcipher.database.SQLiteDatabase;
|
|
26
|
+
import net.sqlcipher.database.SQLiteException;
|
|
27
|
+
|
|
28
|
+
import org.json.JSONArray;
|
|
29
|
+
import org.json.JSONException;
|
|
30
|
+
import org.json.JSONObject;
|
|
31
|
+
|
|
26
32
|
import java.io.File;
|
|
27
33
|
import java.text.SimpleDateFormat;
|
|
28
34
|
import java.util.ArrayList;
|
|
@@ -31,51 +37,44 @@ import java.util.Collections;
|
|
|
31
37
|
import java.util.Date;
|
|
32
38
|
import java.util.Dictionary;
|
|
33
39
|
import java.util.Hashtable;
|
|
34
|
-
import java.util.Iterator;
|
|
35
40
|
import java.util.List;
|
|
36
|
-
import net.sqlcipher.Cursor;
|
|
37
|
-
import net.sqlcipher.database.SQLiteDatabase;
|
|
38
|
-
import net.sqlcipher.database.SQLiteException;
|
|
39
|
-
import org.json.JSONArray;
|
|
40
|
-
import org.json.JSONException;
|
|
41
|
-
import org.json.JSONObject;
|
|
42
41
|
|
|
43
42
|
public class Database {
|
|
44
43
|
|
|
45
44
|
private static final String TAG = Database.class.getName();
|
|
46
45
|
private Boolean _isOpen = false;
|
|
47
|
-
private String _dbName;
|
|
48
|
-
private Context _context;
|
|
49
|
-
private String _mode;
|
|
46
|
+
private final String _dbName;
|
|
47
|
+
private final Context _context;
|
|
48
|
+
private final String _mode;
|
|
50
49
|
private String _secret;
|
|
51
|
-
private Boolean _encrypted;
|
|
52
|
-
private Boolean _isEncryption;
|
|
53
|
-
private SharedPreferences _sharedPreferences;
|
|
54
|
-
private File _file;
|
|
55
|
-
private int _version;
|
|
56
|
-
private GlobalSQLite _globVar;
|
|
50
|
+
private final Boolean _encrypted;
|
|
51
|
+
private final Boolean _isEncryption;
|
|
52
|
+
private final SharedPreferences _sharedPreferences;
|
|
53
|
+
private final File _file;
|
|
54
|
+
private final int _version;
|
|
55
|
+
private final GlobalSQLite _globVar;
|
|
57
56
|
private SupportSQLiteDatabase _db = null;
|
|
58
|
-
private UtilsSQLite _uSqlite;
|
|
59
|
-
private UtilsSQLCipher _uCipher;
|
|
60
|
-
private UtilsFile _uFile;
|
|
61
|
-
private UtilsJson _uJson;
|
|
62
|
-
private UtilsUpgrade _uUpg;
|
|
63
|
-
private UtilsDrop _uDrop;
|
|
64
|
-
private UtilsSecret _uSecret;
|
|
57
|
+
private final UtilsSQLite _uSqlite;
|
|
58
|
+
private final UtilsSQLCipher _uCipher;
|
|
59
|
+
private final UtilsFile _uFile;
|
|
60
|
+
private final UtilsJson _uJson;
|
|
61
|
+
private final UtilsUpgrade _uUpg;
|
|
62
|
+
private final UtilsDrop _uDrop;
|
|
63
|
+
private final UtilsSecret _uSecret;
|
|
65
64
|
private Dictionary<Integer, JSONObject> _vUpgObject = new Hashtable<>();
|
|
66
|
-
private ImportFromJson fromJson = new ImportFromJson();
|
|
67
|
-
private ExportToJson toJson = new ExportToJson();
|
|
65
|
+
private final ImportFromJson fromJson = new ImportFromJson();
|
|
66
|
+
private final ExportToJson toJson = new ExportToJson();
|
|
68
67
|
private Boolean ncDB = false;
|
|
69
68
|
|
|
70
69
|
public Database(
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
70
|
+
Context context,
|
|
71
|
+
String dbName,
|
|
72
|
+
Boolean encrypted,
|
|
73
|
+
String mode,
|
|
74
|
+
int version,
|
|
75
|
+
Boolean isEncryption,
|
|
76
|
+
Dictionary<Integer, JSONObject> vUpgObject,
|
|
77
|
+
SharedPreferences sharedPreferences
|
|
79
78
|
) {
|
|
80
79
|
this._context = context;
|
|
81
80
|
this._dbName = dbName;
|
|
@@ -155,8 +154,8 @@ public class Database {
|
|
|
155
154
|
int curVersion;
|
|
156
155
|
|
|
157
156
|
String password = _uSecret != null && _encrypted && (_mode.equals("secret") || _mode.equals("encryption"))
|
|
158
|
-
|
|
159
|
-
|
|
157
|
+
? _uSecret.getPassphrase()
|
|
158
|
+
: "";
|
|
160
159
|
if (_mode.equals("encryption")) {
|
|
161
160
|
if (_isEncryption) {
|
|
162
161
|
try {
|
|
@@ -190,11 +189,7 @@ public class Database {
|
|
|
190
189
|
}
|
|
191
190
|
if (!isNCDB()) {
|
|
192
191
|
try {
|
|
193
|
-
curVersion = _db.getVersion();
|
|
194
|
-
if (curVersion == 0) {
|
|
195
|
-
_db.setVersion(1);
|
|
196
|
-
curVersion = _db.getVersion();
|
|
197
|
-
}
|
|
192
|
+
curVersion = _db.getVersion(); // default 0
|
|
198
193
|
} catch (IllegalStateException e) {
|
|
199
194
|
String msg = "Failed in get/setVersion " + e.getMessage();
|
|
200
195
|
Log.v(TAG, msg);
|
|
@@ -211,7 +206,10 @@ public class Database {
|
|
|
211
206
|
if (_version > curVersion && _vUpgObject != null && _vUpgObject.size() > 0) {
|
|
212
207
|
// if (_vUpgObject != null && _vUpgObject.size() > 0) {
|
|
213
208
|
try {
|
|
214
|
-
|
|
209
|
+
this._uFile.copyFile(_context, _dbName, "backup-" + _dbName);
|
|
210
|
+
|
|
211
|
+
_uUpg.onUpgrade(this, _vUpgObject, curVersion, _version);
|
|
212
|
+
|
|
215
213
|
boolean ret = _uFile.deleteBackupDB(_context, _dbName);
|
|
216
214
|
if (!ret) {
|
|
217
215
|
String msg = "Failed in deleteBackupDB backup-\" + _dbName";
|
|
@@ -230,17 +228,6 @@ public class Database {
|
|
|
230
228
|
_db = null;
|
|
231
229
|
throw new Exception(msg);
|
|
232
230
|
}
|
|
233
|
-
/* } else {
|
|
234
|
-
try {
|
|
235
|
-
_db.setVersion(_version);
|
|
236
|
-
} catch (Exception e) {
|
|
237
|
-
String msg = e.getMessage() + "Failed in setting version " + _version;
|
|
238
|
-
close();
|
|
239
|
-
_db = null;
|
|
240
|
-
throw new Exception(msg);
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
*/
|
|
244
231
|
}
|
|
245
232
|
_isOpen = true;
|
|
246
233
|
return;
|
|
@@ -306,11 +293,7 @@ public class Database {
|
|
|
306
293
|
* @return the existence of the database on folder
|
|
307
294
|
*/
|
|
308
295
|
public boolean isDBExists() {
|
|
309
|
-
|
|
310
|
-
return true;
|
|
311
|
-
} else {
|
|
312
|
-
return false;
|
|
313
|
-
}
|
|
296
|
+
return _file.exists();
|
|
314
297
|
}
|
|
315
298
|
|
|
316
299
|
/**
|
|
@@ -524,7 +507,7 @@ public class Database {
|
|
|
524
507
|
// Replace DELETE by UPDATE and set sql_deleted to 1
|
|
525
508
|
Integer wIdx = statement.toUpperCase().indexOf("WHERE");
|
|
526
509
|
String preStmt = statement.substring(0, wIdx - 1);
|
|
527
|
-
String clauseStmt = statement.substring(wIdx
|
|
510
|
+
String clauseStmt = statement.substring(wIdx);
|
|
528
511
|
String tableName = preStmt.substring(("DELETE FROM").length()).trim();
|
|
529
512
|
sqlStmt = "UPDATE " + tableName + " SET sql_deleted = 1 " + clauseStmt;
|
|
530
513
|
// Find REFERENCES if any and update the sql_deleted column
|
|
@@ -738,6 +721,7 @@ public class Database {
|
|
|
738
721
|
"sql LIKE('%" +
|
|
739
722
|
tableName +
|
|
740
723
|
"%') AND sql LIKE('%ON DELETE%');";
|
|
724
|
+
|
|
741
725
|
try {
|
|
742
726
|
JSArray references = mDB.selectSQL(sqlStmt, new ArrayList<Object>());
|
|
743
727
|
ArrayList<String> retRefs = new ArrayList<String>();
|
|
@@ -895,8 +879,8 @@ public class Database {
|
|
|
895
879
|
Date date = new Date();
|
|
896
880
|
long syncTime = date.getTime() / 1000L;
|
|
897
881
|
String[] statements = {
|
|
898
|
-
|
|
899
|
-
|
|
882
|
+
"CREATE TABLE IF NOT EXISTS sync_table (" + "id INTEGER PRIMARY KEY NOT NULL," + "sync_date INTEGER);",
|
|
883
|
+
"INSERT INTO sync_table (sync_date) VALUES ('" + syncTime + "');"
|
|
900
884
|
};
|
|
901
885
|
try {
|
|
902
886
|
retObj = execute(statements);
|
|
@@ -914,50 +898,50 @@ public class Database {
|
|
|
914
898
|
}
|
|
915
899
|
|
|
916
900
|
/**
|
|
917
|
-
*
|
|
918
|
-
*
|
|
901
|
+
* GetSyncDate method
|
|
902
|
+
* get the synchronization date
|
|
919
903
|
*
|
|
920
|
-
* @param syncDate
|
|
921
904
|
* @return
|
|
905
|
+
* @throws Exception
|
|
922
906
|
*/
|
|
923
|
-
public
|
|
924
|
-
|
|
907
|
+
public Long getSyncDate() throws Exception {
|
|
908
|
+
long syncDate = 0;
|
|
925
909
|
try {
|
|
926
910
|
boolean isSyncTable = _uJson.isTableExists(this, "sync_table");
|
|
927
911
|
if (!isSyncTable) {
|
|
928
912
|
throw new Exception("No sync_table available");
|
|
929
913
|
}
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
long syncTime = date.getTime() / 1000L;
|
|
933
|
-
String[] statements = { "UPDATE sync_table SET sync_date = " + syncTime + " WHERE id = 1;" };
|
|
934
|
-
retObj = execute(statements);
|
|
935
|
-
if (retObj.getInteger("changes") != Integer.valueOf(-1)) {
|
|
936
|
-
return;
|
|
937
|
-
} else {
|
|
938
|
-
throw new Exception("changes < 0");
|
|
939
|
-
}
|
|
914
|
+
syncDate = toJson.getSyncDate(this);
|
|
915
|
+
return syncDate;
|
|
940
916
|
} catch (Exception e) {
|
|
941
917
|
throw new Exception(e.getMessage());
|
|
942
918
|
}
|
|
943
919
|
}
|
|
944
920
|
|
|
945
921
|
/**
|
|
946
|
-
*
|
|
947
|
-
*
|
|
922
|
+
* SetSyncDate Method
|
|
923
|
+
* Set the synchronization date
|
|
948
924
|
*
|
|
925
|
+
* @param syncDate
|
|
949
926
|
* @return
|
|
950
|
-
* @throws Exception
|
|
951
927
|
*/
|
|
952
|
-
public
|
|
953
|
-
|
|
928
|
+
public void setSyncDate(String syncDate) throws Exception {
|
|
929
|
+
JSObject retObj = new JSObject();
|
|
954
930
|
try {
|
|
955
931
|
boolean isSyncTable = _uJson.isTableExists(this, "sync_table");
|
|
956
932
|
if (!isSyncTable) {
|
|
957
933
|
throw new Exception("No sync_table available");
|
|
958
934
|
}
|
|
959
|
-
|
|
960
|
-
|
|
935
|
+
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
|
|
936
|
+
Date date = formatter.parse(syncDate.replaceAll("Z$", "+0000"));
|
|
937
|
+
long syncTime = date.getTime() / 1000L;
|
|
938
|
+
String[] statements = {"UPDATE sync_table SET sync_date = " + syncTime + " WHERE id = 1;"};
|
|
939
|
+
retObj = execute(statements);
|
|
940
|
+
if (retObj.getInteger("changes") != Integer.valueOf(-1)) {
|
|
941
|
+
return;
|
|
942
|
+
} else {
|
|
943
|
+
throw new Exception("changes < 0");
|
|
944
|
+
}
|
|
961
945
|
} catch (Exception e) {
|
|
962
946
|
throw new Exception(e.getMessage());
|
|
963
947
|
}
|
|
@@ -1037,7 +1021,8 @@ public class Database {
|
|
|
1037
1021
|
} catch (Exception e) {
|
|
1038
1022
|
Log.e(TAG, "Error: exportToJson " + e.getMessage());
|
|
1039
1023
|
throw new Exception(e.getMessage());
|
|
1040
|
-
} finally {
|
|
1024
|
+
} finally {
|
|
1025
|
+
}
|
|
1041
1026
|
}
|
|
1042
1027
|
|
|
1043
1028
|
/**
|