@capacitor-community/sqlite 3.4.3-1 → 3.4.3
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/README.md +55 -21
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLite.java +41 -2
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLitePlugin.java +26 -0
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/NotificationCenter.java +1 -1
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java +220 -7
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ExportToJson.java +100 -3
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ImportFromJson.java +37 -34
- package/dist/esm/definitions.d.ts +18 -4
- package/dist/esm/definitions.js +9 -17
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/web.d.ts +2 -1
- package/dist/esm/web.js +11 -0
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +22 -19
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +1035 -1032
- package/dist/plugin.js.map +1 -1
- package/electron/dist/plugin.js +662 -335
- package/electron/dist/plugin.js.map +1 -1
- package/ios/Plugin/CapacitorSQLite.swift +34 -1
- package/ios/Plugin/CapacitorSQLitePlugin.m +1 -0
- package/ios/Plugin/CapacitorSQLitePlugin.swift +25 -0
- package/ios/Plugin/Database.swift +29 -1
- package/ios/Plugin/Extensions/String.swift +8 -0
- package/ios/Plugin/ImportExportJson/ExportToJson.swift +154 -25
- package/ios/Plugin/ImportExportJson/ImportFromJson.swift +53 -27
- package/ios/Plugin/Utils/UtilsDrop.swift +2 -2
- package/ios/Plugin/Utils/UtilsSQLCipher.swift +277 -8
- package/ios/Plugin/Utils/UtilsUpgrade.swift +0 -1
- package/package.json +2 -2
package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java
CHANGED
|
@@ -18,6 +18,7 @@ import com.getcapacitor.JSObject;
|
|
|
18
18
|
import com.getcapacitor.community.database.sqlite.SQLite.GlobalSQLite;
|
|
19
19
|
import com.getcapacitor.community.database.sqlite.SQLite.ImportExportJson.ExportToJson;
|
|
20
20
|
import com.getcapacitor.community.database.sqlite.SQLite.ImportExportJson.ImportFromJson;
|
|
21
|
+
import com.getcapacitor.community.database.sqlite.SQLite.ImportExportJson.JsonIndex;
|
|
21
22
|
import com.getcapacitor.community.database.sqlite.SQLite.ImportExportJson.JsonSQLite;
|
|
22
23
|
import com.getcapacitor.community.database.sqlite.SQLite.ImportExportJson.UtilsJson;
|
|
23
24
|
import com.getcapacitor.community.database.sqlite.SQLite.UtilsSQLCipher;
|
|
@@ -34,6 +35,7 @@ import net.sqlcipher.Cursor;
|
|
|
34
35
|
import net.sqlcipher.database.SQLiteDatabase;
|
|
35
36
|
import net.sqlcipher.database.SQLiteException;
|
|
36
37
|
import org.json.JSONArray;
|
|
38
|
+
import org.json.JSONException;
|
|
37
39
|
import org.json.JSONObject;
|
|
38
40
|
|
|
39
41
|
public class Database {
|
|
@@ -116,6 +118,7 @@ public class Database {
|
|
|
116
118
|
|
|
117
119
|
/**
|
|
118
120
|
* isOpen Method
|
|
121
|
+
*
|
|
119
122
|
* @return database status
|
|
120
123
|
*/
|
|
121
124
|
public Boolean isOpen() {
|
|
@@ -124,6 +127,7 @@ public class Database {
|
|
|
124
127
|
|
|
125
128
|
/**
|
|
126
129
|
* isNCDB Method
|
|
130
|
+
*
|
|
127
131
|
* @return non-conformed database status
|
|
128
132
|
*/
|
|
129
133
|
public Boolean isNCDB() {
|
|
@@ -132,6 +136,7 @@ public class Database {
|
|
|
132
136
|
|
|
133
137
|
/**
|
|
134
138
|
* GetUrl method
|
|
139
|
+
*
|
|
135
140
|
* @return database url
|
|
136
141
|
*/
|
|
137
142
|
public String getUrl() {
|
|
@@ -141,6 +146,7 @@ public class Database {
|
|
|
141
146
|
|
|
142
147
|
/**
|
|
143
148
|
* Open method
|
|
149
|
+
*
|
|
144
150
|
* @return open status
|
|
145
151
|
*/
|
|
146
152
|
public void open() throws Exception {
|
|
@@ -257,6 +263,7 @@ public class Database {
|
|
|
257
263
|
|
|
258
264
|
/**
|
|
259
265
|
* Close Method
|
|
266
|
+
*
|
|
260
267
|
* @return close status
|
|
261
268
|
*/
|
|
262
269
|
|
|
@@ -293,6 +300,7 @@ public class Database {
|
|
|
293
300
|
|
|
294
301
|
/**
|
|
295
302
|
* IsDBExists Method
|
|
303
|
+
*
|
|
296
304
|
* @return the existence of the database on folder
|
|
297
305
|
*/
|
|
298
306
|
public boolean isDBExists() {
|
|
@@ -306,6 +314,7 @@ public class Database {
|
|
|
306
314
|
/**
|
|
307
315
|
* Execute Method
|
|
308
316
|
* Execute an Array of SQL Statements
|
|
317
|
+
*
|
|
309
318
|
* @param statements Array of Strings
|
|
310
319
|
* @return
|
|
311
320
|
*/
|
|
@@ -319,7 +328,13 @@ public class Database {
|
|
|
319
328
|
if (transaction) _db.beginTransaction();
|
|
320
329
|
for (String cmd : statements) {
|
|
321
330
|
if (!cmd.endsWith(";")) cmd += ";";
|
|
322
|
-
|
|
331
|
+
String nCmd = cmd;
|
|
332
|
+
String trimCmd = nCmd.trim().substring(0, 11).toUpperCase();
|
|
333
|
+
if (trimCmd.equals("DELETE FROM") && nCmd.toLowerCase().contains("WHERE".toLowerCase())) {
|
|
334
|
+
String whereStmt = nCmd.trim();
|
|
335
|
+
nCmd = deleteSQL(this, whereStmt, new ArrayList<Object>());
|
|
336
|
+
}
|
|
337
|
+
_db.execSQL(nCmd);
|
|
323
338
|
}
|
|
324
339
|
changes = _uSqlite.dbChanges(_db) - initChanges;
|
|
325
340
|
if (changes != -1) {
|
|
@@ -340,6 +355,7 @@ public class Database {
|
|
|
340
355
|
/**
|
|
341
356
|
* ExecuteSet Method
|
|
342
357
|
* Execute a Set of SQL Statements
|
|
358
|
+
*
|
|
343
359
|
* @param set JSArray of statements
|
|
344
360
|
* @return
|
|
345
361
|
*/
|
|
@@ -368,11 +384,11 @@ public class Database {
|
|
|
368
384
|
for (int k = 0; k < valsJson.length(); k++) {
|
|
369
385
|
vals.add(valsJson.get(k));
|
|
370
386
|
}
|
|
371
|
-
lastId = prepareSQL(statement, vals);
|
|
387
|
+
lastId = prepareSQL(statement, vals, false);
|
|
372
388
|
if (lastId == -1) break;
|
|
373
389
|
}
|
|
374
390
|
} else {
|
|
375
|
-
lastId = prepareSQL(statement, values);
|
|
391
|
+
lastId = prepareSQL(statement, values, false);
|
|
376
392
|
}
|
|
377
393
|
if (lastId == -1) break;
|
|
378
394
|
}
|
|
@@ -399,6 +415,7 @@ public class Database {
|
|
|
399
415
|
/**
|
|
400
416
|
* InTransaction Method
|
|
401
417
|
* Check if a transaction is still running
|
|
418
|
+
*
|
|
402
419
|
* @return
|
|
403
420
|
*/
|
|
404
421
|
public boolean inTransaction() {
|
|
@@ -407,8 +424,9 @@ public class Database {
|
|
|
407
424
|
|
|
408
425
|
/**
|
|
409
426
|
* RunSQL Method
|
|
427
|
+
*
|
|
410
428
|
* @param statement a raw SQL statement
|
|
411
|
-
* @param values
|
|
429
|
+
* @param values Array of Strings to bind to the statement
|
|
412
430
|
* @return
|
|
413
431
|
*/
|
|
414
432
|
public JSObject runSQL(String statement, ArrayList<Object> values, Boolean... others) throws Exception {
|
|
@@ -420,7 +438,7 @@ public class Database {
|
|
|
420
438
|
if (_db != null && _db.isOpen() && statement.length() > 0) {
|
|
421
439
|
Integer initChanges = _uSqlite.dbChanges(_db);
|
|
422
440
|
if (transaction) _db.beginTransaction();
|
|
423
|
-
lastId = prepareSQL(statement, values);
|
|
441
|
+
lastId = prepareSQL(statement, values, false);
|
|
424
442
|
if (lastId != -1 && transaction) _db.setTransactionSuccessful();
|
|
425
443
|
changes = _uSqlite.dbChanges(_db) - initChanges;
|
|
426
444
|
retObj.put("changes", changes);
|
|
@@ -438,15 +456,20 @@ public class Database {
|
|
|
438
456
|
|
|
439
457
|
/**
|
|
440
458
|
* PrepareSQL Method
|
|
459
|
+
*
|
|
441
460
|
* @param statement
|
|
442
461
|
* @param values
|
|
443
462
|
* @return
|
|
444
463
|
*/
|
|
445
|
-
public long prepareSQL(String statement, ArrayList<Object> values) throws Exception {
|
|
464
|
+
public long prepareSQL(String statement, ArrayList<Object> values, Boolean fromJson) throws Exception {
|
|
446
465
|
String stmtType = statement.replaceAll("\n", "").trim().substring(0, 6).toUpperCase();
|
|
447
466
|
SupportSQLiteStatement stmt = null;
|
|
467
|
+
String sqlStmt = statement;
|
|
448
468
|
try {
|
|
449
|
-
|
|
469
|
+
if (!fromJson && stmtType.equals("DELETE")) {
|
|
470
|
+
sqlStmt = deleteSQL(this, statement, values);
|
|
471
|
+
}
|
|
472
|
+
stmt = _db.compileStatement(sqlStmt);
|
|
450
473
|
if (values != null && values.size() > 0) {
|
|
451
474
|
Object[] valObj = new Object[values.size()];
|
|
452
475
|
for (int i = 0; i < values.size(); i++) {
|
|
@@ -481,9 +504,160 @@ public class Database {
|
|
|
481
504
|
}
|
|
482
505
|
}
|
|
483
506
|
|
|
507
|
+
/**
|
|
508
|
+
* DeleteSQL method
|
|
509
|
+
*
|
|
510
|
+
* @param mDB
|
|
511
|
+
* @param statement
|
|
512
|
+
* @param values
|
|
513
|
+
* @return
|
|
514
|
+
* @throws Exception
|
|
515
|
+
*/
|
|
516
|
+
public String deleteSQL(Database mDB, String statement, ArrayList<Object> values) throws Exception {
|
|
517
|
+
String sqlStmt = statement;
|
|
518
|
+
try {
|
|
519
|
+
Boolean isLast = _uJson.isLastModified(mDB);
|
|
520
|
+
if (isLast) {
|
|
521
|
+
// Replace DELETE by UPDATE and set sql_deleted to 1
|
|
522
|
+
Integer wIdx = statement.toUpperCase().indexOf("WHERE");
|
|
523
|
+
String preStmt = statement.substring(0, wIdx - 1);
|
|
524
|
+
String clauseStmt = statement.substring(wIdx, statement.length());
|
|
525
|
+
String tableName = preStmt.substring(("DELETE FROM").length()).trim();
|
|
526
|
+
sqlStmt = "UPDATE " + tableName + " SET sql_deleted = 1 " + clauseStmt;
|
|
527
|
+
// Find REFERENCES if any and update the sql_deleted column
|
|
528
|
+
findReferencesAndUpdate(mDB, tableName, clauseStmt, values);
|
|
529
|
+
}
|
|
530
|
+
return sqlStmt;
|
|
531
|
+
} catch (Exception e) {
|
|
532
|
+
throw new Exception(e.getMessage());
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* FindReferencesAndUpdate method
|
|
538
|
+
*
|
|
539
|
+
* @param mDB
|
|
540
|
+
* @param tableName
|
|
541
|
+
* @param whereStmt
|
|
542
|
+
* @param values
|
|
543
|
+
* @throws Exception
|
|
544
|
+
*/
|
|
545
|
+
public void findReferencesAndUpdate(Database mDB, String tableName, String whereStmt, ArrayList<Object> values) throws Exception {
|
|
546
|
+
try {
|
|
547
|
+
JSArray references = getReferences(mDB, tableName);
|
|
548
|
+
for (int j = 0; j < references.length(); j++) {
|
|
549
|
+
// get the tableName of the reference
|
|
550
|
+
String refTable = getReferenceTableName(references.getJSONObject(j).getString("sql"));
|
|
551
|
+
if (refTable.length() <= 0) {
|
|
552
|
+
continue;
|
|
553
|
+
}
|
|
554
|
+
// get the columnName
|
|
555
|
+
String colName = getReferenceColumnName(references.getJSONObject(j).getString("sql"));
|
|
556
|
+
if (refTable.length() <= 0) {
|
|
557
|
+
continue;
|
|
558
|
+
}
|
|
559
|
+
// update the where clause
|
|
560
|
+
String uWhereStmt = updateWhere(whereStmt, colName);
|
|
561
|
+
|
|
562
|
+
if (uWhereStmt.length() <= 0) {
|
|
563
|
+
continue;
|
|
564
|
+
}
|
|
565
|
+
//update sql_deleted for this reference
|
|
566
|
+
String stmt = "UPDATE " + refTable + " SET sql_deleted = 1 " + uWhereStmt;
|
|
567
|
+
long lastId = prepareSQL(stmt, values, false);
|
|
568
|
+
if (lastId == -1) {
|
|
569
|
+
String msg = "UPDATE sql_deleted failed for references " + "table: " + refTable + ";";
|
|
570
|
+
throw new Exception(msg);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
return;
|
|
574
|
+
} catch (JSONException e) {
|
|
575
|
+
throw new Exception(e.getMessage());
|
|
576
|
+
} catch (Exception e) {
|
|
577
|
+
throw new Exception(e.getMessage());
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/**
|
|
582
|
+
* GetReferenceTableName method
|
|
583
|
+
*
|
|
584
|
+
* @param refValue
|
|
585
|
+
* @return
|
|
586
|
+
*/
|
|
587
|
+
public String getReferenceTableName(String refValue) {
|
|
588
|
+
String tableName = "";
|
|
589
|
+
if (refValue.length() > 0 && refValue.substring(0, 12).equalsIgnoreCase("CREATE TABLE")) {
|
|
590
|
+
Integer oPar = refValue.indexOf("(");
|
|
591
|
+
tableName = refValue.substring(13, oPar).trim();
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
return tableName;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/**
|
|
598
|
+
* GetReferenceColumnName method
|
|
599
|
+
*
|
|
600
|
+
* @param refValue
|
|
601
|
+
* @return
|
|
602
|
+
*/
|
|
603
|
+
public String getReferenceColumnName(String refValue) {
|
|
604
|
+
String colName = "";
|
|
605
|
+
if (refValue.length() > 0) {
|
|
606
|
+
Integer index = refValue.toLowerCase().indexOf("FOREIGN KEY".toLowerCase());
|
|
607
|
+
String stmt = refValue.substring(index + 12);
|
|
608
|
+
Integer oPar = stmt.indexOf("(");
|
|
609
|
+
Integer cPar = stmt.indexOf(")");
|
|
610
|
+
colName = stmt.substring(oPar + 1, cPar).trim();
|
|
611
|
+
}
|
|
612
|
+
return colName;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/**
|
|
616
|
+
* UpdateWhere method
|
|
617
|
+
*
|
|
618
|
+
* @param whStmt
|
|
619
|
+
* @param colName
|
|
620
|
+
* @return
|
|
621
|
+
*/
|
|
622
|
+
public String updateWhere(String whStmt, String colName) {
|
|
623
|
+
String whereStmt = "";
|
|
624
|
+
if (whStmt.length() > 0) {
|
|
625
|
+
Integer index = whStmt.toLowerCase().indexOf("WHERE".toLowerCase());
|
|
626
|
+
String stmt = whStmt.substring(index + 6);
|
|
627
|
+
Integer fEqual = stmt.indexOf("=");
|
|
628
|
+
String whereColName = stmt.substring(0, fEqual).trim();
|
|
629
|
+
whereStmt = whStmt.replaceFirst(whereColName, colName);
|
|
630
|
+
}
|
|
631
|
+
return whereStmt;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* GetReferences method
|
|
636
|
+
*
|
|
637
|
+
* @param mDB
|
|
638
|
+
* @param tableName
|
|
639
|
+
* @return
|
|
640
|
+
* @throws Exception
|
|
641
|
+
*/
|
|
642
|
+
public JSArray getReferences(Database mDB, String tableName) throws Exception {
|
|
643
|
+
String sqlStmt =
|
|
644
|
+
"SELECT sql FROM sqlite_master " +
|
|
645
|
+
"WHERE sql LIKE('%REFERENCES%') AND " +
|
|
646
|
+
"sql LIKE('%" +
|
|
647
|
+
tableName +
|
|
648
|
+
"%') AND sql LIKE('%ON DELETE%');";
|
|
649
|
+
try {
|
|
650
|
+
JSArray references = mDB.selectSQL(sqlStmt, new ArrayList<Object>());
|
|
651
|
+
return references;
|
|
652
|
+
} catch (Exception e) {
|
|
653
|
+
throw new Exception(e.getMessage());
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
484
657
|
/**
|
|
485
658
|
* SelectSQL Method
|
|
486
659
|
* Query a raw sql statement with or without binding values
|
|
660
|
+
*
|
|
487
661
|
* @param statement
|
|
488
662
|
* @param values
|
|
489
663
|
* @return
|
|
@@ -534,6 +708,7 @@ public class Database {
|
|
|
534
708
|
/**
|
|
535
709
|
* GetTableNames Method
|
|
536
710
|
* Returned a JSArray of table's name
|
|
711
|
+
*
|
|
537
712
|
* @return
|
|
538
713
|
* @throws Exception
|
|
539
714
|
*/
|
|
@@ -553,6 +728,7 @@ public class Database {
|
|
|
553
728
|
/**
|
|
554
729
|
* DeleteDB Method
|
|
555
730
|
* Delete the database file
|
|
731
|
+
*
|
|
556
732
|
* @param dbName
|
|
557
733
|
* @return
|
|
558
734
|
*/
|
|
@@ -584,6 +760,7 @@ public class Database {
|
|
|
584
760
|
/**
|
|
585
761
|
* CreateSyncTable Method
|
|
586
762
|
* create the synchronization table
|
|
763
|
+
*
|
|
587
764
|
* @return
|
|
588
765
|
*/
|
|
589
766
|
public JSObject createSyncTable() throws Exception {
|
|
@@ -618,6 +795,7 @@ public class Database {
|
|
|
618
795
|
/**
|
|
619
796
|
* SetSyncDate Method
|
|
620
797
|
* Set the synchronization date
|
|
798
|
+
*
|
|
621
799
|
* @param syncDate
|
|
622
800
|
* @return
|
|
623
801
|
*/
|
|
@@ -643,6 +821,13 @@ public class Database {
|
|
|
643
821
|
}
|
|
644
822
|
}
|
|
645
823
|
|
|
824
|
+
/**
|
|
825
|
+
* GetSyncDate method
|
|
826
|
+
* get the synchronization date
|
|
827
|
+
*
|
|
828
|
+
* @return
|
|
829
|
+
* @throws Exception
|
|
830
|
+
*/
|
|
646
831
|
public Long getSyncDate() throws Exception {
|
|
647
832
|
long syncDate = 0;
|
|
648
833
|
try {
|
|
@@ -659,6 +844,7 @@ public class Database {
|
|
|
659
844
|
|
|
660
845
|
/**
|
|
661
846
|
* Import from Json object
|
|
847
|
+
*
|
|
662
848
|
* @param jsonSQL
|
|
663
849
|
* @return
|
|
664
850
|
*/
|
|
@@ -666,6 +852,9 @@ public class Database {
|
|
|
666
852
|
JSObject retObj = new JSObject();
|
|
667
853
|
int changes = Integer.valueOf(0);
|
|
668
854
|
try {
|
|
855
|
+
// set Foreign Keys OFF
|
|
856
|
+
_db.setForeignKeyConstraintsEnabled(false);
|
|
857
|
+
|
|
669
858
|
if (jsonSQL.getTables().size() > 0) {
|
|
670
859
|
// create the database schema
|
|
671
860
|
changes = fromJson.createDatabaseSchema(this, jsonSQL);
|
|
@@ -676,6 +865,9 @@ public class Database {
|
|
|
676
865
|
if (jsonSQL.getViews().size() > 0) {
|
|
677
866
|
changes += fromJson.createViews(this, jsonSQL.getViews());
|
|
678
867
|
}
|
|
868
|
+
// set Foreign Keys ON
|
|
869
|
+
_db.setForeignKeyConstraintsEnabled(true);
|
|
870
|
+
|
|
679
871
|
retObj.put("changes", changes);
|
|
680
872
|
return retObj;
|
|
681
873
|
} catch (Exception e) {
|
|
@@ -685,6 +877,7 @@ public class Database {
|
|
|
685
877
|
|
|
686
878
|
/**
|
|
687
879
|
* Export to JSON Object
|
|
880
|
+
*
|
|
688
881
|
* @param mode
|
|
689
882
|
* @return
|
|
690
883
|
*/
|
|
@@ -696,6 +889,11 @@ public class Database {
|
|
|
696
889
|
inJson.setEncrypted(_encrypted);
|
|
697
890
|
inJson.setMode(mode);
|
|
698
891
|
try {
|
|
892
|
+
// set the last export date
|
|
893
|
+
Date date = new Date();
|
|
894
|
+
long syncTime = date.getTime() / 1000L;
|
|
895
|
+
toJson.setLastExportDate(this, syncTime);
|
|
896
|
+
// launch the export process
|
|
699
897
|
JsonSQLite retJson = toJson.createExportObject(this, inJson);
|
|
700
898
|
// retJson.print();
|
|
701
899
|
ArrayList<String> keys = retJson.getKeys();
|
|
@@ -717,4 +915,19 @@ public class Database {
|
|
|
717
915
|
throw new Exception(e.getMessage());
|
|
718
916
|
} finally {}
|
|
719
917
|
}
|
|
918
|
+
|
|
919
|
+
/**
|
|
920
|
+
* Delete exported rows
|
|
921
|
+
*
|
|
922
|
+
* @throws Exception
|
|
923
|
+
*/
|
|
924
|
+
public void deleteExportedRows() throws Exception {
|
|
925
|
+
try {
|
|
926
|
+
toJson.delExportedRows(this);
|
|
927
|
+
return;
|
|
928
|
+
} catch (Exception e) {
|
|
929
|
+
Log.e(TAG, "Error: exportToJson " + e.getMessage());
|
|
930
|
+
throw new Exception(e.getMessage());
|
|
931
|
+
}
|
|
932
|
+
}
|
|
720
933
|
}
|
|
@@ -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
|
|
@@ -467,7 +560,7 @@ public class ExportToJson {
|
|
|
467
560
|
if (modTables.getString(tableName).equals("Create")) {
|
|
468
561
|
query = "SELECT * FROM " + tableName + ";";
|
|
469
562
|
} else {
|
|
470
|
-
query = "SELECT * FROM " + tableName + " WHERE last_modified
|
|
563
|
+
query = "SELECT * FROM " + tableName + " WHERE last_modified >= " + syncDate + ";";
|
|
471
564
|
}
|
|
472
565
|
ArrayList<ArrayList<Object>> values = uJson.getValues(mDb, query, tableName);
|
|
473
566
|
|
|
@@ -544,9 +637,13 @@ public class ExportToJson {
|
|
|
544
637
|
*/
|
|
545
638
|
public Long getSyncDate(Database mDb) throws Exception {
|
|
546
639
|
long ret = -1;
|
|
547
|
-
String stmt = "SELECT sync_date FROM sync_table;";
|
|
640
|
+
String stmt = "SELECT sync_date FROM sync_table WHERE id = 1;";
|
|
548
641
|
JSArray retQuery = new JSArray();
|
|
549
642
|
try {
|
|
643
|
+
boolean isSyncTable = uJson.isTableExists(mDb, "sync_table");
|
|
644
|
+
if (!isSyncTable) {
|
|
645
|
+
throw new Exception("No sync_table available");
|
|
646
|
+
}
|
|
550
647
|
retQuery = mDb.selectSQL(stmt, new ArrayList<Object>());
|
|
551
648
|
List<JSObject> lQuery = retQuery.toList();
|
|
552
649
|
if (lQuery.size() == 1) {
|
|
@@ -586,7 +683,7 @@ public class ExportToJson {
|
|
|
586
683
|
if (lQuery.size() != 1) break;
|
|
587
684
|
long totalCount = lQuery.get(0).getLong("count");
|
|
588
685
|
// get total count of modified since last sync
|
|
589
|
-
stmt = "SELECT count(*) AS count FROM " + tableName + " WHERE last_modified
|
|
686
|
+
stmt = "SELECT count(*) AS count FROM " + tableName + " WHERE last_modified >= " + syncDate + ";";
|
|
590
687
|
retQuery = mDb.selectSQL(stmt, new ArrayList<Object>());
|
|
591
688
|
lQuery = retQuery.toList();
|
|
592
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);
|
|
@@ -406,7 +388,10 @@ public class ImportFromJson {
|
|
|
406
388
|
isRun = checkUpdate(mDb, stmt, row, tableName, tColNames, tColTypes);
|
|
407
389
|
if (isRun) {
|
|
408
390
|
// load the values
|
|
409
|
-
|
|
391
|
+
if (stmt.substring(0, 6).toUpperCase().equals("DELETE")) {
|
|
392
|
+
row = new ArrayList<>();
|
|
393
|
+
}
|
|
394
|
+
long lastId = mDb.prepareSQL(stmt, row, true);
|
|
410
395
|
if (lastId < 0) {
|
|
411
396
|
throw new Exception("CreateTableData: lastId < 0");
|
|
412
397
|
}
|
|
@@ -549,23 +534,41 @@ public class ImportFromJson {
|
|
|
549
534
|
.append(");")
|
|
550
535
|
.toString();
|
|
551
536
|
} else {
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
if (
|
|
555
|
-
|
|
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
|
+
}
|
|
556
552
|
}
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
.
|
|
560
|
-
.
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
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(" = ");
|
|
565
567
|
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
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
|
+
}
|
|
569
572
|
}
|
|
570
573
|
return stmt;
|
|
571
574
|
}
|