@capacitor-community/sqlite 3.4.2-5 → 3.4.3-2

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 (33) hide show
  1. package/README.md +46 -4
  2. package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLite.java +41 -2
  3. package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLitePlugin.java +26 -0
  4. package/android/src/main/java/com/getcapacitor/community/database/sqlite/NotificationCenter.java +1 -1
  5. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java +220 -7
  6. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ExportToJson.java +102 -86
  7. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ImportFromJson.java +127 -38
  8. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/UtilsJson.java +90 -0
  9. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsUpgrade.java +8 -4
  10. package/dist/esm/definitions.d.ts +17 -1
  11. package/dist/esm/definitions.js +22 -28
  12. package/dist/esm/definitions.js.map +1 -1
  13. package/dist/esm/web.d.ts +20 -11
  14. package/dist/esm/web.js +288 -473
  15. package/dist/esm/web.js.map +1 -1
  16. package/dist/plugin.cjs.js +286 -477
  17. package/dist/plugin.cjs.js.map +1 -1
  18. package/dist/plugin.js +1036 -1227
  19. package/dist/plugin.js.map +1 -1
  20. package/electron/dist/plugin.js +589 -744
  21. package/electron/dist/plugin.js.map +1 -1
  22. package/ios/Plugin/CapacitorSQLite.swift +34 -1
  23. package/ios/Plugin/CapacitorSQLitePlugin.m +1 -0
  24. package/ios/Plugin/CapacitorSQLitePlugin.swift +25 -0
  25. package/ios/Plugin/Database.swift +29 -1
  26. package/ios/Plugin/Extensions/String.swift +8 -0
  27. package/ios/Plugin/ImportExportJson/ExportToJson.swift +190 -32
  28. package/ios/Plugin/ImportExportJson/ImportFromJson.swift +73 -38
  29. package/ios/Plugin/Utils/UtilsDrop.swift +2 -2
  30. package/ios/Plugin/Utils/UtilsJson.swift +68 -1
  31. package/ios/Plugin/Utils/UtilsSQLCipher.swift +277 -29
  32. package/ios/Plugin/Utils/UtilsUpgrade.swift +33 -13
  33. package/package.json +6 -6
@@ -4,8 +4,10 @@ import com.getcapacitor.JSArray;
4
4
  import com.getcapacitor.JSObject;
5
5
  import com.getcapacitor.community.database.sqlite.NotificationCenter;
6
6
  import com.getcapacitor.community.database.sqlite.SQLite.Database;
7
+ import com.getcapacitor.community.database.sqlite.SQLite.UtilsDrop;
7
8
  import com.getcapacitor.community.database.sqlite.SQLite.UtilsSQLite;
8
9
  import java.util.ArrayList;
10
+ import java.util.Date;
9
11
  import java.util.HashMap;
10
12
  import java.util.List;
11
13
  import java.util.Map;
