@capgo/capacitor-data-storage-sqlite 6.0.59 → 7.1.0

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 (52) hide show
  1. package/CapgoCapacitorDataStorageSqlite.podspec +2 -2
  2. package/Package.swift +30 -0
  3. package/README.md +724 -0
  4. package/android/build.gradle +11 -11
  5. package/android/src/main/java/com/jeep/plugin/capacitor/capgocapacitordatastoragesqlite/CapgoCapacitorDataStorageSqlite.java +290 -320
  6. package/android/src/main/java/com/jeep/plugin/capacitor/capgocapacitordatastoragesqlite/CapgoCapacitorDataStorageSqlitePlugin.java +373 -388
  7. package/android/src/main/java/com/jeep/plugin/capacitor/capgocapacitordatastoragesqlite/RetHandler.java +97 -97
  8. package/android/src/main/java/com/jeep/plugin/capacitor/capgocapacitordatastoragesqlite/cdssUtils/Data.java +3 -3
  9. package/android/src/main/java/com/jeep/plugin/capacitor/capgocapacitordatastoragesqlite/cdssUtils/Global.java +2 -2
  10. package/android/src/main/java/com/jeep/plugin/capacitor/capgocapacitordatastoragesqlite/cdssUtils/ImportExportJson/JsonStore.java +108 -110
  11. package/android/src/main/java/com/jeep/plugin/capacitor/capgocapacitordatastoragesqlite/cdssUtils/ImportExportJson/JsonTable.java +78 -80
  12. package/android/src/main/java/com/jeep/plugin/capacitor/capgocapacitordatastoragesqlite/cdssUtils/ImportExportJson/JsonValue.java +62 -64
  13. package/android/src/main/java/com/jeep/plugin/capacitor/capgocapacitordatastoragesqlite/cdssUtils/StorageDatabaseHelper.java +557 -624
  14. package/android/src/main/java/com/jeep/plugin/capacitor/capgocapacitordatastoragesqlite/cdssUtils/UtilsSQLCipher.java +103 -144
  15. package/dist/docs.json +0 -64
  16. package/dist/esm/definitions.d.ts +0 -19
  17. package/dist/esm/definitions.js.map +1 -1
  18. package/dist/esm/index.d.ts +2 -2
  19. package/dist/esm/index.js +5 -6
  20. package/dist/esm/index.js.map +1 -1
  21. package/dist/esm/web-utils/StorageDatabaseHelper.d.ts +2 -2
  22. package/dist/esm/web-utils/StorageDatabaseHelper.js +8 -8
  23. package/dist/esm/web-utils/StorageDatabaseHelper.js.map +1 -1
  24. package/dist/esm/web-utils/json-utils.js +15 -18
  25. package/dist/esm/web-utils/json-utils.js.map +1 -1
  26. package/dist/esm/web.d.ts +2 -3
  27. package/dist/esm/web.js +37 -44
  28. package/dist/esm/web.js.map +1 -1
  29. package/dist/plugin.cjs.js +56 -67
  30. package/dist/plugin.cjs.js.map +1 -1
  31. package/dist/plugin.js +56 -67
  32. package/dist/plugin.js.map +1 -1
  33. package/ios/{Plugin → Sources/CapgoCapacitorDataStorageSqlitePlugin}/CapgoCapacitorDataStorageSqlitePlugin.swift +27 -10
  34. package/ios/{Plugin → Sources/CapgoCapacitorDataStorageSqlitePlugin}/StorageDatabaseHelper.swift +1 -1
  35. package/ios/{Plugin → Sources/CapgoCapacitorDataStorageSqlitePlugin}/Utils/UtilsFile.swift +1 -1
  36. package/ios/{Plugin → Sources/CapgoCapacitorDataStorageSqlitePlugin}/Utils/UtilsSQLCipher.swift +3 -3
  37. package/ios/Tests/CapgoCapacitorDataStorageSqlitePluginTests/CapacitorDataStorageSqlitePluginTests.swift +13 -0
  38. package/package.json +34 -38
  39. package/electron/dist/plugin.js +0 -1044
  40. package/electron/dist/plugin.js.map +0 -1
  41. package/ios/Plugin/CapgoCapacitorDataStorageSqlitePlugin.h +0 -10
  42. package/ios/Plugin/CapgoCapacitorDataStorageSqlitePlugin.m +0 -29
  43. package/ios/Plugin/Info.plist +0 -24
  44. package/readme.md +0 -210
  45. /package/ios/{Plugin → Sources/CapgoCapacitorDataStorageSqlitePlugin}/CapgoCapacitorDataStorageSqlite.swift +0 -0
  46. /package/ios/{Plugin → Sources/CapgoCapacitorDataStorageSqlitePlugin}/Data.swift +0 -0
  47. /package/ios/{Plugin → Sources/CapgoCapacitorDataStorageSqlitePlugin}/Global.swift +0 -0
  48. /package/ios/{Plugin → Sources/CapgoCapacitorDataStorageSqlitePlugin}/ImportExportJson/JsonStore.swift +0 -0
  49. /package/ios/{Plugin → Sources/CapgoCapacitorDataStorageSqlitePlugin}/ReturnHandler.swift +0 -0
  50. /package/ios/{Plugin → Sources/CapgoCapacitorDataStorageSqlitePlugin}/Utils/Blob.swift +0 -0
  51. /package/ios/{Plugin → Sources/CapgoCapacitorDataStorageSqlitePlugin}/Utils/UtilsBinding.swift +0 -0
  52. /package/ios/{Plugin → Sources/CapgoCapacitorDataStorageSqlitePlugin}/Utils/UtilsEncryption.swift +0 -0
@@ -18,674 +18,607 @@ import net.sqlcipher.database.SQLiteDatabase;
18
18
 
