@capacitor-community/sqlite 3.5.0 → 3.5.1-1
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 +2 -1
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java +4 -2
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ImportFromJson.java +8 -1
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/UtilsJson.java +33 -0
- package/electron/dist/plugin.js +35 -3
- package/electron/dist/plugin.js.map +1 -1
- package/ios/Plugin/Database.swift +4 -1
- package/ios/Plugin/ImportExportJson/ImportFromJson.swift +9 -1
- package/ios/Plugin/Utils/UtilsJson.swift +28 -0
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -51,7 +51,7 @@ The main change is related to the delete table's rows when a synchronization tab
|
|
|
51
51
|
|
|
52
52
|
- In the JSON object outputs by the `exportToJson`, all the deleted rows in your local database have got the `sql_deleted` column set to 1 to help in your synchronization management process with the remote server database. A system `last_exported_date` is automatically saved in the synchronization table at the start of the export process flow.
|
|
53
53
|
|
|
54
|
-
- On
|
|
54
|
+
- On successful completion of your synchronization management process with the remote server database, you must
|
|
55
55
|
- Set a new synchronization date (as `(new Date()).toISOString()`) with the `setSyncDate` method.
|
|
56
56
|
- Execute the `deleteExportedRows` method which physically deletes all table's rows having 1 as value for the `sql_deleted` column prior to the `last_exported_date` in your local database.
|
|
57
57
|
|
|
@@ -465,6 +465,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
|
|
|
465
465
|
<a href="https://github.com/tobiasmuecksch"><img src="https://github.com/tobiasmuecksch.png?size=100" width="50" height="50" /></a>
|
|
466
466
|
<a href="https://github.com/dragermrb"><img src="https://github.com/dragermrb.png?size=100" width="50" height="50" /></a>
|
|
467
467
|
<a href="https://github.com/iamcco"><img src="https://github.com/iamcco.png?size=100" width="50" height="50" /></a>
|
|
468
|
+
<a href="https://github.com/eltociear"><img src="https://github.com/eltociear.png?size=100" width="50" height="50" /></a>
|
|
468
469
|
</p>
|
|
469
470
|
|
|
470
471
|
<!-- markdownlint-enable -->
|
package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java
CHANGED
|
@@ -517,7 +517,8 @@ public class Database {
|
|
|
517
517
|
String sqlStmt = statement;
|
|
518
518
|
try {
|
|
519
519
|
Boolean isLast = _uJson.isLastModified(mDB);
|
|
520
|
-
|
|
520
|
+
Boolean isDel = _uJson.isSqlDeleted(mDB);
|
|
521
|
+
if (isLast && isDel) {
|
|
521
522
|
// Replace DELETE by UPDATE and set sql_deleted to 1
|
|
522
523
|
Integer wIdx = statement.toUpperCase().indexOf("WHERE");
|
|
523
524
|
String preStmt = statement.substring(0, wIdx - 1);
|
|
@@ -770,7 +771,8 @@ public class Database {
|
|
|
770
771
|
boolean isExists = _uJson.isTableExists(this, "sync_table");
|
|
771
772
|
if (!isExists) {
|
|
772
773
|
boolean isLastModified = _uJson.isLastModified(this);
|
|
773
|
-
|
|
774
|
+
boolean isSqlDeleted = _uJson.isSqlDeleted(this);
|
|
775
|
+
if (isLastModified && isSqlDeleted) {
|
|
774
776
|
Date date = new Date();
|
|
775
777
|
long syncTime = date.getTime() / 1000L;
|
|
776
778
|
String[] statements = {
|
|
@@ -159,6 +159,7 @@ public class ImportFromJson {
|
|
|
159
159
|
ArrayList<String> statements = new ArrayList<>();
|
|
160
160
|
String stmt = new StringBuilder("CREATE TABLE IF NOT EXISTS ").append(tableName).append(" (").toString();
|
|
161
161
|
Boolean isLastModified = false;
|
|
162
|
+
Boolean isSqlDeleted = false;
|
|
162
163
|
for (int j = 0; j < mSchema.size(); j++) {
|
|
163
164
|
if (j == mSchema.size() - 1) {
|
|
164
165
|
if (mSchema.get(j).getColumn() != null) {
|
|
@@ -167,6 +168,9 @@ public class ImportFromJson {
|
|
|
167
168
|
if (mSchema.get(j).getColumn().equals("last_modified")) {
|
|
168
169
|
isLastModified = true;
|
|
169
170
|
}
|
|
171
|
+
if (mSchema.get(j).getColumn().equals("sql_deleted")) {
|
|
172
|
+
isSqlDeleted = true;
|
|
173
|
+
}
|
|
170
174
|
} else if (mSchema.get(j).getForeignkey() != null) {
|
|
171
175
|
stmt =
|
|
172
176
|
new StringBuilder(stmt)
|
|
@@ -196,6 +200,9 @@ public class ImportFromJson {
|
|
|
196
200
|
if (mSchema.get(j).getColumn().equals("last_modified")) {
|
|
197
201
|
isLastModified = true;
|
|
198
202
|
}
|
|
203
|
+
if (mSchema.get(j).getColumn().equals("sql_deleted")) {
|
|
204
|
+
isSqlDeleted = true;
|
|
205
|
+
}
|
|
199
206
|
} else if (mSchema.get(j).getForeignkey() != null) {
|
|
200
207
|
stmt =
|
|
201
208
|
new StringBuilder(stmt)
|
|
@@ -219,7 +226,7 @@ public class ImportFromJson {
|
|
|
219
226
|
}
|
|
220
227
|
stmt = new StringBuilder(stmt).append(");").toString();
|
|
221
228
|
statements.add(stmt);
|
|
222
|
-
if (isLastModified) {
|
|
229
|
+
if (isLastModified && isSqlDeleted) {
|
|
223
230
|
// create trigger last_modified associated with the table
|
|
224
231
|
String stmtTrigger = new StringBuilder("CREATE TRIGGER IF NOT EXISTS ")
|
|
225
232
|
.append(tableName)
|
|
@@ -19,6 +19,12 @@ public class UtilsJson {
|
|
|
19
19
|
private JsonView uJView = new JsonView();
|
|
20
20
|
private UtilsDrop _uDrop = new UtilsDrop();
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Check existence of last_modified column
|
|
24
|
+
* @param db
|
|
25
|
+
* @return
|
|
26
|
+
* @throws Exception
|
|
27
|
+
*/
|
|
22
28
|
public boolean isLastModified(Database db) throws Exception {
|
|
23
29
|
if (!db.isOpen()) {
|
|
24
30
|
throw new Exception("isLastModified: Database not opened");
|
|
@@ -40,6 +46,33 @@ public class UtilsJson {
|
|
|
40
46
|
}
|
|
41
47
|
}
|
|
42
48
|
|
|
49
|
+
/**
|
|
50
|
+
* Check existence of sql_deleted column
|
|
51
|
+
* @param db
|
|
52
|
+
* @return
|
|
53
|
+
* @throws Exception
|
|
54
|
+
*/
|
|
55
|
+
public boolean isSqlDeleted(Database db) throws Exception {
|
|
56
|
+
if (!db.isOpen()) {
|
|
57
|
+
throw new Exception("isSqlDeleted: Database not opened");
|
|
58
|
+
}
|
|
59
|
+
boolean ret = false;
|
|
60
|
+
try {
|
|
61
|
+
List<String> tables = _uDrop.getTablesNames(db);
|
|
62
|
+
for (String tableName : tables) {
|
|
63
|
+
JSObject namesTypes = getTableColumnNamesTypes(db, tableName);
|
|
64
|
+
ArrayList<String> colNames = (ArrayList<String>) namesTypes.get("names");
|
|
65
|
+
if (colNames.contains("sql_deleted")) {
|
|
66
|
+
ret = true;
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return ret;
|
|
71
|
+
} catch (Exception e) {
|
|
72
|
+
throw new Exception("isSqlDeleted: " + e.getMessage());
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
43
76
|
/**
|
|
44
77
|
* Check if a table exists
|
|
45
78
|
*
|
package/electron/dist/plugin.js
CHANGED
|
@@ -470,7 +470,8 @@ class UtilsSQLite {
|
|
|
470
470
|
let sqlStmt = statement;
|
|
471
471
|
try {
|
|
472
472
|
const isLast = await this.isLastModified(db, true);
|
|
473
|
-
|
|
473
|
+
const isDel = await this.isSqlDeleted(db, true);
|
|
474
|
+
if (isLast && isDel) {
|
|
474
475
|
// Replace DELETE by UPDATE and set sql_deleted to 1
|
|
475
476
|
const wIdx = statement.toUpperCase().indexOf('WHERE');
|
|
476
477
|
const preStmt = statement.substring(0, wIdx - 1);
|
|
@@ -671,6 +672,29 @@ class UtilsSQLite {
|
|
|
671
672
|
return Promise.reject(`isLastModified: ${err}`);
|
|
672
673
|
}
|
|
673
674
|
}
|
|
675
|
+
/**
|
|
676
|
+
* isSqlDeleted
|
|
677
|
+
* @param db
|
|
678
|
+
* @param isOpen
|
|
679
|
+
*/
|
|
680
|
+
async isSqlDeleted(db, isOpen) {
|
|
681
|
+
if (!isOpen) {
|
|
682
|
+
return Promise.reject('isSqlDeleted: database not opened');
|
|
683
|
+
}
|
|
684
|
+
try {
|
|
685
|
+
const tableList = await this.getTablesNames(db);
|
|
686
|
+
for (const table of tableList) {
|
|
687
|
+
const tableNamesTypes = await this.getTableColumnNamesTypes(db, table);
|
|
688
|
+
const tableColumnNames = tableNamesTypes.names;
|
|
689
|
+
if (tableColumnNames.includes('sql_deleted')) {
|
|
690
|
+
return Promise.resolve(true);
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
catch (err) {
|
|
695
|
+
return Promise.reject(`isSqlDeleted: ${err}`);
|
|
696
|
+
}
|
|
697
|
+
}
|
|
674
698
|
/**
|
|
675
699
|
* GetTableColumnNamesTypes
|
|
676
700
|
* @param mDB
|
|
@@ -820,6 +844,7 @@ class UtilsJson {
|
|
|
820
844
|
async createSchemaStatement(jsonData) {
|
|
821
845
|
const statements = [];
|
|
822
846
|
let isLastModified = false;
|
|
847
|
+
let isSqlDeleted = false;
|
|
823
848
|
// Prepare the statement to execute
|
|
824
849
|
try {
|
|
825
850
|
for (const jTable of jsonData.tables) {
|
|
@@ -833,6 +858,9 @@ class UtilsJson {
|
|
|
833
858
|
if (jTable.schema[j].column === 'last_modified') {
|
|
834
859
|
isLastModified = true;
|
|
835
860
|
}
|
|
861
|
+
if (jTable.schema[j].column === 'sql_deleted') {
|
|
862
|
+
isSqlDeleted = true;
|
|
863
|
+
}
|
|
836
864
|
}
|
|
837
865
|
else if (jTable.schema[j].foreignkey) {
|
|
838
866
|
statements.push(`FOREIGN KEY (${jTable.schema[j].foreignkey}) ${jTable.schema[j].value}`);
|
|
@@ -847,6 +875,9 @@ class UtilsJson {
|
|
|
847
875
|
if (jTable.schema[j].column === 'last_modified') {
|
|
848
876
|
isLastModified = true;
|
|
849
877
|
}
|
|
878
|
+
if (jTable.schema[j].column === 'sql_deleted') {
|
|
879
|
+
isSqlDeleted = true;
|
|
880
|
+
}
|
|
850
881
|
}
|
|
851
882
|
else if (jTable.schema[j].foreignkey) {
|
|
852
883
|
statements.push(`FOREIGN KEY (${jTable.schema[j].foreignkey}) ${jTable.schema[j].value},`);
|
|
@@ -857,7 +888,7 @@ class UtilsJson {
|
|
|
857
888
|
}
|
|
858
889
|
}
|
|
859
890
|
statements.push(');');
|
|
860
|
-
if (isLastModified) {
|
|
891
|
+
if (isLastModified && isSqlDeleted) {
|
|
861
892
|
// create trigger last_modified associated with the table
|
|
862
893
|
let trig = 'CREATE TRIGGER IF NOT EXISTS ';
|
|
863
894
|
trig += `${jTable.name}`;
|
|
@@ -3443,7 +3474,8 @@ class Database {
|
|
|
3443
3474
|
const retB = await this.jsonUtil.isTableExists(this.database, isOpen, 'sync_table');
|
|
3444
3475
|
if (!retB) {
|
|
3445
3476
|
const isLastModified = await this.sqliteUtil.isLastModified(this.database, isOpen);
|
|
3446
|
-
|
|
3477
|
+
const isSqlDeleted = await this.sqliteUtil.isSqlDeleted(this.database, isOpen);
|
|
3478
|
+
if (isLastModified && isSqlDeleted) {
|
|
3447
3479
|
const date = Math.round(new Date().getTime() / 1000);
|
|
3448
3480
|
let stmts = `
|
|
3449
3481
|
CREATE TABLE IF NOT EXISTS sync_table (
|