@capacitor-community/sqlite 3.4.1-1 → 3.4.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/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java +24 -11
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ExportToJson.java +5 -0
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ImportFromJson.java +24 -15
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/UtilsJson.java +23 -0
- package/electron/dist/plugin.js +228 -176
- package/electron/dist/plugin.js.map +1 -1
- package/ios/Plugin/Database.swift +32 -9
- package/ios/Plugin/ImportExportJson/ExportToJson.swift +8 -0
- package/ios/Plugin/ImportExportJson/ImportFromJson.swift +20 -14
- package/ios/Plugin/Utils/UtilsJson.swift +28 -0
- package/package.json +4 -1
package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java
CHANGED
|
@@ -569,17 +569,22 @@ public class Database {
|
|
|
569
569
|
// check if the table has already been created
|
|
570
570
|
boolean isExists = _uJson.isTableExists(this, "sync_table");
|
|
571
571
|
if (!isExists) {
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
572
|
+
boolean isLastModified = _uJson.isLastModified(this);
|
|
573
|
+
if (isLastModified) {
|
|
574
|
+
Date date = new Date();
|
|
575
|
+
long syncTime = date.getTime() / 1000L;
|
|
576
|
+
String[] statements = {
|
|
577
|
+
"CREATE TABLE IF NOT EXISTS sync_table (" + "id INTEGER PRIMARY KEY NOT NULL," + "sync_date INTEGER);",
|
|
578
|
+
"INSERT INTO sync_table (sync_date) VALUES ('" + syncTime + "');"
|
|
579
|
+
};
|
|
580
|
+
try {
|
|
581
|
+
retObj = execute(statements);
|
|
582
|
+
return retObj;
|
|
583
|
+
} catch (Exception e) {
|
|
584
|
+
throw new Exception(e.getMessage());
|
|
585
|
+
}
|
|
586
|
+
} else {
|
|
587
|
+
throw new Exception("No last_modified column in tables");
|
|
583
588
|
}
|
|
584
589
|
} else {
|
|
585
590
|
retObj.put("changes", Integer.valueOf(0));
|
|
@@ -596,6 +601,10 @@ public class Database {
|
|
|
596
601
|
public void setSyncDate(String syncDate) throws Exception {
|
|
597
602
|
JSObject retObj = new JSObject();
|
|
598
603
|
try {
|
|
604
|
+
boolean isSyncTable = _uJson.isTableExists(this, "sync_table");
|
|
605
|
+
if (!isSyncTable) {
|
|
606
|
+
throw new Exception("No sync_table available");
|
|
607
|
+
}
|
|
599
608
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
|
|
600
609
|
Date date = formatter.parse(syncDate.replaceAll("Z$", "+0000"));
|
|
601
610
|
long syncTime = date.getTime() / 1000L;
|
|
@@ -614,6 +623,10 @@ public class Database {
|
|
|
614
623
|
public Long getSyncDate() throws Exception {
|
|
615
624
|
long syncDate = 0;
|
|
616
625
|
try {
|
|
626
|
+
boolean isSyncTable = _uJson.isTableExists(this, "sync_table");
|
|
627
|
+
if (!isSyncTable) {
|
|
628
|
+
throw new Exception("No sync_table available");
|
|
629
|
+
}
|
|
617
630
|
syncDate = toJson.getSyncDate(this);
|
|
618
631
|
return syncDate;
|
|
619
632
|
} catch (Exception e) {
|
|
@@ -68,6 +68,11 @@ public class ExportToJson {
|
|
|
68
68
|
if (resTables.length() == 0) {
|
|
69
69
|
throw new Exception("CreateExportObject: table's names failed");
|
|
70
70
|
} else {
|
|
71
|
+
boolean isSyncTable = uJson.isTableExists(db, "sync_table");
|
|
72
|
+
if (!isSyncTable && sqlObj.getMode().equals("partial")) {
|
|
73
|
+
throw new Exception("No sync_table available");
|
|
74
|
+
}
|
|
75
|
+
|
|
71
76
|
switch (sqlObj.getMode()) {
|
|
72
77
|
case "partial":
|
|
73
78
|
tables = getTablesPartial(db, resTables);
|
|
@@ -167,11 +167,15 @@ public class ImportFromJson {
|
|
|
167
167
|
private ArrayList<String> createTableSchema(ArrayList<JsonColumn> mSchema, String tableName) {
|
|
168
168
|
ArrayList<String> statements = new ArrayList<>();
|
|
169
169
|
String stmt = new StringBuilder("CREATE TABLE IF NOT EXISTS ").append(tableName).append(" (").toString();
|
|
170
|
+
Boolean isLastModified = false;
|
|
170
171
|
for (int j = 0; j < mSchema.size(); j++) {
|
|
171
172
|
if (j == mSchema.size() - 1) {
|
|
172
173
|
if (mSchema.get(j).getColumn() != null) {
|
|
173
174
|
stmt =
|
|
174
175
|
new StringBuilder(stmt).append(mSchema.get(j).getColumn()).append(" ").append(mSchema.get(j).getValue()).toString();
|
|
176
|
+
if (mSchema.get(j).getColumn().equals("last_modified")) {
|
|
177
|
+
isLastModified = true;
|
|
178
|
+
}
|
|
175
179
|
} else if (mSchema.get(j).getForeignkey() != null) {
|
|
176
180
|
stmt =
|
|
177
181
|
new StringBuilder(stmt)
|
|
@@ -198,6 +202,9 @@ public class ImportFromJson {
|
|
|
198
202
|
.append(mSchema.get(j).getValue())
|
|
199
203
|
.append(",")
|
|
200
204
|
.toString();
|
|
205
|
+
if (mSchema.get(j).getColumn().equals("last_modified")) {
|
|
206
|
+
isLastModified = true;
|
|
207
|
+
}
|
|
201
208
|
} else if (mSchema.get(j).getForeignkey() != null) {
|
|
202
209
|
stmt =
|
|
203
210
|
new StringBuilder(stmt)
|
|
@@ -221,21 +228,23 @@ public class ImportFromJson {
|
|
|
221
228
|
}
|
|
222
229
|
stmt = new StringBuilder(stmt).append(");").toString();
|
|
223
230
|
statements.add(stmt);
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
231
|
+
if (isLastModified) {
|
|
232
|
+
// create trigger last_modified associated with the table
|
|
233
|
+
String stmtTrigger = new StringBuilder("CREATE TRIGGER IF NOT EXISTS ")
|
|
234
|
+
.append(tableName)
|
|
235
|
+
.append("_trigger_last_modified")
|
|
236
|
+
.append(" AFTER UPDATE ON ")
|
|
237
|
+
.append(tableName)
|
|
238
|
+
.append(" FOR EACH ROW ")
|
|
239
|
+
.append("WHEN NEW.last_modified <= " + "OLD.last_modified BEGIN ")
|
|
240
|
+
.append("UPDATE ")
|
|
241
|
+
.append(tableName)
|
|
242
|
+
.append(" SET last_modified = (strftime('%s','now')) ")
|
|
243
|
+
.append("WHERE id=OLD.id; ")
|
|
244
|
+
.append("END;")
|
|
245
|
+
.toString();
|
|
246
|
+
statements.add(stmtTrigger);
|
|
247
|
+
}
|
|
239
248
|
return statements;
|
|
240
249
|
}
|
|
241
250
|
|
|
@@ -3,6 +3,7 @@ package com.getcapacitor.community.database.sqlite.SQLite.ImportExportJson;
|
|
|
3
3
|
import com.getcapacitor.JSArray;
|
|
4
4
|
import com.getcapacitor.JSObject;
|
|
5
5
|
import com.getcapacitor.community.database.sqlite.SQLite.Database;
|
|
6
|
+
import com.getcapacitor.community.database.sqlite.SQLite.UtilsDrop;
|
|
6
7
|
import java.sql.Blob;
|
|
7
8
|
import java.util.ArrayList;
|
|
8
9
|
import java.util.Iterator;
|
|
@@ -16,6 +17,28 @@ public class UtilsJson {
|
|
|
16
17
|
private JsonIndex uJIdx = new JsonIndex();
|
|
17
18
|
private JsonTrigger uJTrg = new JsonTrigger();
|
|
18
19
|
private JsonView uJView = new JsonView();
|
|
20
|
+
private UtilsDrop _uDrop = new UtilsDrop();
|
|
21
|
+
|
|
22
|
+
public boolean isLastModified(Database db) throws Exception {
|
|
23
|
+
if (!db.isOpen()) {
|
|
24
|
+
throw new Exception("isLastModified: Database not opened");
|
|
25
|
+
}
|
|
26
|
+
boolean ret = false;
|
|
27
|
+
try {
|
|
28
|
+
List<String> tables = _uDrop.getTablesNames(db);
|
|
29
|
+
for (String tableName : tables) {
|
|
30
|
+
JSObject namesTypes = getTableColumnNamesTypes(db, tableName);
|
|
31
|
+
ArrayList<String> colNames = (ArrayList<String>) namesTypes.get("names");
|
|
32
|
+
if (colNames.contains("last_modified")) {
|
|
33
|
+
ret = true;
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return ret;
|
|
38
|
+
} catch (Exception e) {
|
|
39
|
+
throw new Exception("isLastModified: " + e.getMessage());
|
|
40
|
+
}
|
|
41
|
+
}
|
|
19
42
|
|
|
20
43
|
/**
|
|
21
44
|
* Check if a table exists
|
package/electron/dist/plugin.js
CHANGED
|
@@ -435,12 +435,187 @@ utilsSQLite.UtilsSQLite = UtilsSQLite;
|
|
|
435
435
|
|
|
436
436
|
var utilsJson = {};
|
|
437
437
|
|
|
438
|
+
var utilsDrop = {};
|
|
439
|
+
|
|
440
|
+
Object.defineProperty(utilsDrop, "__esModule", { value: true });
|
|
441
|
+
utilsDrop.UtilsDrop = void 0;
|
|
442
|
+
const utilsSQLite_1$5 = utilsSQLite;
|
|
443
|
+
class UtilsDrop {
|
|
444
|
+
constructor() {
|
|
445
|
+
this._uSQLite = new utilsSQLite_1$5.UtilsSQLite();
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* GetTablesNames
|
|
449
|
+
* @param mDb
|
|
450
|
+
*/
|
|
451
|
+
async getTablesNames(mDb) {
|
|
452
|
+
let sql = 'SELECT name FROM sqlite_master WHERE ';
|
|
453
|
+
sql += "type='table' AND name NOT LIKE 'sync_table' ";
|
|
454
|
+
sql += "AND name NOT LIKE '_temp_%' ";
|
|
455
|
+
sql += "AND name NOT LIKE 'sqlite_%' ";
|
|
456
|
+
sql += 'ORDER BY rootpage DESC;';
|
|
457
|
+
const retArr = [];
|
|
458
|
+
try {
|
|
459
|
+
const retQuery = await this._uSQLite.queryAll(mDb, sql, []);
|
|
460
|
+
for (const query of retQuery) {
|
|
461
|
+
retArr.push(query.name);
|
|
462
|
+
}
|
|
463
|
+
return Promise.resolve(retArr);
|
|
464
|
+
}
|
|
465
|
+
catch (err) {
|
|
466
|
+
return Promise.reject(`getTablesNames: ${err}`);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* GetViewsNames
|
|
471
|
+
* @param mDb
|
|
472
|
+
*/
|
|
473
|
+
async getViewsNames(mDb) {
|
|
474
|
+
let sql = 'SELECT name FROM sqlite_master WHERE ';
|
|
475
|
+
sql += "type='view' AND name NOT LIKE 'sqlite_%' ";
|
|
476
|
+
sql += 'ORDER BY rootpage DESC;';
|
|
477
|
+
const retArr = [];
|
|
478
|
+
try {
|
|
479
|
+
const retQuery = await this._uSQLite.queryAll(mDb, sql, []);
|
|
480
|
+
for (const query of retQuery) {
|
|
481
|
+
retArr.push(query.name);
|
|
482
|
+
}
|
|
483
|
+
return Promise.resolve(retArr);
|
|
484
|
+
}
|
|
485
|
+
catch (err) {
|
|
486
|
+
return Promise.reject(`getViewsNames: ${err}`);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* DropElements
|
|
491
|
+
* @param db
|
|
492
|
+
* @param type ["table","index","trigger"]
|
|
493
|
+
*/
|
|
494
|
+
async dropElements(db, type) {
|
|
495
|
+
let msg = '';
|
|
496
|
+
switch (type) {
|
|
497
|
+
case 'index':
|
|
498
|
+
msg = 'DropIndexes';
|
|
499
|
+
break;
|
|
500
|
+
case 'trigger':
|
|
501
|
+
msg = 'DropTriggers';
|
|
502
|
+
break;
|
|
503
|
+
case 'table':
|
|
504
|
+
msg = 'DropTables';
|
|
505
|
+
break;
|
|
506
|
+
case 'view':
|
|
507
|
+
msg = 'DropViews';
|
|
508
|
+
break;
|
|
509
|
+
default:
|
|
510
|
+
return Promise.reject(`DropElements: ${type} ` + 'not found');
|
|
511
|
+
}
|
|
512
|
+
// get the element's names
|
|
513
|
+
let stmt = 'SELECT name FROM sqlite_master WHERE ';
|
|
514
|
+
stmt += `type = '${type}' AND name NOT LIKE 'sqlite_%';`;
|
|
515
|
+
try {
|
|
516
|
+
const elements = await this._uSQLite.queryAll(db, stmt, []);
|
|
517
|
+
if (elements.length > 0) {
|
|
518
|
+
const upType = type.toUpperCase();
|
|
519
|
+
const statements = [];
|
|
520
|
+
for (const elem of elements) {
|
|
521
|
+
let stmt = `DROP ${upType} IF EXISTS `;
|
|
522
|
+
stmt += `${elem.name};`;
|
|
523
|
+
statements.push(stmt);
|
|
524
|
+
}
|
|
525
|
+
for (const stmt of statements) {
|
|
526
|
+
const lastId = await this._uSQLite.prepareRun(db, stmt, []);
|
|
527
|
+
if (lastId < 0) {
|
|
528
|
+
return Promise.reject(`${msg}: lastId < 0`);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
return Promise.resolve();
|
|
533
|
+
}
|
|
534
|
+
catch (err) {
|
|
535
|
+
return Promise.reject(`${msg}: ${err}`);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
/**
|
|
539
|
+
* DropAll
|
|
540
|
+
* Drop all database's elements
|
|
541
|
+
* @param db
|
|
542
|
+
*/
|
|
543
|
+
async dropAll(db) {
|
|
544
|
+
try {
|
|
545
|
+
// drop tables
|
|
546
|
+
await this.dropElements(db, 'table');
|
|
547
|
+
// drop indexes
|
|
548
|
+
await this.dropElements(db, 'index');
|
|
549
|
+
// drop triggers
|
|
550
|
+
await this.dropElements(db, 'trigger');
|
|
551
|
+
// drop views
|
|
552
|
+
await this.dropElements(db, 'view');
|
|
553
|
+
// vacuum the database
|
|
554
|
+
await this._uSQLite.prepareRun(db, 'VACUUM;', []);
|
|
555
|
+
return Promise.resolve();
|
|
556
|
+
}
|
|
557
|
+
catch (err) {
|
|
558
|
+
return Promise.reject(`DropAll: ${err}`);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* DropTempTables
|
|
563
|
+
* @param db
|
|
564
|
+
* @param alterTables
|
|
565
|
+
*/
|
|
566
|
+
async dropTempTables(db, alterTables) {
|
|
567
|
+
const tempTables = Object.keys(alterTables);
|
|
568
|
+
const statements = [];
|
|
569
|
+
for (const tTable of tempTables) {
|
|
570
|
+
let stmt = 'DROP TABLE IF EXISTS ';
|
|
571
|
+
stmt += `_temp_${tTable};`;
|
|
572
|
+
statements.push(stmt);
|
|
573
|
+
}
|
|
574
|
+
try {
|
|
575
|
+
const changes = await this._uSQLite.execute(db, statements.join('\n'));
|
|
576
|
+
if (changes < 0) {
|
|
577
|
+
return Promise.reject('DropTempTables: changes < 0');
|
|
578
|
+
}
|
|
579
|
+
return Promise.resolve();
|
|
580
|
+
}
|
|
581
|
+
catch (err) {
|
|
582
|
+
return Promise.reject(`DropTempTables: ${err}`);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
utilsDrop.UtilsDrop = UtilsDrop;
|
|
587
|
+
|
|
438
588
|
Object.defineProperty(utilsJson, "__esModule", { value: true });
|
|
439
589
|
utilsJson.UtilsJson = void 0;
|
|
440
|
-
const
|
|
590
|
+
const utilsDrop_1$2 = utilsDrop;
|
|
591
|
+
const utilsSQLite_1$4 = utilsSQLite;
|
|
441
592
|
class UtilsJson {
|
|
442
593
|
constructor() {
|
|
443
|
-
this._uSQLite = new utilsSQLite_1$
|
|
594
|
+
this._uSQLite = new utilsSQLite_1$4.UtilsSQLite();
|
|
595
|
+
this._uDrop = new utilsDrop_1$2.UtilsDrop();
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* isLastModified
|
|
599
|
+
* @param db
|
|
600
|
+
* @param isOpen
|
|
601
|
+
*/
|
|
602
|
+
async isLastModified(db, isOpen) {
|
|
603
|
+
if (!isOpen) {
|
|
604
|
+
return Promise.reject('isLastModified: database not opened');
|
|
605
|
+
}
|
|
606
|
+
try {
|
|
607
|
+
const tableList = await this._uDrop.getTablesNames(db);
|
|
608
|
+
for (const table of tableList) {
|
|
609
|
+
const tableNamesTypes = await this.getTableColumnNamesTypes(db, table);
|
|
610
|
+
const tableColumnNames = tableNamesTypes.names;
|
|
611
|
+
if (tableColumnNames.includes('last_modified')) {
|
|
612
|
+
return Promise.resolve(true);
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
catch (err) {
|
|
617
|
+
return Promise.reject(`isLastModified: ${err}`);
|
|
618
|
+
}
|
|
444
619
|
}
|
|
445
620
|
/**
|
|
446
621
|
* IsTableExists
|
|
@@ -554,6 +729,7 @@ class UtilsJson {
|
|
|
554
729
|
*/
|
|
555
730
|
async createSchemaStatement(jsonData) {
|
|
556
731
|
const statements = [];
|
|
732
|
+
let isLastModified = false;
|
|
557
733
|
// Prepare the statement to execute
|
|
558
734
|
try {
|
|
559
735
|
for (const jTable of jsonData.tables) {
|
|
@@ -564,6 +740,9 @@ class UtilsJson {
|
|
|
564
740
|
if (j === jTable.schema.length - 1) {
|
|
565
741
|
if (jTable.schema[j].column) {
|
|
566
742
|
statements.push(`${jTable.schema[j].column} ${jTable.schema[j].value}`);
|
|
743
|
+
if (jTable.schema[j].column === 'last_modified') {
|
|
744
|
+
isLastModified = true;
|
|
745
|
+
}
|
|
567
746
|
}
|
|
568
747
|
else if (jTable.schema[j].foreignkey) {
|
|
569
748
|
statements.push(`FOREIGN KEY (${jTable.schema[j].foreignkey}) ${jTable.schema[j].value}`);
|
|
@@ -575,6 +754,9 @@ class UtilsJson {
|
|
|
575
754
|
else {
|
|
576
755
|
if (jTable.schema[j].column) {
|
|
577
756
|
statements.push(`${jTable.schema[j].column} ${jTable.schema[j].value},`);
|
|
757
|
+
if (jTable.schema[j].column === 'last_modified') {
|
|
758
|
+
isLastModified = true;
|
|
759
|
+
}
|
|
578
760
|
}
|
|
579
761
|
else if (jTable.schema[j].foreignkey) {
|
|
580
762
|
statements.push(`FOREIGN KEY (${jTable.schema[j].foreignkey}) ${jTable.schema[j].value},`);
|
|
@@ -585,17 +767,19 @@ class UtilsJson {
|
|
|
585
767
|
}
|
|
586
768
|
}
|
|
587
769
|
statements.push(');');
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
770
|
+
if (isLastModified) {
|
|
771
|
+
// create trigger last_modified associated with the table
|
|
772
|
+
let trig = 'CREATE TRIGGER IF NOT EXISTS ';
|
|
773
|
+
trig += `${jTable.name}`;
|
|
774
|
+
trig += `_trigger_last_modified `;
|
|
775
|
+
trig += `AFTER UPDATE ON ${jTable.name} `;
|
|
776
|
+
trig += 'FOR EACH ROW WHEN NEW.last_modified <= ';
|
|
777
|
+
trig += 'OLD.last_modified BEGIN UPDATE ';
|
|
778
|
+
trig += `${jTable.name} `;
|
|
779
|
+
trig += `SET last_modified = `;
|
|
780
|
+
trig += "(strftime('%s','now')) WHERE id=OLD.id; END;";
|
|
781
|
+
statements.push(trig);
|
|
782
|
+
}
|
|
599
783
|
}
|
|
600
784
|
if (jTable.indexes != null && jTable.indexes.length >= 1) {
|
|
601
785
|
for (const jIndex of jTable.indexes) {
|
|
@@ -1165,11 +1349,11 @@ utilsJson.UtilsJson = UtilsJson;
|
|
|
1165
1349
|
|
|
1166
1350
|
Object.defineProperty(exportToJson, "__esModule", { value: true });
|
|
1167
1351
|
exportToJson.ExportToJson = void 0;
|
|
1168
|
-
const utilsSQLite_1$
|
|
1352
|
+
const utilsSQLite_1$3 = utilsSQLite;
|
|
1169
1353
|
const utilsJson_1$4 = utilsJson;
|
|
1170
1354
|
class ExportToJson {
|
|
1171
1355
|
constructor() {
|
|
1172
|
-
this._uSQLite = new utilsSQLite_1$
|
|
1356
|
+
this._uSQLite = new utilsSQLite_1$3.UtilsSQLite();
|
|
1173
1357
|
this._uJson = new utilsJson_1$4.UtilsJson();
|
|
1174
1358
|
}
|
|
1175
1359
|
/**
|
|
@@ -1191,6 +1375,10 @@ class ExportToJson {
|
|
|
1191
1375
|
return Promise.reject("createExportObject: table's names failed");
|
|
1192
1376
|
}
|
|
1193
1377
|
else {
|
|
1378
|
+
const isTable = await this._uJson.isTableExists(mDB, true, 'sync_table');
|
|
1379
|
+
if (!isTable && sqlObj.mode === 'partial') {
|
|
1380
|
+
return Promise.reject('No sync_table available');
|
|
1381
|
+
}
|
|
1194
1382
|
switch (sqlObj.mode) {
|
|
1195
1383
|
case 'partial': {
|
|
1196
1384
|
tables = await this.getTablesPartial(mDB, resTables);
|
|
@@ -1807,156 +1995,6 @@ exportToJson.ExportToJson = ExportToJson;
|
|
|
1807
1995
|
|
|
1808
1996
|
var importFromJson = {};
|
|
1809
1997
|
|
|
1810
|
-
var utilsDrop = {};
|
|
1811
|
-
|
|
1812
|
-
Object.defineProperty(utilsDrop, "__esModule", { value: true });
|
|
1813
|
-
utilsDrop.UtilsDrop = void 0;
|
|
1814
|
-
const utilsSQLite_1$3 = utilsSQLite;
|
|
1815
|
-
class UtilsDrop {
|
|
1816
|
-
constructor() {
|
|
1817
|
-
this._uSQLite = new utilsSQLite_1$3.UtilsSQLite();
|
|
1818
|
-
}
|
|
1819
|
-
/**
|
|
1820
|
-
* GetTablesNames
|
|
1821
|
-
* @param mDb
|
|
1822
|
-
*/
|
|
1823
|
-
async getTablesNames(mDb) {
|
|
1824
|
-
let sql = 'SELECT name FROM sqlite_master WHERE ';
|
|
1825
|
-
sql += "type='table' AND name NOT LIKE 'sync_table' ";
|
|
1826
|
-
sql += "AND name NOT LIKE '_temp_%' ";
|
|
1827
|
-
sql += "AND name NOT LIKE 'sqlite_%' ";
|
|
1828
|
-
sql += 'ORDER BY rootpage DESC;';
|
|
1829
|
-
const retArr = [];
|
|
1830
|
-
try {
|
|
1831
|
-
const retQuery = await this._uSQLite.queryAll(mDb, sql, []);
|
|
1832
|
-
for (const query of retQuery) {
|
|
1833
|
-
retArr.push(query.name);
|
|
1834
|
-
}
|
|
1835
|
-
return Promise.resolve(retArr);
|
|
1836
|
-
}
|
|
1837
|
-
catch (err) {
|
|
1838
|
-
return Promise.reject(`getTablesNames: ${err}`);
|
|
1839
|
-
}
|
|
1840
|
-
}
|
|
1841
|
-
/**
|
|
1842
|
-
* GetViewsNames
|
|
1843
|
-
* @param mDb
|
|
1844
|
-
*/
|
|
1845
|
-
async getViewsNames(mDb) {
|
|
1846
|
-
let sql = 'SELECT name FROM sqlite_master WHERE ';
|
|
1847
|
-
sql += "type='view' AND name NOT LIKE 'sqlite_%' ";
|
|
1848
|
-
sql += 'ORDER BY rootpage DESC;';
|
|
1849
|
-
const retArr = [];
|
|
1850
|
-
try {
|
|
1851
|
-
const retQuery = await this._uSQLite.queryAll(mDb, sql, []);
|
|
1852
|
-
for (const query of retQuery) {
|
|
1853
|
-
retArr.push(query.name);
|
|
1854
|
-
}
|
|
1855
|
-
return Promise.resolve(retArr);
|
|
1856
|
-
}
|
|
1857
|
-
catch (err) {
|
|
1858
|
-
return Promise.reject(`getViewsNames: ${err}`);
|
|
1859
|
-
}
|
|
1860
|
-
}
|
|
1861
|
-
/**
|
|
1862
|
-
* DropElements
|
|
1863
|
-
* @param db
|
|
1864
|
-
* @param type ["table","index","trigger"]
|
|
1865
|
-
*/
|
|
1866
|
-
async dropElements(db, type) {
|
|
1867
|
-
let msg = '';
|
|
1868
|
-
switch (type) {
|
|
1869
|
-
case 'index':
|
|
1870
|
-
msg = 'DropIndexes';
|
|
1871
|
-
break;
|
|
1872
|
-
case 'trigger':
|
|
1873
|
-
msg = 'DropTriggers';
|
|
1874
|
-
break;
|
|
1875
|
-
case 'table':
|
|
1876
|
-
msg = 'DropTables';
|
|
1877
|
-
break;
|
|
1878
|
-
case 'view':
|
|
1879
|
-
msg = 'DropViews';
|
|
1880
|
-
break;
|
|
1881
|
-
default:
|
|
1882
|
-
return Promise.reject(`DropElements: ${type} ` + 'not found');
|
|
1883
|
-
}
|
|
1884
|
-
// get the element's names
|
|
1885
|
-
let stmt = 'SELECT name FROM sqlite_master WHERE ';
|
|
1886
|
-
stmt += `type = '${type}' AND name NOT LIKE 'sqlite_%';`;
|
|
1887
|
-
try {
|
|
1888
|
-
const elements = await this._uSQLite.queryAll(db, stmt, []);
|
|
1889
|
-
if (elements.length > 0) {
|
|
1890
|
-
const upType = type.toUpperCase();
|
|
1891
|
-
const statements = [];
|
|
1892
|
-
for (const elem of elements) {
|
|
1893
|
-
let stmt = `DROP ${upType} IF EXISTS `;
|
|
1894
|
-
stmt += `${elem.name};`;
|
|
1895
|
-
statements.push(stmt);
|
|
1896
|
-
}
|
|
1897
|
-
for (const stmt of statements) {
|
|
1898
|
-
const lastId = await this._uSQLite.prepareRun(db, stmt, []);
|
|
1899
|
-
if (lastId < 0) {
|
|
1900
|
-
return Promise.reject(`${msg}: lastId < 0`);
|
|
1901
|
-
}
|
|
1902
|
-
}
|
|
1903
|
-
}
|
|
1904
|
-
return Promise.resolve();
|
|
1905
|
-
}
|
|
1906
|
-
catch (err) {
|
|
1907
|
-
return Promise.reject(`${msg}: ${err}`);
|
|
1908
|
-
}
|
|
1909
|
-
}
|
|
1910
|
-
/**
|
|
1911
|
-
* DropAll
|
|
1912
|
-
* Drop all database's elements
|
|
1913
|
-
* @param db
|
|
1914
|
-
*/
|
|
1915
|
-
async dropAll(db) {
|
|
1916
|
-
try {
|
|
1917
|
-
// drop tables
|
|
1918
|
-
await this.dropElements(db, 'table');
|
|
1919
|
-
// drop indexes
|
|
1920
|
-
await this.dropElements(db, 'index');
|
|
1921
|
-
// drop triggers
|
|
1922
|
-
await this.dropElements(db, 'trigger');
|
|
1923
|
-
// drop views
|
|
1924
|
-
await this.dropElements(db, 'view');
|
|
1925
|
-
// vacuum the database
|
|
1926
|
-
await this._uSQLite.prepareRun(db, 'VACUUM;', []);
|
|
1927
|
-
return Promise.resolve();
|
|
1928
|
-
}
|
|
1929
|
-
catch (err) {
|
|
1930
|
-
return Promise.reject(`DropAll: ${err}`);
|
|
1931
|
-
}
|
|
1932
|
-
}
|
|
1933
|
-
/**
|
|
1934
|
-
* DropTempTables
|
|
1935
|
-
* @param db
|
|
1936
|
-
* @param alterTables
|
|
1937
|
-
*/
|
|
1938
|
-
async dropTempTables(db, alterTables) {
|
|
1939
|
-
const tempTables = Object.keys(alterTables);
|
|
1940
|
-
const statements = [];
|
|
1941
|
-
for (const tTable of tempTables) {
|
|
1942
|
-
let stmt = 'DROP TABLE IF EXISTS ';
|
|
1943
|
-
stmt += `_temp_${tTable};`;
|
|
1944
|
-
statements.push(stmt);
|
|
1945
|
-
}
|
|
1946
|
-
try {
|
|
1947
|
-
const changes = await this._uSQLite.execute(db, statements.join('\n'));
|
|
1948
|
-
if (changes < 0) {
|
|
1949
|
-
return Promise.reject('DropTempTables: changes < 0');
|
|
1950
|
-
}
|
|
1951
|
-
return Promise.resolve();
|
|
1952
|
-
}
|
|
1953
|
-
catch (err) {
|
|
1954
|
-
return Promise.reject(`DropTempTables: ${err}`);
|
|
1955
|
-
}
|
|
1956
|
-
}
|
|
1957
|
-
}
|
|
1958
|
-
utilsDrop.UtilsDrop = UtilsDrop;
|
|
1959
|
-
|
|
1960
1998
|
Object.defineProperty(importFromJson, "__esModule", { value: true });
|
|
1961
1999
|
importFromJson.ImportFromJson = void 0;
|
|
1962
2000
|
const utilsDrop_1$1 = utilsDrop;
|
|
@@ -3069,17 +3107,23 @@ class Database {
|
|
|
3069
3107
|
try {
|
|
3070
3108
|
const retB = await this._uJson.isTableExists(this._mDB, isOpen, 'sync_table');
|
|
3071
3109
|
if (!retB) {
|
|
3072
|
-
const
|
|
3073
|
-
|
|
3074
|
-
|
|
3075
|
-
|
|
3076
|
-
|
|
3077
|
-
|
|
3078
|
-
|
|
3079
|
-
|
|
3080
|
-
|
|
3081
|
-
|
|
3082
|
-
|
|
3110
|
+
const isLastModified = await this._uJson.isLastModified(this._mDB, isOpen);
|
|
3111
|
+
if (isLastModified) {
|
|
3112
|
+
const date = Math.round(new Date().getTime() / 1000);
|
|
3113
|
+
let stmts = `
|
|
3114
|
+
CREATE TABLE IF NOT EXISTS sync_table (
|
|
3115
|
+
id INTEGER PRIMARY KEY NOT NULL,
|
|
3116
|
+
sync_date INTEGER
|
|
3117
|
+
);`;
|
|
3118
|
+
stmts += `INSERT INTO sync_table (sync_date) VALUES (
|
|
3119
|
+
"${date}");`;
|
|
3120
|
+
changes = await this._uSQLite.execute(this._mDB, stmts);
|
|
3121
|
+
if (changes < 0) {
|
|
3122
|
+
return Promise.reject(`CreateSyncTable: failed changes < 0`);
|
|
3123
|
+
}
|
|
3124
|
+
}
|
|
3125
|
+
else {
|
|
3126
|
+
return Promise.reject('No last_modified column in tables');
|
|
3083
3127
|
}
|
|
3084
3128
|
}
|
|
3085
3129
|
return Promise.resolve(changes);
|
|
@@ -3101,6 +3145,10 @@ class Database {
|
|
|
3101
3145
|
return { result: false, message: msg };
|
|
3102
3146
|
}
|
|
3103
3147
|
try {
|
|
3148
|
+
const isTable = await this._uJson.isTableExists(this._mDB, this._isDBOpen, 'sync_table');
|
|
3149
|
+
if (!isTable) {
|
|
3150
|
+
return Promise.reject('No sync_table available');
|
|
3151
|
+
}
|
|
3104
3152
|
const sDate = Math.round(new Date(syncDate).getTime() / 1000);
|
|
3105
3153
|
let stmt = `UPDATE sync_table SET sync_date = `;
|
|
3106
3154
|
stmt += `${sDate} WHERE id = 1;`;
|
|
@@ -3128,6 +3176,10 @@ class Database {
|
|
|
3128
3176
|
return { syncDate: 0, message: msg };
|
|
3129
3177
|
}
|
|
3130
3178
|
try {
|
|
3179
|
+
const isTable = await this._uJson.isTableExists(this._mDB, this._isDBOpen, 'sync_table');
|
|
3180
|
+
if (!isTable) {
|
|
3181
|
+
return Promise.reject('No sync_table available');
|
|
3182
|
+
}
|
|
3131
3183
|
const syncDate = await this._eTJson.getSyncDate(this._mDB);
|
|
3132
3184
|
if (syncDate > 0) {
|
|
3133
3185
|
return { syncDate: syncDate };
|