@capgo/capacitor-data-storage-sqlite 6.0.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 (59) hide show
  1. package/CapacitorDataStorageSqlite.podspec +18 -0
  2. package/LICENSE +21 -0
  3. package/android/build.gradle +63 -0
  4. package/android/src/main/AndroidManifest.xml +3 -0
  5. package/android/src/main/java/com/jeep/plugin/capacitor/capacitordatastoragesqlite/CapacitorDataStorageSqlite.java +387 -0
  6. package/android/src/main/java/com/jeep/plugin/capacitor/capacitordatastoragesqlite/CapacitorDataStorageSqlitePlugin.java +447 -0
  7. package/android/src/main/java/com/jeep/plugin/capacitor/capacitordatastoragesqlite/RetHandler.java +117 -0
  8. package/android/src/main/java/com/jeep/plugin/capacitor/capacitordatastoragesqlite/cdssUtils/Data.java +8 -0
  9. package/android/src/main/java/com/jeep/plugin/capacitor/capacitordatastoragesqlite/cdssUtils/Global.java +7 -0
  10. package/android/src/main/java/com/jeep/plugin/capacitor/capacitordatastoragesqlite/cdssUtils/ImportExportJson/JsonStore.java +131 -0
  11. package/android/src/main/java/com/jeep/plugin/capacitor/capacitordatastoragesqlite/cdssUtils/ImportExportJson/JsonTable.java +110 -0
  12. package/android/src/main/java/com/jeep/plugin/capacitor/capacitordatastoragesqlite/cdssUtils/ImportExportJson/JsonValue.java +89 -0
  13. package/android/src/main/java/com/jeep/plugin/capacitor/capacitordatastoragesqlite/cdssUtils/StorageDatabaseHelper.java +691 -0
  14. package/android/src/main/java/com/jeep/plugin/capacitor/capacitordatastoragesqlite/cdssUtils/UtilsSQLCipher.java +162 -0
  15. package/android/src/main/res/.gitkeep +0 -0
  16. package/dist/docs.json +995 -0
  17. package/dist/esm/definitions.d.ts +296 -0
  18. package/dist/esm/definitions.js +2 -0
  19. package/dist/esm/definitions.js.map +1 -0
  20. package/dist/esm/index.d.ts +4 -0
  21. package/dist/esm/index.js +9 -0
  22. package/dist/esm/index.js.map +1 -0
  23. package/dist/esm/web-utils/Data.d.ts +5 -0
  24. package/dist/esm/web-utils/Data.js +3 -0
  25. package/dist/esm/web-utils/Data.js.map +1 -0
  26. package/dist/esm/web-utils/StorageDatabaseHelper.d.ts +23 -0
  27. package/dist/esm/web-utils/StorageDatabaseHelper.js +247 -0
  28. package/dist/esm/web-utils/StorageDatabaseHelper.js.map +1 -0
  29. package/dist/esm/web-utils/json-utils.d.ts +15 -0
  30. package/dist/esm/web-utils/json-utils.js +76 -0
  31. package/dist/esm/web-utils/json-utils.js.map +1 -0
  32. package/dist/esm/web.d.ts +27 -0
  33. package/dist/esm/web.js +295 -0
  34. package/dist/esm/web.js.map +1 -0
  35. package/dist/plugin.cjs.js +633 -0
  36. package/dist/plugin.cjs.js.map +1 -0
  37. package/dist/plugin.js +635 -0
  38. package/dist/plugin.js.map +1 -0
  39. package/electron/dist/plugin.js +1044 -0
  40. package/electron/dist/plugin.js.map +1 -0
  41. package/electron/rollup.config.mjs +17 -0
  42. package/electron/tsconfig.json +19 -0
  43. package/ios/Plugin/CapacitorDataStorageSqlite.swift +550 -0
  44. package/ios/Plugin/CapacitorDataStorageSqlitePlugin.h +10 -0
  45. package/ios/Plugin/CapacitorDataStorageSqlitePlugin.m +29 -0
  46. package/ios/Plugin/CapacitorDataStorageSqlitePlugin.swift +550 -0
  47. package/ios/Plugin/Data.swift +16 -0
  48. package/ios/Plugin/Global.swift +13 -0
  49. package/ios/Plugin/ImportExportJson/JsonStore.swift +47 -0
  50. package/ios/Plugin/Info.plist +24 -0
  51. package/ios/Plugin/ReturnHandler.swift +85 -0
  52. package/ios/Plugin/StorageDatabaseHelper.swift +603 -0
  53. package/ios/Plugin/Utils/Blob.swift +41 -0
  54. package/ios/Plugin/Utils/UtilsBinding.swift +73 -0
  55. package/ios/Plugin/Utils/UtilsEncryption.swift +79 -0
  56. package/ios/Plugin/Utils/UtilsFile.swift +244 -0
  57. package/ios/Plugin/Utils/UtilsSQLCipher.swift +605 -0
  58. package/package.json +96 -0
  59. package/readme.md +203 -0
