@capacitor-community/sqlite 3.4.2-4 → 3.4.3-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 +13 -4
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ExportToJson.java +2 -83
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ImportFromJson.java +91 -5
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/UtilsJson.java +90 -0
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsUpgrade.java +8 -4
- package/dist/esm/definitions.d.ts +3 -1
- package/dist/esm/definitions.js +14 -14
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/web.d.ts +19 -11
- package/dist/esm/web.js +277 -473
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +264 -460
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +264 -460
- package/dist/plugin.js.map +1 -1
- package/electron/dist/plugin.js +580 -744
- package/electron/dist/plugin.js.map +1 -1
- package/ios/Plugin/ImportExportJson/ExportToJson.swift +63 -19
- package/ios/Plugin/ImportExportJson/ImportFromJson.swift +21 -12
- package/ios/Plugin/Utils/UtilsBinding.swift +2 -2
- package/ios/Plugin/Utils/UtilsDrop.swift +8 -4
- package/ios/Plugin/Utils/UtilsJson.swift +90 -9
- package/ios/Plugin/Utils/UtilsSQLCipher.swift +7 -25
- package/ios/Plugin/Utils/UtilsUpgrade.swift +35 -13
- package/package.json +6 -6
package/electron/dist/plugin.js
CHANGED
|
@@ -777,7 +777,7 @@ class UtilsJson {
|
|
|
777
777
|
trig += `${jTable.name}`;
|
|
778
778
|
trig += `_trigger_last_modified `;
|
|
779
779
|
trig += `AFTER UPDATE ON ${jTable.name} `;
|
|
780
|
-
trig += 'FOR EACH ROW WHEN NEW.last_modified
|
|
780
|
+
trig += 'FOR EACH ROW WHEN NEW.last_modified < ';
|
|
781
781
|
trig += 'OLD.last_modified BEGIN UPDATE ';
|
|
782
782
|
trig += `${jTable.name} `;
|
|
783
783
|
trig += `SET last_modified = `;
|
|
@@ -859,6 +859,7 @@ class UtilsJson {
|
|
|
859
859
|
*/
|
|
860
860
|
const retisIdExists = await this.isIdExists(mDB, table.name, tableColumnNames[0], table.values[j][0]);
|
|
861
861
|
let stmt;
|
|
862
|
+
let isRun = true;
|
|
862
863
|
if (mode === 'full' || (mode === 'partial' && !retisIdExists)) {
|
|
863
864
|
// Insert
|
|
864
865
|
const nameString = tableColumnNames.join();
|
|
@@ -880,10 +881,16 @@ class UtilsJson {
|
|
|
880
881
|
else {
|
|
881
882
|
stmt += `${tableColumnNames[0]} = ${table.values[j][0]};`;
|
|
882
883
|
}
|
|
884
|
+
isRun = await this.checkUpdate(mDB, table.values[j], table.name, tableColumnNames);
|
|
883
885
|
}
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
886
|
+
if (isRun) {
|
|
887
|
+
lastId = await this._uSQLite.prepareRun(mDB, stmt, table.values[j]);
|
|
888
|
+
if (lastId < 0) {
|
|
889
|
+
return Promise.reject('CreateDataTable: lastId < 0');
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
else {
|
|
893
|
+
lastId = 0;
|
|
887
894
|
}
|
|
888
895
|
}
|
|
889
896
|
return Promise.resolve(lastId);
|
|
@@ -892,6 +899,84 @@ class UtilsJson {
|
|
|
892
899
|
return Promise.reject(`CreateDataTable: ${err}`);
|
|
893
900
|
}
|
|
894
901
|
}
|
|
902
|
+
/**
|
|
903
|
+
*
|
|
904
|
+
* @param db
|
|
905
|
+
* @param values
|
|
906
|
+
* @param tbName
|
|
907
|
+
* @param tColNames
|
|
908
|
+
* @returns
|
|
909
|
+
*/
|
|
910
|
+
async checkUpdate(db, values, tbName, tColNames) {
|
|
911
|
+
try {
|
|
912
|
+
let query = `SELECT * FROM ${tbName} WHERE `;
|
|
913
|
+
if (typeof values[0] == 'string') {
|
|
914
|
+
query += `${tColNames[0]} = '${values[0]}';`;
|
|
915
|
+
}
|
|
916
|
+
else {
|
|
917
|
+
query += `${tColNames[0]} = ${values[0]};`;
|
|
918
|
+
}
|
|
919
|
+
const resQuery = await this.getValues(db, query, tbName);
|
|
920
|
+
let resValues = [];
|
|
921
|
+
if (resQuery.length > 0) {
|
|
922
|
+
resValues = resQuery[0];
|
|
923
|
+
}
|
|
924
|
+
if (values.length > 0 &&
|
|
925
|
+
resValues.length > 0 &&
|
|
926
|
+
values.length === resValues.length) {
|
|
927
|
+
for (let i = 0; i < values.length; i++) {
|
|
928
|
+
if (values[i] !== resValues[i]) {
|
|
929
|
+
return Promise.resolve(true);
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
return Promise.resolve(false);
|
|
933
|
+
}
|
|
934
|
+
else {
|
|
935
|
+
const msg = 'Both arrays not the same length';
|
|
936
|
+
return Promise.reject(new Error(`CheckUpdate: ${msg}`));
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
catch (err) {
|
|
940
|
+
return Promise.reject(new Error(`CheckUpdate: ${err.message}`));
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
/**
|
|
944
|
+
* GetValues
|
|
945
|
+
* @param mDb
|
|
946
|
+
* @param query
|
|
947
|
+
* @param tableName
|
|
948
|
+
*/
|
|
949
|
+
async getValues(mDb, query, tableName) {
|
|
950
|
+
const values = [];
|
|
951
|
+
try {
|
|
952
|
+
// get table column names and types
|
|
953
|
+
const tableNamesTypes = await this.getTableColumnNamesTypes(mDb, tableName);
|
|
954
|
+
let rowNames = [];
|
|
955
|
+
if (Object.keys(tableNamesTypes).includes('names')) {
|
|
956
|
+
rowNames = tableNamesTypes.names;
|
|
957
|
+
}
|
|
958
|
+
else {
|
|
959
|
+
return Promise.reject(`GetValues: Table ${tableName} no names`);
|
|
960
|
+
}
|
|
961
|
+
const retValues = await this._uSQLite.queryAll(mDb, query, []);
|
|
962
|
+
for (const rValue of retValues) {
|
|
963
|
+
const row = [];
|
|
964
|
+
for (const rName of rowNames) {
|
|
965
|
+
if (Object.keys(rValue).includes(rName)) {
|
|
966
|
+
row.push(rValue[rName]);
|
|
967
|
+
}
|
|
968
|
+
else {
|
|
969
|
+
row.push('NULL');
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
values.push(row);
|
|
973
|
+
}
|
|
974
|
+
return Promise.resolve(values);
|
|
975
|
+
}
|
|
976
|
+
catch (err) {
|
|
977
|
+
return Promise.reject(`GetValues: ${err}`);
|
|
978
|
+
}
|
|
979
|
+
}
|
|
895
980
|
/**
|
|
896
981
|
* GetTableColumnNamesTypes
|
|
897
982
|
* @param mDB
|
|
@@ -1539,7 +1624,7 @@ class ExportToJson {
|
|
|
1539
1624
|
}
|
|
1540
1625
|
// create Table's Data
|
|
1541
1626
|
const query = `SELECT * FROM ${tableName};`;
|
|
1542
|
-
const values = await this.getValues(mDb, query, tableName);
|
|
1627
|
+
const values = await this._uJson.getValues(mDb, query, tableName);
|
|
1543
1628
|
table.name = tableName;
|
|
1544
1629
|
if (schema.length > 0) {
|
|
1545
1630
|
table.schema = schema;
|
|
@@ -1742,43 +1827,6 @@ class ExportToJson {
|
|
|
1742
1827
|
return Promise.reject(`GetTriggers: ${err}`);
|
|
1743
1828
|
}
|
|
1744
1829
|
}
|
|
1745
|
-
/**
|
|
1746
|
-
* GetValues
|
|
1747
|
-
* @param mDb
|
|
1748
|
-
* @param query
|
|
1749
|
-
* @param tableName
|
|
1750
|
-
*/
|
|
1751
|
-
async getValues(mDb, query, tableName) {
|
|
1752
|
-
const values = [];
|
|
1753
|
-
try {
|
|
1754
|
-
// get table column names and types
|
|
1755
|
-
const tableNamesTypes = await this._uJson.getTableColumnNamesTypes(mDb, tableName);
|
|
1756
|
-
let rowNames = [];
|
|
1757
|
-
if (Object.keys(tableNamesTypes).includes('names')) {
|
|
1758
|
-
rowNames = tableNamesTypes.names;
|
|
1759
|
-
}
|
|
1760
|
-
else {
|
|
1761
|
-
return Promise.reject(`GetValues: Table ${tableName} no names`);
|
|
1762
|
-
}
|
|
1763
|
-
const retValues = await this._uSQLite.queryAll(mDb, query, []);
|
|
1764
|
-
for (const rValue of retValues) {
|
|
1765
|
-
const row = [];
|
|
1766
|
-
for (const rName of rowNames) {
|
|
1767
|
-
if (Object.keys(rValue).includes(rName)) {
|
|
1768
|
-
row.push(rValue[rName]);
|
|
1769
|
-
}
|
|
1770
|
-
else {
|
|
1771
|
-
row.push('NULL');
|
|
1772
|
-
}
|
|
1773
|
-
}
|
|
1774
|
-
values.push(row);
|
|
1775
|
-
}
|
|
1776
|
-
return Promise.resolve(values);
|
|
1777
|
-
}
|
|
1778
|
-
catch (err) {
|
|
1779
|
-
return Promise.reject(`GetValues: ${err}`);
|
|
1780
|
-
}
|
|
1781
|
-
}
|
|
1782
1830
|
/**
|
|
1783
1831
|
* GetTablesPartial
|
|
1784
1832
|
* @param mDb
|
|
@@ -1858,7 +1906,7 @@ class ExportToJson {
|
|
|
1858
1906
|
`SELECT * FROM ${tableName} ` +
|
|
1859
1907
|
`WHERE last_modified > ${syncDate};`;
|
|
1860
1908
|
}
|
|
1861
|
-
const values = await this.getValues(mDb, query, tableName);
|
|
1909
|
+
const values = await this._uJson.getValues(mDb, query, tableName);
|
|
1862
1910
|
// check the table object validity
|
|
1863
1911
|
table.name = tableName;
|
|
1864
1912
|
if (schema.length > 0) {
|
|
@@ -2687,15 +2735,17 @@ class UtilsUpgrade {
|
|
|
2687
2735
|
if (Object.keys(this._commonColumns).length > 0) {
|
|
2688
2736
|
await this.updateNewTablesData(mDB);
|
|
2689
2737
|
}
|
|
2738
|
+
return Promise.resolve();
|
|
2739
|
+
}
|
|
2740
|
+
catch (err) {
|
|
2741
|
+
return Promise.reject(`ExecuteStatementProcess: ${err}`);
|
|
2742
|
+
}
|
|
2743
|
+
finally {
|
|
2690
2744
|
// -> Drop _temp_tables
|
|
2691
2745
|
await this._uDrop.dropTempTables(mDB, this._alterTables);
|
|
2692
2746
|
// -> Do some cleanup
|
|
2693
2747
|
this._alterTables = {};
|
|
2694
2748
|
this._commonColumns = {};
|
|
2695
|
-
return Promise.resolve();
|
|
2696
|
-
}
|
|
2697
|
-
catch (err) {
|
|
2698
|
-
return Promise.reject(`ExecuteStatementProcess: ${err}`);
|
|
2699
2749
|
}
|
|
2700
2750
|
}
|
|
2701
2751
|
/**
|
|
@@ -2764,9 +2814,13 @@ class UtilsUpgrade {
|
|
|
2764
2814
|
// get the table's column names
|
|
2765
2815
|
const colNames = await this.getTableColumnNames(mDB, table);
|
|
2766
2816
|
this._alterTables[`${table}`] = colNames;
|
|
2817
|
+
const tmpTable = `_temp_${table}`;
|
|
2818
|
+
// Drop the tmpTable if exists
|
|
2819
|
+
const delStmt = `DROP TABLE IF EXISTS ${tmpTable};`;
|
|
2820
|
+
await this._uSQLite.prepareRun(mDB, delStmt, []);
|
|
2767
2821
|
// prefix the table with _temp_
|
|
2768
2822
|
let stmt = `ALTER TABLE ${table} RENAME `;
|
|
2769
|
-
stmt += `TO
|
|
2823
|
+
stmt += `TO ${tmpTable};`;
|
|
2770
2824
|
const lastId = await this._uSQLite.prepareRun(mDB, stmt, []);
|
|
2771
2825
|
if (lastId < 0) {
|
|
2772
2826
|
let msg = 'BackupTable: lastId < 0';
|
|
@@ -2915,24 +2969,24 @@ class Database {
|
|
|
2915
2969
|
// encrypted: boolean,
|
|
2916
2970
|
// mode: string,
|
|
2917
2971
|
version, upgDict) {
|
|
2918
|
-
this.
|
|
2919
|
-
this.
|
|
2920
|
-
this.
|
|
2921
|
-
this.
|
|
2972
|
+
this.fileUtil = new utilsFile_1$1.UtilsFile();
|
|
2973
|
+
this.sqliteUtil = new utilsSQLite_1.UtilsSQLite();
|
|
2974
|
+
this.jsonUtil = new utilsJson_1$1.UtilsJson();
|
|
2975
|
+
this.dropUtil = new utilsDrop_1.UtilsDrop();
|
|
2922
2976
|
// private _uGlobal: GlobalSQLite = new GlobalSQLite();
|
|
2923
2977
|
// private _uEncrypt: UtilsEncryption = new UtilsEncryption();
|
|
2924
|
-
this.
|
|
2925
|
-
this.
|
|
2926
|
-
this.
|
|
2927
|
-
this.
|
|
2928
|
-
this.
|
|
2978
|
+
this.upgradeUtil = new utilsUpgrade_1.UtilsUpgrade();
|
|
2979
|
+
this.importFromJsonUtil = new importFromJson_1.ImportFromJson();
|
|
2980
|
+
this.exportToJsonUtil = new exportToJson_1.ExportToJson();
|
|
2981
|
+
this.upgradeVersionDict = {};
|
|
2982
|
+
this.dbName = dbName;
|
|
2929
2983
|
// this._encrypted = encrypted;
|
|
2930
2984
|
// this._mode = mode;
|
|
2931
|
-
this.
|
|
2932
|
-
this.
|
|
2933
|
-
this.
|
|
2934
|
-
this.
|
|
2935
|
-
if (this.
|
|
2985
|
+
this.version = version;
|
|
2986
|
+
this.upgradeVersionDict = upgDict;
|
|
2987
|
+
this.pathDB = this.fileUtil.getFilePath(dbName);
|
|
2988
|
+
this._isDbOpen = false;
|
|
2989
|
+
if (this.pathDB.length === 0)
|
|
2936
2990
|
throw new Error('Could not generate a path to ' + dbName);
|
|
2937
2991
|
}
|
|
2938
2992
|
/**
|
|
@@ -2943,7 +2997,7 @@ class Database {
|
|
|
2943
2997
|
* @since 0.0.1
|
|
2944
2998
|
*/
|
|
2945
2999
|
isDBOpen() {
|
|
2946
|
-
return this.
|
|
3000
|
+
return this._isDbOpen;
|
|
2947
3001
|
}
|
|
2948
3002
|
/**
|
|
2949
3003
|
* Open
|
|
@@ -2951,7 +3005,7 @@ class Database {
|
|
|
2951
3005
|
* @returns Promise<boolean>
|
|
2952
3006
|
*/
|
|
2953
3007
|
async open() {
|
|
2954
|
-
this.
|
|
3008
|
+
this._isDbOpen = false;
|
|
2955
3009
|
// let password = '';
|
|
2956
3010
|
try {
|
|
2957
3011
|
/*
|
|
@@ -2973,34 +3027,34 @@ class Database {
|
|
|
2973
3027
|
await this._uEncrypt.encryptDatabase(this._pathDB, password);
|
|
2974
3028
|
}
|
|
2975
3029
|
*/
|
|
2976
|
-
this.
|
|
3030
|
+
this.database = await this.sqliteUtil.openOrCreateDatabase(this.pathDB /*,
|
|
2977
3031
|
password,*/);
|
|
2978
|
-
const curVersion = await this.
|
|
2979
|
-
this.
|
|
2980
|
-
if (this.
|
|
2981
|
-
Object.keys(this.
|
|
3032
|
+
const curVersion = await this.sqliteUtil.getVersion(this.database);
|
|
3033
|
+
this._isDbOpen = true;
|
|
3034
|
+
if (this.version > curVersion &&
|
|
3035
|
+
Object.keys(this.upgradeVersionDict).length > 0) {
|
|
2982
3036
|
try {
|
|
2983
3037
|
// execute the upgrade flow process
|
|
2984
|
-
await this.
|
|
3038
|
+
await this.upgradeUtil.onUpgrade(this.database, this.upgradeVersionDict, this.dbName, curVersion, this.version);
|
|
2985
3039
|
// delete the backup database
|
|
2986
|
-
await this.
|
|
3040
|
+
await this.fileUtil.deleteFileName(`backup-${this.dbName}`);
|
|
2987
3041
|
}
|
|
2988
3042
|
catch (err) {
|
|
2989
3043
|
// restore the database from backup
|
|
2990
3044
|
try {
|
|
2991
|
-
await this.
|
|
3045
|
+
await this.fileUtil.restoreFileName(this.dbName, 'backup');
|
|
2992
3046
|
}
|
|
2993
3047
|
catch (err) {
|
|
2994
|
-
|
|
3048
|
+
throw new Error(`Open: ${err}`);
|
|
2995
3049
|
}
|
|
2996
3050
|
}
|
|
2997
3051
|
}
|
|
2998
|
-
return
|
|
3052
|
+
return;
|
|
2999
3053
|
}
|
|
3000
3054
|
catch (err) {
|
|
3001
|
-
if (this.
|
|
3055
|
+
if (this._isDbOpen)
|
|
3002
3056
|
this.close();
|
|
3003
|
-
|
|
3057
|
+
throw new Error(`Open: ${err}`);
|
|
3004
3058
|
}
|
|
3005
3059
|
}
|
|
3006
3060
|
/**
|
|
@@ -3009,18 +3063,13 @@ class Database {
|
|
|
3009
3063
|
* @returns Promise<boolean>
|
|
3010
3064
|
*/
|
|
3011
3065
|
async close() {
|
|
3012
|
-
|
|
3013
|
-
|
|
3014
|
-
|
|
3015
|
-
|
|
3016
|
-
|
|
3017
|
-
|
|
3018
|
-
|
|
3019
|
-
this._isDBOpen = false;
|
|
3020
|
-
return Promise.resolve();
|
|
3021
|
-
});
|
|
3022
|
-
}
|
|
3023
|
-
return Promise.resolve();
|
|
3066
|
+
this.ensureDatabaseIsOpen();
|
|
3067
|
+
this.database.close((err) => {
|
|
3068
|
+
if (err) {
|
|
3069
|
+
throw new Error('Close failed: ${this.dbName} ${err}');
|
|
3070
|
+
}
|
|
3071
|
+
this._isDbOpen = false;
|
|
3072
|
+
});
|
|
3024
3073
|
}
|
|
3025
3074
|
/**
|
|
3026
3075
|
* GetVersion
|
|
@@ -3028,21 +3077,15 @@ class Database {
|
|
|
3028
3077
|
* @returns Promise<number>
|
|
3029
3078
|
*/
|
|
3030
3079
|
async getVersion() {
|
|
3031
|
-
|
|
3032
|
-
|
|
3033
|
-
|
|
3034
|
-
|
|
3035
|
-
}
|
|
3036
|
-
catch (err) {
|
|
3037
|
-
if (this._isDBOpen)
|
|
3038
|
-
this.close();
|
|
3039
|
-
return Promise.reject(`getVersion: ${err}`);
|
|
3040
|
-
}
|
|
3080
|
+
this.ensureDatabaseIsOpen();
|
|
3081
|
+
try {
|
|
3082
|
+
const currentVersion = await this.sqliteUtil.getVersion(this.database);
|
|
3083
|
+
return currentVersion;
|
|
3041
3084
|
}
|
|
3042
|
-
|
|
3043
|
-
|
|
3044
|
-
|
|
3045
|
-
|
|
3085
|
+
catch (err) {
|
|
3086
|
+
if (this._isDbOpen)
|
|
3087
|
+
this.close();
|
|
3088
|
+
throw new Error(`getVersion: ${err}`);
|
|
3046
3089
|
}
|
|
3047
3090
|
}
|
|
3048
3091
|
/**
|
|
@@ -3053,14 +3096,14 @@ class Database {
|
|
|
3053
3096
|
*/
|
|
3054
3097
|
async deleteDB(dbName) {
|
|
3055
3098
|
// test if file exists
|
|
3056
|
-
const isExists = this.
|
|
3057
|
-
if (isExists && !this.
|
|
3099
|
+
const isExists = this.fileUtil.isFileExists(dbName);
|
|
3100
|
+
if (isExists && !this._isDbOpen) {
|
|
3058
3101
|
// open the database
|
|
3059
3102
|
try {
|
|
3060
3103
|
await this.open();
|
|
3061
3104
|
}
|
|
3062
3105
|
catch (err) {
|
|
3063
|
-
|
|
3106
|
+
throw new Error(`DeleteDB: ${err}`);
|
|
3064
3107
|
}
|
|
3065
3108
|
}
|
|
3066
3109
|
// close the database
|
|
@@ -3068,20 +3111,18 @@ class Database {
|
|
|
3068
3111
|
await this.close();
|
|
3069
3112
|
}
|
|
3070
3113
|
catch (err) {
|
|
3071
|
-
|
|
3114
|
+
throw new Error('DeleteDB: Close failed');
|
|
3072
3115
|
}
|
|
3073
3116
|
// delete the database
|
|
3074
3117
|
if (isExists) {
|
|
3075
3118
|
try {
|
|
3076
|
-
await this.
|
|
3119
|
+
await this.fileUtil.deleteFileName(dbName);
|
|
3077
3120
|
}
|
|
3078
3121
|
catch (err) {
|
|
3079
|
-
|
|
3080
|
-
msg += ` failed ${err}`;
|
|
3081
|
-
return Promise.reject(msg);
|
|
3122
|
+
throw new Error(`DeleteDB: deleteFile ${dbName} failed ${err}`);
|
|
3082
3123
|
}
|
|
3083
3124
|
}
|
|
3084
|
-
return
|
|
3125
|
+
return;
|
|
3085
3126
|
}
|
|
3086
3127
|
/**
|
|
3087
3128
|
* IsTableExists
|
|
@@ -3089,20 +3130,14 @@ class Database {
|
|
|
3089
3130
|
* @returns
|
|
3090
3131
|
*/
|
|
3091
3132
|
async isTableExists(tableName) {
|
|
3092
|
-
|
|
3093
|
-
|
|
3094
|
-
|
|
3095
|
-
|
|
3096
|
-
|
|
3097
|
-
}
|
|
3098
|
-
catch (err) {
|
|
3099
|
-
return Promise.reject(`IsTableExists: ${err}`);
|
|
3100
|
-
}
|
|
3133
|
+
this.ensureDatabaseIsOpen();
|
|
3134
|
+
const isOpen = this._isDbOpen;
|
|
3135
|
+
try {
|
|
3136
|
+
const tableExistsResult = await this.jsonUtil.isTableExists(this.database, isOpen, tableName);
|
|
3137
|
+
return tableExistsResult;
|
|
3101
3138
|
}
|
|
3102
|
-
|
|
3103
|
-
|
|
3104
|
-
msg += `not opened`;
|
|
3105
|
-
return Promise.reject(msg);
|
|
3139
|
+
catch (err) {
|
|
3140
|
+
throw new Error(`IsTableExists: ${err}`);
|
|
3106
3141
|
}
|
|
3107
3142
|
}
|
|
3108
3143
|
/**
|
|
@@ -3111,18 +3146,14 @@ class Database {
|
|
|
3111
3146
|
* @returns Promise<number>
|
|
3112
3147
|
*/
|
|
3113
3148
|
async createSyncTable() {
|
|
3114
|
-
|
|
3115
|
-
let msg = `CreateSyncTable: Database ${this._dbName} `;
|
|
3116
|
-
msg += `not opened`;
|
|
3117
|
-
return Promise.reject(msg);
|
|
3118
|
-
}
|
|
3149
|
+
this.ensureDatabaseIsOpen();
|
|
3119
3150
|
let changes = -1;
|
|
3120
|
-
const isOpen = this.
|
|
3151
|
+
const isOpen = this._isDbOpen;
|
|
3121
3152
|
// check if the table has already being created
|
|
3122
3153
|
try {
|
|
3123
|
-
const retB = await this.
|
|
3154
|
+
const retB = await this.jsonUtil.isTableExists(this.database, isOpen, 'sync_table');
|
|
3124
3155
|
if (!retB) {
|
|
3125
|
-
const isLastModified = await this.
|
|
3156
|
+
const isLastModified = await this.jsonUtil.isLastModified(this.database, isOpen);
|
|
3126
3157
|
if (isLastModified) {
|
|
3127
3158
|
const date = Math.round(new Date().getTime() / 1000);
|
|
3128
3159
|
let stmts = `
|
|
@@ -3132,23 +3163,23 @@ class Database {
|
|
|
3132
3163
|
);`;
|
|
3133
3164
|
stmts += `INSERT INTO sync_table (sync_date) VALUES (
|
|
3134
3165
|
"${date}");`;
|
|
3135
|
-
changes = await this.
|
|
3166
|
+
changes = await this.sqliteUtil.execute(this.database, stmts);
|
|
3136
3167
|
if (changes < 0) {
|
|
3137
|
-
|
|
3168
|
+
throw new Error(`CreateSyncTable: failed changes < 0`);
|
|
3138
3169
|
}
|
|
3139
3170
|
}
|
|
3140
3171
|
else {
|
|
3141
|
-
|
|
3172
|
+
throw new Error('No last_modified column in tables');
|
|
3142
3173
|
}
|
|
3143
3174
|
}
|
|
3144
3175
|
else {
|
|
3145
3176
|
changes = 0;
|
|
3146
3177
|
}
|
|
3147
3178
|
console.log(`>>> CreateSyncTable changes: ${changes}`);
|
|
3148
|
-
return
|
|
3179
|
+
return changes;
|
|
3149
3180
|
}
|
|
3150
3181
|
catch (err) {
|
|
3151
|
-
|
|
3182
|
+
throw new Error(`CreateSyncTable: ${err}`);
|
|
3152
3183
|
}
|
|
3153
3184
|
}
|
|
3154
3185
|
/**
|
|
@@ -3158,20 +3189,16 @@ class Database {
|
|
|
3158
3189
|
* @returns Promise<{result: boolean, message: string}>
|
|
3159
3190
|
*/
|
|
3160
3191
|
async setSyncDate(syncDate) {
|
|
3161
|
-
|
|
3162
|
-
let msg = `SetSyncDate: Database ${this._dbName} `;
|
|
3163
|
-
msg += `not opened`;
|
|
3164
|
-
return { result: false, message: msg };
|
|
3165
|
-
}
|
|
3192
|
+
this.ensureDatabaseIsOpen();
|
|
3166
3193
|
try {
|
|
3167
|
-
const isTable = await this.
|
|
3194
|
+
const isTable = await this.jsonUtil.isTableExists(this.database, this._isDbOpen, 'sync_table');
|
|
3168
3195
|
if (!isTable) {
|
|
3169
|
-
|
|
3196
|
+
throw new Error('No sync_table available');
|
|
3170
3197
|
}
|
|
3171
|
-
const
|
|
3198
|
+
const syncDateUnixTimestamp = Math.round(new Date(syncDate).getTime() / 1000);
|
|
3172
3199
|
let stmt = `UPDATE sync_table SET sync_date = `;
|
|
3173
|
-
stmt += `${
|
|
3174
|
-
const changes = await this.
|
|
3200
|
+
stmt += `${syncDateUnixTimestamp} WHERE id = 1;`;
|
|
3201
|
+
const changes = await this.sqliteUtil.execute(this.database, stmt);
|
|
3175
3202
|
if (changes < 0) {
|
|
3176
3203
|
return { result: false, message: 'setSyncDate failed' };
|
|
3177
3204
|
}
|
|
@@ -3189,19 +3216,15 @@ class Database {
|
|
|
3189
3216
|
* @returns Promise<{syncDate: number, message: string}>
|
|
3190
3217
|
*/
|
|
3191
3218
|
async getSyncDate() {
|
|
3192
|
-
|
|
3193
|
-
let msg = `GetSyncDate: Database ${this._dbName} `;
|
|
3194
|
-
msg += `not opened`;
|
|
3195
|
-
return { syncDate: 0, message: msg };
|
|
3196
|
-
}
|
|
3219
|
+
this.ensureDatabaseIsOpen();
|
|
3197
3220
|
try {
|
|
3198
|
-
const isTable = await this.
|
|
3221
|
+
const isTable = await this.jsonUtil.isTableExists(this.database, this._isDbOpen, 'sync_table');
|
|
3199
3222
|
if (!isTable) {
|
|
3200
|
-
|
|
3223
|
+
throw new Error('No sync_table available');
|
|
3201
3224
|
}
|
|
3202
|
-
const syncDate = await this.
|
|
3225
|
+
const syncDate = await this.exportToJsonUtil.getSyncDate(this.database);
|
|
3203
3226
|
if (syncDate > 0) {
|
|
3204
|
-
return { syncDate
|
|
3227
|
+
return { syncDate };
|
|
3205
3228
|
}
|
|
3206
3229
|
else {
|
|
3207
3230
|
return { syncDate: 0, message: `setSyncDate failed` };
|
|
@@ -3218,32 +3241,31 @@ class Database {
|
|
|
3218
3241
|
* @returns Promise<number>
|
|
3219
3242
|
*/
|
|
3220
3243
|
async executeSQL(sql, transaction) {
|
|
3221
|
-
|
|
3222
|
-
let msg = `ExecuteSQL: Database ${this._dbName} `;
|
|
3223
|
-
msg += `not opened`;
|
|
3224
|
-
return Promise.reject(msg);
|
|
3225
|
-
}
|
|
3244
|
+
this.ensureDatabaseIsOpen();
|
|
3226
3245
|
try {
|
|
3227
|
-
if (transaction)
|
|
3228
|
-
await this.
|
|
3229
|
-
|
|
3246
|
+
if (transaction) {
|
|
3247
|
+
await this.sqliteUtil.beginTransaction(this.database, this._isDbOpen);
|
|
3248
|
+
}
|
|
3249
|
+
const changes = await this.sqliteUtil.execute(this.database, sql);
|
|
3230
3250
|
if (changes < 0) {
|
|
3231
|
-
|
|
3251
|
+
throw new Error('ExecuteSQL: changes < 0');
|
|
3232
3252
|
}
|
|
3233
|
-
if (transaction)
|
|
3234
|
-
await this.
|
|
3235
|
-
|
|
3253
|
+
if (transaction) {
|
|
3254
|
+
await this.sqliteUtil.commitTransaction(this.database, this._isDbOpen);
|
|
3255
|
+
}
|
|
3256
|
+
return changes;
|
|
3236
3257
|
}
|
|
3237
|
-
catch (
|
|
3238
|
-
let
|
|
3258
|
+
catch (executeError) {
|
|
3259
|
+
let message = `${executeError}`;
|
|
3239
3260
|
try {
|
|
3240
|
-
if (transaction)
|
|
3241
|
-
await this.
|
|
3261
|
+
if (transaction) {
|
|
3262
|
+
await this.sqliteUtil.rollbackTransaction(this.database, this._isDbOpen);
|
|
3263
|
+
}
|
|
3242
3264
|
}
|
|
3243
|
-
catch (
|
|
3244
|
-
|
|
3265
|
+
catch (rollbackErr) {
|
|
3266
|
+
message += ` : ${rollbackErr}`;
|
|
3245
3267
|
}
|
|
3246
|
-
|
|
3268
|
+
throw new Error(`ExecuteSQL: ${message}`);
|
|
3247
3269
|
}
|
|
3248
3270
|
}
|
|
3249
3271
|
/**
|
|
@@ -3254,17 +3276,13 @@ class Database {
|
|
|
3254
3276
|
* @returns Promise<any[]>
|
|
3255
3277
|
*/
|
|
3256
3278
|
async selectSQL(sql, values) {
|
|
3257
|
-
|
|
3258
|
-
let msg = `SelectSQL: Database ${this._dbName} `;
|
|
3259
|
-
msg += `not opened`;
|
|
3260
|
-
return Promise.reject(msg);
|
|
3261
|
-
}
|
|
3279
|
+
this.ensureDatabaseIsOpen();
|
|
3262
3280
|
try {
|
|
3263
|
-
const
|
|
3264
|
-
return
|
|
3281
|
+
const selectResult = await this.sqliteUtil.queryAll(this.database, sql, values);
|
|
3282
|
+
return selectResult;
|
|
3265
3283
|
}
|
|
3266
3284
|
catch (err) {
|
|
3267
|
-
|
|
3285
|
+
throw new Error(`SelectSQL: ${err}`);
|
|
3268
3286
|
}
|
|
3269
3287
|
}
|
|
3270
3288
|
/**
|
|
@@ -3275,39 +3293,39 @@ class Database {
|
|
|
3275
3293
|
* @returns Promise<{changes:number, lastId:number}>
|
|
3276
3294
|
*/
|
|
3277
3295
|
async runSQL(statement, values, transaction) {
|
|
3278
|
-
|
|
3279
|
-
|
|
3280
|
-
msg += `not opened`;
|
|
3281
|
-
return Promise.reject(msg);
|
|
3282
|
-
}
|
|
3283
|
-
const retRes = { changes: -1, lastId: -1 };
|
|
3296
|
+
this.ensureDatabaseIsOpen();
|
|
3297
|
+
const result = { changes: -1, lastId: -1 };
|
|
3284
3298
|
let initChanges = -1;
|
|
3285
3299
|
try {
|
|
3286
|
-
initChanges = await this.
|
|
3300
|
+
initChanges = await this.sqliteUtil.dbChanges(this.database);
|
|
3287
3301
|
// start a transaction
|
|
3288
|
-
if (transaction)
|
|
3289
|
-
await this.
|
|
3302
|
+
if (transaction) {
|
|
3303
|
+
await this.sqliteUtil.beginTransaction(this.database, this._isDbOpen);
|
|
3304
|
+
}
|
|
3290
3305
|
}
|
|
3291
3306
|
catch (err) {
|
|
3292
|
-
|
|
3307
|
+
throw new Error(`RunSQL: ${err}`);
|
|
3293
3308
|
}
|
|
3294
3309
|
try {
|
|
3295
|
-
const lastId = await this.
|
|
3310
|
+
const lastId = await this.sqliteUtil.prepareRun(this.database, statement, values);
|
|
3296
3311
|
if (lastId < 0) {
|
|
3297
|
-
if (transaction)
|
|
3298
|
-
await this.
|
|
3299
|
-
|
|
3312
|
+
if (transaction) {
|
|
3313
|
+
await this.sqliteUtil.rollbackTransaction(this.database, this._isDbOpen);
|
|
3314
|
+
}
|
|
3315
|
+
throw new Error(`RunSQL: return LastId < 0`);
|
|
3316
|
+
}
|
|
3317
|
+
if (transaction) {
|
|
3318
|
+
await this.sqliteUtil.commitTransaction(this.database, this._isDbOpen);
|
|
3300
3319
|
}
|
|
3301
|
-
|
|
3302
|
-
|
|
3303
|
-
|
|
3304
|
-
retRes.lastId = lastId;
|
|
3305
|
-
return Promise.resolve(retRes);
|
|
3320
|
+
result.changes = (await this.sqliteUtil.dbChanges(this.database)) - initChanges;
|
|
3321
|
+
result.lastId = lastId;
|
|
3322
|
+
return result;
|
|
3306
3323
|
}
|
|
3307
3324
|
catch (err) {
|
|
3308
|
-
if (transaction)
|
|
3309
|
-
await this.
|
|
3310
|
-
|
|
3325
|
+
if (transaction) {
|
|
3326
|
+
await this.sqliteUtil.rollbackTransaction(this.database, this._isDbOpen);
|
|
3327
|
+
}
|
|
3328
|
+
throw new Error(`RunSQL: ${err}`);
|
|
3311
3329
|
}
|
|
3312
3330
|
}
|
|
3313
3331
|
/**
|
|
@@ -3317,103 +3335,98 @@ class Database {
|
|
|
3317
3335
|
* @returns Promise<{changes:number, lastId:number}>
|
|
3318
3336
|
*/
|
|
3319
3337
|
async execSet(set, transaction) {
|
|
3320
|
-
|
|
3321
|
-
|
|
3322
|
-
msg += `not opened`;
|
|
3323
|
-
return Promise.reject(msg);
|
|
3324
|
-
}
|
|
3325
|
-
const retRes = { changes: -1, lastId: -1 };
|
|
3338
|
+
this.ensureDatabaseIsOpen();
|
|
3339
|
+
const result = { changes: -1, lastId: -1 };
|
|
3326
3340
|
let initChanges = -1;
|
|
3327
3341
|
try {
|
|
3328
|
-
initChanges = await this.
|
|
3342
|
+
initChanges = await this.sqliteUtil.dbChanges(this.database);
|
|
3329
3343
|
// start a transaction
|
|
3330
|
-
if (transaction)
|
|
3331
|
-
await this.
|
|
3344
|
+
if (transaction) {
|
|
3345
|
+
await this.sqliteUtil.beginTransaction(this.database, this._isDbOpen);
|
|
3346
|
+
}
|
|
3332
3347
|
}
|
|
3333
3348
|
catch (err) {
|
|
3334
|
-
|
|
3349
|
+
throw new Error(`ExecSet: ${err}`);
|
|
3335
3350
|
}
|
|
3336
3351
|
try {
|
|
3337
|
-
|
|
3338
|
-
if (transaction)
|
|
3339
|
-
await this.
|
|
3340
|
-
|
|
3341
|
-
|
|
3352
|
+
result.lastId = await this.sqliteUtil.executeSet(this.database, set);
|
|
3353
|
+
if (transaction) {
|
|
3354
|
+
await this.sqliteUtil.commitTransaction(this.database, this._isDbOpen);
|
|
3355
|
+
}
|
|
3356
|
+
result.changes = (await this.sqliteUtil.dbChanges(this.database)) - initChanges;
|
|
3357
|
+
return result;
|
|
3342
3358
|
}
|
|
3343
3359
|
catch (err) {
|
|
3344
|
-
const
|
|
3360
|
+
const message = err;
|
|
3345
3361
|
try {
|
|
3346
|
-
if (transaction)
|
|
3347
|
-
await this.
|
|
3362
|
+
if (transaction) {
|
|
3363
|
+
await this.sqliteUtil.rollbackTransaction(this.database, this._isDbOpen);
|
|
3364
|
+
}
|
|
3348
3365
|
}
|
|
3349
3366
|
catch (err) {
|
|
3350
|
-
|
|
3367
|
+
throw new Error(`ExecSet: ${message}: ` + `${err}`);
|
|
3351
3368
|
}
|
|
3352
3369
|
}
|
|
3353
3370
|
}
|
|
3354
3371
|
async getTableList() {
|
|
3355
|
-
|
|
3356
|
-
let msg = `GetTableList: Database ${this._dbName} `;
|
|
3357
|
-
msg += `not opened`;
|
|
3358
|
-
return Promise.reject(msg);
|
|
3359
|
-
}
|
|
3372
|
+
this.ensureDatabaseIsOpen();
|
|
3360
3373
|
try {
|
|
3361
|
-
const
|
|
3362
|
-
return
|
|
3374
|
+
const tableNames = await this.dropUtil.getTablesNames(this.database);
|
|
3375
|
+
return tableNames;
|
|
3363
3376
|
}
|
|
3364
3377
|
catch (err) {
|
|
3365
|
-
|
|
3378
|
+
throw new Error(`GetTableList: ${err}`);
|
|
3366
3379
|
}
|
|
3367
3380
|
}
|
|
3368
3381
|
async importJson(jsonData) {
|
|
3369
3382
|
let changes = 0;
|
|
3370
|
-
|
|
3371
|
-
|
|
3372
|
-
|
|
3373
|
-
|
|
3374
|
-
|
|
3375
|
-
|
|
3376
|
-
|
|
3377
|
-
|
|
3378
|
-
}
|
|
3379
|
-
}
|
|
3380
|
-
if (jsonData.views && jsonData.views.length > 0) {
|
|
3381
|
-
// create the views
|
|
3382
|
-
changes += await this._iFJson.createViews(this._mDB, jsonData);
|
|
3383
|
+
this.ensureDatabaseIsOpen();
|
|
3384
|
+
try {
|
|
3385
|
+
if (jsonData.tables && jsonData.tables.length > 0) {
|
|
3386
|
+
// create the database schema
|
|
3387
|
+
changes = await this.importFromJsonUtil.createDatabaseSchema(this.database, jsonData);
|
|
3388
|
+
if (changes != -1) {
|
|
3389
|
+
// create the tables data
|
|
3390
|
+
changes += await this.importFromJsonUtil.createTablesData(this.database, jsonData);
|
|
3383
3391
|
}
|
|
3384
|
-
return Promise.resolve(changes);
|
|
3385
3392
|
}
|
|
3386
|
-
|
|
3387
|
-
|
|
3393
|
+
if (jsonData.views && jsonData.views.length > 0) {
|
|
3394
|
+
// create the views
|
|
3395
|
+
changes += await this.importFromJsonUtil.createViews(this.database, jsonData);
|
|
3388
3396
|
}
|
|
3397
|
+
return changes;
|
|
3389
3398
|
}
|
|
3390
|
-
|
|
3391
|
-
|
|
3399
|
+
catch (err) {
|
|
3400
|
+
throw new Error(`ImportJson: ${err}`);
|
|
3392
3401
|
}
|
|
3393
3402
|
}
|
|
3394
3403
|
async exportJson(mode) {
|
|
3395
3404
|
const inJson = {};
|
|
3396
|
-
inJson.database = this.
|
|
3397
|
-
inJson.version = this.
|
|
3405
|
+
inJson.database = this.dbName.slice(0, -9);
|
|
3406
|
+
inJson.version = this.version;
|
|
3398
3407
|
inJson.encrypted = false;
|
|
3399
3408
|
inJson.mode = mode;
|
|
3400
|
-
|
|
3401
|
-
|
|
3402
|
-
|
|
3403
|
-
|
|
3404
|
-
|
|
3405
|
-
|
|
3406
|
-
}
|
|
3407
|
-
else {
|
|
3408
|
-
return Promise.reject(`ExportJson: retJson not valid`);
|
|
3409
|
-
}
|
|
3409
|
+
this.ensureDatabaseIsOpen();
|
|
3410
|
+
try {
|
|
3411
|
+
const jsonResult = await this.exportToJsonUtil.createExportObject(this.database, inJson);
|
|
3412
|
+
const isValid = this.jsonUtil.isJsonSQLite(jsonResult);
|
|
3413
|
+
if (isValid) {
|
|
3414
|
+
return jsonResult;
|
|
3410
3415
|
}
|
|
3411
|
-
|
|
3412
|
-
|
|
3416
|
+
else {
|
|
3417
|
+
throw new Error(`ExportJson: retJson not valid`);
|
|
3413
3418
|
}
|
|
3414
3419
|
}
|
|
3415
|
-
|
|
3416
|
-
|
|
3420
|
+
catch (err) {
|
|
3421
|
+
throw new Error(`ExportJson: ${err}`);
|
|
3422
|
+
}
|
|
3423
|
+
}
|
|
3424
|
+
/**
|
|
3425
|
+
* Throws an error if `this._isDbOpen` is `false`.
|
|
3426
|
+
*/
|
|
3427
|
+
ensureDatabaseIsOpen() {
|
|
3428
|
+
if (!this._isDbOpen || !this.database) {
|
|
3429
|
+
throw new Error(`getVersion: Database ${this.dbName} is not open yet. You should open it first.`);
|
|
3417
3430
|
}
|
|
3418
3431
|
}
|
|
3419
3432
|
}
|
|
@@ -3426,49 +3439,15 @@ const utilsJson_1 = utilsJson;
|
|
|
3426
3439
|
const utilsFile_1 = utilsFile;
|
|
3427
3440
|
class CapacitorSQLite {
|
|
3428
3441
|
constructor() {
|
|
3429
|
-
this.
|
|
3430
|
-
this.
|
|
3431
|
-
this.
|
|
3432
|
-
this.
|
|
3433
|
-
}
|
|
3434
|
-
async initWebStore() {
|
|
3435
|
-
return Promise.reject('Method not implemented.');
|
|
3436
|
-
}
|
|
3437
|
-
async saveToStore(options) {
|
|
3438
|
-
console.log(`${JSON.stringify(options)}`);
|
|
3439
|
-
return Promise.reject('Method not implemented.');
|
|
3440
|
-
}
|
|
3441
|
-
async isSecretStored() {
|
|
3442
|
-
return Promise.reject('Method not implemented.');
|
|
3443
|
-
}
|
|
3444
|
-
async setEncryptionSecret(options) {
|
|
3445
|
-
console.log(`${JSON.stringify(options)}`);
|
|
3446
|
-
return Promise.reject('Method not implemented.');
|
|
3447
|
-
}
|
|
3448
|
-
async changeEncryptionSecret(options) {
|
|
3449
|
-
console.log(`${JSON.stringify(options)}`);
|
|
3450
|
-
return Promise.reject('Method not implemented.');
|
|
3451
|
-
}
|
|
3452
|
-
async getNCDatabasePath(options) {
|
|
3453
|
-
console.log('getNCDatabasePath', options);
|
|
3454
|
-
return Promise.reject('Method not implemented.');
|
|
3455
|
-
}
|
|
3456
|
-
async createNCConnection(options) {
|
|
3457
|
-
console.log('createNCConnection', options);
|
|
3458
|
-
return Promise.reject('Method not implemented.');
|
|
3459
|
-
}
|
|
3460
|
-
async closeNCConnection(options) {
|
|
3461
|
-
console.log('closeNCConnection', options);
|
|
3462
|
-
return Promise.reject('Method not implemented.');
|
|
3463
|
-
}
|
|
3464
|
-
async isNCDatabase(options) {
|
|
3465
|
-
console.log('isNCDatabase', options);
|
|
3466
|
-
return Promise.reject('Method not implemented.');
|
|
3442
|
+
this.versionUpgrades = {};
|
|
3443
|
+
this.databases = {};
|
|
3444
|
+
this.fileUtil = new utilsFile_1.UtilsFile();
|
|
3445
|
+
this.jsonUtil = new utilsJson_1.UtilsJson();
|
|
3467
3446
|
}
|
|
3468
3447
|
async createConnection(options) {
|
|
3469
|
-
const
|
|
3470
|
-
if (!
|
|
3471
|
-
|
|
3448
|
+
const optionKeys = Object.keys(options);
|
|
3449
|
+
if (!optionKeys.includes('database')) {
|
|
3450
|
+
throw new Error('Must provide a database name');
|
|
3472
3451
|
}
|
|
3473
3452
|
const dbName = options.database;
|
|
3474
3453
|
const version = options.version ? options.version : 1;
|
|
@@ -3484,612 +3463,381 @@ class CapacitorSQLite {
|
|
|
3484
3463
|
? options.mode
|
|
3485
3464
|
: 'no-encryption';
|
|
3486
3465
|
*/
|
|
3487
|
-
let
|
|
3488
|
-
const
|
|
3489
|
-
if (
|
|
3490
|
-
|
|
3491
|
-
}
|
|
3492
|
-
|
|
3493
|
-
|
|
3494
|
-
|
|
3495
|
-
|
|
3496
|
-
|
|
3497
|
-
|
|
3498
|
-
|
|
3499
|
-
return Promise.resolve();
|
|
3500
|
-
}
|
|
3501
|
-
catch (err) {
|
|
3502
|
-
return Promise.reject(err);
|
|
3503
|
-
}
|
|
3466
|
+
let upgrades = {};
|
|
3467
|
+
const versionUpgradeKeys = Object.keys(this.versionUpgrades);
|
|
3468
|
+
if (versionUpgradeKeys.length !== 0 && versionUpgradeKeys.includes(dbName)) {
|
|
3469
|
+
upgrades = this.versionUpgrades[dbName];
|
|
3470
|
+
}
|
|
3471
|
+
const databaseConnection = new Database_1.Database(dbName + 'SQLite.db',
|
|
3472
|
+
/* encrypted,
|
|
3473
|
+
inMode,
|
|
3474
|
+
*/
|
|
3475
|
+
version, upgrades);
|
|
3476
|
+
this.databases[dbName] = databaseConnection;
|
|
3477
|
+
return;
|
|
3504
3478
|
}
|
|
3505
3479
|
async closeConnection(options) {
|
|
3506
|
-
|
|
3507
|
-
|
|
3508
|
-
|
|
3509
|
-
}
|
|
3510
|
-
const dbName = options.database;
|
|
3511
|
-
keys = Object.keys(this._dbDict);
|
|
3512
|
-
if (!keys.includes(dbName)) {
|
|
3513
|
-
return Promise.resolve();
|
|
3514
|
-
}
|
|
3515
|
-
const mDB = this._dbDict[dbName];
|
|
3516
|
-
if (mDB.isDBOpen()) {
|
|
3480
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3481
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
3482
|
+
if (database.isDBOpen()) {
|
|
3517
3483
|
// close the database
|
|
3518
3484
|
try {
|
|
3519
|
-
await
|
|
3485
|
+
await database.close();
|
|
3520
3486
|
}
|
|
3521
3487
|
catch (err) {
|
|
3522
|
-
|
|
3523
|
-
'close ' +
|
|
3524
|
-
dbName +
|
|
3525
|
-
' failed ' +
|
|
3526
|
-
err);
|
|
3488
|
+
throw new Error(`CloseConnection command failed: close ${dbName} failed ${err.message}`);
|
|
3527
3489
|
}
|
|
3528
3490
|
}
|
|
3529
3491
|
// remove the connection from dictionary
|
|
3530
|
-
delete this.
|
|
3531
|
-
return Promise.resolve();
|
|
3492
|
+
delete this.databases[dbName];
|
|
3532
3493
|
}
|
|
3533
3494
|
async echo(options) {
|
|
3534
|
-
const
|
|
3535
|
-
|
|
3536
|
-
|
|
3537
|
-
|
|
3538
|
-
const ret = {};
|
|
3539
|
-
ret.value = options.value;
|
|
3540
|
-
return Promise.resolve(ret);
|
|
3495
|
+
const echoValue = this.getOptionValue(options, 'value');
|
|
3496
|
+
const echoResult = {};
|
|
3497
|
+
echoResult.value = echoValue;
|
|
3498
|
+
return echoResult;
|
|
3541
3499
|
}
|
|
3542
3500
|
async open(options) {
|
|
3543
|
-
|
|
3544
|
-
|
|
3545
|
-
return Promise.reject('Must provide a database name');
|
|
3546
|
-
}
|
|
3547
|
-
const dbName = options.database;
|
|
3548
|
-
keys = Object.keys(this._dbDict);
|
|
3549
|
-
if (!keys.includes(dbName)) {
|
|
3550
|
-
return Promise.reject(`Open: No available connection for ${dbName}`);
|
|
3551
|
-
}
|
|
3552
|
-
const mDB = this._dbDict[dbName];
|
|
3501
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3502
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
3553
3503
|
try {
|
|
3554
|
-
await
|
|
3555
|
-
return
|
|
3504
|
+
await database.open();
|
|
3505
|
+
return;
|
|
3556
3506
|
}
|
|
3557
3507
|
catch (err) {
|
|
3558
|
-
|
|
3508
|
+
throw new Error(`Open: ${err}`);
|
|
3559
3509
|
}
|
|
3560
3510
|
}
|
|
3561
3511
|
async close(options) {
|
|
3562
|
-
|
|
3563
|
-
|
|
3564
|
-
return Promise.reject('Must provide a database name');
|
|
3565
|
-
}
|
|
3566
|
-
const dbName = options.database;
|
|
3567
|
-
keys = Object.keys(this._dbDict);
|
|
3568
|
-
if (!keys.includes(dbName)) {
|
|
3569
|
-
return Promise.reject(`Close: No available connection for ${dbName}`);
|
|
3570
|
-
}
|
|
3571
|
-
const mDB = this._dbDict[dbName];
|
|
3512
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3513
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
3572
3514
|
try {
|
|
3573
|
-
await
|
|
3574
|
-
return
|
|
3515
|
+
await database.close();
|
|
3516
|
+
return;
|
|
3575
3517
|
}
|
|
3576
3518
|
catch (err) {
|
|
3577
|
-
|
|
3519
|
+
throw new Error(`Close: ${err}`);
|
|
3578
3520
|
}
|
|
3579
3521
|
}
|
|
3580
|
-
async getUrl() {
|
|
3581
|
-
return Promise.reject('Method not implemented.');
|
|
3582
|
-
}
|
|
3583
3522
|
async getVersion(options) {
|
|
3584
|
-
|
|
3585
|
-
|
|
3586
|
-
return Promise.reject('Must provide a database name');
|
|
3587
|
-
}
|
|
3588
|
-
const dbName = options.database;
|
|
3589
|
-
keys = Object.keys(this._dbDict);
|
|
3590
|
-
if (!keys.includes(dbName)) {
|
|
3591
|
-
return Promise.reject(`Open: No available connection for ${dbName}`);
|
|
3592
|
-
}
|
|
3593
|
-
const mDB = this._dbDict[dbName];
|
|
3523
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3524
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
3594
3525
|
try {
|
|
3595
|
-
const version = await
|
|
3596
|
-
const
|
|
3597
|
-
|
|
3598
|
-
return
|
|
3526
|
+
const version = await database.getVersion();
|
|
3527
|
+
const versionResult = {};
|
|
3528
|
+
versionResult.version = version;
|
|
3529
|
+
return versionResult;
|
|
3599
3530
|
}
|
|
3600
3531
|
catch (err) {
|
|
3601
|
-
|
|
3532
|
+
throw new Error(`GetVersion: ${err}`);
|
|
3602
3533
|
}
|
|
3603
3534
|
}
|
|
3604
3535
|
async getTableList(options) {
|
|
3605
|
-
|
|
3606
|
-
|
|
3607
|
-
return Promise.reject('Must provide a database name');
|
|
3608
|
-
}
|
|
3609
|
-
const dbName = options.database;
|
|
3610
|
-
keys = Object.keys(this._dbDict);
|
|
3611
|
-
if (!keys.includes(dbName)) {
|
|
3612
|
-
return Promise.reject(`Open: No available connection for ${dbName}`);
|
|
3613
|
-
}
|
|
3614
|
-
const mDB = this._dbDict[dbName];
|
|
3536
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3537
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
3615
3538
|
try {
|
|
3616
|
-
const tableList = await
|
|
3617
|
-
const
|
|
3618
|
-
|
|
3619
|
-
return
|
|
3539
|
+
const tableList = await database.getTableList();
|
|
3540
|
+
const tableListResult = {};
|
|
3541
|
+
tableListResult.values = tableList;
|
|
3542
|
+
return tableListResult;
|
|
3620
3543
|
}
|
|
3621
3544
|
catch (err) {
|
|
3622
|
-
|
|
3545
|
+
throw new Error(`GetTableList: ${err}`);
|
|
3623
3546
|
}
|
|
3624
3547
|
}
|
|
3625
3548
|
async execute(options) {
|
|
3626
|
-
|
|
3627
|
-
|
|
3628
|
-
|
|
3629
|
-
|
|
3630
|
-
if (!keys.includes('statements') || options.statements.length === 0) {
|
|
3631
|
-
return Promise.reject('Must provide raw SQL statements');
|
|
3632
|
-
}
|
|
3633
|
-
const dbName = options.database;
|
|
3634
|
-
const statements = options.statements;
|
|
3635
|
-
let transaction;
|
|
3636
|
-
if (!keys.includes('transaction')) {
|
|
3637
|
-
transaction = true;
|
|
3638
|
-
}
|
|
3639
|
-
else {
|
|
3640
|
-
transaction = options.transaction;
|
|
3641
|
-
}
|
|
3642
|
-
keys = Object.keys(this._dbDict);
|
|
3643
|
-
if (!keys.includes(dbName)) {
|
|
3644
|
-
return Promise.reject(`Execute: No available connection for ${dbName}`);
|
|
3645
|
-
}
|
|
3646
|
-
const mDB = this._dbDict[dbName];
|
|
3549
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3550
|
+
const statements = this.getOptionValue(options, 'statements');
|
|
3551
|
+
const transaction = this.getOptionValue(options, 'transaction', true);
|
|
3552
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
3647
3553
|
try {
|
|
3648
|
-
const
|
|
3649
|
-
if (
|
|
3650
|
-
|
|
3554
|
+
const executeResult = await database.executeSQL(statements, transaction);
|
|
3555
|
+
if (executeResult < 0) {
|
|
3556
|
+
throw new Error('Execute failed changes < 0');
|
|
3651
3557
|
}
|
|
3652
3558
|
else {
|
|
3653
|
-
return
|
|
3559
|
+
return { changes: { changes: executeResult } };
|
|
3654
3560
|
}
|
|
3655
3561
|
}
|
|
3656
3562
|
catch (err) {
|
|
3657
|
-
|
|
3563
|
+
throw new Error(`Execute failed: ${err}`);
|
|
3658
3564
|
}
|
|
3659
3565
|
}
|
|
3660
3566
|
async executeSet(options) {
|
|
3661
|
-
|
|
3662
|
-
|
|
3663
|
-
|
|
3664
|
-
|
|
3665
|
-
if (!keys.includes('set') || options.set.length === 0) {
|
|
3666
|
-
return Promise.reject('Must provide a non-empty set of SQL statements');
|
|
3667
|
-
}
|
|
3668
|
-
const dbName = options.database;
|
|
3669
|
-
const setOfStatements = options.set;
|
|
3670
|
-
let transaction;
|
|
3671
|
-
if (!keys.includes('transaction')) {
|
|
3672
|
-
transaction = true;
|
|
3673
|
-
}
|
|
3674
|
-
else {
|
|
3675
|
-
transaction = options.transaction;
|
|
3676
|
-
}
|
|
3677
|
-
keys = Object.keys(this._dbDict);
|
|
3678
|
-
if (!keys.includes(dbName)) {
|
|
3679
|
-
return Promise.reject(`ExecuteSet: No available connection for ${dbName}`);
|
|
3680
|
-
}
|
|
3681
|
-
const mDB = this._dbDict[dbName];
|
|
3567
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3568
|
+
const setOfStatements = this.getOptionValue(options, 'set');
|
|
3569
|
+
const transaction = this.getOptionValue(options, 'transaction', true);
|
|
3570
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
3682
3571
|
for (const sStmt of setOfStatements) {
|
|
3683
3572
|
if (!('statement' in sStmt) || !('values' in sStmt)) {
|
|
3684
|
-
|
|
3573
|
+
throw new Error('ExecuteSet: Must provide a set as ' + 'Array of {statement,values}');
|
|
3685
3574
|
}
|
|
3686
3575
|
}
|
|
3687
3576
|
try {
|
|
3688
|
-
const
|
|
3689
|
-
if (
|
|
3690
|
-
|
|
3577
|
+
const execSetResult = await database.execSet(setOfStatements, transaction);
|
|
3578
|
+
if (execSetResult < 0) {
|
|
3579
|
+
throw new Error(`ExecuteSet failed changes <0`);
|
|
3691
3580
|
}
|
|
3692
3581
|
else {
|
|
3693
|
-
return
|
|
3582
|
+
return { changes: execSetResult };
|
|
3694
3583
|
}
|
|
3695
3584
|
}
|
|
3696
3585
|
catch (err) {
|
|
3697
|
-
|
|
3586
|
+
throw new Error(`ExecuteSet failed: ${err}`);
|
|
3698
3587
|
}
|
|
3699
3588
|
}
|
|
3700
3589
|
async run(options) {
|
|
3701
|
-
|
|
3702
|
-
|
|
3703
|
-
|
|
3704
|
-
|
|
3705
|
-
|
|
3706
|
-
return Promise.reject('Must provide a query statement');
|
|
3707
|
-
}
|
|
3708
|
-
if (!keys.includes('values')) {
|
|
3709
|
-
return Promise.reject('Must provide an Array of values');
|
|
3710
|
-
}
|
|
3711
|
-
const dbName = options.database;
|
|
3712
|
-
const statement = options.statement;
|
|
3713
|
-
const values = options.values.length > 0 ? options.values : [];
|
|
3714
|
-
let transaction;
|
|
3715
|
-
if (!keys.includes('transaction')) {
|
|
3716
|
-
transaction = true;
|
|
3717
|
-
}
|
|
3718
|
-
else {
|
|
3719
|
-
transaction = options.transaction;
|
|
3720
|
-
}
|
|
3721
|
-
keys = Object.keys(this._dbDict);
|
|
3722
|
-
if (!keys.includes(dbName)) {
|
|
3723
|
-
return Promise.reject(`Run: No available connection for ${dbName}`);
|
|
3724
|
-
}
|
|
3725
|
-
const mDB = this._dbDict[dbName];
|
|
3590
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3591
|
+
const statement = this.getOptionValue(options, 'statement');
|
|
3592
|
+
const values = this.getOptionValue(options, 'values', []);
|
|
3593
|
+
const transaction = this.getOptionValue(options, 'transaction', true);
|
|
3594
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
3726
3595
|
try {
|
|
3727
|
-
const
|
|
3728
|
-
return
|
|
3596
|
+
const runResult = await database.runSQL(statement, values, transaction);
|
|
3597
|
+
return { changes: runResult };
|
|
3729
3598
|
}
|
|
3730
3599
|
catch (err) {
|
|
3731
|
-
|
|
3600
|
+
throw new Error(`RUN failed: ${err} `);
|
|
3732
3601
|
}
|
|
3733
3602
|
}
|
|
3734
3603
|
async query(options) {
|
|
3735
|
-
|
|
3736
|
-
|
|
3737
|
-
|
|
3738
|
-
|
|
3739
|
-
|
|
3740
|
-
return Promise.reject('Must provide a query statement');
|
|
3741
|
-
}
|
|
3742
|
-
if (!keys.includes('values')) {
|
|
3743
|
-
return Promise.reject('Must provide an Array of any');
|
|
3604
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3605
|
+
const statement = this.getOptionValue(options, 'statement');
|
|
3606
|
+
const values = this.getOptionValue(options, 'values', []);
|
|
3607
|
+
if (statement.length === 0) {
|
|
3608
|
+
throw new Error('Statement may not be an empty string.');
|
|
3744
3609
|
}
|
|
3745
|
-
const
|
|
3746
|
-
const statement = options.statement;
|
|
3747
|
-
const values = options.values.length > 0 ? options.values : [];
|
|
3748
|
-
keys = Object.keys(this._dbDict);
|
|
3749
|
-
if (!keys.includes(dbName)) {
|
|
3750
|
-
return Promise.reject(`Query: No available connection for ${dbName}`);
|
|
3751
|
-
}
|
|
3752
|
-
const mDB = this._dbDict[dbName];
|
|
3753
|
-
let ret = [];
|
|
3610
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
3754
3611
|
try {
|
|
3755
|
-
|
|
3756
|
-
return
|
|
3612
|
+
const queryResult = await database.selectSQL(statement, values);
|
|
3613
|
+
return { values: queryResult };
|
|
3757
3614
|
}
|
|
3758
3615
|
catch (err) {
|
|
3759
|
-
|
|
3616
|
+
throw new Error(`Query failed: ${err}`);
|
|
3760
3617
|
}
|
|
3761
3618
|
}
|
|
3762
3619
|
async isDBExists(options) {
|
|
3763
|
-
|
|
3764
|
-
if
|
|
3765
|
-
|
|
3766
|
-
|
|
3767
|
-
|
|
3768
|
-
keys = Object.keys(this._dbDict);
|
|
3769
|
-
if (!keys.includes(dbName)) {
|
|
3770
|
-
return Promise.reject('IsDBExists command failed: No available ' + 'connection for ' + dbName);
|
|
3771
|
-
}
|
|
3772
|
-
const isExists = this._uFile.isFileExists(dbName + 'SQLite.db');
|
|
3773
|
-
return Promise.resolve({
|
|
3774
|
-
result: isExists,
|
|
3775
|
-
});
|
|
3620
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3621
|
+
// Throw an error, if db connection is not opened yet:
|
|
3622
|
+
this.getDatabaseConnectionOrThrowError(dbName);
|
|
3623
|
+
const isExists = this.fileUtil.isFileExists(dbName + 'SQLite.db');
|
|
3624
|
+
return { result: isExists };
|
|
3776
3625
|
}
|
|
3777
3626
|
async isDBOpen(options) {
|
|
3778
|
-
|
|
3779
|
-
|
|
3780
|
-
|
|
3781
|
-
}
|
|
3782
|
-
const dbName = options.database;
|
|
3783
|
-
keys = Object.keys(this._dbDict);
|
|
3784
|
-
if (!keys.includes(dbName)) {
|
|
3785
|
-
return Promise.reject('isDBOpen command failed: No available ' + 'connection for ' + dbName);
|
|
3786
|
-
}
|
|
3787
|
-
const mDB = this._dbDict[dbName];
|
|
3788
|
-
const isOpen = await mDB.isDBOpen();
|
|
3789
|
-
return Promise.resolve({ result: isOpen });
|
|
3627
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3628
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
3629
|
+
const isOpen = await database.isDBOpen();
|
|
3630
|
+
return { result: isOpen };
|
|
3790
3631
|
}
|
|
3791
3632
|
async isDatabase(options) {
|
|
3792
|
-
const
|
|
3793
|
-
|
|
3794
|
-
|
|
3795
|
-
}
|
|
3796
|
-
const dbName = options.database;
|
|
3797
|
-
const isExists = this._uFile.isFileExists(dbName + 'SQLite.db');
|
|
3798
|
-
return Promise.resolve({
|
|
3799
|
-
result: isExists,
|
|
3800
|
-
});
|
|
3633
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3634
|
+
const isExists = this.fileUtil.isFileExists(dbName + 'SQLite.db');
|
|
3635
|
+
return { result: isExists };
|
|
3801
3636
|
}
|
|
3802
3637
|
async isTableExists(options) {
|
|
3803
|
-
|
|
3804
|
-
|
|
3805
|
-
|
|
3806
|
-
}
|
|
3807
|
-
const dbName = options.database;
|
|
3808
|
-
if (!keys.includes('table')) {
|
|
3809
|
-
return Promise.reject('Must provide a table name');
|
|
3810
|
-
}
|
|
3811
|
-
const tableName = options.table;
|
|
3812
|
-
keys = Object.keys(this._dbDict);
|
|
3813
|
-
if (!keys.includes(dbName)) {
|
|
3814
|
-
return Promise.reject('isTableExists command failed: No available ' +
|
|
3815
|
-
'connection for ' +
|
|
3816
|
-
dbName);
|
|
3817
|
-
}
|
|
3818
|
-
const mDB = this._dbDict[dbName];
|
|
3638
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3639
|
+
const tableName = this.getOptionValue(options, 'table');
|
|
3640
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
3819
3641
|
try {
|
|
3820
|
-
const
|
|
3821
|
-
return
|
|
3642
|
+
const isTableExistsResult = await database.isTableExists(tableName);
|
|
3643
|
+
return { result: isTableExistsResult };
|
|
3822
3644
|
}
|
|
3823
3645
|
catch (err) {
|
|
3824
|
-
|
|
3646
|
+
throw new Error(`isTableExists: ${err}`);
|
|
3825
3647
|
}
|
|
3826
3648
|
}
|
|
3827
3649
|
async deleteDatabase(options) {
|
|
3828
|
-
|
|
3829
|
-
|
|
3830
|
-
return Promise.reject('Must provide a database name');
|
|
3831
|
-
}
|
|
3832
|
-
const dbName = options.database;
|
|
3833
|
-
keys = Object.keys(this._dbDict);
|
|
3834
|
-
if (!keys.includes(dbName)) {
|
|
3835
|
-
return Promise.reject('deleteDatabase: No available connection for ' + `${dbName}`);
|
|
3836
|
-
}
|
|
3837
|
-
const mDB = this._dbDict[dbName];
|
|
3650
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3651
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
3838
3652
|
try {
|
|
3839
|
-
await
|
|
3840
|
-
return
|
|
3653
|
+
await database.deleteDB(dbName + 'SQLite.db');
|
|
3654
|
+
return;
|
|
3841
3655
|
}
|
|
3842
3656
|
catch (err) {
|
|
3843
|
-
|
|
3657
|
+
throw new Error(`Delete: ${err}`);
|
|
3844
3658
|
}
|
|
3845
3659
|
}
|
|
3846
3660
|
async isJsonValid(options) {
|
|
3847
|
-
const
|
|
3848
|
-
|
|
3849
|
-
|
|
3850
|
-
}
|
|
3851
|
-
const jsonStrObj = options.jsonstring;
|
|
3852
|
-
const jsonObj = JSON.parse(jsonStrObj);
|
|
3853
|
-
const isValid = this._uJson.isJsonSQLite(jsonObj);
|
|
3661
|
+
const jsonString = this.getOptionValue(options, 'jsonstring');
|
|
3662
|
+
const jsonObj = JSON.parse(jsonString);
|
|
3663
|
+
const isValid = this.jsonUtil.isJsonSQLite(jsonObj);
|
|
3854
3664
|
if (!isValid) {
|
|
3855
|
-
|
|
3665
|
+
throw new Error('Stringify Json Object not Valid');
|
|
3856
3666
|
}
|
|
3857
3667
|
else {
|
|
3858
|
-
return
|
|
3668
|
+
return { result: true };
|
|
3859
3669
|
}
|
|
3860
3670
|
}
|
|
3861
3671
|
async importFromJson(options) {
|
|
3862
3672
|
var _a, _b;
|
|
3863
|
-
const
|
|
3864
|
-
|
|
3865
|
-
|
|
3866
|
-
}
|
|
3867
|
-
const jsonStrObj = options.jsonstring;
|
|
3868
|
-
const jsonObj = JSON.parse(jsonStrObj);
|
|
3869
|
-
const isValid = this._uJson.isJsonSQLite(jsonObj);
|
|
3673
|
+
const jsonString = this.getOptionValue(options, 'jsonstring');
|
|
3674
|
+
const jsonObj = JSON.parse(jsonString);
|
|
3675
|
+
const isValid = this.jsonUtil.isJsonSQLite(jsonObj);
|
|
3870
3676
|
if (!isValid) {
|
|
3871
|
-
|
|
3677
|
+
throw new Error('Must provide a valid JsonSQLite Object');
|
|
3872
3678
|
}
|
|
3873
3679
|
const vJsonObj = jsonObj;
|
|
3874
3680
|
const dbName = `${vJsonObj.database}SQLite.db`;
|
|
3875
|
-
const
|
|
3681
|
+
const targetDbVersion = (_a = vJsonObj.version) !== null && _a !== void 0 ? _a : 1;
|
|
3876
3682
|
const mode = vJsonObj.mode;
|
|
3877
3683
|
const overwrite = (_b = vJsonObj.overwrite) !== null && _b !== void 0 ? _b : false;
|
|
3878
3684
|
// const encrypted: boolean = vJsonObj.encrypted ?? false;
|
|
3879
3685
|
// const mode: string = encrypted ? 'secret' : 'no-encryption';
|
|
3880
3686
|
// Create the database
|
|
3881
|
-
const
|
|
3882
|
-
/*encrypted, mode, */
|
|
3687
|
+
const database = new Database_1.Database(dbName,
|
|
3688
|
+
/*encrypted, mode, */
|
|
3689
|
+
targetDbVersion, {});
|
|
3883
3690
|
try {
|
|
3884
3691
|
if (overwrite && mode === 'full') {
|
|
3885
|
-
const isExists = this.
|
|
3692
|
+
const isExists = this.fileUtil.isFileExists(dbName);
|
|
3886
3693
|
if (isExists) {
|
|
3887
|
-
await this.
|
|
3694
|
+
await this.fileUtil.deleteFileName(dbName);
|
|
3888
3695
|
}
|
|
3889
3696
|
}
|
|
3890
3697
|
// Open the database
|
|
3891
|
-
await
|
|
3892
|
-
const tableList = await
|
|
3698
|
+
await database.open();
|
|
3699
|
+
const tableList = await database.getTableList();
|
|
3893
3700
|
if (mode === 'full' && tableList.length > 0) {
|
|
3894
|
-
const
|
|
3895
|
-
if (
|
|
3896
|
-
|
|
3701
|
+
const currentVersion = await database.getVersion();
|
|
3702
|
+
if (targetDbVersion < currentVersion) {
|
|
3703
|
+
throw new Error(`ImportFromJson: Cannot import a version lower than ${currentVersion}`);
|
|
3897
3704
|
}
|
|
3898
|
-
if (
|
|
3899
|
-
return
|
|
3705
|
+
if (currentVersion === targetDbVersion) {
|
|
3706
|
+
return { changes: { changes: 0 } };
|
|
3900
3707
|
}
|
|
3901
3708
|
}
|
|
3902
3709
|
// Import the JsonSQLite Object
|
|
3903
|
-
const changes = await
|
|
3710
|
+
const changes = await database.importJson(vJsonObj);
|
|
3904
3711
|
// Close the database
|
|
3905
|
-
await
|
|
3906
|
-
return
|
|
3712
|
+
await database.close();
|
|
3713
|
+
return { changes: { changes: changes } };
|
|
3907
3714
|
}
|
|
3908
3715
|
catch (err) {
|
|
3909
|
-
|
|
3716
|
+
throw new Error(`ImportFromJson: ${err}`);
|
|
3910
3717
|
}
|
|
3911
3718
|
}
|
|
3912
3719
|
async exportToJson(options) {
|
|
3913
|
-
|
|
3914
|
-
|
|
3915
|
-
|
|
3916
|
-
}
|
|
3917
|
-
if (!keys.includes('jsonexportmode')) {
|
|
3918
|
-
return Promise.reject('Must provide a json export mode');
|
|
3919
|
-
}
|
|
3920
|
-
const dbName = options.database;
|
|
3921
|
-
const exportMode = options.jsonexportmode;
|
|
3922
|
-
keys = Object.keys(this._dbDict);
|
|
3923
|
-
if (!keys.includes(dbName)) {
|
|
3924
|
-
return Promise.reject('exportToJson: No available connection for ' + `${dbName}`);
|
|
3925
|
-
}
|
|
3926
|
-
const mDB = this._dbDict[dbName];
|
|
3720
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3721
|
+
const exportMode = this.getOptionValue(options, 'jsonexportmode');
|
|
3722
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
3927
3723
|
try {
|
|
3928
|
-
const
|
|
3929
|
-
const
|
|
3930
|
-
if (
|
|
3931
|
-
|
|
3724
|
+
const exportJsonResult = await database.exportJson(exportMode);
|
|
3725
|
+
const resultKeys = Object.keys(exportJsonResult);
|
|
3726
|
+
if (resultKeys.includes('message')) {
|
|
3727
|
+
throw new Error(`exportToJson: ${exportJsonResult.message}`);
|
|
3932
3728
|
}
|
|
3933
3729
|
else {
|
|
3934
|
-
return
|
|
3730
|
+
return { export: exportJsonResult };
|
|
3935
3731
|
}
|
|
3936
3732
|
}
|
|
3937
3733
|
catch (err) {
|
|
3938
|
-
|
|
3734
|
+
throw new Error(`exportToJson: ${err}`);
|
|
3939
3735
|
}
|
|
3940
3736
|
}
|
|
3941
3737
|
async createSyncTable(options) {
|
|
3942
|
-
|
|
3943
|
-
|
|
3944
|
-
return Promise.reject('Must provide a database name');
|
|
3945
|
-
}
|
|
3946
|
-
const dbName = options.database;
|
|
3947
|
-
keys = Object.keys(this._dbDict);
|
|
3948
|
-
if (!keys.includes(dbName)) {
|
|
3949
|
-
return Promise.reject('CreateSyncTable: No available connection for ' + `${dbName}`);
|
|
3950
|
-
}
|
|
3951
|
-
const mDB = this._dbDict[dbName];
|
|
3738
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3739
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
3952
3740
|
try {
|
|
3953
|
-
const
|
|
3954
|
-
return
|
|
3741
|
+
const createTableSyncResult = await database.createSyncTable();
|
|
3742
|
+
return {
|
|
3743
|
+
changes: { changes: createTableSyncResult }
|
|
3744
|
+
};
|
|
3955
3745
|
}
|
|
3956
3746
|
catch (err) {
|
|
3957
|
-
|
|
3747
|
+
throw new Error(`createSyncTable: ${err}`);
|
|
3958
3748
|
}
|
|
3959
3749
|
}
|
|
3960
3750
|
async setSyncDate(options) {
|
|
3961
|
-
|
|
3962
|
-
|
|
3963
|
-
|
|
3964
|
-
}
|
|
3965
|
-
if (!keys.includes('syncdate')) {
|
|
3966
|
-
return Promise.reject('Must provide a synchronization date');
|
|
3967
|
-
}
|
|
3968
|
-
const dbName = options.database;
|
|
3969
|
-
const syncDate = options.syncdate;
|
|
3970
|
-
keys = Object.keys(this._dbDict);
|
|
3971
|
-
if (!keys.includes(dbName)) {
|
|
3972
|
-
return Promise.reject(`SetSyncDate: No available connection for ${dbName}`);
|
|
3973
|
-
}
|
|
3974
|
-
const mDB = this._dbDict[dbName];
|
|
3751
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3752
|
+
const syncDate = this.getOptionValue(options, 'syncdate');
|
|
3753
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
3975
3754
|
try {
|
|
3976
|
-
await
|
|
3977
|
-
return
|
|
3755
|
+
await database.setSyncDate(syncDate);
|
|
3756
|
+
return;
|
|
3978
3757
|
}
|
|
3979
3758
|
catch (err) {
|
|
3980
|
-
|
|
3759
|
+
throw new Error(`SetSyncDate: ${err}`);
|
|
3981
3760
|
}
|
|
3982
3761
|
}
|
|
3983
3762
|
async getSyncDate(options) {
|
|
3984
|
-
|
|
3985
|
-
|
|
3986
|
-
return Promise.reject('Must provide a database name');
|
|
3987
|
-
}
|
|
3988
|
-
const dbName = options.database;
|
|
3989
|
-
keys = Object.keys(this._dbDict);
|
|
3990
|
-
if (!keys.includes(dbName)) {
|
|
3991
|
-
return Promise.reject(`GetSyncDate: No available connection for ${dbName}`);
|
|
3992
|
-
}
|
|
3993
|
-
const mDB = this._dbDict[dbName];
|
|
3763
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3764
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
3994
3765
|
try {
|
|
3995
|
-
const ret = await
|
|
3766
|
+
const ret = await database.getSyncDate();
|
|
3996
3767
|
return Promise.resolve(ret);
|
|
3997
3768
|
}
|
|
3998
3769
|
catch (err) {
|
|
3999
|
-
|
|
3770
|
+
throw new Error(`GetSyncDate: ${err}`);
|
|
4000
3771
|
}
|
|
4001
3772
|
}
|
|
4002
3773
|
async addUpgradeStatement(options) {
|
|
4003
|
-
|
|
4004
|
-
|
|
4005
|
-
|
|
4006
|
-
|
|
4007
|
-
if (!
|
|
4008
|
-
|
|
4009
|
-
|
|
4010
|
-
|
|
4011
|
-
|
|
4012
|
-
|
|
4013
|
-
|
|
4014
|
-
|
|
4015
|
-
|
|
4016
|
-
|
|
4017
|
-
|
|
4018
|
-
|
|
4019
|
-
return Promise.reject('ugrade.fromVersion must be a number');
|
|
4020
|
-
}
|
|
4021
|
-
const upgVDict = {};
|
|
4022
|
-
upgVDict[upgrade.fromVersion] = upgrade;
|
|
4023
|
-
this._versionUpgrades[dbName] = upgVDict;
|
|
4024
|
-
return Promise.resolve();
|
|
3774
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3775
|
+
const upgrades = this.getOptionValue(options, 'upgrade');
|
|
3776
|
+
const firstUpgrade = upgrades[0];
|
|
3777
|
+
const versionUpgradeKeys = Object.keys(firstUpgrade);
|
|
3778
|
+
if (!versionUpgradeKeys.includes('fromVersion') ||
|
|
3779
|
+
!versionUpgradeKeys.includes('toVersion') ||
|
|
3780
|
+
!versionUpgradeKeys.includes('statement')) {
|
|
3781
|
+
throw new Error('Must provide an upgrade capSQLiteVersionUpgrade Object');
|
|
3782
|
+
}
|
|
3783
|
+
if (typeof firstUpgrade.fromVersion != 'number') {
|
|
3784
|
+
throw new Error('upgrade.fromVersion must be a number');
|
|
3785
|
+
}
|
|
3786
|
+
const upgradeVersionDict = {};
|
|
3787
|
+
upgradeVersionDict[firstUpgrade.fromVersion] = firstUpgrade;
|
|
3788
|
+
this.versionUpgrades[dbName] = upgradeVersionDict;
|
|
3789
|
+
return;
|
|
4025
3790
|
}
|
|
4026
3791
|
async copyFromAssets(options) {
|
|
4027
|
-
const
|
|
4028
|
-
const mOverwrite = keys.includes('overwrite') ? options.overwrite : true;
|
|
3792
|
+
const overwrite = this.getOptionValue(options, 'overwrite', false);
|
|
4029
3793
|
// check if the assets/database folder exists
|
|
4030
|
-
const assetsDbPath = this.
|
|
4031
|
-
const
|
|
4032
|
-
if (
|
|
3794
|
+
const assetsDbPath = this.fileUtil.getAssetsDatabasesPath();
|
|
3795
|
+
const pathExists = this.fileUtil.isPathExists(assetsDbPath);
|
|
3796
|
+
if (pathExists) {
|
|
4033
3797
|
// get the database files
|
|
4034
|
-
const dbList = await this.
|
|
3798
|
+
const dbList = await this.fileUtil.getFileList(assetsDbPath);
|
|
4035
3799
|
// loop through the database files
|
|
4036
3800
|
dbList.forEach(async (db) => {
|
|
4037
3801
|
if (db.substring(db.length - 3) === '.db') {
|
|
4038
3802
|
// for each copy the file to the Application database folder
|
|
4039
|
-
await this.
|
|
3803
|
+
await this.fileUtil.copyFromAssetToDatabase(db, overwrite);
|
|
4040
3804
|
}
|
|
4041
3805
|
if (db.substring(db.length - 4) === '.zip') {
|
|
4042
|
-
await this.
|
|
3806
|
+
await this.fileUtil.unzipDatabase(db, overwrite);
|
|
4043
3807
|
}
|
|
4044
3808
|
});
|
|
4045
|
-
return
|
|
3809
|
+
return;
|
|
4046
3810
|
}
|
|
4047
3811
|
else {
|
|
4048
|
-
|
|
3812
|
+
throw new Error('CopyFromAssets: assets/databases folder does not exist');
|
|
4049
3813
|
}
|
|
4050
3814
|
}
|
|
4051
3815
|
async getDatabaseList() {
|
|
4052
3816
|
// get the database folder
|
|
4053
|
-
const pathDatabase = this.
|
|
3817
|
+
const pathDatabase = this.fileUtil.getDatabasesPath();
|
|
4054
3818
|
// get the list of databases
|
|
4055
|
-
const files = await this.
|
|
3819
|
+
const files = await this.fileUtil.getFileList(pathDatabase);
|
|
4056
3820
|
if (files.length > 0) {
|
|
4057
|
-
return
|
|
3821
|
+
return { values: files };
|
|
4058
3822
|
}
|
|
4059
3823
|
else {
|
|
4060
|
-
|
|
3824
|
+
throw new Error(`isTableExists: No databases found`);
|
|
4061
3825
|
}
|
|
4062
3826
|
}
|
|
4063
|
-
async getMigratableDbList(options) {
|
|
4064
|
-
console.log('getCordovaDbList', options);
|
|
4065
|
-
return Promise.reject('Method not implemented.');
|
|
4066
|
-
}
|
|
4067
|
-
async addSQLiteSuffix(options) {
|
|
4068
|
-
console.log(`${JSON.stringify(options)}`);
|
|
4069
|
-
throw new Error('Method not implemented.');
|
|
4070
|
-
}
|
|
4071
|
-
async deleteOldDatabases(options) {
|
|
4072
|
-
console.log(`${JSON.stringify(options)}`);
|
|
4073
|
-
throw new Error('Method not implemented.');
|
|
4074
|
-
}
|
|
4075
3827
|
async checkConnectionsConsistency(options) {
|
|
4076
|
-
const
|
|
4077
|
-
|
|
4078
|
-
|
|
4079
|
-
}
|
|
4080
|
-
const dbNames = options.dbNames;
|
|
4081
|
-
const ret = {};
|
|
4082
|
-
ret.result = false;
|
|
3828
|
+
const dbNames = this.getOptionValue(options, 'dbNames');
|
|
3829
|
+
const checkConsistencyResult = {};
|
|
3830
|
+
checkConsistencyResult.result = false;
|
|
4083
3831
|
try {
|
|
4084
|
-
let inConnectionsSet = new Set(Object.keys(this.
|
|
3832
|
+
let inConnectionsSet = new Set(Object.keys(this.databases));
|
|
4085
3833
|
const outConnectionSet = new Set(dbNames);
|
|
4086
3834
|
if (outConnectionSet.size === 0) {
|
|
4087
|
-
await this.resetDbDict(Object.keys(this.
|
|
4088
|
-
return Promise.resolve(
|
|
3835
|
+
await this.resetDbDict(Object.keys(this.databases));
|
|
3836
|
+
return Promise.resolve(checkConsistencyResult);
|
|
4089
3837
|
}
|
|
4090
3838
|
if (inConnectionsSet.size < outConnectionSet.size) {
|
|
4091
|
-
await this.resetDbDict(Object.keys(this.
|
|
4092
|
-
return Promise.resolve(
|
|
3839
|
+
await this.resetDbDict(Object.keys(this.databases));
|
|
3840
|
+
return Promise.resolve(checkConsistencyResult);
|
|
4093
3841
|
}
|
|
4094
3842
|
if (inConnectionsSet.size > outConnectionSet.size) {
|
|
4095
3843
|
for (const key of inConnectionsSet) {
|
|
@@ -4100,25 +3848,25 @@ class CapacitorSQLite {
|
|
|
4100
3848
|
}
|
|
4101
3849
|
}
|
|
4102
3850
|
}
|
|
4103
|
-
inConnectionsSet = new Set(Object.keys(this.
|
|
3851
|
+
inConnectionsSet = new Set(Object.keys(this.databases));
|
|
4104
3852
|
if (inConnectionsSet.size === outConnectionSet.size) {
|
|
4105
|
-
const
|
|
4106
|
-
if (
|
|
4107
|
-
|
|
4108
|
-
return
|
|
3853
|
+
const symmetricDifferenceSet = await this.symmetricDifference(inConnectionsSet, outConnectionSet);
|
|
3854
|
+
if (symmetricDifferenceSet.size === 0) {
|
|
3855
|
+
checkConsistencyResult.result = true;
|
|
3856
|
+
return checkConsistencyResult;
|
|
4109
3857
|
}
|
|
4110
3858
|
else {
|
|
4111
|
-
await this.resetDbDict(Object.keys(this.
|
|
4112
|
-
return
|
|
3859
|
+
await this.resetDbDict(Object.keys(this.databases));
|
|
3860
|
+
return checkConsistencyResult;
|
|
4113
3861
|
}
|
|
4114
3862
|
}
|
|
4115
3863
|
else {
|
|
4116
|
-
await this.resetDbDict(Object.keys(this.
|
|
4117
|
-
return
|
|
3864
|
+
await this.resetDbDict(Object.keys(this.databases));
|
|
3865
|
+
return checkConsistencyResult;
|
|
4118
3866
|
}
|
|
4119
3867
|
}
|
|
4120
3868
|
catch (err) {
|
|
4121
|
-
|
|
3869
|
+
throw new Error(`CheckConnectionsConsistency: ${err}`);
|
|
4122
3870
|
}
|
|
4123
3871
|
}
|
|
4124
3872
|
async resetDbDict(keys) {
|
|
@@ -4130,7 +3878,7 @@ class CapacitorSQLite {
|
|
|
4130
3878
|
}
|
|
4131
3879
|
}
|
|
4132
3880
|
catch (err) {
|
|
4133
|
-
|
|
3881
|
+
throw new Error(`ResetDbDict: ${err}`);
|
|
4134
3882
|
}
|
|
4135
3883
|
}
|
|
4136
3884
|
async symmetricDifference(setA, setB) {
|
|
@@ -4143,7 +3891,95 @@ class CapacitorSQLite {
|
|
|
4143
3891
|
difference.add(elem);
|
|
4144
3892
|
}
|
|
4145
3893
|
}
|
|
4146
|
-
return
|
|
3894
|
+
return difference;
|
|
3895
|
+
}
|
|
3896
|
+
/**
|
|
3897
|
+
* Returns a database connection, if it already exists.
|
|
3898
|
+
* If the conneciton does not exist yet, it throws an error.
|
|
3899
|
+
*
|
|
3900
|
+
* @param dbName
|
|
3901
|
+
* @returns
|
|
3902
|
+
*/
|
|
3903
|
+
getDatabaseConnectionOrThrowError(dbName) {
|
|
3904
|
+
const databaseNames = Object.keys(this.databases);
|
|
3905
|
+
if (!databaseNames.includes(dbName)) {
|
|
3906
|
+
throw new Error(`No connection available for database "${dbName}"`);
|
|
3907
|
+
}
|
|
3908
|
+
return this.databases[dbName];
|
|
3909
|
+
}
|
|
3910
|
+
/**
|
|
3911
|
+
* Gets the value of an option from the options object.
|
|
3912
|
+
* If the `optionKey` does not exist and there is no `defaultValue` defined, an exception is thrown.
|
|
3913
|
+
* If the `optionKey` does not exist but there is a `defaultValue`, the `defaultValue` is returned.
|
|
3914
|
+
*
|
|
3915
|
+
* @param options
|
|
3916
|
+
* @param optionKey
|
|
3917
|
+
* @param defaultValue
|
|
3918
|
+
* @returns
|
|
3919
|
+
*/
|
|
3920
|
+
getOptionValue(options, optionKey, defaultValue = undefined) {
|
|
3921
|
+
const optionKeys = Object.keys(options);
|
|
3922
|
+
if (!optionKeys.includes(optionKey)) {
|
|
3923
|
+
if (defaultValue === undefined) {
|
|
3924
|
+
throw new Error(`Must provide "${optionKey}" in options.`);
|
|
3925
|
+
}
|
|
3926
|
+
else {
|
|
3927
|
+
return defaultValue;
|
|
3928
|
+
}
|
|
3929
|
+
}
|
|
3930
|
+
return options[optionKey];
|
|
3931
|
+
}
|
|
3932
|
+
////////////////////////////////
|
|
3933
|
+
//// UNIMPLEMENTED MTEHODS
|
|
3934
|
+
////////////////////////////////
|
|
3935
|
+
async getMigratableDbList(options) {
|
|
3936
|
+
console.log('getCordovaDbList', options);
|
|
3937
|
+
throw new Error('Method not implemented.');
|
|
3938
|
+
}
|
|
3939
|
+
async addSQLiteSuffix(options) {
|
|
3940
|
+
console.log(`${JSON.stringify(options)}`);
|
|
3941
|
+
throw new Error('Method not implemented.');
|
|
3942
|
+
}
|
|
3943
|
+
async deleteOldDatabases(options) {
|
|
3944
|
+
console.log(`${JSON.stringify(options)}`);
|
|
3945
|
+
throw new Error('Method not implemented.');
|
|
3946
|
+
}
|
|
3947
|
+
async getUrl() {
|
|
3948
|
+
throw new Error('Method not implemented.');
|
|
3949
|
+
}
|
|
3950
|
+
async initWebStore() {
|
|
3951
|
+
throw new Error('Method not implemented.');
|
|
3952
|
+
}
|
|
3953
|
+
async saveToStore(options) {
|
|
3954
|
+
console.log(`${JSON.stringify(options)}`);
|
|
3955
|
+
throw new Error('Method not implemented.');
|
|
3956
|
+
}
|
|
3957
|
+
async isSecretStored() {
|
|
3958
|
+
throw new Error('Method not implemented.');
|
|
3959
|
+
}
|
|
3960
|
+
async setEncryptionSecret(options) {
|
|
3961
|
+
console.log(`${JSON.stringify(options)}`);
|
|
3962
|
+
throw new Error('Method not implemented.');
|
|
3963
|
+
}
|
|
3964
|
+
async changeEncryptionSecret(options) {
|
|
3965
|
+
console.log(`${JSON.stringify(options)}`);
|
|
3966
|
+
throw new Error('Method not implemented.');
|
|
3967
|
+
}
|
|
3968
|
+
async getNCDatabasePath(options) {
|
|
3969
|
+
console.log('getNCDatabasePath', options);
|
|
3970
|
+
throw new Error('Method not implemented.');
|
|
3971
|
+
}
|
|
3972
|
+
async createNCConnection(options) {
|
|
3973
|
+
console.log('createNCConnection', options);
|
|
3974
|
+
throw new Error('Method not implemented.');
|
|
3975
|
+
}
|
|
3976
|
+
async closeNCConnection(options) {
|
|
3977
|
+
console.log('closeNCConnection', options);
|
|
3978
|
+
throw new Error('Method not implemented.');
|
|
3979
|
+
}
|
|
3980
|
+
async isNCDatabase(options) {
|
|
3981
|
+
console.log('isNCDatabase', options);
|
|
3982
|
+
throw new Error('Method not implemented.');
|
|
4147
3983
|
}
|
|
4148
3984
|
}
|
|
4149
3985
|
exports.CapacitorSQLite = src.CapacitorSQLite = CapacitorSQLite;
|