@capacitor-community/sqlite 5.0.5-2 → 5.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLite.java +118 -156
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLitePlugin.java +81 -249
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/NotificationCenter.java +1 -1
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/RetHandler.java +0 -12
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java +184 -40
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ExportToJson.java +141 -135
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ImportFromJson.java +2 -1
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/UtilsEncryption.java +111 -0
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsBiometric.java +0 -4
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsFile.java +30 -18
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsMigrate.java +12 -4
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsNCDatabase.java +4 -1
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsSQLCipher.java +8 -6
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsSQLite.java +14 -28
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsSecret.java +3 -4
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsUpgrade.java +0 -1
- package/dist/esm/definitions.d.ts +91 -4
- package/dist/esm/definitions.js +79 -19
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/web.d.ts +3 -1
- package/dist/esm/web.js +8 -0
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +87 -19
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +87 -19
- package/dist/plugin.js.map +1 -1
- package/electron/dist/plugin.js +333 -148
- package/electron/dist/plugin.js.map +1 -1
- package/electron/rollup.config.js +2 -0
- package/ios/Plugin/CapacitorSQLite.swift +125 -92
- package/ios/Plugin/CapacitorSQLitePlugin.swift +6 -3
- package/ios/Plugin/Database.swift +45 -19
- package/ios/Plugin/ImportExportJson/ExportToJson.swift +10 -5
- package/ios/Plugin/ImportExportJson/ImportData.swift +434 -0
- package/ios/Plugin/ImportExportJson/ImportFromJson.swift +47 -59
- package/ios/Plugin/ImportExportJson/JsonSQLite.swift +7 -0
- package/ios/Plugin/Utils/UtilsDownloadFromHTTP.swift +61 -61
- package/ios/Plugin/Utils/UtilsDrop.swift +2 -1
- package/ios/Plugin/Utils/UtilsJson.swift +123 -1
- package/ios/Plugin/Utils/UtilsSQLCipher.swift +254 -23
- package/ios/Plugin/Utils/UtilsUpgrade.swift +0 -1
- package/package.json +2 -2
- package/src/definitions.ts +171 -18
- package/src/web.ts +10 -0
|
@@ -5,29 +5,27 @@ import com.getcapacitor.JSObject;
|
|
|
5
5
|
import com.getcapacitor.community.database.sqlite.NotificationCenter;
|
|
6
6
|
import com.getcapacitor.community.database.sqlite.SQLite.Database;
|
|
7
7
|
import com.getcapacitor.community.database.sqlite.SQLite.UtilsDrop;
|
|
8
|
-
import com.getcapacitor.community.database.sqlite.SQLite.UtilsSQLite;
|
|
9
8
|
import java.util.ArrayList;
|
|
10
|
-
import java.util.Date;
|
|
11
9
|
import java.util.HashMap;
|
|
12
10
|
import java.util.List;
|
|
13
11
|
import java.util.Map;
|
|
12
|
+
import java.util.Objects;
|
|
14
13
|
import org.json.JSONException;
|
|
15
14
|
import org.json.JSONObject;
|
|
16
15
|
|
|
17
16
|
public class ExportToJson {
|
|
18
17
|
|
|
19
18
|
private static final String TAG = ImportFromJson.class.getName();
|
|
20
|
-
private UtilsJson uJson = new UtilsJson();
|
|
21
|
-
private
|
|
22
|
-
private UtilsDrop _uDrop = new UtilsDrop();
|
|
19
|
+
private final UtilsJson uJson = new UtilsJson();
|
|
20
|
+
private final UtilsDrop _uDrop = new UtilsDrop();
|
|
23
21
|
|
|
24
22
|
/**
|
|
25
23
|
* Notify progress export event
|
|
26
|
-
* @param msg
|
|
24
|
+
* @param msg message to notify
|
|
27
25
|
*/
|
|
28
26
|
public void notifyExportProgressEvent(String msg) {
|
|
29
27
|
String message = "Export: " + msg;
|
|
30
|
-
Map<String, Object> info = new HashMap
|
|
28
|
+
Map<String, Object> info = new HashMap<>() {
|
|
31
29
|
{
|
|
32
30
|
put("progress", message);
|
|
33
31
|
}
|
|
@@ -39,29 +37,29 @@ public class ExportToJson {
|
|
|
39
37
|
* GetLastExportDate method
|
|
40
38
|
* get the last export date
|
|
41
39
|
*
|
|
42
|
-
* @
|
|
43
|
-
* @
|
|
40
|
+
* @param mDb Database
|
|
41
|
+
* @return Long lastExportDate
|
|
42
|
+
* @throws Exception message
|
|
44
43
|
*/
|
|
44
|
+
|
|
45
45
|
public Long getLastExportDate(Database mDb) throws Exception {
|
|
46
46
|
long lastExportDate = -1;
|
|
47
47
|
String stmt = "SELECT sync_date FROM sync_table WHERE id = 2;";
|
|
48
|
-
JSArray retQuery = new JSArray();
|
|
49
48
|
|
|
50
49
|
try {
|
|
51
50
|
boolean isSyncTable = uJson.isTableExists(mDb, "sync_table");
|
|
52
51
|
if (!isSyncTable) {
|
|
53
52
|
throw new Exception("GetSyncDate: No sync_table available");
|
|
54
53
|
}
|
|
55
|
-
retQuery = mDb.selectSQL(stmt, new ArrayList
|
|
54
|
+
JSArray retQuery = mDb.selectSQL(stmt, new ArrayList<>());
|
|
56
55
|
List<JSObject> lQuery = retQuery.toList();
|
|
57
56
|
if (lQuery.size() == 1) {
|
|
58
57
|
long syncDate = lQuery.get(0).getLong("sync_date");
|
|
59
58
|
if (syncDate > 0) lastExportDate = syncDate;
|
|
60
59
|
}
|
|
60
|
+
return lastExportDate;
|
|
61
61
|
} catch (Exception e) {
|
|
62
62
|
throw new Exception("GetSyncDate: " + e.getMessage());
|
|
63
|
-
} finally {
|
|
64
|
-
return lastExportDate;
|
|
65
63
|
}
|
|
66
64
|
}
|
|
67
65
|
|
|
@@ -71,18 +69,18 @@ public class ExportToJson {
|
|
|
71
69
|
if (!isSyncTable) {
|
|
72
70
|
throw new Exception("SetLastExportDate: No sync_table available");
|
|
73
71
|
}
|
|
74
|
-
String stmt
|
|
72
|
+
String stmt;
|
|
75
73
|
Long lastExportDate = getLastExportDate(mDb);
|
|
76
74
|
if (lastExportDate > 0) {
|
|
77
75
|
stmt = "UPDATE sync_table SET sync_date = " + sTime + " WHERE id = 2;";
|
|
78
76
|
} else {
|
|
79
77
|
stmt = "INSERT INTO sync_table (sync_date) VALUES (" + sTime + ");";
|
|
80
78
|
}
|
|
81
|
-
|
|
79
|
+
JSObject retObj = mDb.prepareSQL(stmt, new ArrayList<>(), false, "no");
|
|
80
|
+
long lastId = retObj.getLong("lastId");
|
|
82
81
|
if (lastId < 0) {
|
|
83
82
|
throw new Exception("SetLastExportDate: lastId < 0");
|
|
84
83
|
}
|
|
85
|
-
return;
|
|
86
84
|
} catch (Exception e) {
|
|
87
85
|
throw new Exception("SetLastExportDate: " + e.getMessage());
|
|
88
86
|
}
|
|
@@ -90,7 +88,7 @@ public class ExportToJson {
|
|
|
90
88
|
|
|
91
89
|
/**
|
|
92
90
|
* Delete Exported Rows
|
|
93
|
-
* @throws Exception
|
|
91
|
+
* @throws Exception message
|
|
94
92
|
*/
|
|
95
93
|
public void delExportedRows(Database mDb) throws Exception {
|
|
96
94
|
try {
|
|
@@ -111,15 +109,15 @@ public class ExportToJson {
|
|
|
111
109
|
}
|
|
112
110
|
// Loop through the tables
|
|
113
111
|
for (String table : tables) {
|
|
114
|
-
long lastId
|
|
112
|
+
long lastId;
|
|
115
113
|
// define the delete statement
|
|
116
114
|
String delStmt = "DELETE FROM " + table + " WHERE sql_deleted = 1 " + "AND last_modified < " + lastExportDate + ";";
|
|
117
|
-
|
|
115
|
+
JSObject retObj = mDb.prepareSQL(delStmt, new ArrayList<>(), true, "no");
|
|
116
|
+
lastId = retObj.getLong("lastId");
|
|
118
117
|
if (lastId < 0) {
|
|
119
118
|
throw new Exception("SetLastExportDate: lastId < 0");
|
|
120
119
|
}
|
|
121
120
|
}
|
|
122
|
-
return;
|
|
123
121
|
} catch (Exception e) {
|
|
124
122
|
throw new Exception("DelExportedRows: " + e.getMessage());
|
|
125
123
|
}
|
|
@@ -127,20 +125,20 @@ public class ExportToJson {
|
|
|
127
125
|
|
|
128
126
|
/**
|
|
129
127
|
* Create Export Json Object from Database (Schema, Data)
|
|
130
|
-
* @param db
|
|
131
|
-
* @param sqlObj
|
|
132
|
-
* @return
|
|
128
|
+
* @param db Database
|
|
129
|
+
* @param sqlObj Json SQLite Object
|
|
130
|
+
* @return JsonSQLite
|
|
133
131
|
*/
|
|
134
132
|
public JsonSQLite createExportObject(Database db, JsonSQLite sqlObj) throws Exception {
|
|
135
133
|
JsonSQLite retObj = new JsonSQLite();
|
|
136
134
|
ArrayList<JsonView> views = new ArrayList<>();
|
|
137
|
-
ArrayList<JsonTable> tables
|
|
138
|
-
|
|
135
|
+
ArrayList<JsonTable> tables;
|
|
136
|
+
boolean isSyncTable;
|
|
139
137
|
try {
|
|
140
138
|
// Get Views
|
|
141
139
|
String stmtV = "SELECT name,sql FROM sqlite_master WHERE ";
|
|
142
140
|
stmtV += "type = 'view' AND name NOT LIKE 'sqlite_%';";
|
|
143
|
-
JSArray resViews = db.selectSQL(stmtV, new ArrayList
|
|
141
|
+
JSArray resViews = db.selectSQL(stmtV, new ArrayList<>());
|
|
144
142
|
if (resViews.length() > 0) {
|
|
145
143
|
for (int i = 0; i < resViews.length(); i++) {
|
|
146
144
|
JSONObject oView = resViews.getJSONObject(i);
|
|
@@ -157,7 +155,7 @@ public class ExportToJson {
|
|
|
157
155
|
stmt += "type = 'table' AND name NOT LIKE 'sqlite_%' AND ";
|
|
158
156
|
stmt += "name NOT LIKE 'android_%' AND ";
|
|
159
157
|
stmt += "name NOT LIKE 'sync_table';";
|
|
160
|
-
JSArray resTables = db.selectSQL(stmt, new ArrayList
|
|
158
|
+
JSArray resTables = db.selectSQL(stmt, new ArrayList<>());
|
|
161
159
|
if (resTables.length() == 0) {
|
|
162
160
|
throw new Exception("CreateExportObject: table's names failed");
|
|
163
161
|
} else {
|
|
@@ -166,16 +164,12 @@ public class ExportToJson {
|
|
|
166
164
|
throw new Exception("No sync_table available");
|
|
167
165
|
}
|
|
168
166
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
break;
|
|
176
|
-
default:
|
|
177
|
-
throw new Exception("CreateExportObject: expMode " + sqlObj.getMode() + " not defined");
|
|
178
|
-
}
|
|
167
|
+
tables =
|
|
168
|
+
switch (sqlObj.getMode()) {
|
|
169
|
+
case "partial" -> getTablesPartial(db, resTables);
|
|
170
|
+
case "full" -> getTablesFull(db, resTables);
|
|
171
|
+
default -> throw new Exception("CreateExportObject: expMode " + sqlObj.getMode() + " not defined");
|
|
172
|
+
};
|
|
179
173
|
if (tables.size() > 0) {
|
|
180
174
|
retObj.setDatabase(sqlObj.getDatabase());
|
|
181
175
|
retObj.setVersion(sqlObj.getVersion());
|
|
@@ -195,10 +189,10 @@ public class ExportToJson {
|
|
|
195
189
|
|
|
196
190
|
/**
|
|
197
191
|
* get Tables when Mode is Full
|
|
198
|
-
* @param mDb
|
|
199
|
-
* @param resTables
|
|
200
|
-
* @return
|
|
201
|
-
* @throws Exception
|
|
192
|
+
* @param mDb Database
|
|
193
|
+
* @param resTables JSArray
|
|
194
|
+
* @return ArrayList of JsonTable
|
|
195
|
+
* @throws Exception message
|
|
202
196
|
*/
|
|
203
197
|
private ArrayList<JsonTable> getTablesFull(Database mDb, JSArray resTables) throws Exception {
|
|
204
198
|
ArrayList<JsonTable> tables = new ArrayList<>();
|
|
@@ -213,6 +207,8 @@ public class ExportToJson {
|
|
|
213
207
|
} else {
|
|
214
208
|
throw new Exception("GetTablesFull: no name");
|
|
215
209
|
}
|
|
210
|
+
if (tableName == null) throw new Exception("GetTablesFull: no name");
|
|
211
|
+
|
|
216
212
|
if (lTables.get(i).has("sql")) {
|
|
217
213
|
sqlStmt = lTables.get(i).getString("sql");
|
|
218
214
|
} else {
|
|
@@ -270,53 +266,53 @@ public class ExportToJson {
|
|
|
270
266
|
msg += " " + (i + 1) + "/" + lTables.size() + " ...";
|
|
271
267
|
notifyExportProgressEvent(msg);
|
|
272
268
|
}
|
|
273
|
-
} catch (Exception e) {
|
|
274
|
-
throw new Exception("GetTablesFull: " + e.getMessage());
|
|
275
|
-
} finally {
|
|
276
269
|
String msg = "Full: Table's export completed";
|
|
277
270
|
notifyExportProgressEvent(msg);
|
|
278
271
|
return tables;
|
|
272
|
+
} catch (Exception e) {
|
|
273
|
+
String msg = "Full: Table's export failed";
|
|
274
|
+
notifyExportProgressEvent(msg);
|
|
275
|
+
throw new Exception("GetTablesFull: " + e.getMessage());
|
|
279
276
|
}
|
|
280
277
|
}
|
|
281
278
|
|
|
282
279
|
/**
|
|
283
280
|
* Modify ',' by '§' for Embedded Parentheses
|
|
284
|
-
* @param sqlStmt
|
|
285
|
-
* @return
|
|
281
|
+
* @param sqlStmt SQLite statement
|
|
282
|
+
* @return String
|
|
286
283
|
*/
|
|
287
284
|
private String modEmbeddedParentheses(String sqlStmt) throws Exception {
|
|
288
|
-
|
|
289
|
-
List<Integer>
|
|
290
|
-
List<Integer> cPars = getIndices(stmt, ")");
|
|
285
|
+
List<Integer> oPars = getIndices(sqlStmt, "(");
|
|
286
|
+
List<Integer> cPars = getIndices(sqlStmt, ")");
|
|
291
287
|
if (oPars.size() != cPars.size()) {
|
|
292
288
|
throw new Exception("ModEmbeddedParentheses: Not same number of " + "opening and closing parentheses");
|
|
293
289
|
}
|
|
294
|
-
if (oPars.size() == 0) return
|
|
295
|
-
|
|
290
|
+
if (oPars.size() == 0) return sqlStmt;
|
|
291
|
+
StringBuilder resStmt = new StringBuilder(sqlStmt.substring(0, oPars.get(0) - 1));
|
|
296
292
|
for (int i = 0; i < oPars.size(); i++) {
|
|
297
293
|
String str;
|
|
298
294
|
if (i < oPars.size() - 1) {
|
|
299
295
|
if (oPars.get(i + 1) < cPars.get(i)) {
|
|
300
|
-
str =
|
|
296
|
+
str = sqlStmt.substring(oPars.get(i) - 1, cPars.get(i + 1));
|
|
301
297
|
i++;
|
|
302
298
|
} else {
|
|
303
|
-
str =
|
|
299
|
+
str = sqlStmt.substring(oPars.get(i) - 1, cPars.get(i));
|
|
304
300
|
}
|
|
305
301
|
} else {
|
|
306
|
-
str =
|
|
302
|
+
str = sqlStmt.substring(oPars.get(i) - 1, cPars.get(i));
|
|
307
303
|
}
|
|
308
304
|
String newS = str.replaceAll(",", "§");
|
|
309
|
-
resStmt
|
|
305
|
+
resStmt.append(newS);
|
|
310
306
|
if (i < oPars.size() - 1) {
|
|
311
|
-
resStmt
|
|
307
|
+
resStmt.append(sqlStmt.substring(cPars.get(i), oPars.get(i + 1) - 1));
|
|
312
308
|
}
|
|
313
309
|
}
|
|
314
|
-
resStmt
|
|
315
|
-
return resStmt;
|
|
310
|
+
resStmt.append(sqlStmt.substring(cPars.get(cPars.size() - 1)));
|
|
311
|
+
return resStmt.toString();
|
|
316
312
|
}
|
|
317
313
|
|
|
318
314
|
private List<Integer> getIndices(String textString, String search) {
|
|
319
|
-
List<Integer> indexes = new ArrayList
|
|
315
|
+
List<Integer> indexes = new ArrayList<>();
|
|
320
316
|
|
|
321
317
|
int index = 0;
|
|
322
318
|
while (index != -1) {
|
|
@@ -331,8 +327,8 @@ public class ExportToJson {
|
|
|
331
327
|
|
|
332
328
|
/**
|
|
333
329
|
* Get Schema
|
|
334
|
-
* @param sqlStmt
|
|
335
|
-
* @return
|
|
330
|
+
* @param sqlStmt SQLite statement
|
|
331
|
+
* @return ArrayList<JsonColumn>
|
|
336
332
|
*/
|
|
337
333
|
private ArrayList<JsonColumn> getSchema(String sqlStmt) throws Exception {
|
|
338
334
|
String msg = "GetSchema: ";
|
|
@@ -345,22 +341,22 @@ public class ExportToJson {
|
|
|
345
341
|
String[] sch = sqlStmt.split(",");
|
|
346
342
|
// for each element of the array split the
|
|
347
343
|
// first word as key
|
|
348
|
-
for (
|
|
349
|
-
String sc =
|
|
344
|
+
for (String s : sch) {
|
|
345
|
+
String sc = s.replaceAll("\n", "").trim();
|
|
350
346
|
String[] row = sc.trim().split(" ", 2);
|
|
351
347
|
JsonColumn jsonRow = new JsonColumn();
|
|
352
|
-
|
|
353
|
-
|
|
348
|
+
int oPar;
|
|
349
|
+
int cPar;
|
|
354
350
|
switch (row[0].toUpperCase()) {
|
|
355
|
-
case "FOREIGN"
|
|
351
|
+
case "FOREIGN" -> {
|
|
356
352
|
oPar = sc.indexOf("(");
|
|
357
353
|
cPar = sc.indexOf(")");
|
|
358
354
|
String fk = sc.substring(oPar + 1, cPar);
|
|
359
355
|
row[0] = fk.replaceAll("§", ",");
|
|
360
356
|
row[1] = sc.substring(cPar + 2);
|
|
361
357
|
jsonRow.setForeignkey(row[0]);
|
|
362
|
-
|
|
363
|
-
case "PRIMARY"
|
|
358
|
+
}
|
|
359
|
+
case "PRIMARY" -> {
|
|
364
360
|
oPar = sc.indexOf("(");
|
|
365
361
|
cPar = sc.indexOf(")");
|
|
366
362
|
String pk = sc.substring(oPar + 1, cPar);
|
|
@@ -368,32 +364,30 @@ public class ExportToJson {
|
|
|
368
364
|
row[0] = row[0].replaceAll("_ ", "_");
|
|
369
365
|
row[1] = sc.substring(cPar + 2);
|
|
370
366
|
jsonRow.setConstraint(row[0]);
|
|
371
|
-
|
|
372
|
-
case "CONSTRAINT"
|
|
367
|
+
}
|
|
368
|
+
case "CONSTRAINT" -> {
|
|
373
369
|
String[] tRow = row[1].trim().split(" ", 2);
|
|
374
370
|
row[0] = tRow[0];
|
|
375
371
|
jsonRow.setConstraint(row[0]);
|
|
376
372
|
row[1] = tRow[1];
|
|
377
|
-
|
|
378
|
-
default
|
|
379
|
-
jsonRow.setColumn(row[0]);
|
|
373
|
+
}
|
|
374
|
+
default -> jsonRow.setColumn(row[0]);
|
|
380
375
|
}
|
|
381
376
|
jsonRow.setValue(row[1].replaceAll("§", ","));
|
|
382
377
|
schema.add(jsonRow);
|
|
383
378
|
}
|
|
379
|
+
return schema;
|
|
384
380
|
} catch (JSONException e) {
|
|
385
381
|
throw new Exception(msg + e.getMessage());
|
|
386
|
-
} finally {
|
|
387
|
-
return schema;
|
|
388
382
|
}
|
|
389
383
|
}
|
|
390
384
|
|
|
391
385
|
/**
|
|
392
386
|
* Get Indexes
|
|
393
|
-
* @param mDb
|
|
394
|
-
* @param tableName
|
|
395
|
-
* @return
|
|
396
|
-
* @throws Exception
|
|
387
|
+
* @param mDb Database
|
|
388
|
+
* @param tableName table name
|
|
389
|
+
* @return ArrayList<JsonIndex>
|
|
390
|
+
* @throws Exception message
|
|
397
391
|
*/
|
|
398
392
|
private ArrayList<JsonIndex> getIndexes(Database mDb, String tableName) throws Exception {
|
|
399
393
|
String msg = "GetIndexes: ";
|
|
@@ -403,39 +397,42 @@ public class ExportToJson {
|
|
|
403
397
|
stmt += "type = 'index' AND tbl_name = '" + tableName;
|
|
404
398
|
stmt += "' AND sql NOTNULL;";
|
|
405
399
|
try {
|
|
406
|
-
JSArray retIndexes = mDb.selectSQL(stmt, new ArrayList
|
|
400
|
+
JSArray retIndexes = mDb.selectSQL(stmt, new ArrayList<>());
|
|
407
401
|
List<JSObject> lIndexes = retIndexes.toList();
|
|
408
402
|
if (lIndexes.size() > 0) {
|
|
409
403
|
for (int j = 0; j < lIndexes.size(); j++) {
|
|
410
404
|
JsonIndex jsonRow = new JsonIndex();
|
|
411
|
-
if (lIndexes.get(j).getString("tbl_name")
|
|
405
|
+
if (Objects.equals(lIndexes.get(j).getString("tbl_name"), tableName)) {
|
|
412
406
|
jsonRow.setName(lIndexes.get(j).getString("name"));
|
|
413
407
|
String sql = lIndexes.get(j).getString("sql");
|
|
414
|
-
if (sql.contains("UNIQUE")) {
|
|
408
|
+
if (sql != null && sql.contains("UNIQUE")) {
|
|
415
409
|
jsonRow.setMode("UNIQUE");
|
|
416
410
|
}
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
411
|
+
if (sql != null) {
|
|
412
|
+
int oPar = sql.lastIndexOf("(");
|
|
413
|
+
int cPar = sql.lastIndexOf(")");
|
|
414
|
+
jsonRow.setValue(sql.substring(oPar + 1, cPar));
|
|
415
|
+
indexes.add(jsonRow);
|
|
416
|
+
} else {
|
|
417
|
+
throw new Exception(msg + "sql statement is null");
|
|
418
|
+
}
|
|
421
419
|
} else {
|
|
422
420
|
throw new Exception(msg + "table name doesn't match");
|
|
423
421
|
}
|
|
424
422
|
}
|
|
425
423
|
}
|
|
424
|
+
return indexes;
|
|
426
425
|
} catch (JSONException e) {
|
|
427
426
|
throw new Exception(msg + e.getMessage());
|
|
428
|
-
} finally {
|
|
429
|
-
return indexes;
|
|
430
427
|
}
|
|
431
428
|
}
|
|
432
429
|
|
|
433
430
|
/**
|
|
434
431
|
* Get Triggers
|
|
435
|
-
* @param mDb
|
|
436
|
-
* @param tableName
|
|
437
|
-
* @return
|
|
438
|
-
* @throws Exception
|
|
432
|
+
* @param mDb Database
|
|
433
|
+
* @param tableName table name
|
|
434
|
+
* @return ArrayList<JsonTrigger>
|
|
435
|
+
* @throws Exception message
|
|
439
436
|
*/
|
|
440
437
|
private ArrayList<JsonTrigger> getTriggers(Database mDb, String tableName) throws Exception {
|
|
441
438
|
String msg = "Error: getTriggers ";
|
|
@@ -445,15 +442,19 @@ public class ExportToJson {
|
|
|
445
442
|
stmt += "type = 'trigger' AND tbl_name = '" + tableName;
|
|
446
443
|
stmt += "' AND sql NOTNULL;";
|
|
447
444
|
try {
|
|
448
|
-
JSArray retTriggers = mDb.selectSQL(stmt, new ArrayList
|
|
445
|
+
JSArray retTriggers = mDb.selectSQL(stmt, new ArrayList<>());
|
|
449
446
|
List<JSObject> lTriggers = retTriggers.toList();
|
|
450
447
|
if (lTriggers.size() > 0) {
|
|
451
448
|
for (int j = 0; j < lTriggers.size(); j++) {
|
|
452
449
|
JsonTrigger jsonRow = new JsonTrigger();
|
|
453
|
-
if (lTriggers.get(j).getString("tbl_name")
|
|
450
|
+
if (Objects.equals(lTriggers.get(j).getString("tbl_name"), tableName)) {
|
|
454
451
|
String name = lTriggers.get(j).getString("name");
|
|
455
452
|
String sql = lTriggers.get(j).getString("sql");
|
|
456
|
-
String[] sqlArr
|
|
453
|
+
String[] sqlArr;
|
|
454
|
+
if (sql == null || name == null) {
|
|
455
|
+
throw new Exception(msg + "sql statement or name is null");
|
|
456
|
+
}
|
|
457
|
+
sqlArr = sql.split(name);
|
|
457
458
|
if (sqlArr.length != 2) {
|
|
458
459
|
throw new Exception(msg + "sql split name does not return 2 values");
|
|
459
460
|
}
|
|
@@ -466,8 +467,8 @@ public class ExportToJson {
|
|
|
466
467
|
throw new Exception(msg + "sql split tableName does not return 2 values");
|
|
467
468
|
}
|
|
468
469
|
String condition = "";
|
|
469
|
-
String logic
|
|
470
|
-
if (!sqlArr[1].trim().substring(0, 5).
|
|
470
|
+
String logic;
|
|
471
|
+
if (!sqlArr[1].trim().substring(0, 5).equalsIgnoreCase("BEGIN")) {
|
|
471
472
|
sqlArr = sqlArr[1].trim().split("BEGIN");
|
|
472
473
|
if (sqlArr.length != 2) {
|
|
473
474
|
throw new Exception(msg + "sql split BEGIN does not return 2 values");
|
|
@@ -490,25 +491,24 @@ public class ExportToJson {
|
|
|
490
491
|
}
|
|
491
492
|
}
|
|
492
493
|
}
|
|
494
|
+
return triggers;
|
|
493
495
|
} catch (JSONException e) {
|
|
494
496
|
throw new Exception(msg + e.getMessage());
|
|
495
|
-
} finally {
|
|
496
|
-
return triggers;
|
|
497
497
|
}
|
|
498
498
|
}
|
|
499
499
|
|
|
500
500
|
/**
|
|
501
501
|
* Get Tables when Mode is Partial
|
|
502
|
-
* @param mDb
|
|
503
|
-
* @param resTables
|
|
504
|
-
* @return
|
|
505
|
-
* @throws Exception
|
|
502
|
+
* @param mDb Database
|
|
503
|
+
* @param resTables tables
|
|
504
|
+
* @return ArrayList<JsonTable>
|
|
505
|
+
* @throws Exception message
|
|
506
506
|
*/
|
|
507
507
|
private ArrayList<JsonTable> getTablesPartial(Database mDb, JSArray resTables) throws Exception {
|
|
508
508
|
ArrayList<JsonTable> tables = new ArrayList<>();
|
|
509
|
-
long syncDate
|
|
510
|
-
JSObject modTables
|
|
511
|
-
ArrayList<String> modTablesKeys
|
|
509
|
+
long syncDate;
|
|
510
|
+
JSObject modTables;
|
|
511
|
+
ArrayList<String> modTablesKeys;
|
|
512
512
|
|
|
513
513
|
try {
|
|
514
514
|
// Get the syncDate and the Modified Tables
|
|
@@ -523,6 +523,9 @@ public class ExportToJson {
|
|
|
523
523
|
} else {
|
|
524
524
|
throw new Exception("GetTablesPartial: no modTables");
|
|
525
525
|
}
|
|
526
|
+
if (modTables == null) {
|
|
527
|
+
throw new Exception("GetTablesPartial: no modTables");
|
|
528
|
+
}
|
|
526
529
|
modTablesKeys = uJson.getJSObjectKeys(modTables);
|
|
527
530
|
|
|
528
531
|
// Loop trough tables
|
|
@@ -535,12 +538,17 @@ public class ExportToJson {
|
|
|
535
538
|
} else {
|
|
536
539
|
throw new Exception("GetTablesPartial: no name");
|
|
537
540
|
}
|
|
541
|
+
if (tableName == null) {
|
|
542
|
+
throw new Exception("GetTablesPartial: no name");
|
|
543
|
+
}
|
|
538
544
|
if (lTables.get(i).has("sql")) {
|
|
539
545
|
sqlStmt = lTables.get(i).getString("sql");
|
|
540
546
|
} else {
|
|
541
547
|
throw new Exception("GetTablesPartial: no sql");
|
|
542
548
|
}
|
|
543
|
-
if (
|
|
549
|
+
if (
|
|
550
|
+
modTablesKeys.size() == 0 || !modTablesKeys.contains(tableName) || Objects.equals(modTables.getString(tableName), "No")
|
|
551
|
+
) {
|
|
544
552
|
continue;
|
|
545
553
|
}
|
|
546
554
|
JsonTable table = new JsonTable();
|
|
@@ -548,7 +556,7 @@ public class ExportToJson {
|
|
|
548
556
|
ArrayList<JsonColumn> schema = new ArrayList<>();
|
|
549
557
|
ArrayList<JsonIndex> indexes = new ArrayList<>();
|
|
550
558
|
ArrayList<JsonTrigger> triggers = new ArrayList<>();
|
|
551
|
-
if (modTables.getString(tableName)
|
|
559
|
+
if (Objects.equals(modTables.getString(tableName), "Create")) {
|
|
552
560
|
// create Table's Schema
|
|
553
561
|
schema = getSchema(sqlStmt);
|
|
554
562
|
if (schema.size() > 0) {
|
|
@@ -573,7 +581,7 @@ public class ExportToJson {
|
|
|
573
581
|
}
|
|
574
582
|
// create Table's Data
|
|
575
583
|
String query;
|
|
576
|
-
if (modTables.getString(tableName)
|
|
584
|
+
if (Objects.equals(modTables.getString(tableName), "Create")) {
|
|
577
585
|
query = "SELECT * FROM " + tableName + ";";
|
|
578
586
|
} else {
|
|
579
587
|
query = "SELECT * FROM " + tableName + " WHERE last_modified >= " + syncDate + ";";
|
|
@@ -606,26 +614,27 @@ public class ExportToJson {
|
|
|
606
614
|
msg += " " + (i + 1) + "/" + lTables.size() + " ...";
|
|
607
615
|
notifyExportProgressEvent(msg);
|
|
608
616
|
}
|
|
609
|
-
} catch (Exception e) {
|
|
610
|
-
throw new Exception("GetTablesPartial: " + e.getMessage());
|
|
611
|
-
} finally {
|
|
612
617
|
String msg = "Partial: Table's export completed";
|
|
613
618
|
notifyExportProgressEvent(msg);
|
|
614
619
|
return tables;
|
|
620
|
+
} catch (Exception e) {
|
|
621
|
+
String msg = "Partial: Table's export failed";
|
|
622
|
+
notifyExportProgressEvent(msg);
|
|
623
|
+
throw new Exception("GetTablesPartial: " + e.getMessage());
|
|
615
624
|
}
|
|
616
625
|
}
|
|
617
626
|
|
|
618
627
|
/**
|
|
619
628
|
* Get Tables Data when Mode is Partial
|
|
620
|
-
* @param mDb
|
|
621
|
-
* @param resTables
|
|
622
|
-
* @return
|
|
623
|
-
* @throws Exception
|
|
629
|
+
* @param mDb Database
|
|
630
|
+
* @param resTables tables
|
|
631
|
+
* @return JSObject
|
|
632
|
+
* @throws Exception message
|
|
624
633
|
*/
|
|
625
634
|
private JSObject getPartialModeData(Database mDb, JSArray resTables) throws Exception {
|
|
626
635
|
JSObject retData = new JSObject();
|
|
627
636
|
Long syncDate;
|
|
628
|
-
JSObject modTables
|
|
637
|
+
JSObject modTables;
|
|
629
638
|
|
|
630
639
|
try {
|
|
631
640
|
// get the sync date if expMode = "partial"
|
|
@@ -638,48 +647,46 @@ public class ExportToJson {
|
|
|
638
647
|
modTables = getTablesModified(mDb, resTables, syncDate);
|
|
639
648
|
retData.put("syncDate", syncDate);
|
|
640
649
|
retData.put("modTables", modTables);
|
|
650
|
+
return retData;
|
|
641
651
|
} catch (Exception e) {
|
|
642
652
|
throw new Exception("GetPartialModeData: " + e.getMessage());
|
|
643
|
-
} finally {
|
|
644
|
-
return retData;
|
|
645
653
|
}
|
|
646
654
|
}
|
|
647
655
|
|
|
648
656
|
/**
|
|
649
657
|
* Get Synchronization Date
|
|
650
|
-
* @param mDb
|
|
651
|
-
* @return
|
|
652
|
-
* @throws Exception
|
|
658
|
+
* @param mDb Database
|
|
659
|
+
* @return Long synchronization date
|
|
660
|
+
* @throws Exception message
|
|
653
661
|
*/
|
|
654
662
|
public Long getSyncDate(Database mDb) throws Exception {
|
|
655
663
|
long ret = -1;
|
|
656
664
|
String stmt = "SELECT sync_date FROM sync_table WHERE id = 1;";
|
|
657
|
-
JSArray retQuery
|
|
665
|
+
JSArray retQuery;
|
|
658
666
|
try {
|
|
659
667
|
boolean isSyncTable = uJson.isTableExists(mDb, "sync_table");
|
|
660
668
|
if (!isSyncTable) {
|
|
661
669
|
throw new Exception("No sync_table available");
|
|
662
670
|
}
|
|
663
|
-
retQuery = mDb.selectSQL(stmt, new ArrayList
|
|
671
|
+
retQuery = mDb.selectSQL(stmt, new ArrayList<>());
|
|
664
672
|
List<JSObject> lQuery = retQuery.toList();
|
|
665
673
|
if (lQuery.size() == 1) {
|
|
666
674
|
long syncDate = lQuery.get(0).getLong("sync_date");
|
|
667
675
|
if (syncDate > 0) ret = syncDate;
|
|
668
676
|
}
|
|
677
|
+
return ret;
|
|
669
678
|
} catch (Exception e) {
|
|
670
679
|
throw new Exception("GetSyncDate: " + e.getMessage());
|
|
671
|
-
} finally {
|
|
672
|
-
return ret;
|
|
673
680
|
}
|
|
674
681
|
}
|
|
675
682
|
|
|
676
683
|
/**
|
|
677
684
|
* Get the tables which have been modified since last sync
|
|
678
|
-
* @param mDb
|
|
679
|
-
* @param resTables
|
|
680
|
-
* @param syncDate
|
|
681
|
-
* @return
|
|
682
|
-
* @throws Exception
|
|
685
|
+
* @param mDb Database
|
|
686
|
+
* @param resTables tables
|
|
687
|
+
* @param syncDate synchronization date
|
|
688
|
+
* @return JSObject
|
|
689
|
+
* @throws Exception message
|
|
683
690
|
*/
|
|
684
691
|
private JSObject getTablesModified(Database mDb, JSArray resTables, Long syncDate) throws Exception {
|
|
685
692
|
JSObject retObj = new JSObject();
|
|
@@ -694,13 +701,13 @@ public class ExportToJson {
|
|
|
694
701
|
throw new Exception("GetTablesModified: no name");
|
|
695
702
|
}
|
|
696
703
|
String stmt = "SELECT count(*) AS count FROM " + tableName + ";";
|
|
697
|
-
JSArray retQuery = mDb.selectSQL(stmt, new ArrayList
|
|
704
|
+
JSArray retQuery = mDb.selectSQL(stmt, new ArrayList<>());
|
|
698
705
|
List<JSObject> lQuery = retQuery.toList();
|
|
699
706
|
if (lQuery.size() != 1) break;
|
|
700
707
|
long totalCount = lQuery.get(0).getLong("count");
|
|
701
708
|
// get total count of modified since last sync
|
|
702
709
|
stmt = "SELECT count(*) AS count FROM " + tableName + " WHERE last_modified >= " + syncDate + ";";
|
|
703
|
-
retQuery = mDb.selectSQL(stmt, new ArrayList
|
|
710
|
+
retQuery = mDb.selectSQL(stmt, new ArrayList<>());
|
|
704
711
|
lQuery = retQuery.toList();
|
|
705
712
|
if (lQuery.size() != 1) break;
|
|
706
713
|
long totalModCnt = lQuery.get(0).getLong("count");
|
|
@@ -713,10 +720,9 @@ public class ExportToJson {
|
|
|
713
720
|
}
|
|
714
721
|
retObj.put(tableName, mode);
|
|
715
722
|
}
|
|
723
|
+
return retObj;
|
|
716
724
|
} catch (Exception e) {
|
|
717
725
|
throw new Exception("GetTablesModified: " + e.getMessage());
|
|
718
|
-
} finally {
|
|
719
|
-
return retObj;
|
|
720
726
|
}
|
|
721
727
|
}
|
|
722
728
|
}
|
|
@@ -408,7 +408,8 @@ public class ImportFromJson {
|
|
|
408
408
|
if (stmt.substring(0, 6).toUpperCase().equals("DELETE")) {
|
|
409
409
|
row = new ArrayList<>();
|
|
410
410
|
}
|
|
411
|
-
|
|
411
|
+
JSObject retObj = mDb.prepareSQL(stmt, row, true, "no");
|
|
412
|
+
long lastId = retObj.getLong("lastId");
|
|
412
413
|
if (lastId < 0) {
|
|
413
414
|
throw new Exception("CreateTableData: lastId < 0");
|
|
414
415
|
}
|