@@ -0,0 +1,691 @@
1
+ package com.jeep.plugin.capacitor.capacitordatastoragesqlite.cdssUtils;
2
+
3
+ import android.content.Context;
4
+ import android.util.Log;
5
+ import androidx.sqlite.db.SimpleSQLiteQuery;
6
+ import androidx.sqlite.db.SupportSQLiteDatabase;
7
+ import androidx.sqlite.db.SupportSQLiteStatement;
8
+ import com.getcapacitor.JSObject;
9
+ import com.jeep.plugin.capacitor.capacitordatastoragesqlite.cdssUtils.ImportExportJson.JsonStore;
10
+ import com.jeep.plugin.capacitor.capacitordatastoragesqlite.cdssUtils.ImportExportJson.JsonTable;
11
+ import com.jeep.plugin.capacitor.capacitordatastoragesqlite.cdssUtils.ImportExportJson.JsonValue;
12
+ import java.io.File;
13
+ import java.util.ArrayList;
14
+ import java.util.Collections;
15
+ import java.util.List;
16
+ import net.sqlcipher.Cursor;
17
+ import net.sqlcipher.database.SQLiteDatabase;
18
+
19
+ public class StorageDatabaseHelper {
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();
63
+ }
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
+ }
110
+ }
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
+ }
123
+ }
124
+
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());
136
+ }
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
+ }
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");
165
+ }
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");
214
+ }
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));
245
+ } else {
246
+ data = new Data();
247
+ data.id = null;
248
+ }
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
+ }
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 =
295
+ "UPDATE " + this._tableName + " SET " + COL_VALUE + " = '" + value + "' WHERE " + COL_NAME + " = '" + name + "';";
296
+ */
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
+ /*
310
+ stmt =
311
+ "INSERT INTO " + _tableName + " (" + COL_NAME + "," + COL_VALUE + ") " + "VALUES('" + name + "','" + value + "');";
312
+ */
313
+ }
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
+ }
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");
368
+ }
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");
390
+ }
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
+ }
415
+ } else {
416
+ data = Collections.emptyList();
417
+ }
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
+ }
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
+ }
452
+ } else {
453
+ data = Collections.emptyList();
454
+ }
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
+ }
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
+ }
485
+ } else {
486
+ data = Collections.emptyList();
487
+ }
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
+ }
498
+ }
499
+
500
+ public List<String> filtervalues(String filter) throws Exception {
501
+ String inFilter = filter;
502
+ if (!inFilter.startsWith("%") && !inFilter.endsWith("%")) {
503
+ inFilter = "%" + inFilter + "%";
504
+ }
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
+ }
530
+ } else {
531
+ data = Collections.emptyList();
532
+ }
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
+ }
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;
552
+ }
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
+ }
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);
570
+ }
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;
580
+ } else {
581
+ throw new Exception("table " + table + " does not exist");
582
+ }
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
+ }
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
+ }
612
+ } else {
613
+ tables = Collections.emptyList();
614
+ }
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
+ }
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);
671
+ }
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());
683
+ }
684
+ }
685
+ } catch (Exception e) {
686
+ Log.e(TAG, "Error: exportToJson " + e.getMessage());
687
+ } finally {
688
+ return retObj;
689
+ }
690
+ }
691
+ }