@capacitor-community/sqlite 4.1.0-5 → 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.
@@ -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
- import com.getcapacitor.community.database.sqlite.SQLite.UtilsSQLCipher;
25
- import com.getcapacitor.community.database.sqlite.SQLite.UtilsSQLite;
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
- Context context,
72
- String dbName,
73
- Boolean encrypted,
74
- String mode,
75
- int version,
76
- Boolean isEncryption,
77
- Dictionary<Integer, JSONObject> vUpgObject,
78
- SharedPreferences sharedPreferences
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
- ? _uSecret.getPassphrase()
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
- _uUpg.onUpgrade(this, _context, _dbName, _vUpgObject, curVersion, _version);
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
- if (_file.exists()) {
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, statement.length());
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
- "CREATE TABLE IF NOT EXISTS sync_table (" + "id INTEGER PRIMARY KEY NOT NULL," + "sync_date INTEGER);",
899
- "INSERT INTO sync_table (sync_date) VALUES ('" + syncTime + "');"
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
- * SetSyncDate Method
918
- * Set the synchronization date
901
+ * GetSyncDate method
902
+ * get the synchronization date
919
903
  *
920
- * @param syncDate
921
904
  * @return
905
+ * @throws Exception
922
906
  */
923
- public void setSyncDate(String syncDate) throws Exception {
924
- JSObject retObj = new JSObject();
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
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
931
- Date date = formatter.parse(syncDate.replaceAll("Z$", "+0000"));
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
- * GetSyncDate method
947
- * get the synchronization date
922
+ * SetSyncDate Method
923
+ * Set the synchronization date
948
924
  *
925
+ * @param syncDate
949
926
  * @return
950
- * @throws Exception
951
927
  */
952
- public Long getSyncDate() throws Exception {
953
- long syncDate = 0;
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
- syncDate = toJson.getSyncDate(this);
960
- return syncDate;
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
  /**