@@ -17,6 +19,7 @@ public class ExportToJson {
17
19
  private static final String TAG = ImportFromJson.class.getName();
18
20
  private UtilsJson uJson = new UtilsJson();
19
21
  private UtilsSQLite uSqlite = new UtilsSQLite();
22
+ private UtilsDrop _uDrop = new UtilsDrop();
20
23
 
21
24
  /**
22
25
  * Notify progress export event
@@ -32,6 +35,96 @@ public class ExportToJson {
32
35
  NotificationCenter.defaultCenter().postNotification("exportJsonProgress", info);
33
36
  }
34
37
 
38
+ /**
39
+ * GetLastExportDate method
40
+ * get the last export date
41
+ *
42
+ * @return
43
+ * @throws Exception
44
+ */
45
+ public Long getLastExportDate(Database mDb) throws Exception {
46
+ long lastExportDate = -1;
47
+ String stmt = "SELECT sync_date FROM sync_table WHERE id = 2;";
48
+ JSArray retQuery = new JSArray();
49
+
50
+ try {
51
+ boolean isSyncTable = uJson.isTableExists(mDb, "sync_table");
52
+ if (!isSyncTable) {
53
+ throw new Exception("GetSyncDate: No sync_table available");
54
+ }
55
+ retQuery = mDb.selectSQL(stmt, new ArrayList<Object>());
56
+ List<JSObject> lQuery = retQuery.toList();
57
+ if (lQuery.size() == 1) {
58
+ long syncDate = lQuery.get(0).getLong("sync_date");
59
+ if (syncDate > 0) lastExportDate = syncDate;
60
+ }
61
+ } catch (Exception e) {
62
+ throw new Exception("GetSyncDate: " + e.getMessage());
63
+ } finally {
64
+ return lastExportDate;
65
+ }
66
+ }
67
+
68
+ public void setLastExportDate(Database mDb, Long sTime) throws Exception {
69
+ try {
70
+ boolean isSyncTable = uJson.isTableExists(mDb, "sync_table");
71
+ if (!isSyncTable) {
72
+ throw new Exception("SetLastExportDate: No sync_table available");
73
+ }
74
+ String stmt = "";
75
+ Long lastExportDate = getLastExportDate(mDb);
76
+ if (lastExportDate > 0) {
77
+ stmt = "UPDATE sync_table SET sync_date = " + sTime + " WHERE id = 2;";
78
+ } else {
79
+ stmt = "INSERT INTO sync_table (sync_date) VALUES (" + sTime + ");";
80
+ }
81
+ long lastId = mDb.prepareSQL(stmt, new ArrayList<>(), false);
82
+ if (lastId < 0) {
83
+ throw new Exception("SetLastExportDate: lastId < 0");
84
+ }
85
+ return;
86
+ } catch (Exception e) {
87
+ throw new Exception("SetLastExportDate: " + e.getMessage());
88
+ }
89
+ }
90
+
91
+ /**
92
+ * Delete Exported Rows
93
+ * @throws Exception
94
+ */
95
+ public void delExportedRows(Database mDb) throws Exception {
96
+ try {
97
+ // check if 'sync_table' exists
98
+ boolean isSyncTable = uJson.isTableExists(mDb, "sync_table");
99
+ if (!isSyncTable) {
100
+ throw new Exception("DelExportedRows: No sync_table available");
101
+ }
102
+ // get the last export date
103
+ long lastExportDate = getLastExportDate(mDb);
104
+ if (lastExportDate < 0) {
105
+ throw new Exception("DelExportedRows: No last exported date available");
106
+ }
107
+ // get the table' name list
108
+ List<String> tables = _uDrop.getTablesNames(mDb);
109
+ if (tables.size() == 0) {
110
+ throw new Exception("DelExportedRows: No table's names returned");
111
+ }
112
+ // Loop through the tables
113
+ for (String table : tables) {
114
+ long lastId = -1;
115
+ // define the delete statement
116
+ String delStmt = "DELETE FROM " + table + " WHERE sql_deleted = 1 " + "AND last_modified < " + lastExportDate + ";";
117
+ lastId = mDb.prepareSQL(delStmt, new ArrayList<>(), true);
118
+ if (lastId < 0) {
119
+ throw new Exception("SetLastExportDate: lastId < 0");
120
+ }
121
+ }
122
+ return;
123
+ } catch (Exception e) {
124
+ throw new Exception("DelExportedRows: " + e.getMessage());
125
+ }
126
+ }
127
+
35
128
  /**
36
129
  * Create Export Json Object from Database (Schema, Data)
37
130
  * @param db
@@ -149,7 +242,7 @@ public class ExportToJson {
149
242
 
150
243
  // create Table's Data
151
244
  String query = "SELECT * FROM " + tableName + ";";
152
- ArrayList<ArrayList<Object>> values = getValues(mDb, query, tableName);
245
+ ArrayList<ArrayList<Object>> values = uJson.getValues(mDb, query, tableName);
153
246
 
154
247
  table.setName(tableName);
155
248
  if (schema.size() != 0) {
@@ -388,87 +481,6 @@ public class ExportToJson {
388
481
  }
389
482
  }
390
483
 
391
- /**
392
- * Get Tables Values
393
- * @param mDb
394
- * @param query
395
- * @param tableName
396
- * @return
397
- * @throws Exception
398
- */
399
- private ArrayList<ArrayList<Object>> getValues(Database mDb, String query, String tableName) throws Exception {
400
- ArrayList<ArrayList<Object>> values = new ArrayList<>();
401
- try {
402
- JSObject tableNamesTypes = uJson.getTableColumnNamesTypes(mDb, tableName);
403
- ArrayList<String> rowNames = new ArrayList<>();
404
- ArrayList<String> rowTypes = new ArrayList<>();
405
- if (tableNamesTypes.has("names")) {
406
- rowNames = (ArrayList<String>) tableNamesTypes.get("names");
407
- } else {
408
- throw new Exception("GetValues: Table " + tableName + " no names");
409
- }
410
- if (tableNamesTypes.has("types")) {
411
- rowTypes = (ArrayList<String>) tableNamesTypes.get("types");
412
- } else {
413
- throw new Exception("GetValues: Table " + tableName + " no types");
414
- }
415
- JSArray retValues = mDb.selectSQL(query, new ArrayList<Object>());
416
- List<JSObject> lValues = retValues.toList();
417
- if (lValues.size() > 0) {
418
- for (int j = 0; j < lValues.size(); j++) {
419
- ArrayList<Object> row = new ArrayList<>();
420
- for (int k = 0; k < rowNames.size(); k++) {
421
- String nType = rowTypes.get(k);
422
- String nName = rowNames.get(k);
423
-
424
- // if (nType.equals("INTEGER")) {
425
- if (lValues.get(j).has(nName)) {
426
- Object obj = lValues.get(j).get(nName);
427
- if (obj.toString().equals("null")) {
428
- row.add(JSONObject.NULL);
429
- } else if (obj instanceof Long) {
430
- row.add(lValues.get(j).getLong(nName));
431
- } else if (obj instanceof String) {
432
- row.add(lValues.get(j).getString(nName));
433
- } else if (obj instanceof Double) {
434
- row.add(lValues.get(j).getDouble(nName));
435
- }
436
- } else {
437
- String msg = "GetValues: value is not (string, nsnull," + "int64,double";
438
- throw new Exception("GetValues: " + msg);
439
- }
440
- /* } else if (nType.equals("REAL")) {
441
- if (lValues.get(j).has(nName)) {
442
- Object obj = lValues.get(j).get(nName);
443
- if (obj instanceof Double) {
444
- row.add(lValues.get(j).getDouble(nName));
445
- }
446
- if (obj instanceof String) {
447
- row.add(lValues.get(j).getString(nName));
448
- }
449
- } else {
450
- row.add(null);
451
- }
452
- } else {
453
- if (lValues.get(j).has(nName)) {
454
- row.add(lValues.get(j).getString(nName));
455
- } else {
456
- row.add(null);
457
- }
458
-
459
- }
460
- */
461
- }
462
- values.add(row);
463
- }
464
- }
465
- } catch (Exception e) {
466
- throw new Exception("GetValues: " + e.getMessage());
467
- } finally {
468
- return values;
469
- }
470
- }
471
-
472
484
  /**
473
485
  * Get Tables when Mode is Partial
474
486
  * @param mDb
@@ -548,9 +560,9 @@ public class ExportToJson {
548
560
  if (modTables.getString(tableName).equals("Create")) {
549
561
  query = "SELECT * FROM " + tableName + ";";
550
562
  } else {
551
- query = "SELECT * FROM " + tableName + " WHERE last_modified > " + syncDate + ";";
563
+ query = "SELECT * FROM " + tableName + " WHERE last_modified >= " + syncDate + ";";
552
564
  }
553
- ArrayList<ArrayList<Object>> values = getValues(mDb, query, tableName);
565
+ ArrayList<ArrayList<Object>> values = uJson.getValues(mDb, query, tableName);
554
566
 
555
567
  // check the table object validity
556
568
  table.setName(tableName);
@@ -625,9 +637,13 @@ public class ExportToJson {
625
637
  */
626
638
  public Long getSyncDate(Database mDb) throws Exception {
627
639
  long ret = -1;
628
- String stmt = "SELECT sync_date FROM sync_table;";
640
+ String stmt = "SELECT sync_date FROM sync_table WHERE id = 1;";
629
641
  JSArray retQuery = new JSArray();
630
642
  try {
643
+ boolean isSyncTable = uJson.isTableExists(mDb, "sync_table");
644
+ if (!isSyncTable) {
645
+ throw new Exception("No sync_table available");
646
+ }
631
647
  retQuery = mDb.selectSQL(stmt, new ArrayList<Object>());
632
648
  List<JSObject> lQuery = retQuery.toList();
633
649
  if (lQuery.size() == 1) {
@@ -667,7 +683,7 @@ public class ExportToJson {
667
683
  if (lQuery.size() != 1) break;
668
684
  long totalCount = lQuery.get(0).getLong("count");
669
685
  // get total count of modified since last sync
670
- stmt = "SELECT count(*) AS count FROM " + tableName + " WHERE last_modified > " + syncDate + ";";
686
+ stmt = "SELECT count(*) AS count FROM " + tableName + " WHERE last_modified >= " + syncDate + ";";
671
687
  retQuery = mDb.selectSQL(stmt, new ArrayList<Object>());
672
688
  lQuery = retQuery.toList();
673
689
  if (lQuery.size() != 1) break;
@@ -48,15 +48,6 @@ public class ImportFromJson {
48
48
  db.getDb().setVersion(version);
49
49
 
50
50
  if (jsonSQL.getMode().equals("full")) {
51
- // Set Foreign Key OFF
52
- try {
53
- db.getDb().setForeignKeyConstraintsEnabled(false);
54
- } catch (IllegalStateException e) {
55
- String msg = "CreateDatabaseSchema: ";
56
- msg += "setForeignKeyConstraintsEnabled failed ";
57
- msg += e.getMessage();
58
- throw new Exception(msg);
59
- }
60
51
  try {
61
52
  _uDrop.dropAll(db);
62
53
  } catch (Exception e) {
@@ -64,15 +55,6 @@ public class ImportFromJson {
64
55
  throw new Exception(msg);
65
56
  }
66
57
  }
67
- // Set Foreign Key ON
68
- try {
69
- db.getDb().setForeignKeyConstraintsEnabled(true);
70
- } catch (IllegalStateException e) {
71
- String msg = "CreateDatabaseSchema: ";
72
- msg += "setForeignKeyConstraintsEnabled failed ";
73
- msg += e.getMessage();
74
- throw new Exception(msg);
75
- }
76
58
  try {
77
59
  changes = createSchema(db, jsonSQL);
78
60
  notifyImportProgressEvent("Schema creation completed changes: " + changes);
@@ -245,7 +227,7 @@ public class ImportFromJson {
245
227
  .append(" AFTER UPDATE ON ")
246
228
  .append(tableName)
247
229
  .append(" FOR EACH ROW ")
248
- .append("WHEN NEW.last_modified <= " + "OLD.last_modified BEGIN ")
230
+ .append("WHEN NEW.last_modified < " + "OLD.last_modified BEGIN ")
249
231
  .append("UPDATE ")
250
232
  .append(tableName)
251
233
  .append(" SET last_modified = (strftime('%s','now')) ")
@@ -401,11 +383,18 @@ public class ImportFromJson {
401
383
  // Check row validity remove to accept RDBMS types
402
384
  // _uJson.checkRowValidity(mDb, tColNames, tColTypes, row, j, tableName);
403
385
  // Create INSERT or UPDATE Statements
386
+ Boolean isRun = true;
404
387
  String stmt = createRowStatement(mDb, tColNames, tColTypes, row, j, tableName, mode);
405
- // load the values
406
- long lastId = mDb.prepareSQL(stmt, row);
407
- if (lastId < 0) {
408
- throw new Exception("CreateTableData: lastId < 0");
388
+ isRun = checkUpdate(mDb, stmt, row, tableName, tColNames, tColTypes);
389
+ if (isRun) {
390
+ // load the values
391
+ if (stmt.substring(0, 6).toUpperCase().equals("DELETE")) {
392
+ row = new ArrayList<>();
393
+ }
394
+ long lastId = mDb.prepareSQL(stmt, row, true);
395
+ if (lastId < 0) {
396
+ throw new Exception("CreateTableData: lastId < 0");
397
+ }
409
398
  }
410
399
  }
411
400
  return;
@@ -416,6 +405,88 @@ public class ImportFromJson {
416
405
  }
417
406
  }
418
407
 
408
+ /**
409
+ * Check when UPDATE if the values are updated
410
+ * @param mDb
411
+ * @param stmt
412
+ * @param values
413
+ * @param tableName
414
+ * @param tColNames
415
+ * @param tColTypes
416
+ * @return
417
+ * @throws Exception
418
+ */
419
+ private Boolean checkUpdate(
420
+ Database mDb,
421
+ String stmt,
422
+ ArrayList<Object> values,
423
+ String tableName,
424
+ ArrayList<String> tColNames,
425
+ ArrayList<String> tColTypes
426
+ ) throws Exception {
427
+ Boolean isRun = true;
428
+ if (stmt.substring(0, 6).equals("UPDATE")) {
429
+ StringBuilder sbQuery = new StringBuilder("SELECT * FROM ").append(tableName).append(" WHERE ").append(tColNames.get(0));
430
+
431
+ if (values.get(0) instanceof String) {
432
+ sbQuery.append(" = '").append(values.get(0)).append("';");
433
+ } else {
434
+ sbQuery.append(" = ").append(values.get(0)).append(";");
435
+ }
436
+ String query = sbQuery.toString();
437
+
438
+ try {
439
+ ArrayList<ArrayList<Object>> resValues = _uJson.getValues(mDb, query, tableName);
440
+ if (resValues.size() > 0) {
441
+ isRun = checkValues(values, resValues.get(0));
442
+ } else {
443
+ throw new Exception("CheckUpdate: CheckUpdate statement returns nothing");
444
+ }
445
+ } catch (Exception e) {
446
+ throw new Exception("CheckUpdate: " + e.getMessage());
447
+ }
448
+ }
449
+ return isRun;
450
+ }
451
+
452
+ /**
453
+ * Check Values
454
+ * @param values
455
+ * @param nValues
456
+ * @return
457
+ * @throws Exception
458
+ */
459
+ private Boolean checkValues(ArrayList<Object> values, ArrayList<Object> nValues) throws Exception {
460
+ if (values.size() > 0 && nValues.size() > 0 && values.size() == nValues.size()) {
461
+ for (int i = 0; i < values.size(); i++) {
462
+ if (nValues.get(i) instanceof String) {
463
+ if (!values.get(i).equals(nValues.get(i))) {
464
+ return true;
465
+ }
466
+ } else if (nValues.get(i) instanceof Long && values.get(i) instanceof Integer) {
467
+ // int iVal = (Integer) values.get(i);
468
+ long lVal = (Integer) values.get(i);
469
+ // long nlVal = (Long) nValues.get(i);
470
+ if (lVal != (Long) nValues.get(i)) {
471
+ return true;
472
+ }
473
+ } else if (nValues.get(i) instanceof Double && values.get(i) instanceof Integer) {
474
+ double dVal = (Integer) values.get(i);
475
+ if (dVal != (Double) nValues.get(i)) {
476
+ return true;
477
+ }
478
+ } else {
479
+ if (values.get(i) != nValues.get(i)) {
480
+ return true;
481
+ }
482
+ }
483
+ }
484
+ return false;
485
+ } else {
486
+ throw new Exception("CheckValues: Both arrays not the same length");
487
+ }
488
+ }
489
+
419
490
  /**
420
491
  * Create the Row Statement to load the data
421
492
  * @param mDb
@@ -463,23 +534,41 @@ public class ImportFromJson {
463
534
  .append(");")
464
535
  .toString();
465
536
  } else {
466
- // Update
467
- String setString = _uJson.setNameForUpdate(tColNames);
468
- if (setString.length() == 0) {
469
- throw new Exception(msg + j + "setString is empty");
537
+ Boolean isUpdate = true;
538
+ Integer idxDelete = tColNames.indexOf("sql_deleted");
539
+ if (idxDelete >= 0) {
540
+ if (row.get(idxDelete).equals(1)) {
541
+ // Delete
542
+ isUpdate = false;
543
+ stmt =
544
+ new StringBuilder("DELETE FROM ")
545
+ .append(tableName)
546
+ .append(" WHERE ")
547
+ .append(tColNames.get(0))
548
+ .append(" = ")
549
+ .append(row.get(0))
550
+ .toString();
551
+ }
470
552
  }
471
- Object key = tColNames.get(0);
472
- StringBuilder sbQuery = new StringBuilder("UPDATE ")
473
- .append(tableName)
474
- .append(" SET ")
475
- .append(setString)
476
- .append(" WHERE ")
477
- .append(tColNames.get(0))
478
- .append(" = ");
553
+ if (isUpdate) {
554
+ // Update
555
+ String setString = _uJson.setNameForUpdate(tColNames);
556
+ if (setString.length() == 0) {
557
+ throw new Exception(msg + j + "setString is empty");
558
+ }
559
+ Object key = tColNames.get(0);
560
+ StringBuilder sbQuery = new StringBuilder("UPDATE ")
561
+ .append(tableName)
562
+ .append(" SET ")
563
+ .append(setString)
564
+ .append(" WHERE ")
565
+ .append(tColNames.get(0))
566
+ .append(" = ");
479
567
 
480
- if (key instanceof Integer) sbQuery.append(row.get(0)).append(";");
481
- if (key instanceof String) sbQuery.append("'").append(row.get(0)).append("';");
482
- stmt = sbQuery.toString();
568
+ if (key instanceof Integer) sbQuery.append(row.get(0)).append(";");
569
+ if (key instanceof String) sbQuery.append("'").append(row.get(0)).append("';");
570
+ stmt = sbQuery.toString();
571
+ }
483
572
  }
484
573
  return stmt;
485
574
  }
@@ -42,6 +42,7 @@ public class UtilsJson {
42
42
 
43
43
  /**
44
44
  * Check if a table exists
45
+ *
45
46
  * @param db
46
47
  * @param tableName
47
48
  * @return
@@ -63,6 +64,7 @@ public class UtilsJson {
63
64
 
64
65
  /**
65
66
  * Check if a view exists
67
+ *
66
68
  * @param db
67
69
  * @param viewName
68
70
  * @return
@@ -84,6 +86,7 @@ public class UtilsJson {
84
86
 
85
87
  /**
86
88
  * Check if the Id already exsists
89
+ *
87
90
  * @param mDb
88
91
  * @param tableName
89
92
  * @param firstColumnName
@@ -118,6 +121,7 @@ public class UtilsJson {
118
121
  /**
119
122
  * Create a String from a given Array of Strings with
120
123
  * a given separator
124
+ *
121
125
  * @param arr
122
126
  * @param sep
123
127
  * @return
@@ -137,6 +141,7 @@ public class UtilsJson {
137
141
 
138
142
  /**
139
143
  * Convert ArrayList to JSArray
144
+ *
140
145
  * @param row
141
146
  * @return
142
147
  */
@@ -150,6 +155,7 @@ public class UtilsJson {
150
155
 
151
156
  /**
152
157
  * Create the ? string for a given values length
158
+ *
153
159
  * @param length
154
160
  * @return
155
161
  */
@@ -166,6 +172,7 @@ public class UtilsJson {
166
172
 
167
173
  /**
168
174
  * Create the Name string from a given Names array
175
+ *
169
176
  * @param names
170
177
  * @return
171
178
  */
@@ -182,6 +189,7 @@ public class UtilsJson {
182
189
 
183
190
  /**
184
191
  * Check the values type from fields type
192
+ *
185
193
  * @param types
186
194
  * @param values
187
195
  * @return
@@ -197,6 +205,7 @@ public class UtilsJson {
197
205
 
198
206
  /**
199
207
  * Check if the the value type is the same than the field type
208
+ *
200
209
  * @param type
201
210
  * @param value
202
211
  * @return
@@ -223,6 +232,7 @@ public class UtilsJson {
223
232
 
224
233
  /**
225
234
  * Get Field's type and name for a given table
235
+ *
226
236
  * @param mDb
227
237
  * @param tableName
228
238
  * @return
@@ -254,6 +264,7 @@ public class UtilsJson {
254
264
 
255
265
  /**
256
266
  * Get JSObject keys
267
+ *
257
268
  * @param jsonObject
258
269
  * @return
259
270
  */
@@ -270,6 +281,7 @@ public class UtilsJson {
270
281
 
271
282
  /**
272
283
  * Check Row validity
284
+ *
273
285
  * @param mDb
274
286
  * @param tColNames
275
287
  * @param tColTypes
@@ -299,6 +311,7 @@ public class UtilsJson {
299
311
 
300
312
  /**
301
313
  * Check Schema Validity
314
+ *
302
315
  * @param schema
303
316
  * @throws Exception
304
317
  */
@@ -328,6 +341,7 @@ public class UtilsJson {
328
341
 
329
342
  /**
330
343
  * Check Indexes Validity
344
+ *
331
345
  * @param indexes
332
346
  * @throws Exception
333
347
  */
@@ -357,6 +371,7 @@ public class UtilsJson {
357
371
 
358
372
  /**
359
373
  * Check Triggers Validity
374
+ *
360
375
  * @param triggers
361
376
  * @throws Exception
362
377
  */
@@ -386,6 +401,7 @@ public class UtilsJson {
386
401
 
387
402
  /**
388
403
  * Check Views Validity
404
+ *
389
405
  * @param views
390
406
  * @throws Exception
391
407
  */
@@ -406,4 +422,78 @@ public class UtilsJson {
406
422
  }
407
423
  return;
408
424
  }
425
+
426
+ /**
427
+ * Get Tables Values
428
+ *
429
+ * @param mDb
430
+ * @param query
431
+ * @param tableName
432
+ * @return
433
+ * @throws Exception
434
+ */
435
+ public ArrayList<ArrayList<Object>> getValues(Database mDb, String query, String tableName) throws Exception {
436
+ ArrayList<ArrayList<Object>> values = new ArrayList<>();
437
+ try {
438
+ JSObject tableNamesTypes = getTableColumnNamesTypes(mDb, tableName);
439
+ ArrayList<String> rowNames = new ArrayList<>();
440
+ ArrayList<String> rowTypes = new ArrayList<>();
441
+ if (tableNamesTypes.has("names")) {
442
+ rowNames = (ArrayList<String>) tableNamesTypes.get("names");
443
+ } else {
444
+ throw new Exception("GetValues: Table " + tableName + " no names");
445
+ }
446
+ if (tableNamesTypes.has("types")) {
447
+ rowTypes = (ArrayList<String>) tableNamesTypes.get("types");
448
+ } else {
449
+ throw new Exception("GetValues: Table " + tableName + " no types");
450
+ }
451
+ JSArray retValues = mDb.selectSQL(query, new ArrayList<Object>());
452
+ List<JSObject> lValues = retValues.toList();
453
+ if (lValues.size() > 0) {
454
+ for (int j = 0; j < lValues.size(); j++) {
455
+ ArrayList<Object> row = createRowValues(lValues, j, rowNames, rowTypes);
456
+ values.add(row);
457
+ }
458
+ }
459
+ } catch (Exception e) {
460
+ throw new Exception("GetValues: " + e.getMessage());
461
+ } finally {
462
+ return values;
463
+ }
464
+ }
465
+
466
+ /**
467
+ * Get Table Row Values
468
+ * @param values
469
+ * @param pos
470
+ * @param rowNames
471
+ * @param rowTypes
472
+ * @return
473
+ */
474
+ public ArrayList<Object> createRowValues(List<JSObject> values, int pos, ArrayList<String> rowNames, ArrayList<String> rowTypes)
475
+ throws Exception {
476
+ ArrayList<Object> row = new ArrayList<>();
477
+ for (int k = 0; k < rowNames.size(); k++) {
478
+ String nType = rowTypes.get(k);
479
+ String nName = rowNames.get(k);
480
+
481
+ if (values.get(pos).has(nName)) {
482
+ Object obj = values.get(pos).get(nName);
483
+ if (obj.toString().equals("null")) {
484
+ row.add(JSONObject.NULL);
485
+ } else if (obj instanceof Long) {
486
+ row.add(values.get(pos).getLong(nName));
487
+ } else if (obj instanceof String) {
488
+ row.add(values.get(pos).getString(nName));
489
+ } else if (obj instanceof Double) {
490
+ row.add(values.get(pos).getDouble(nName));
491
+ }
492
+ } else {
493
+ String msg = "value is not (string, nsnull," + "int64,double";
494
+ throw new Exception("CreateRowValues: " + msg);
495
+ }
496
+ }
497
+ return row;
498
+ }
409
499
  }
@@ -150,15 +150,15 @@ public class UtilsUpgrade {
150
150
 
151
151
  // -> Update the new table's data from old table's data
152
152
  updateNewTablesData(db);
153
-
153
+ } catch (Exception e) {
154
+ throw new Exception("Error: executeStatementProcess " + " failed " + e);
155
+ } finally {
154
156
  // -> Drop _temp_tables
155
157
  _uDrop.dropTempTables(db, _alterTables);
156
158
 
157
159
  // -> Do some cleanup
158
160
  _alterTables = new Hashtable<>();
159
161
  _commonColumns = new Hashtable<>();
160
- } catch (Exception e) {
161
- throw new Exception("Error: executeStatementProcess " + " failed " + e);
162
162
  }
163
163
  }
164
164
 
@@ -194,9 +194,13 @@ public class UtilsUpgrade {
194
194
  // get the column's name
195
195
  List<String> colNames = getColumnNames(db, table);
196
196
  _alterTables.put(table, colNames);
197
+ // Delete _temp_table if exists
198
+ String tmpTable = "_temp_" + table;
199
+ String delStmt = "DROP TABLE IF EXISTS " + tmpTable + ";";
200
+ db.runSQL(delStmt, new ArrayList<>());
197
201
  // prefix the table with _temp_
198
202
  String stmt = "ALTER TABLE " + table + " RENAME ";
199
- stmt += "TO _temp_" + table + ";";
203
+ stmt += "TO " + tmpTable + ";";
200
204
  JSObject ret = db.runSQL(stmt, new ArrayList<>());
201
205
  long lastId = ret.getLong("lastId");
202
206
  if (lastId == -1) {