19
19
  public class StorageDatabaseHelper {
20
20
 
21
- public Boolean isOpen = false;
22
- private static final String TAG = "StorageDatabaseHelper";
23
- private Context _context;
24
- private String _tableName;
25
- private String _dbName;
26
- private Boolean _encrypted;
27
- private String _mode;
28
- private String _secret;
29
- private String _newsecret;
30
- private int _dbVersion;
31
- private File _file;
32
- private Global _globVar;
33
- private UtilsSQLCipher _uCipher;
34
- private SupportSQLiteDatabase _db = null;
35
-
36
- // Storage Table Columns
37
- private static final String COL_ID = "id";
38
- private static final String COL_NAME = "name";
39
- private static final String COL_VALUE = "value";
40
- private static final String IDX_COL_NAME = "name";
41
-
42
- public StorageDatabaseHelper(
43
- Context context,
44
- String dbName,
45
- String tableName,
46
- Boolean encrypted,
47
- String mode,
48
- int vNumber
49
- ) {
50
- this._context = context;
51
- this._dbName = dbName;
52
- this._tableName = tableName;
53
- this._dbVersion = vNumber;
54
- this._encrypted = encrypted;
55
- this._mode = mode;
56
- this._file = context.getDatabasePath(dbName);
57
- this._globVar = new Global();
58
- this._uCipher = new UtilsSQLCipher();
59
-
60
- this.InitializeSQLCipher();
61
- if (!this._file.getParentFile().exists()) {
62
- this._file.getParentFile().mkdirs();
21
+ public Boolean isOpen = false;
22
+ private static final String TAG = "StorageDatabaseHelper";
23
+ private Context _context;
24
+ private String _tableName;
25
+ private String _dbName;
26
+ private Boolean _encrypted;
27
+ private String _mode;
28
+ private String _secret;
29
+ private String _newsecret;
30
+ private int _dbVersion;
31
+ private File _file;
32
+ private Global _globVar;
33
+ private UtilsSQLCipher _uCipher;
34
+ private SupportSQLiteDatabase _db = null;
35
+
36
+ // Storage Table Columns
37
+ private static final String COL_ID = "id";
38
+ private static final String COL_NAME = "name";
39
+ private static final String COL_VALUE = "value";
40
+ private static final String IDX_COL_NAME = "name";
41
+
42
+ public StorageDatabaseHelper(Context context, String dbName, String tableName, Boolean encrypted, String mode, int vNumber) {
43
+ this._context = context;
44
+ this._dbName = dbName;
45
+ this._tableName = tableName;
46
+ this._dbVersion = vNumber;
47
+ this._encrypted = encrypted;
48
+ this._mode = mode;
49
+ this._file = context.getDatabasePath(dbName);
50
+ this._globVar = new Global();
51
+ this._uCipher = new UtilsSQLCipher();
52
+
53
+ this.InitializeSQLCipher();
54
+ if (!this._file.getParentFile().exists()) {
55
+ this._file.getParentFile().mkdirs();
56
+ }
57
+ Log.v(TAG, "&&& file path " + this._file.getAbsolutePath());
63
58
  }
64
- Log.v(TAG, "&&& file path " + this._file.getAbsolutePath());
65
- }
66
-
67
- /**
68
- * InitializeSQLCipher Method
69
- * Initialize the SQLCipher Libraries
70
- */
71
- private void InitializeSQLCipher() {
72
- Log.d(TAG, " in InitializeSQLCipher: ");
73
- SQLiteDatabase.loadLibs(_context);
74
- }
75
-
76
- /**
77
- * GetStoreName
78
- * Return the current store name
79
- * @return
80
- */
81
- public String getStoreName() {
82
- return this._dbName;
83
- }
84
-
85
- /**
86
- * Open Method
87
- * Open the store
88
- */
89
- public void open() throws Exception {
90
- int curVersion;
91
-
92
- String password = _encrypted &&
93
- (_mode.equals("secret") || _mode.equals("encryption"))
94
- ? _globVar.secret
95
- : "";
96
- if (_mode.equals("newsecret")) {
97
- try {
98
- _uCipher.changePassword(
99
- _context,
100
- _file,
101
- _globVar.secret,
102
- _globVar.newsecret
103
- );
104
- password = _globVar.newsecret;
105
- } catch (Exception e) {
106
- String msg = "Failed in change password" + e.getMessage();
107
- Log.v(TAG, msg);
108
- throw new Exception(msg);
109
- }
59
+
60
+ /**
61
+ * InitializeSQLCipher Method
62
+ * Initialize the SQLCipher Libraries
63
+ */
64
+ private void InitializeSQLCipher() {
65
+ Log.d(TAG, " in InitializeSQLCipher: ");
66
+ SQLiteDatabase.loadLibs(_context);
110
67
  }
111
- if (_mode.equals("encryption")) {
112
- try {
113
- _uCipher.encrypt(
114
- _context,
115
- _file,
116
- SQLiteDatabase.getBytes(password.toCharArray())
117
- );
118
- } catch (Exception e) {
119
- String msg = "Failed in encryption " + e.getMessage();
120
- Log.v(TAG, msg);
121
- throw new Exception(msg);
122
- }
68
+
69
+ /**
70
+ * GetStoreName
71
+ * Return the current store name
72
+ * @return
73
+ */
74
+ public String getStoreName() {
75
+ return this._dbName;
123
76
  }
124
77
 
125
- _db = SQLiteDatabase.openOrCreateDatabase(_file, password, null);
126
- if (_db != null) {
127
- if (_db.isOpen()) {
128
- try {
129
- setTable(_tableName, true);
130
- isOpen = true;
131
- return;
132
- } catch (Exception e) {
133
- isOpen = false;
134
- _db = null;
135
- throw new Exception("Store not opened " + e.getMessage());
78
+ /**
79
+ * Open Method
80
+ * Open the store
81
+ */
82
+ public void open() throws Exception {
83
+ int curVersion;
84
+
85
+ String password = _encrypted && (_mode.equals("secret") || _mode.equals("encryption")) ? _globVar.secret : "";
86
+ if (_mode.equals("newsecret")) {
87
+ try {
88
+ _uCipher.changePassword(_context, _file, _globVar.secret, _globVar.newsecret);
89
+ password = _globVar.newsecret;
90
+ } catch (Exception e) {
91
+ String msg = "Failed in change password" + e.getMessage();
92
+ Log.v(TAG, msg);
93
+ throw new Exception(msg);
94
+ }
95
+ }
96
+ if (_mode.equals("encryption")) {
97
+ try {
98
+ _uCipher.encrypt(_context, _file, SQLiteDatabase.getBytes(password.toCharArray()));
99
+ } catch (Exception e) {
100
+ String msg = "Failed in encryption " + e.getMessage();
101
+ Log.v(TAG, msg);
102
+ throw new Exception(msg);
103
+ }
104
+ }
105
+
106
+ _db = SQLiteDatabase.openOrCreateDatabase(_file, password, null);
107
+ if (_db != null) {
108
+ if (_db.isOpen()) {
109
+ try {
110
+ setTable(_tableName, true);
111
+ isOpen = true;
112
+ return;
113
+ } catch (Exception e) {
114
+ isOpen = false;
115
+ _db = null;
116
+ throw new Exception("Store not opened " + e.getMessage());
117
+ }
118
+ } else {
119
+ isOpen = false;
120
+ _db = null;
121
+ throw new Exception("Store not opened");
122
+ }
123
+ } else {
124
+ isOpen = false;
125
+ throw new Exception("No store returned");
136
126
  }
137
- } else {
138
- isOpen = false;
139
- _db = null;
140
- throw new Exception("Store not opened");
141
- }
142
- } else {
143
- isOpen = false;
144
- throw new Exception("No store returned");
145
127
  }
146
- }
147
-
148
- /**
149
- * Close Method
150
- * Close the store
151
- */
152
- public void close() throws Exception {
153
- if (_db.isOpen()) {
154
- try {
155
- _db.close();
156
- isOpen = false;
157
- return;
158
- } catch (Exception e) {
159
- String msg = "Failed in closing the store" + e.getMessage();
160
- Log.v(TAG, msg);
161
- throw new Exception(msg);
162
- }
163
- } else {
164
- throw new Exception("Store not opened");
128
+
129
+ /**
130
+ * Close Method
131
+ * Close the store
132
+ */
133
+ public void close() throws Exception {
134
+ if (_db.isOpen()) {
135
+ try {
136
+ _db.close();
137
+ isOpen = false;
138
+ return;
139
+ } catch (Exception e) {
140
+ String msg = "Failed in closing the store" + e.getMessage();
141
+ Log.v(TAG, msg);
142
+ throw new Exception(msg);
143
+ }
144
+ } else {
145
+ throw new Exception("Store not opened");
146
+ }
165
147
  }
166
- }
167
-
168
- /**
169
- * SetTable Method
170
- * Set a table and associated index
171
- * @param name
172
- * @param ifNotExists
173
- * @throws Exception
174
- */
175
- public void setTable(String name, Boolean ifNotExists) throws Exception {
176
- String exist = ifNotExists ? "IF NOT EXISTS" : "";
177
- if (_db.isOpen()) {
178
- try {
179
- String CREATE_STORAGE_TABLE =
180
- "CREATE TABLE " +
181
- exist +
182
- " " +
183
- name +
184
- "(" +
185
- COL_ID +
186
- " INTEGER PRIMARY KEY AUTOINCREMENT," +
187
- COL_NAME +
188
- " TEXT," +
189
- COL_VALUE +
190
- " TEXT" +
191
- ");";
192
- _db.execSQL(CREATE_STORAGE_TABLE);
193
- String idx = "index_" + name + "_on_" + IDX_COL_NAME;
194
- String CREATE_INDEX_NAME =
195
- "CREATE INDEX " +
196
- exist +
197
- " " +
198
- idx +
199
- " ON " +
200
- name +
201
- " (" +
202
- IDX_COL_NAME +
203
- ");";
204
- _db.execSQL(CREATE_INDEX_NAME);
205
- if (!this._tableName.equals(name)) this._tableName = name;
206
- return;
207
- } catch (Exception e) {
208
- String msg = "Failed in creating table " + name + " " + e.getMessage();
209
- Log.v(TAG, msg);
210
- throw new Exception(msg);
211
- }
212
- } else {
213
- throw new Exception("Store not opened");
148
+
149
+ /**
150
+ * SetTable Method
151
+ * Set a table and associated index
152
+ * @param name
153
+ * @param ifNotExists
154
+ * @throws Exception
155
+ */
156
+ public void setTable(String name, Boolean ifNotExists) throws Exception {
157
+ String exist = ifNotExists ? "IF NOT EXISTS" : "";
158
+ if (_db.isOpen()) {
159
+ try {
160
+ String CREATE_STORAGE_TABLE =
161
+ "CREATE TABLE " +
162
+ exist +
163
+ " " +
164
+ name +
165
+ "(" +
166
+ COL_ID +
167
+ " INTEGER PRIMARY KEY AUTOINCREMENT," +
168
+ COL_NAME +
169
+ " TEXT," +
170
+ COL_VALUE +
171
+ " TEXT" +
172
+ ");";
173
+ _db.execSQL(CREATE_STORAGE_TABLE);
174
+ String idx = "index_" + name + "_on_" + IDX_COL_NAME;
175
+ String CREATE_INDEX_NAME = "CREATE INDEX " + exist + " " + idx + " ON " + name + " (" + IDX_COL_NAME + ");";
176
+ _db.execSQL(CREATE_INDEX_NAME);
177
+ if (!this._tableName.equals(name)) this._tableName = name;
178
+ return;
179
+ } catch (Exception e) {
180
+ String msg = "Failed in creating table " + name + " " + e.getMessage();
181
+ Log.v(TAG, msg);
182
+ throw new Exception(msg);
183
+ }
184
+ } else {
185
+ throw new Exception("Store not opened");
186
+ }
214
187
  }
215
- }
216
-
217
- /**
218
- * Get Method
219
- * Get a value from a given key
220
- * @param name
221
- * @return
222
- * @throws Exception
223
- */
224
- public Data get(String name) throws Exception {
225
- Cursor c = null;
226
- Data data = null;
227
- if (_db.isOpen()) {
228
- try {
229
- String DATA_SELECT_QUERY =
230
- "SELECT * FROM " +
231
- this._tableName +
232
- " WHERE " +
233
- COL_NAME +
234
- " = '" +
235
- name +
236
- "';";
237
-
238
- c = (Cursor) _db.query(DATA_SELECT_QUERY);
239
- if (c.getCount() > 0) {
240
- c.moveToFirst();
241
- data = new Data();
242
- data.id = c.getLong(c.getColumnIndex(COL_ID));
243
- data.name = c.getString(c.getColumnIndex(COL_NAME));
244
- data.value = c.getString(c.getColumnIndex(COL_VALUE));
188
+
189
+ /**
190
+ * Get Method
191
+ * Get a value from a given key
192
+ * @param name
193
+ * @return
194
+ * @throws Exception
195
+ */
196
+ public Data get(String name) throws Exception {
197
+ Cursor c = null;
198
+ Data data = null;
199
+ if (_db.isOpen()) {
200
+ try {
201
+ String DATA_SELECT_QUERY = "SELECT * FROM " + this._tableName + " WHERE " + COL_NAME + " = '" + name + "';";
202
+
203
+ c = (Cursor) _db.query(DATA_SELECT_QUERY);
204
+ if (c.getCount() > 0) {
205
+ c.moveToFirst();
206
+ data = new Data();
207
+ data.id = c.getLong(c.getColumnIndex(COL_ID));
208
+ data.name = c.getString(c.getColumnIndex(COL_NAME));
209
+ data.value = c.getString(c.getColumnIndex(COL_VALUE));
210
+ } else {
211
+ data = new Data();
212
+ data.id = null;
213
+ }
214
+ return data;
215
+ } catch (Exception e) {
216
+ String msg = "Failed in get" + e.getMessage();
217
+ Log.v(TAG, msg);
218
+ throw new Exception(msg);
219
+ } finally {
220
+ if (c != null) c.close();
221
+ }
245
222
  } else {
246
- data = new Data();
247
- data.id = null;
223
+ throw new Exception("Store not opened");
248
224
  }
249
- return data;
250
- } catch (Exception e) {
251
- String msg = "Failed in get" + e.getMessage();
252
- Log.v(TAG, msg);
253
- throw new Exception(msg);
254
- } finally {
255
- if (c != null) c.close();
256
- }
257
- } else {
258
- throw new Exception("Store not opened");
259
225
  }
260
- }
261
-
262
- /**
263
- * Set Method
264
- * Set the value for a given key
265
- * @param data
266
- * @throws Exception
267
- */
268
- public void set(Data data) throws Exception {
269
- String name = data.name;
270
- String value = data.value;
271
- if (_db.isOpen()) {
272
- try {
273
- boolean ret = iskey(name);
274
- String statement = "";
275
- SupportSQLiteStatement stmt = null;
276
-
277
- if (ret) {
278
- // update
279
- statement = statement
280
- .concat("UPDATE ")
281
- .concat(this._tableName)
282
- .concat(" SET ");
283
- statement = statement.concat(COL_VALUE).concat(" = ? ");
284
- statement = statement
285
- .concat(" WHERE ")
286
- .concat(COL_NAME)
287
- .concat(" = ?;");
288
- stmt = _db.compileStatement(statement);
289
- Object[] valObj = new Object[2];
290
- valObj[0] = value;
291
- valObj[1] = name;
292
- SimpleSQLiteQuery.bind(stmt, valObj);
293
- stmt.executeUpdateDelete();
294
- /* stmt =
226
+
227
+ /**
228
+ * Set Method
229
+ * Set the value for a given key
230
+ * @param data
231
+ * @throws Exception
232
+ */
233
+ public void set(Data data) throws Exception {
234
+ String name = data.name;
235
+ String value = data.value;
236
+ if (_db.isOpen()) {
237
+ try {
238
+ boolean ret = iskey(name);
239
+ String statement = "";
240
+ SupportSQLiteStatement stmt = null;
241
+
242
+ if (ret) {
243
+ // update
244
+ statement = statement.concat("UPDATE ").concat(this._tableName).concat(" SET ");
245
+ statement = statement.concat(COL_VALUE).concat(" = ? ");
246
+ statement = statement.concat(" WHERE ").concat(COL_NAME).concat(" = ?;");
247
+ stmt = _db.compileStatement(statement);
248
+ Object[] valObj = new Object[2];
249
+ valObj[0] = value;
250
+ valObj[1] = name;
251
+ SimpleSQLiteQuery.bind(stmt, valObj);
252
+ stmt.executeUpdateDelete();
253
+ /* stmt =
295
254
  "UPDATE " + this._tableName + " SET " + COL_VALUE + " = '" + value + "' WHERE " + COL_NAME + " = '" + name + "';";
296
255
  */
297
- } else {
298
- // insert
299
-
300
- statement = statement.concat("INSERT INTO ").concat(this._tableName);
301
- statement = statement.concat(" (").concat(COL_NAME).concat(",");
302
- statement = statement.concat(COL_VALUE).concat(") VALUES(?,?);");
303
- stmt = _db.compileStatement(statement);
304
- Object[] valObj = new Object[2];
305
- valObj[0] = name;
306
- valObj[1] = value;
307
- SimpleSQLiteQuery.bind(stmt, valObj);
308
- stmt.executeInsert();
309
- /*
256
+ } else {
257
+ // insert
258
+
259
+ statement = statement.concat("INSERT INTO ").concat(this._tableName);
260
+ statement = statement.concat(" (").concat(COL_NAME).concat(",");
261
+ statement = statement.concat(COL_VALUE).concat(") VALUES(?,?);");
262
+ stmt = _db.compileStatement(statement);
263
+ Object[] valObj = new Object[2];
264
+ valObj[0] = name;
265
+ valObj[1] = value;
266
+ SimpleSQLiteQuery.bind(stmt, valObj);
267
+ stmt.executeInsert();
268
+ /*
310
269
  stmt =
311
270
  "INSERT INTO " + _tableName + " (" + COL_NAME + "," + COL_VALUE + ") " + "VALUES('" + name + "','" + value + "');";
312
271
  */
272
+ }
273
+ // _db.execSQL(stmt);
274
+ } catch (IllegalStateException e) {
275
+ String msg = "Failed in set" + e.getMessage();
276
+ Log.v(TAG, msg);
277
+ throw new Exception(msg);
278
+ } catch (IllegalArgumentException e) {
279
+ String msg = "Failed in set" + e.getMessage();
280
+ Log.v(TAG, msg);
281
+ throw new Exception(msg);
282
+ } catch (Exception e) {
283
+ String msg = "Failed in set" + e.getMessage();
284
+ Log.v(TAG, msg);
285
+ throw new Exception(msg);
286
+ }
287
+ } else {
288
+ throw new Exception("Store not opened");
313
289
  }
314
- // _db.execSQL(stmt);
315
- } catch (IllegalStateException e) {
316
- String msg = "Failed in set" + e.getMessage();
317
- Log.v(TAG, msg);
318
- throw new Exception(msg);
319
- } catch (IllegalArgumentException e) {
320
- String msg = "Failed in set" + e.getMessage();
321
- Log.v(TAG, msg);
322
- throw new Exception(msg);
323
- } catch (Exception e) {
324
- String msg = "Failed in set" + e.getMessage();
325
- Log.v(TAG, msg);
326
- throw new Exception(msg);
327
- }
328
- } else {
329
- throw new Exception("Store not opened");
330
290
  }
331
- }
332
-
333
- /**
334
- * Iskey
335
- * Check if a key is existing in the store
336
- * @param name
337
- * @return
338
- * @throws Exception
339
- */
340
- public boolean iskey(String name) throws Exception {
341
- boolean ret = false;
342
- if (_db.isOpen()) {
343
- try {
344
- Data data = get(name);
345
- if (data.id != null) ret = true;
346
- return ret;
347
- } catch (Exception e) {
348
- String msg = "Failed in iskey" + e.getMessage();
349
- Log.v(TAG, msg);
350
- throw new Exception(msg);
351
- }
352
- } else {
353
- throw new Exception("Store not opened");
354
- }
355
- }
356
-
357
- public void clear() throws Exception {
358
- if (_db.isOpen()) {
359
- try {
360
- _db.execSQL("DELETE FROM " + this._tableName);
361
- return;
362
- } catch (Exception e) {
363
- String msg = "Failed in clear" + e.getMessage();
364
- throw new Exception(msg);
365
- }
366
- } else {
367
- throw new Exception("Store not opened");
291
+
292
+ /**
293
+ * Iskey
294
+ * Check if a key is existing in the store
295
+ * @param name
296
+ * @return
297
+ * @throws Exception
298
+ */
299
+ public boolean iskey(String name) throws Exception {
300
+ boolean ret = false;
301
+ if (_db.isOpen()) {
302
+ try {
303
+ Data data = get(name);
304
+ if (data.id != null) ret = true;
305
+ return ret;
306
+ } catch (Exception e) {
307
+ String msg = "Failed in iskey" + e.getMessage();
308
+ Log.v(TAG, msg);
309
+ throw new Exception(msg);
310
+ }
311
+ } else {
312
+ throw new Exception("Store not opened");
313
+ }
368
314
  }
369
- }
370
-
371
- public void remove(String name) throws Exception {
372
- if (_db.isOpen()) {
373
- try {
374
- _db.execSQL(
375
- "DELETE FROM " +
376
- this._tableName +
377
- " WHERE " +
378
- COL_NAME +
379
- " = '" +
380
- name +
381
- "';"
382
- );
383
- return;
384
- } catch (Exception e) {
385
- String msg = "Failed in remove" + e.getMessage();
386
- throw new Exception(msg);
387
- }
388
- } else {
389
- throw new Exception("Store not opened");
315
+
316
+ public void clear() throws Exception {
317
+ if (_db.isOpen()) {
318
+ try {
319
+ _db.execSQL("DELETE FROM " + this._tableName);
320
+ return;
321
+ } catch (Exception e) {
322
+ String msg = "Failed in clear" + e.getMessage();
323
+ throw new Exception(msg);
324
+ }
325
+ } else {
326
+ throw new Exception("Store not opened");
327
+ }
390
328
  }
391
- }
392
-
393
- public List<String> keys() throws Exception {
394
- Cursor c = null;
395
- List<String> data = new ArrayList<>();
396
- if (_db.isOpen()) {
397
- try {
398
- String DATA_SELECT_QUERY =
399
- "SELECT " +
400
- COL_NAME +
401
- " FROM " +
402
- this._tableName +
403
- " ORDER BY " +
404
- COL_NAME +
405
- ";";
406
-
407
- c = (Cursor) _db.query(DATA_SELECT_QUERY);
408
- if (c.getCount() > 0) {
409
- if (c.moveToFirst()) {
410
- do {
411
- String key = c.getString(c.getColumnIndex(COL_NAME));
412
- data.add(key);
413
- } while (c.moveToNext());
414
- }
329
+
330
+ public void remove(String name) throws Exception {
331
+ if (_db.isOpen()) {
332
+ try {
333
+ _db.execSQL("DELETE FROM " + this._tableName + " WHERE " + COL_NAME + " = '" + name + "';");
334
+ return;
335
+ } catch (Exception e) {
336
+ String msg = "Failed in remove" + e.getMessage();
337
+ throw new Exception(msg);
338
+ }
415
339
  } else {
416
- data = Collections.emptyList();
340
+ throw new Exception("Store not opened");
417
341
  }
418
- return data;
419
- } catch (Exception e) {
420
- String msg = "Failed in keys" + e.getMessage();
421
- throw new Exception(msg);
422
- } finally {
423
- if (c != null) c.close();
424
- }
425
- } else {
426
- throw new Exception("Store not opened");
427
342
  }
428
- }
429
-
430
- public List<String> values() throws Exception {
431
- Cursor c = null;
432
- List<String> data = new ArrayList<>();
433
- if (_db.isOpen()) {
434
- try {
435
- String DATA_SELECT_QUERY =
436
- "SELECT " +
437
- COL_VALUE +
438
- " FROM " +
439
- this._tableName +
440
- " ORDER BY " +
441
- COL_NAME +
442
- ";";
443
-
444
- c = (Cursor) _db.query(DATA_SELECT_QUERY);
445
- if (c.getCount() > 0) {
446
- if (c.moveToFirst()) {
447
- do {
448
- String key = c.getString(c.getColumnIndex(COL_VALUE));
449
- data.add(key);
450
- } while (c.moveToNext());
451
- }
343
+
344
+ public List<String> keys() throws Exception {
345
+ Cursor c = null;
346
+ List<String> data = new ArrayList<>();
347
+ if (_db.isOpen()) {
348
+ try {
349
+ String DATA_SELECT_QUERY = "SELECT " + COL_NAME + " FROM " + this._tableName + " ORDER BY " + COL_NAME + ";";
350
+
351
+ c = (Cursor) _db.query(DATA_SELECT_QUERY);
352
+ if (c.getCount() > 0) {
353
+ if (c.moveToFirst()) {
354
+ do {
355
+ String key = c.getString(c.getColumnIndex(COL_NAME));
356
+ data.add(key);
357
+ } while (c.moveToNext());
358
+ }
359
+ } else {
360
+ data = Collections.emptyList();
361
+ }
362
+ return data;
363
+ } catch (Exception e) {
364
+ String msg = "Failed in keys" + e.getMessage();
365
+ throw new Exception(msg);
366
+ } finally {
367
+ if (c != null) c.close();
368
+ }
452
369
  } else {
453
- data = Collections.emptyList();
370
+ throw new Exception("Store not opened");
454
371
  }
455
- return data;
456
- } catch (Exception e) {
457
- String msg = "Failed in values" + e.getMessage();
458
- throw new Exception(msg);
459
- } finally {
460
- if (c != null) c.close();
461
- }
462
- } else {
463
- throw new Exception("Store not opened");
464
372
  }
465
- }
466
-
467
- public List<Data> keysvalues() throws Exception {
468
- Cursor c = null;
469
- List<Data> data = new ArrayList<>();
470
- if (_db.isOpen()) {
471
- try {
472
- String DATA_SELECT_QUERY =
473
- "SELECT * FROM " + this._tableName + " ORDER BY " + COL_NAME + ";";
474
-
475
- c = (Cursor) _db.query(DATA_SELECT_QUERY);
476
- if (c.getCount() > 0) {
477
- if (c.moveToFirst()) {
478
- do {
479
- Data newData = new Data();
480
- newData.name = c.getString(c.getColumnIndex(COL_NAME));
481
- newData.value = c.getString(c.getColumnIndex(COL_VALUE));
482
- data.add(newData);
483
- } while (c.moveToNext());
484
- }
373
+
374
+ public List<String> values() throws Exception {
375
+ Cursor c = null;
376
+ List<String> data = new ArrayList<>();
377
+ if (_db.isOpen()) {
378
+ try {
379
+ String DATA_SELECT_QUERY = "SELECT " + COL_VALUE + " FROM " + this._tableName + " ORDER BY " + COL_NAME + ";";
380
+
381
+ c = (Cursor) _db.query(DATA_SELECT_QUERY);
382
+ if (c.getCount() > 0) {
383
+ if (c.moveToFirst()) {
384
+ do {
385
+ String key = c.getString(c.getColumnIndex(COL_VALUE));
386
+ data.add(key);
387
+ } while (c.moveToNext());
388
+ }
389
+ } else {
390
+ data = Collections.emptyList();
391
+ }
392
+ return data;
393
+ } catch (Exception e) {
394
+ String msg = "Failed in values" + e.getMessage();
395
+ throw new Exception(msg);
396
+ } finally {
397
+ if (c != null) c.close();
398
+ }
485
399
  } else {
486
- data = Collections.emptyList();
400
+ throw new Exception("Store not opened");
487
401
  }
488
- return data;
489
- } catch (Exception e) {
490
- String msg = "Failed in keysvalues" + e.getMessage();
491
- throw new Exception(msg);
492
- } finally {
493
- if (c != null) c.close();
494
- }
495
- } else {
496
- throw new Exception("Store not opened");
497
402
  }
498
- }
499
403
 
500
- public List<String> filtervalues(String filter) throws Exception {
501
- String inFilter = filter;
502
- if (!inFilter.startsWith("%") && !inFilter.endsWith("%")) {
503
- inFilter = "%" + inFilter + "%";
404
+ public List<Data> keysvalues() throws Exception {
405
+ Cursor c = null;
406
+ List<Data> data = new ArrayList<>();
407
+ if (_db.isOpen()) {
408
+ try {
409
+ String DATA_SELECT_QUERY = "SELECT * FROM " + this._tableName + " ORDER BY " + COL_NAME + ";";
410
+
411
+ c = (Cursor) _db.query(DATA_SELECT_QUERY);
412
+ if (c.getCount() > 0) {
413
+ if (c.moveToFirst()) {
414
+ do {
415
+ Data newData = new Data();
416
+ newData.name = c.getString(c.getColumnIndex(COL_NAME));
417
+ newData.value = c.getString(c.getColumnIndex(COL_VALUE));
418
+ data.add(newData);
419
+ } while (c.moveToNext());
420
+ }
421
+ } else {
422
+ data = Collections.emptyList();
423
+ }
424
+ return data;
425
+ } catch (Exception e) {
426
+ String msg = "Failed in keysvalues" + e.getMessage();
427
+ throw new Exception(msg);
428
+ } finally {
429
+ if (c != null) c.close();
430
+ }
431
+ } else {
432
+ throw new Exception("Store not opened");
433
+ }
504
434
  }
505
- Cursor c = null;
506
- List<String> data = new ArrayList<>();
507
- if (_db.isOpen()) {
508
- try {
509
- String DATA_SELECT_QUERY =
510
- "SELECT " +
511
- COL_VALUE +
512
- " FROM " +
513
- this._tableName +
514
- " WHERE " +
515
- COL_NAME +
516
- " LIKE '" +
517
- inFilter +
518
- "'" +
519
- " ORDER BY " +
520
- COL_NAME +
521
- ";";
522
- c = (Cursor) _db.query(DATA_SELECT_QUERY);
523
- if (c.getCount() > 0) {
524
- if (c.moveToFirst()) {
525
- do {
526
- String key = c.getString(c.getColumnIndex(COL_VALUE));
527
- data.add(key);
528
- } while (c.moveToNext());
529
- }
435
+
436
+ public List<String> filtervalues(String filter) throws Exception {
437
+ String inFilter = filter;
438
+ if (!inFilter.startsWith("%") && !inFilter.endsWith("%")) {
439
+ inFilter = "%" + inFilter + "%";
440
+ }
441
+ Cursor c = null;
442
+ List<String> data = new ArrayList<>();
443
+ if (_db.isOpen()) {
444
+ try {
445
+ String DATA_SELECT_QUERY =
446
+ "SELECT " +
447
+ COL_VALUE +
448
+ " FROM " +
449
+ this._tableName +
450
+ " WHERE " +
451
+ COL_NAME +
452
+ " LIKE '" +
453
+ inFilter +
454
+ "'" +
455
+ " ORDER BY " +
456
+ COL_NAME +
457
+ ";";
458
+ c = (Cursor) _db.query(DATA_SELECT_QUERY);
459
+ if (c.getCount() > 0) {
460
+ if (c.moveToFirst()) {
461
+ do {
462
+ String key = c.getString(c.getColumnIndex(COL_VALUE));
463
+ data.add(key);
464
+ } while (c.moveToNext());
465
+ }
466
+ } else {
467
+ data = Collections.emptyList();
468
+ }
469
+ return data;
470
+ } catch (Exception e) {
471
+ String msg = "Failed in values" + e.getMessage();
472
+ throw new Exception(msg);
473
+ } finally {
474
+ if (c != null) c.close();
475
+ }
530
476
  } else {
531
- data = Collections.emptyList();
477
+ throw new Exception("Store not opened");
532
478
  }
533
- return data;
534
- } catch (Exception e) {
535
- String msg = "Failed in values" + e.getMessage();
536
- throw new Exception(msg);
537
- } finally {
538
- if (c != null) c.close();
539
- }
540
- } else {
541
- throw new Exception("Store not opened");
542
479
  }
543
- }
544
-
545
- public boolean isTable(String table) throws Exception {
546
- boolean ret = false;
547
- if (_db.isOpen()) {
548
- try {
549
- List<String> tables = getTables();
550
- if (tables.size() > 0 && tables.contains(table)) {
551
- ret = true;
480
+
481
+ public boolean isTable(String table) throws Exception {
482
+ boolean ret = false;
483
+ if (_db.isOpen()) {
484
+ try {
485
+ List<String> tables = getTables();
486
+ if (tables.size() > 0 && tables.contains(table)) {
487
+ ret = true;
488
+ }
489
+ return ret;
490
+ } catch (Exception e) {
491
+ String msg = "Failed in isTable" + e.getMessage();
492
+ throw new Exception(msg);
493
+ }
494
+ } else {
495
+ throw new Exception("Store not opened");
552
496
  }
553
- return ret;
554
- } catch (Exception e) {
555
- String msg = "Failed in isTable" + e.getMessage();
556
- throw new Exception(msg);
557
- }
558
- } else {
559
- throw new Exception("Store not opened");
560
497
  }
561
- }
562
-
563
- public List<String> tables() throws Exception {
564
- try {
565
- List<String> tables = getTables();
566
- return tables;
567
- } catch (Exception e) {
568
- String msg = "Failed in tables" + e.getMessage();
569
- throw new Exception(msg);
498
+
499
+ public List<String> tables() throws Exception {
500
+ try {
501
+ List<String> tables = getTables();
502
+ return tables;
503
+ } catch (Exception e) {
504
+ String msg = "Failed in tables" + e.getMessage();
505
+ throw new Exception(msg);
506
+ }
570
507
  }
571
- }
572
-
573
- public void deleteTable(String table) throws Exception {
574
- if (_db.isOpen()) {
575
- try {
576
- List<String> tables = getTables();
577
- if (tables.size() > 0 && tables.contains(table)) {
578
- _db.execSQL("DROP TABLE IF EXISTS " + table + ";");
579
- return;
508
+
509
+ public void deleteTable(String table) throws Exception {
510
+ if (_db.isOpen()) {
511
+ try {
512
+ List<String> tables = getTables();
513
+ if (tables.size() > 0 && tables.contains(table)) {
514
+ _db.execSQL("DROP TABLE IF EXISTS " + table + ";");
515
+ return;
516
+ } else {
517
+ throw new Exception("table " + table + " does not exist");
518
+ }
519
+ } catch (Exception e) {
520
+ String msg = "Failed in deleteTable" + e.getMessage();
521
+ throw new Exception(msg);
522
+ }
580
523
  } else {
581
- throw new Exception("table " + table + " does not exist");
524
+ throw new Exception("Store not opened");
582
525
  }
583
- } catch (Exception e) {
584
- String msg = "Failed in deleteTable" + e.getMessage();
585
- throw new Exception(msg);
586
- }
587
- } else {
588
- throw new Exception("Store not opened");
589
526
  }
590
- }
591
-
592
- private List<String> getTables() throws Exception {
593
- Cursor c = null;
594
- List<String> tables = new ArrayList<>();
595
- if (_db.isOpen()) {
596
- try {
597
- String DATA_SELECT_QUERY =
598
- "SELECT name FROM sqlite_master WHERE TYPE='table'" +
599
- " ORDER BY name " +
600
- ";";
601
-
602
- c = (Cursor) _db.query(DATA_SELECT_QUERY);
603
- if (c.getCount() > 0) {
604
- if (c.moveToFirst()) {
605
- do {
606
- String table = c.getString(c.getColumnIndex("name"));
607
- if (!table.equals("sqlite_sequence")) {
608
- tables.add(table);
609
- }
610
- } while (c.moveToNext());
611
- }
527
+
528
+ private List<String> getTables() throws Exception {
529
+ Cursor c = null;
530
+ List<String> tables = new ArrayList<>();
531
+ if (_db.isOpen()) {
532
+ try {
533
+ String DATA_SELECT_QUERY = "SELECT name FROM sqlite_master WHERE TYPE='table'" + " ORDER BY name " + ";";
534
+
535
+ c = (Cursor) _db.query(DATA_SELECT_QUERY);
536
+ if (c.getCount() > 0) {
537
+ if (c.moveToFirst()) {
538
+ do {
539
+ String table = c.getString(c.getColumnIndex("name"));
540
+ if (!table.equals("sqlite_sequence")) {
541
+ tables.add(table);
542
+ }
543
+ } while (c.moveToNext());
544
+ }
545
+ } else {
546
+ tables = Collections.emptyList();
547
+ }
548
+ return tables;
549
+ } catch (Exception e) {
550
+ String msg = "Failed in tables" + e.getMessage();
551
+ Log.v(TAG, msg);
552
+ throw new Exception(msg);
553
+ } finally {
554
+ if (c != null) c.close();
555
+ }
612
556
  } else {
613
- tables = Collections.emptyList();
557
+ throw new Exception("Store not opened");
614
558
  }
615
- return tables;
616
- } catch (Exception e) {
617
- String msg = "Failed in tables" + e.getMessage();
618
- Log.v(TAG, msg);
619
- throw new Exception(msg);
620
- } finally {
621
- if (c != null) c.close();
622
- }
623
- } else {
624
- throw new Exception("Store not opened");
625
559
  }
626
- }
627
-
628
- /**
629
- * Import from Json object
630
- * @param values
631
- * @return
632
- * @throws Exception
633
- */
634
- public Integer importFromJson(ArrayList<JsonValue> values) throws Exception {
635
- int changes = Integer.valueOf(0);
636
- try {
637
- for (JsonValue val : values) {
638
- Data data = new Data();
639
- data.name = val.getKey();
640
- data.value = val.getValue();
641
- set(data);
642
- changes += 1;
643
- }
644
- return changes;
645
- } catch (Exception e) {
646
- throw new Exception(e.getMessage());
647
- }
648
- }
649
-
650
- public JSObject exportToJson() {
651
- JsonStore retJson = new JsonStore();
652
- JSObject retObj = new JSObject();
653
- retJson.setDatabase(_dbName.substring(0, _dbName.length() - 9));
654
- retJson.setEncrypted(_encrypted);
655
-
656
- String previousName = _tableName;
657
- try {
658
- List<String> tables = tables();
659
- ArrayList<JsonTable> rTables = new ArrayList<JsonTable>();
660
- for (String table : tables) {
661
- JsonTable rTable = new JsonTable();
662
- _tableName = table;
663
- rTable.setName(table);
664
- List<Data> dataTable = keysvalues();
665
- ArrayList<JsonValue> values = new ArrayList<JsonValue>();
666
- for (Data data : dataTable) {
667
- JsonValue rData = new JsonValue();
668
- rData.setKey(data.name);
669
- rData.setValue(data.value);
670
- values.add(rData);
560
+
561
+ /**
562
+ * Import from Json object
563
+ * @param values
564
+ * @return
565
+ * @throws Exception
566
+ */
567
+ public Integer importFromJson(ArrayList<JsonValue> values) throws Exception {
568
+ int changes = Integer.valueOf(0);
569
+ try {
570
+ for (JsonValue val : values) {
571
+ Data data = new Data();
572
+ data.name = val.getKey();
573
+ data.value = val.getValue();
574
+ set(data);
575
+ changes += 1;
576
+ }
577
+ return changes;
578
+ } catch (Exception e) {
579
+ throw new Exception(e.getMessage());
671
580
  }
672
- rTable.setValues(values);
673
- rTables.add(rTable);
674
- }
675
- retJson.setTables(rTables);
676
- _tableName = previousName;
677
- ArrayList<String> keys = retJson.getKeys();
678
- if (keys.contains("tables")) {
679
- if (retJson.getTables().size() > 0) {
680
- retObj.put("database", retJson.getDatabase());
681
- retObj.put("encrypted", retJson.getEncrypted());
682
- retObj.put("tables", retJson.getTablesAsJSObject());
581
+ }
582
+
583
+ public JSObject exportToJson() {
584
+ JsonStore retJson = new JsonStore();
585
+ JSObject retObj = new JSObject();
586
+ retJson.setDatabase(_dbName.substring(0, _dbName.length() - 9));
587
+ retJson.setEncrypted(_encrypted);
588
+
589
+ String previousName = _tableName;
590
+ try {
591
+ List<String> tables = tables();
592
+ ArrayList<JsonTable> rTables = new ArrayList<JsonTable>();
593
+ for (String table : tables) {
594
+ JsonTable rTable = new JsonTable();
595
+ _tableName = table;
596
+ rTable.setName(table);
597
+ List<Data> dataTable = keysvalues();
598
+ ArrayList<JsonValue> values = new ArrayList<JsonValue>();
599
+ for (Data data : dataTable) {
600
+ JsonValue rData = new JsonValue();
601
+ rData.setKey(data.name);
602
+ rData.setValue(data.value);
603
+ values.add(rData);
604
+ }
605
+ rTable.setValues(values);
606
+ rTables.add(rTable);
607
+ }
608
+ retJson.setTables(rTables);
609
+ _tableName = previousName;
610
+ ArrayList<String> keys = retJson.getKeys();
611
+ if (keys.contains("tables")) {
612
+ if (retJson.getTables().size() > 0) {
613
+ retObj.put("database", retJson.getDatabase());
614
+ retObj.put("encrypted", retJson.getEncrypted());
615
+ retObj.put("tables", retJson.getTablesAsJSObject());
616
+ }
617
+ }
618
+ } catch (Exception e) {
619
+ Log.e(TAG, "Error: exportToJson " + e.getMessage());
620
+ } finally {
621
+ return retObj;
683
622
  }
684
- }
685
- } catch (Exception e) {
686
- Log.e(TAG, "Error: exportToJson " + e.getMessage());
687
- } finally {
688
- return retObj;
689
623
  }
690
- }
691
624
  }