@capacitor-community/sqlite 3.4.3 → 3.5.1-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.
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 successfull completion of your synchronization management process with the remote server database, you must
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 -->
@@ -517,7 +517,8 @@ public class Database {
517
517
  String sqlStmt = statement;
518
518
  try {
519
519
  Boolean isLast = _uJson.isLastModified(mDB);
520
- if (isLast) {
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
- if (isLastModified) {
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
  *
@@ -429,12 +429,7 @@ class UtilsSQLite {
429
429
  if (values != null && values.length > 0) {
430
430
  mVal = await this.replaceUndefinedByNull(values);
431
431
  }
432
- if (mVal.length > 0) {
433
- await db.run(sqlStmt, mVal);
434
- }
435
- else {
436
- await db.exec(sqlStmt);
437
- }
432
+ await this.runExec(db, sqlStmt, mVal);
438
433
  lastId = await this.getLastId(db);
439
434
  return Promise.resolve(lastId);
440
435
  }
@@ -442,6 +437,30 @@ class UtilsSQLite {
442
437
  return Promise.reject(`PrepareRun: ${err}`);
443
438
  }
444
439
  }
440
+ async runExec(db, stmt, values = []) {
441
+ return new Promise((resolve, reject) => {
442
+ if (values != null && values.length > 0) {
443
+ db.run(stmt, values, (err) => {
444
+ if (err) {
445
+ reject(err.message);
446
+ }
447
+ else {
448
+ resolve();
449
+ }
450
+ });
451
+ }
452
+ else {
453
+ db.exec(stmt, (err) => {
454
+ if (err) {
455
+ reject(err.message);
456
+ }
457
+ else {
458
+ resolve();
459
+ }
460
+ });
461
+ }
462
+ });
463
+ }
445
464
  /**
446
465
  * replaceUndefinedByNull
447
466
  * @param values
@@ -470,7 +489,8 @@ class UtilsSQLite {
470
489
  let sqlStmt = statement;
471
490
  try {
472
491
  const isLast = await this.isLastModified(db, true);
473
- if (isLast) {
492
+ const isDel = await this.isSqlDeleted(db, true);
493
+ if (isLast && isDel) {
474
494
  // Replace DELETE by UPDATE and set sql_deleted to 1
475
495
  const wIdx = statement.toUpperCase().indexOf('WHERE');
476
496
  const preStmt = statement.substring(0, wIdx - 1);
@@ -671,6 +691,29 @@ class UtilsSQLite {
671
691
  return Promise.reject(`isLastModified: ${err}`);
672
692
  }
673
693
  }
694
+ /**
695
+ * isSqlDeleted
696
+ * @param db
697
+ * @param isOpen
698
+ */
699
+ async isSqlDeleted(db, isOpen) {
700
+ if (!isOpen) {
701
+ return Promise.reject('isSqlDeleted: database not opened');
702
+ }
703
+ try {
704
+ const tableList = await this.getTablesNames(db);
705
+ for (const table of tableList) {
706
+ const tableNamesTypes = await this.getTableColumnNamesTypes(db, table);
707
+ const tableColumnNames = tableNamesTypes.names;
708
+ if (tableColumnNames.includes('sql_deleted')) {
709
+ return Promise.resolve(true);
710
+ }
711
+ }
712
+ }
713
+ catch (err) {
714
+ return Promise.reject(`isSqlDeleted: ${err}`);
715
+ }
716
+ }
674
717
  /**
675
718
  * GetTableColumnNamesTypes
676
719
  * @param mDB
@@ -820,6 +863,7 @@ class UtilsJson {
820
863
  async createSchemaStatement(jsonData) {
821
864
  const statements = [];
822
865
  let isLastModified = false;
866
+ let isSqlDeleted = false;
823
867
  // Prepare the statement to execute
824
868
  try {
825
869
  for (const jTable of jsonData.tables) {
@@ -833,6 +877,9 @@ class UtilsJson {
833
877
  if (jTable.schema[j].column === 'last_modified') {
834
878
  isLastModified = true;
835
879
  }
880
+ if (jTable.schema[j].column === 'sql_deleted') {
881
+ isSqlDeleted = true;
882
+ }
836
883
  }
837
884
  else if (jTable.schema[j].foreignkey) {
838
885
  statements.push(`FOREIGN KEY (${jTable.schema[j].foreignkey}) ${jTable.schema[j].value}`);
@@ -847,6 +894,9 @@ class UtilsJson {
847
894
  if (jTable.schema[j].column === 'last_modified') {
848
895
  isLastModified = true;
849
896
  }
897
+ if (jTable.schema[j].column === 'sql_deleted') {
898
+ isSqlDeleted = true;
899
+ }
850
900
  }
851
901
  else if (jTable.schema[j].foreignkey) {
852
902
  statements.push(`FOREIGN KEY (${jTable.schema[j].foreignkey}) ${jTable.schema[j].value},`);
@@ -857,7 +907,7 @@ class UtilsJson {
857
907
  }
858
908
  }
859
909
  statements.push(');');
860
- if (isLastModified) {
910
+ if (isLastModified && isSqlDeleted) {
861
911
  // create trigger last_modified associated with the table
862
912
  let trig = 'CREATE TRIGGER IF NOT EXISTS ';
863
913
  trig += `${jTable.name}`;
@@ -3443,7 +3493,8 @@ class Database {
3443
3493
  const retB = await this.jsonUtil.isTableExists(this.database, isOpen, 'sync_table');
3444
3494
  if (!retB) {
3445
3495
  const isLastModified = await this.sqliteUtil.isLastModified(this.database, isOpen);
3446
- if (isLastModified) {
3496
+ const isSqlDeleted = await this.sqliteUtil.isSqlDeleted(this.database, isOpen);
3497
+ if (isLastModified && isSqlDeleted) {
3447
3498
  const date = Math.round(new Date().getTime() / 1000);
3448
3499
  let stmts = `
3449
3500
  CREATE TABLE IF NOT EXISTS sync_table (