@capacitor-community/sqlite 3.4.2 → 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 +9 -2
- 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.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 +251 -447
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +251 -447
- package/dist/plugin.js.map +1 -1
- package/electron/dist/plugin.js +489 -701
- package/electron/dist/plugin.js.map +1 -1
- package/ios/Plugin/Utils/UtilsUpgrade.swift +33 -12
- package/package.json +6 -6
package/electron/dist/plugin.js
CHANGED
|
@@ -2735,15 +2735,17 @@ class UtilsUpgrade {
|
|
|
2735
2735
|
if (Object.keys(this._commonColumns).length > 0) {
|
|
2736
2736
|
await this.updateNewTablesData(mDB);
|
|
2737
2737
|
}
|
|
2738
|
+
return Promise.resolve();
|
|
2739
|
+
}
|
|
2740
|
+
catch (err) {
|
|
2741
|
+
return Promise.reject(`ExecuteStatementProcess: ${err}`);
|
|
2742
|
+
}
|
|
2743
|
+
finally {
|
|
2738
2744
|
// -> Drop _temp_tables
|
|
2739
2745
|
await this._uDrop.dropTempTables(mDB, this._alterTables);
|
|
2740
2746
|
// -> Do some cleanup
|
|
2741
2747
|
this._alterTables = {};
|
|
2742
2748
|
this._commonColumns = {};
|
|
2743
|
-
return Promise.resolve();
|
|
2744
|
-
}
|
|
2745
|
-
catch (err) {
|
|
2746
|
-
return Promise.reject(`ExecuteStatementProcess: ${err}`);
|
|
2747
2749
|
}
|
|
2748
2750
|
}
|
|
2749
2751
|
/**
|
|
@@ -2812,9 +2814,13 @@ class UtilsUpgrade {
|
|
|
2812
2814
|
// get the table's column names
|
|
2813
2815
|
const colNames = await this.getTableColumnNames(mDB, table);
|
|
2814
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, []);
|
|
2815
2821
|
// prefix the table with _temp_
|
|
2816
2822
|
let stmt = `ALTER TABLE ${table} RENAME `;
|
|
2817
|
-
stmt += `TO
|
|
2823
|
+
stmt += `TO ${tmpTable};`;
|
|
2818
2824
|
const lastId = await this._uSQLite.prepareRun(mDB, stmt, []);
|
|
2819
2825
|
if (lastId < 0) {
|
|
2820
2826
|
let msg = 'BackupTable: lastId < 0';
|
|
@@ -2963,24 +2969,24 @@ class Database {
|
|
|
2963
2969
|
// encrypted: boolean,
|
|
2964
2970
|
// mode: string,
|
|
2965
2971
|
version, upgDict) {
|
|
2966
|
-
this.
|
|
2967
|
-
this.
|
|
2968
|
-
this.
|
|
2969
|
-
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();
|
|
2970
2976
|
// private _uGlobal: GlobalSQLite = new GlobalSQLite();
|
|
2971
2977
|
// private _uEncrypt: UtilsEncryption = new UtilsEncryption();
|
|
2972
|
-
this.
|
|
2973
|
-
this.
|
|
2974
|
-
this.
|
|
2975
|
-
this.
|
|
2976
|
-
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;
|
|
2977
2983
|
// this._encrypted = encrypted;
|
|
2978
2984
|
// this._mode = mode;
|
|
2979
|
-
this.
|
|
2980
|
-
this.
|
|
2981
|
-
this.
|
|
2982
|
-
this.
|
|
2983
|
-
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)
|
|
2984
2990
|
throw new Error('Could not generate a path to ' + dbName);
|
|
2985
2991
|
}
|
|
2986
2992
|
/**
|
|
@@ -2991,7 +2997,7 @@ class Database {
|
|
|
2991
2997
|
* @since 0.0.1
|
|
2992
2998
|
*/
|
|
2993
2999
|
isDBOpen() {
|
|
2994
|
-
return this.
|
|
3000
|
+
return this._isDbOpen;
|
|
2995
3001
|
}
|
|
2996
3002
|
/**
|
|
2997
3003
|
* Open
|
|
@@ -2999,7 +3005,7 @@ class Database {
|
|
|
2999
3005
|
* @returns Promise<boolean>
|
|
3000
3006
|
*/
|
|
3001
3007
|
async open() {
|
|
3002
|
-
this.
|
|
3008
|
+
this._isDbOpen = false;
|
|
3003
3009
|
// let password = '';
|
|
3004
3010
|
try {
|
|
3005
3011
|
/*
|
|
@@ -3021,34 +3027,34 @@ class Database {
|
|
|
3021
3027
|
await this._uEncrypt.encryptDatabase(this._pathDB, password);
|
|
3022
3028
|
}
|
|
3023
3029
|
*/
|
|
3024
|
-
this.
|
|
3030
|
+
this.database = await this.sqliteUtil.openOrCreateDatabase(this.pathDB /*,
|
|
3025
3031
|
password,*/);
|
|
3026
|
-
const curVersion = await this.
|
|
3027
|
-
this.
|
|
3028
|
-
if (this.
|
|
3029
|
-
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) {
|
|
3030
3036
|
try {
|
|
3031
3037
|
// execute the upgrade flow process
|
|
3032
|
-
await this.
|
|
3038
|
+
await this.upgradeUtil.onUpgrade(this.database, this.upgradeVersionDict, this.dbName, curVersion, this.version);
|
|
3033
3039
|
// delete the backup database
|
|
3034
|
-
await this.
|
|
3040
|
+
await this.fileUtil.deleteFileName(`backup-${this.dbName}`);
|
|
3035
3041
|
}
|
|
3036
3042
|
catch (err) {
|
|
3037
3043
|
// restore the database from backup
|
|
3038
3044
|
try {
|
|
3039
|
-
await this.
|
|
3045
|
+
await this.fileUtil.restoreFileName(this.dbName, 'backup');
|
|
3040
3046
|
}
|
|
3041
3047
|
catch (err) {
|
|
3042
|
-
|
|
3048
|
+
throw new Error(`Open: ${err}`);
|
|
3043
3049
|
}
|
|
3044
3050
|
}
|
|
3045
3051
|
}
|
|
3046
|
-
return
|
|
3052
|
+
return;
|
|
3047
3053
|
}
|
|
3048
3054
|
catch (err) {
|
|
3049
|
-
if (this.
|
|
3055
|
+
if (this._isDbOpen)
|
|
3050
3056
|
this.close();
|
|
3051
|
-
|
|
3057
|
+
throw new Error(`Open: ${err}`);
|
|
3052
3058
|
}
|
|
3053
3059
|
}
|
|
3054
3060
|
/**
|
|
@@ -3057,18 +3063,13 @@ class Database {
|
|
|
3057
3063
|
* @returns Promise<boolean>
|
|
3058
3064
|
*/
|
|
3059
3065
|
async close() {
|
|
3060
|
-
|
|
3061
|
-
|
|
3062
|
-
|
|
3063
|
-
|
|
3064
|
-
|
|
3065
|
-
|
|
3066
|
-
|
|
3067
|
-
this._isDBOpen = false;
|
|
3068
|
-
return Promise.resolve();
|
|
3069
|
-
});
|
|
3070
|
-
}
|
|
3071
|
-
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
|
+
});
|
|
3072
3073
|
}
|
|
3073
3074
|
/**
|
|
3074
3075
|
* GetVersion
|
|
@@ -3076,21 +3077,15 @@ class Database {
|
|
|
3076
3077
|
* @returns Promise<number>
|
|
3077
3078
|
*/
|
|
3078
3079
|
async getVersion() {
|
|
3079
|
-
|
|
3080
|
-
|
|
3081
|
-
|
|
3082
|
-
|
|
3083
|
-
}
|
|
3084
|
-
catch (err) {
|
|
3085
|
-
if (this._isDBOpen)
|
|
3086
|
-
this.close();
|
|
3087
|
-
return Promise.reject(`getVersion: ${err}`);
|
|
3088
|
-
}
|
|
3080
|
+
this.ensureDatabaseIsOpen();
|
|
3081
|
+
try {
|
|
3082
|
+
const currentVersion = await this.sqliteUtil.getVersion(this.database);
|
|
3083
|
+
return currentVersion;
|
|
3089
3084
|
}
|
|
3090
|
-
|
|
3091
|
-
|
|
3092
|
-
|
|
3093
|
-
|
|
3085
|
+
catch (err) {
|
|
3086
|
+
if (this._isDbOpen)
|
|
3087
|
+
this.close();
|
|
3088
|
+
throw new Error(`getVersion: ${err}`);
|
|
3094
3089
|
}
|
|
3095
3090
|
}
|
|
3096
3091
|
/**
|
|
@@ -3101,14 +3096,14 @@ class Database {
|
|
|
3101
3096
|
*/
|
|
3102
3097
|
async deleteDB(dbName) {
|
|
3103
3098
|
// test if file exists
|
|
3104
|
-
const isExists = this.
|
|
3105
|
-
if (isExists && !this.
|
|
3099
|
+
const isExists = this.fileUtil.isFileExists(dbName);
|
|
3100
|
+
if (isExists && !this._isDbOpen) {
|
|
3106
3101
|
// open the database
|
|
3107
3102
|
try {
|
|
3108
3103
|
await this.open();
|
|
3109
3104
|
}
|
|
3110
3105
|
catch (err) {
|
|
3111
|
-
|
|
3106
|
+
throw new Error(`DeleteDB: ${err}`);
|
|
3112
3107
|
}
|
|
3113
3108
|
}
|
|
3114
3109
|
// close the database
|
|
@@ -3116,20 +3111,18 @@ class Database {
|
|
|
3116
3111
|
await this.close();
|
|
3117
3112
|
}
|
|
3118
3113
|
catch (err) {
|
|
3119
|
-
|
|
3114
|
+
throw new Error('DeleteDB: Close failed');
|
|
3120
3115
|
}
|
|
3121
3116
|
// delete the database
|
|
3122
3117
|
if (isExists) {
|
|
3123
3118
|
try {
|
|
3124
|
-
await this.
|
|
3119
|
+
await this.fileUtil.deleteFileName(dbName);
|
|
3125
3120
|
}
|
|
3126
3121
|
catch (err) {
|
|
3127
|
-
|
|
3128
|
-
msg += ` failed ${err}`;
|
|
3129
|
-
return Promise.reject(msg);
|
|
3122
|
+
throw new Error(`DeleteDB: deleteFile ${dbName} failed ${err}`);
|
|
3130
3123
|
}
|
|
3131
3124
|
}
|
|
3132
|
-
return
|
|
3125
|
+
return;
|
|
3133
3126
|
}
|
|
3134
3127
|
/**
|
|
3135
3128
|
* IsTableExists
|
|
@@ -3137,20 +3130,14 @@ class Database {
|
|
|
3137
3130
|
* @returns
|
|
3138
3131
|
*/
|
|
3139
3132
|
async isTableExists(tableName) {
|
|
3140
|
-
|
|
3141
|
-
|
|
3142
|
-
|
|
3143
|
-
|
|
3144
|
-
|
|
3145
|
-
}
|
|
3146
|
-
catch (err) {
|
|
3147
|
-
return Promise.reject(`IsTableExists: ${err}`);
|
|
3148
|
-
}
|
|
3133
|
+
this.ensureDatabaseIsOpen();
|
|
3134
|
+
const isOpen = this._isDbOpen;
|
|
3135
|
+
try {
|
|
3136
|
+
const tableExistsResult = await this.jsonUtil.isTableExists(this.database, isOpen, tableName);
|
|
3137
|
+
return tableExistsResult;
|
|
3149
3138
|
}
|
|
3150
|
-
|
|
3151
|
-
|
|
3152
|
-
msg += `not opened`;
|
|
3153
|
-
return Promise.reject(msg);
|
|
3139
|
+
catch (err) {
|
|
3140
|
+
throw new Error(`IsTableExists: ${err}`);
|
|
3154
3141
|
}
|
|
3155
3142
|
}
|
|
3156
3143
|
/**
|
|
@@ -3159,18 +3146,14 @@ class Database {
|
|
|
3159
3146
|
* @returns Promise<number>
|
|
3160
3147
|
*/
|
|
3161
3148
|
async createSyncTable() {
|
|
3162
|
-
|
|
3163
|
-
let msg = `CreateSyncTable: Database ${this._dbName} `;
|
|
3164
|
-
msg += `not opened`;
|
|
3165
|
-
return Promise.reject(msg);
|
|
3166
|
-
}
|
|
3149
|
+
this.ensureDatabaseIsOpen();
|
|
3167
3150
|
let changes = -1;
|
|
3168
|
-
const isOpen = this.
|
|
3151
|
+
const isOpen = this._isDbOpen;
|
|
3169
3152
|
// check if the table has already being created
|
|
3170
3153
|
try {
|
|
3171
|
-
const retB = await this.
|
|
3154
|
+
const retB = await this.jsonUtil.isTableExists(this.database, isOpen, 'sync_table');
|
|
3172
3155
|
if (!retB) {
|
|
3173
|
-
const isLastModified = await this.
|
|
3156
|
+
const isLastModified = await this.jsonUtil.isLastModified(this.database, isOpen);
|
|
3174
3157
|
if (isLastModified) {
|
|
3175
3158
|
const date = Math.round(new Date().getTime() / 1000);
|
|
3176
3159
|
let stmts = `
|
|
@@ -3180,23 +3163,23 @@ class Database {
|
|
|
3180
3163
|
);`;
|
|
3181
3164
|
stmts += `INSERT INTO sync_table (sync_date) VALUES (
|
|
3182
3165
|
"${date}");`;
|
|
3183
|
-
changes = await this.
|
|
3166
|
+
changes = await this.sqliteUtil.execute(this.database, stmts);
|
|
3184
3167
|
if (changes < 0) {
|
|
3185
|
-
|
|
3168
|
+
throw new Error(`CreateSyncTable: failed changes < 0`);
|
|
3186
3169
|
}
|
|
3187
3170
|
}
|
|
3188
3171
|
else {
|
|
3189
|
-
|
|
3172
|
+
throw new Error('No last_modified column in tables');
|
|
3190
3173
|
}
|
|
3191
3174
|
}
|
|
3192
3175
|
else {
|
|
3193
3176
|
changes = 0;
|
|
3194
3177
|
}
|
|
3195
3178
|
console.log(`>>> CreateSyncTable changes: ${changes}`);
|
|
3196
|
-
return
|
|
3179
|
+
return changes;
|
|
3197
3180
|
}
|
|
3198
3181
|
catch (err) {
|
|
3199
|
-
|
|
3182
|
+
throw new Error(`CreateSyncTable: ${err}`);
|
|
3200
3183
|
}
|
|
3201
3184
|
}
|
|
3202
3185
|
/**
|
|
@@ -3206,20 +3189,16 @@ class Database {
|
|
|
3206
3189
|
* @returns Promise<{result: boolean, message: string}>
|
|
3207
3190
|
*/
|
|
3208
3191
|
async setSyncDate(syncDate) {
|
|
3209
|
-
|
|
3210
|
-
let msg = `SetSyncDate: Database ${this._dbName} `;
|
|
3211
|
-
msg += `not opened`;
|
|
3212
|
-
return { result: false, message: msg };
|
|
3213
|
-
}
|
|
3192
|
+
this.ensureDatabaseIsOpen();
|
|
3214
3193
|
try {
|
|
3215
|
-
const isTable = await this.
|
|
3194
|
+
const isTable = await this.jsonUtil.isTableExists(this.database, this._isDbOpen, 'sync_table');
|
|
3216
3195
|
if (!isTable) {
|
|
3217
|
-
|
|
3196
|
+
throw new Error('No sync_table available');
|
|
3218
3197
|
}
|
|
3219
|
-
const
|
|
3198
|
+
const syncDateUnixTimestamp = Math.round(new Date(syncDate).getTime() / 1000);
|
|
3220
3199
|
let stmt = `UPDATE sync_table SET sync_date = `;
|
|
3221
|
-
stmt += `${
|
|
3222
|
-
const changes = await this.
|
|
3200
|
+
stmt += `${syncDateUnixTimestamp} WHERE id = 1;`;
|
|
3201
|
+
const changes = await this.sqliteUtil.execute(this.database, stmt);
|
|
3223
3202
|
if (changes < 0) {
|
|
3224
3203
|
return { result: false, message: 'setSyncDate failed' };
|
|
3225
3204
|
}
|
|
@@ -3237,19 +3216,15 @@ class Database {
|
|
|
3237
3216
|
* @returns Promise<{syncDate: number, message: string}>
|
|
3238
3217
|
*/
|
|
3239
3218
|
async getSyncDate() {
|
|
3240
|
-
|
|
3241
|
-
let msg = `GetSyncDate: Database ${this._dbName} `;
|
|
3242
|
-
msg += `not opened`;
|
|
3243
|
-
return { syncDate: 0, message: msg };
|
|
3244
|
-
}
|
|
3219
|
+
this.ensureDatabaseIsOpen();
|
|
3245
3220
|
try {
|
|
3246
|
-
const isTable = await this.
|
|
3221
|
+
const isTable = await this.jsonUtil.isTableExists(this.database, this._isDbOpen, 'sync_table');
|
|
3247
3222
|
if (!isTable) {
|
|
3248
|
-
|
|
3223
|
+
throw new Error('No sync_table available');
|
|
3249
3224
|
}
|
|
3250
|
-
const syncDate = await this.
|
|
3225
|
+
const syncDate = await this.exportToJsonUtil.getSyncDate(this.database);
|
|
3251
3226
|
if (syncDate > 0) {
|
|
3252
|
-
return { syncDate
|
|
3227
|
+
return { syncDate };
|
|
3253
3228
|
}
|
|
3254
3229
|
else {
|
|
3255
3230
|
return { syncDate: 0, message: `setSyncDate failed` };
|
|
@@ -3266,32 +3241,31 @@ class Database {
|
|
|
3266
3241
|
* @returns Promise<number>
|
|
3267
3242
|
*/
|
|
3268
3243
|
async executeSQL(sql, transaction) {
|
|
3269
|
-
|
|
3270
|
-
let msg = `ExecuteSQL: Database ${this._dbName} `;
|
|
3271
|
-
msg += `not opened`;
|
|
3272
|
-
return Promise.reject(msg);
|
|
3273
|
-
}
|
|
3244
|
+
this.ensureDatabaseIsOpen();
|
|
3274
3245
|
try {
|
|
3275
|
-
if (transaction)
|
|
3276
|
-
await this.
|
|
3277
|
-
|
|
3246
|
+
if (transaction) {
|
|
3247
|
+
await this.sqliteUtil.beginTransaction(this.database, this._isDbOpen);
|
|
3248
|
+
}
|
|
3249
|
+
const changes = await this.sqliteUtil.execute(this.database, sql);
|
|
3278
3250
|
if (changes < 0) {
|
|
3279
|
-
|
|
3251
|
+
throw new Error('ExecuteSQL: changes < 0');
|
|
3280
3252
|
}
|
|
3281
|
-
if (transaction)
|
|
3282
|
-
await this.
|
|
3283
|
-
|
|
3253
|
+
if (transaction) {
|
|
3254
|
+
await this.sqliteUtil.commitTransaction(this.database, this._isDbOpen);
|
|
3255
|
+
}
|
|
3256
|
+
return changes;
|
|
3284
3257
|
}
|
|
3285
|
-
catch (
|
|
3286
|
-
let
|
|
3258
|
+
catch (executeError) {
|
|
3259
|
+
let message = `${executeError}`;
|
|
3287
3260
|
try {
|
|
3288
|
-
if (transaction)
|
|
3289
|
-
await this.
|
|
3261
|
+
if (transaction) {
|
|
3262
|
+
await this.sqliteUtil.rollbackTransaction(this.database, this._isDbOpen);
|
|
3263
|
+
}
|
|
3290
3264
|
}
|
|
3291
|
-
catch (
|
|
3292
|
-
|
|
3265
|
+
catch (rollbackErr) {
|
|
3266
|
+
message += ` : ${rollbackErr}`;
|
|
3293
3267
|
}
|
|
3294
|
-
|
|
3268
|
+
throw new Error(`ExecuteSQL: ${message}`);
|
|
3295
3269
|
}
|
|
3296
3270
|
}
|
|
3297
3271
|
/**
|
|
@@ -3302,17 +3276,13 @@ class Database {
|
|
|
3302
3276
|
* @returns Promise<any[]>
|
|
3303
3277
|
*/
|
|
3304
3278
|
async selectSQL(sql, values) {
|
|
3305
|
-
|
|
3306
|
-
let msg = `SelectSQL: Database ${this._dbName} `;
|
|
3307
|
-
msg += `not opened`;
|
|
3308
|
-
return Promise.reject(msg);
|
|
3309
|
-
}
|
|
3279
|
+
this.ensureDatabaseIsOpen();
|
|
3310
3280
|
try {
|
|
3311
|
-
const
|
|
3312
|
-
return
|
|
3281
|
+
const selectResult = await this.sqliteUtil.queryAll(this.database, sql, values);
|
|
3282
|
+
return selectResult;
|
|
3313
3283
|
}
|
|
3314
3284
|
catch (err) {
|
|
3315
|
-
|
|
3285
|
+
throw new Error(`SelectSQL: ${err}`);
|
|
3316
3286
|
}
|
|
3317
3287
|
}
|
|
3318
3288
|
/**
|
|
@@ -3323,39 +3293,39 @@ class Database {
|
|
|
3323
3293
|
* @returns Promise<{changes:number, lastId:number}>
|
|
3324
3294
|
*/
|
|
3325
3295
|
async runSQL(statement, values, transaction) {
|
|
3326
|
-
|
|
3327
|
-
|
|
3328
|
-
msg += `not opened`;
|
|
3329
|
-
return Promise.reject(msg);
|
|
3330
|
-
}
|
|
3331
|
-
const retRes = { changes: -1, lastId: -1 };
|
|
3296
|
+
this.ensureDatabaseIsOpen();
|
|
3297
|
+
const result = { changes: -1, lastId: -1 };
|
|
3332
3298
|
let initChanges = -1;
|
|
3333
3299
|
try {
|
|
3334
|
-
initChanges = await this.
|
|
3300
|
+
initChanges = await this.sqliteUtil.dbChanges(this.database);
|
|
3335
3301
|
// start a transaction
|
|
3336
|
-
if (transaction)
|
|
3337
|
-
await this.
|
|
3302
|
+
if (transaction) {
|
|
3303
|
+
await this.sqliteUtil.beginTransaction(this.database, this._isDbOpen);
|
|
3304
|
+
}
|
|
3338
3305
|
}
|
|
3339
3306
|
catch (err) {
|
|
3340
|
-
|
|
3307
|
+
throw new Error(`RunSQL: ${err}`);
|
|
3341
3308
|
}
|
|
3342
3309
|
try {
|
|
3343
|
-
const lastId = await this.
|
|
3310
|
+
const lastId = await this.sqliteUtil.prepareRun(this.database, statement, values);
|
|
3344
3311
|
if (lastId < 0) {
|
|
3345
|
-
if (transaction)
|
|
3346
|
-
await this.
|
|
3347
|
-
|
|
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);
|
|
3348
3319
|
}
|
|
3349
|
-
|
|
3350
|
-
|
|
3351
|
-
|
|
3352
|
-
retRes.lastId = lastId;
|
|
3353
|
-
return Promise.resolve(retRes);
|
|
3320
|
+
result.changes = (await this.sqliteUtil.dbChanges(this.database)) - initChanges;
|
|
3321
|
+
result.lastId = lastId;
|
|
3322
|
+
return result;
|
|
3354
3323
|
}
|
|
3355
3324
|
catch (err) {
|
|
3356
|
-
if (transaction)
|
|
3357
|
-
await this.
|
|
3358
|
-
|
|
3325
|
+
if (transaction) {
|
|
3326
|
+
await this.sqliteUtil.rollbackTransaction(this.database, this._isDbOpen);
|
|
3327
|
+
}
|
|
3328
|
+
throw new Error(`RunSQL: ${err}`);
|
|
3359
3329
|
}
|
|
3360
3330
|
}
|
|
3361
3331
|
/**
|
|
@@ -3365,103 +3335,98 @@ class Database {
|
|
|
3365
3335
|
* @returns Promise<{changes:number, lastId:number}>
|
|
3366
3336
|
*/
|
|
3367
3337
|
async execSet(set, transaction) {
|
|
3368
|
-
|
|
3369
|
-
|
|
3370
|
-
msg += `not opened`;
|
|
3371
|
-
return Promise.reject(msg);
|
|
3372
|
-
}
|
|
3373
|
-
const retRes = { changes: -1, lastId: -1 };
|
|
3338
|
+
this.ensureDatabaseIsOpen();
|
|
3339
|
+
const result = { changes: -1, lastId: -1 };
|
|
3374
3340
|
let initChanges = -1;
|
|
3375
3341
|
try {
|
|
3376
|
-
initChanges = await this.
|
|
3342
|
+
initChanges = await this.sqliteUtil.dbChanges(this.database);
|
|
3377
3343
|
// start a transaction
|
|
3378
|
-
if (transaction)
|
|
3379
|
-
await this.
|
|
3344
|
+
if (transaction) {
|
|
3345
|
+
await this.sqliteUtil.beginTransaction(this.database, this._isDbOpen);
|
|
3346
|
+
}
|
|
3380
3347
|
}
|
|
3381
3348
|
catch (err) {
|
|
3382
|
-
|
|
3349
|
+
throw new Error(`ExecSet: ${err}`);
|
|
3383
3350
|
}
|
|
3384
3351
|
try {
|
|
3385
|
-
|
|
3386
|
-
if (transaction)
|
|
3387
|
-
await this.
|
|
3388
|
-
|
|
3389
|
-
|
|
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;
|
|
3390
3358
|
}
|
|
3391
3359
|
catch (err) {
|
|
3392
|
-
const
|
|
3360
|
+
const message = err;
|
|
3393
3361
|
try {
|
|
3394
|
-
if (transaction)
|
|
3395
|
-
await this.
|
|
3362
|
+
if (transaction) {
|
|
3363
|
+
await this.sqliteUtil.rollbackTransaction(this.database, this._isDbOpen);
|
|
3364
|
+
}
|
|
3396
3365
|
}
|
|
3397
3366
|
catch (err) {
|
|
3398
|
-
|
|
3367
|
+
throw new Error(`ExecSet: ${message}: ` + `${err}`);
|
|
3399
3368
|
}
|
|
3400
3369
|
}
|
|
3401
3370
|
}
|
|
3402
3371
|
async getTableList() {
|
|
3403
|
-
|
|
3404
|
-
let msg = `GetTableList: Database ${this._dbName} `;
|
|
3405
|
-
msg += `not opened`;
|
|
3406
|
-
return Promise.reject(msg);
|
|
3407
|
-
}
|
|
3372
|
+
this.ensureDatabaseIsOpen();
|
|
3408
3373
|
try {
|
|
3409
|
-
const
|
|
3410
|
-
return
|
|
3374
|
+
const tableNames = await this.dropUtil.getTablesNames(this.database);
|
|
3375
|
+
return tableNames;
|
|
3411
3376
|
}
|
|
3412
3377
|
catch (err) {
|
|
3413
|
-
|
|
3378
|
+
throw new Error(`GetTableList: ${err}`);
|
|
3414
3379
|
}
|
|
3415
3380
|
}
|
|
3416
3381
|
async importJson(jsonData) {
|
|
3417
3382
|
let changes = 0;
|
|
3418
|
-
|
|
3419
|
-
|
|
3420
|
-
|
|
3421
|
-
|
|
3422
|
-
|
|
3423
|
-
|
|
3424
|
-
|
|
3425
|
-
|
|
3426
|
-
}
|
|
3427
|
-
}
|
|
3428
|
-
if (jsonData.views && jsonData.views.length > 0) {
|
|
3429
|
-
// create the views
|
|
3430
|
-
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);
|
|
3431
3391
|
}
|
|
3432
|
-
return Promise.resolve(changes);
|
|
3433
3392
|
}
|
|
3434
|
-
|
|
3435
|
-
|
|
3393
|
+
if (jsonData.views && jsonData.views.length > 0) {
|
|
3394
|
+
// create the views
|
|
3395
|
+
changes += await this.importFromJsonUtil.createViews(this.database, jsonData);
|
|
3436
3396
|
}
|
|
3397
|
+
return changes;
|
|
3437
3398
|
}
|
|
3438
|
-
|
|
3439
|
-
|
|
3399
|
+
catch (err) {
|
|
3400
|
+
throw new Error(`ImportJson: ${err}`);
|
|
3440
3401
|
}
|
|
3441
3402
|
}
|
|
3442
3403
|
async exportJson(mode) {
|
|
3443
3404
|
const inJson = {};
|
|
3444
|
-
inJson.database = this.
|
|
3445
|
-
inJson.version = this.
|
|
3405
|
+
inJson.database = this.dbName.slice(0, -9);
|
|
3406
|
+
inJson.version = this.version;
|
|
3446
3407
|
inJson.encrypted = false;
|
|
3447
3408
|
inJson.mode = mode;
|
|
3448
|
-
|
|
3449
|
-
|
|
3450
|
-
|
|
3451
|
-
|
|
3452
|
-
|
|
3453
|
-
|
|
3454
|
-
}
|
|
3455
|
-
else {
|
|
3456
|
-
return Promise.reject(`ExportJson: retJson not valid`);
|
|
3457
|
-
}
|
|
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;
|
|
3458
3415
|
}
|
|
3459
|
-
|
|
3460
|
-
|
|
3416
|
+
else {
|
|
3417
|
+
throw new Error(`ExportJson: retJson not valid`);
|
|
3461
3418
|
}
|
|
3462
3419
|
}
|
|
3463
|
-
|
|
3464
|
-
|
|
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.`);
|
|
3465
3430
|
}
|
|
3466
3431
|
}
|
|
3467
3432
|
}
|
|
@@ -3474,49 +3439,15 @@ const utilsJson_1 = utilsJson;
|
|
|
3474
3439
|
const utilsFile_1 = utilsFile;
|
|
3475
3440
|
class CapacitorSQLite {
|
|
3476
3441
|
constructor() {
|
|
3477
|
-
this.
|
|
3478
|
-
this.
|
|
3479
|
-
this.
|
|
3480
|
-
this.
|
|
3481
|
-
}
|
|
3482
|
-
async initWebStore() {
|
|
3483
|
-
return Promise.reject('Method not implemented.');
|
|
3484
|
-
}
|
|
3485
|
-
async saveToStore(options) {
|
|
3486
|
-
console.log(`${JSON.stringify(options)}`);
|
|
3487
|
-
return Promise.reject('Method not implemented.');
|
|
3488
|
-
}
|
|
3489
|
-
async isSecretStored() {
|
|
3490
|
-
return Promise.reject('Method not implemented.');
|
|
3491
|
-
}
|
|
3492
|
-
async setEncryptionSecret(options) {
|
|
3493
|
-
console.log(`${JSON.stringify(options)}`);
|
|
3494
|
-
return Promise.reject('Method not implemented.');
|
|
3495
|
-
}
|
|
3496
|
-
async changeEncryptionSecret(options) {
|
|
3497
|
-
console.log(`${JSON.stringify(options)}`);
|
|
3498
|
-
return Promise.reject('Method not implemented.');
|
|
3499
|
-
}
|
|
3500
|
-
async getNCDatabasePath(options) {
|
|
3501
|
-
console.log('getNCDatabasePath', options);
|
|
3502
|
-
return Promise.reject('Method not implemented.');
|
|
3503
|
-
}
|
|
3504
|
-
async createNCConnection(options) {
|
|
3505
|
-
console.log('createNCConnection', options);
|
|
3506
|
-
return Promise.reject('Method not implemented.');
|
|
3507
|
-
}
|
|
3508
|
-
async closeNCConnection(options) {
|
|
3509
|
-
console.log('closeNCConnection', options);
|
|
3510
|
-
return Promise.reject('Method not implemented.');
|
|
3511
|
-
}
|
|
3512
|
-
async isNCDatabase(options) {
|
|
3513
|
-
console.log('isNCDatabase', options);
|
|
3514
|
-
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();
|
|
3515
3446
|
}
|
|
3516
3447
|
async createConnection(options) {
|
|
3517
|
-
const
|
|
3518
|
-
if (!
|
|
3519
|
-
|
|
3448
|
+
const optionKeys = Object.keys(options);
|
|
3449
|
+
if (!optionKeys.includes('database')) {
|
|
3450
|
+
throw new Error('Must provide a database name');
|
|
3520
3451
|
}
|
|
3521
3452
|
const dbName = options.database;
|
|
3522
3453
|
const version = options.version ? options.version : 1;
|
|
@@ -3532,612 +3463,381 @@ class CapacitorSQLite {
|
|
|
3532
3463
|
? options.mode
|
|
3533
3464
|
: 'no-encryption';
|
|
3534
3465
|
*/
|
|
3535
|
-
let
|
|
3536
|
-
const
|
|
3537
|
-
if (
|
|
3538
|
-
|
|
3539
|
-
}
|
|
3540
|
-
|
|
3541
|
-
|
|
3542
|
-
|
|
3543
|
-
|
|
3544
|
-
|
|
3545
|
-
|
|
3546
|
-
|
|
3547
|
-
return Promise.resolve();
|
|
3548
|
-
}
|
|
3549
|
-
catch (err) {
|
|
3550
|
-
return Promise.reject(err);
|
|
3551
|
-
}
|
|
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;
|
|
3552
3478
|
}
|
|
3553
3479
|
async closeConnection(options) {
|
|
3554
|
-
|
|
3555
|
-
|
|
3556
|
-
|
|
3557
|
-
}
|
|
3558
|
-
const dbName = options.database;
|
|
3559
|
-
keys = Object.keys(this._dbDict);
|
|
3560
|
-
if (!keys.includes(dbName)) {
|
|
3561
|
-
return Promise.resolve();
|
|
3562
|
-
}
|
|
3563
|
-
const mDB = this._dbDict[dbName];
|
|
3564
|
-
if (mDB.isDBOpen()) {
|
|
3480
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3481
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
3482
|
+
if (database.isDBOpen()) {
|
|
3565
3483
|
// close the database
|
|
3566
3484
|
try {
|
|
3567
|
-
await
|
|
3485
|
+
await database.close();
|
|
3568
3486
|
}
|
|
3569
3487
|
catch (err) {
|
|
3570
|
-
|
|
3571
|
-
'close ' +
|
|
3572
|
-
dbName +
|
|
3573
|
-
' failed ' +
|
|
3574
|
-
err);
|
|
3488
|
+
throw new Error(`CloseConnection command failed: close ${dbName} failed ${err.message}`);
|
|
3575
3489
|
}
|
|
3576
3490
|
}
|
|
3577
3491
|
// remove the connection from dictionary
|
|
3578
|
-
delete this.
|
|
3579
|
-
return Promise.resolve();
|
|
3492
|
+
delete this.databases[dbName];
|
|
3580
3493
|
}
|
|
3581
3494
|
async echo(options) {
|
|
3582
|
-
const
|
|
3583
|
-
|
|
3584
|
-
|
|
3585
|
-
|
|
3586
|
-
const ret = {};
|
|
3587
|
-
ret.value = options.value;
|
|
3588
|
-
return Promise.resolve(ret);
|
|
3495
|
+
const echoValue = this.getOptionValue(options, 'value');
|
|
3496
|
+
const echoResult = {};
|
|
3497
|
+
echoResult.value = echoValue;
|
|
3498
|
+
return echoResult;
|
|
3589
3499
|
}
|
|
3590
3500
|
async open(options) {
|
|
3591
|
-
|
|
3592
|
-
|
|
3593
|
-
return Promise.reject('Must provide a database name');
|
|
3594
|
-
}
|
|
3595
|
-
const dbName = options.database;
|
|
3596
|
-
keys = Object.keys(this._dbDict);
|
|
3597
|
-
if (!keys.includes(dbName)) {
|
|
3598
|
-
return Promise.reject(`Open: No available connection for ${dbName}`);
|
|
3599
|
-
}
|
|
3600
|
-
const mDB = this._dbDict[dbName];
|
|
3501
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3502
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
3601
3503
|
try {
|
|
3602
|
-
await
|
|
3603
|
-
return
|
|
3504
|
+
await database.open();
|
|
3505
|
+
return;
|
|
3604
3506
|
}
|
|
3605
3507
|
catch (err) {
|
|
3606
|
-
|
|
3508
|
+
throw new Error(`Open: ${err}`);
|
|
3607
3509
|
}
|
|
3608
3510
|
}
|
|
3609
3511
|
async close(options) {
|
|
3610
|
-
|
|
3611
|
-
|
|
3612
|
-
return Promise.reject('Must provide a database name');
|
|
3613
|
-
}
|
|
3614
|
-
const dbName = options.database;
|
|
3615
|
-
keys = Object.keys(this._dbDict);
|
|
3616
|
-
if (!keys.includes(dbName)) {
|
|
3617
|
-
return Promise.reject(`Close: No available connection for ${dbName}`);
|
|
3618
|
-
}
|
|
3619
|
-
const mDB = this._dbDict[dbName];
|
|
3512
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3513
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
3620
3514
|
try {
|
|
3621
|
-
await
|
|
3622
|
-
return
|
|
3515
|
+
await database.close();
|
|
3516
|
+
return;
|
|
3623
3517
|
}
|
|
3624
3518
|
catch (err) {
|
|
3625
|
-
|
|
3519
|
+
throw new Error(`Close: ${err}`);
|
|
3626
3520
|
}
|
|
3627
3521
|
}
|
|
3628
|
-
async getUrl() {
|
|
3629
|
-
return Promise.reject('Method not implemented.');
|
|
3630
|
-
}
|
|
3631
3522
|
async getVersion(options) {
|
|
3632
|
-
|
|
3633
|
-
|
|
3634
|
-
return Promise.reject('Must provide a database name');
|
|
3635
|
-
}
|
|
3636
|
-
const dbName = options.database;
|
|
3637
|
-
keys = Object.keys(this._dbDict);
|
|
3638
|
-
if (!keys.includes(dbName)) {
|
|
3639
|
-
return Promise.reject(`Open: No available connection for ${dbName}`);
|
|
3640
|
-
}
|
|
3641
|
-
const mDB = this._dbDict[dbName];
|
|
3523
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3524
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
3642
3525
|
try {
|
|
3643
|
-
const version = await
|
|
3644
|
-
const
|
|
3645
|
-
|
|
3646
|
-
return
|
|
3526
|
+
const version = await database.getVersion();
|
|
3527
|
+
const versionResult = {};
|
|
3528
|
+
versionResult.version = version;
|
|
3529
|
+
return versionResult;
|
|
3647
3530
|
}
|
|
3648
3531
|
catch (err) {
|
|
3649
|
-
|
|
3532
|
+
throw new Error(`GetVersion: ${err}`);
|
|
3650
3533
|
}
|
|
3651
3534
|
}
|
|
3652
3535
|
async getTableList(options) {
|
|
3653
|
-
|
|
3654
|
-
|
|
3655
|
-
return Promise.reject('Must provide a database name');
|
|
3656
|
-
}
|
|
3657
|
-
const dbName = options.database;
|
|
3658
|
-
keys = Object.keys(this._dbDict);
|
|
3659
|
-
if (!keys.includes(dbName)) {
|
|
3660
|
-
return Promise.reject(`Open: No available connection for ${dbName}`);
|
|
3661
|
-
}
|
|
3662
|
-
const mDB = this._dbDict[dbName];
|
|
3536
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3537
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
3663
3538
|
try {
|
|
3664
|
-
const tableList = await
|
|
3665
|
-
const
|
|
3666
|
-
|
|
3667
|
-
return
|
|
3539
|
+
const tableList = await database.getTableList();
|
|
3540
|
+
const tableListResult = {};
|
|
3541
|
+
tableListResult.values = tableList;
|
|
3542
|
+
return tableListResult;
|
|
3668
3543
|
}
|
|
3669
3544
|
catch (err) {
|
|
3670
|
-
|
|
3545
|
+
throw new Error(`GetTableList: ${err}`);
|
|
3671
3546
|
}
|
|
3672
3547
|
}
|
|
3673
3548
|
async execute(options) {
|
|
3674
|
-
|
|
3675
|
-
|
|
3676
|
-
|
|
3677
|
-
|
|
3678
|
-
if (!keys.includes('statements') || options.statements.length === 0) {
|
|
3679
|
-
return Promise.reject('Must provide raw SQL statements');
|
|
3680
|
-
}
|
|
3681
|
-
const dbName = options.database;
|
|
3682
|
-
const statements = options.statements;
|
|
3683
|
-
let transaction;
|
|
3684
|
-
if (!keys.includes('transaction')) {
|
|
3685
|
-
transaction = true;
|
|
3686
|
-
}
|
|
3687
|
-
else {
|
|
3688
|
-
transaction = options.transaction;
|
|
3689
|
-
}
|
|
3690
|
-
keys = Object.keys(this._dbDict);
|
|
3691
|
-
if (!keys.includes(dbName)) {
|
|
3692
|
-
return Promise.reject(`Execute: No available connection for ${dbName}`);
|
|
3693
|
-
}
|
|
3694
|
-
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);
|
|
3695
3553
|
try {
|
|
3696
|
-
const
|
|
3697
|
-
if (
|
|
3698
|
-
|
|
3554
|
+
const executeResult = await database.executeSQL(statements, transaction);
|
|
3555
|
+
if (executeResult < 0) {
|
|
3556
|
+
throw new Error('Execute failed changes < 0');
|
|
3699
3557
|
}
|
|
3700
3558
|
else {
|
|
3701
|
-
return
|
|
3559
|
+
return { changes: { changes: executeResult } };
|
|
3702
3560
|
}
|
|
3703
3561
|
}
|
|
3704
3562
|
catch (err) {
|
|
3705
|
-
|
|
3563
|
+
throw new Error(`Execute failed: ${err}`);
|
|
3706
3564
|
}
|
|
3707
3565
|
}
|
|
3708
3566
|
async executeSet(options) {
|
|
3709
|
-
|
|
3710
|
-
|
|
3711
|
-
|
|
3712
|
-
|
|
3713
|
-
if (!keys.includes('set') || options.set.length === 0) {
|
|
3714
|
-
return Promise.reject('Must provide a non-empty set of SQL statements');
|
|
3715
|
-
}
|
|
3716
|
-
const dbName = options.database;
|
|
3717
|
-
const setOfStatements = options.set;
|
|
3718
|
-
let transaction;
|
|
3719
|
-
if (!keys.includes('transaction')) {
|
|
3720
|
-
transaction = true;
|
|
3721
|
-
}
|
|
3722
|
-
else {
|
|
3723
|
-
transaction = options.transaction;
|
|
3724
|
-
}
|
|
3725
|
-
keys = Object.keys(this._dbDict);
|
|
3726
|
-
if (!keys.includes(dbName)) {
|
|
3727
|
-
return Promise.reject(`ExecuteSet: No available connection for ${dbName}`);
|
|
3728
|
-
}
|
|
3729
|
-
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);
|
|
3730
3571
|
for (const sStmt of setOfStatements) {
|
|
3731
3572
|
if (!('statement' in sStmt) || !('values' in sStmt)) {
|
|
3732
|
-
|
|
3573
|
+
throw new Error('ExecuteSet: Must provide a set as ' + 'Array of {statement,values}');
|
|
3733
3574
|
}
|
|
3734
3575
|
}
|
|
3735
3576
|
try {
|
|
3736
|
-
const
|
|
3737
|
-
if (
|
|
3738
|
-
|
|
3577
|
+
const execSetResult = await database.execSet(setOfStatements, transaction);
|
|
3578
|
+
if (execSetResult < 0) {
|
|
3579
|
+
throw new Error(`ExecuteSet failed changes <0`);
|
|
3739
3580
|
}
|
|
3740
3581
|
else {
|
|
3741
|
-
return
|
|
3582
|
+
return { changes: execSetResult };
|
|
3742
3583
|
}
|
|
3743
3584
|
}
|
|
3744
3585
|
catch (err) {
|
|
3745
|
-
|
|
3586
|
+
throw new Error(`ExecuteSet failed: ${err}`);
|
|
3746
3587
|
}
|
|
3747
3588
|
}
|
|
3748
3589
|
async run(options) {
|
|
3749
|
-
|
|
3750
|
-
|
|
3751
|
-
|
|
3752
|
-
|
|
3753
|
-
|
|
3754
|
-
return Promise.reject('Must provide a query statement');
|
|
3755
|
-
}
|
|
3756
|
-
if (!keys.includes('values')) {
|
|
3757
|
-
return Promise.reject('Must provide an Array of values');
|
|
3758
|
-
}
|
|
3759
|
-
const dbName = options.database;
|
|
3760
|
-
const statement = options.statement;
|
|
3761
|
-
const values = options.values.length > 0 ? options.values : [];
|
|
3762
|
-
let transaction;
|
|
3763
|
-
if (!keys.includes('transaction')) {
|
|
3764
|
-
transaction = true;
|
|
3765
|
-
}
|
|
3766
|
-
else {
|
|
3767
|
-
transaction = options.transaction;
|
|
3768
|
-
}
|
|
3769
|
-
keys = Object.keys(this._dbDict);
|
|
3770
|
-
if (!keys.includes(dbName)) {
|
|
3771
|
-
return Promise.reject(`Run: No available connection for ${dbName}`);
|
|
3772
|
-
}
|
|
3773
|
-
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);
|
|
3774
3595
|
try {
|
|
3775
|
-
const
|
|
3776
|
-
return
|
|
3596
|
+
const runResult = await database.runSQL(statement, values, transaction);
|
|
3597
|
+
return { changes: runResult };
|
|
3777
3598
|
}
|
|
3778
3599
|
catch (err) {
|
|
3779
|
-
|
|
3600
|
+
throw new Error(`RUN failed: ${err} `);
|
|
3780
3601
|
}
|
|
3781
3602
|
}
|
|
3782
3603
|
async query(options) {
|
|
3783
|
-
|
|
3784
|
-
|
|
3785
|
-
|
|
3786
|
-
|
|
3787
|
-
|
|
3788
|
-
return Promise.reject('Must provide a query statement');
|
|
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.');
|
|
3789
3609
|
}
|
|
3790
|
-
|
|
3791
|
-
return Promise.reject('Must provide an Array of any');
|
|
3792
|
-
}
|
|
3793
|
-
const dbName = options.database;
|
|
3794
|
-
const statement = options.statement;
|
|
3795
|
-
const values = options.values.length > 0 ? options.values : [];
|
|
3796
|
-
keys = Object.keys(this._dbDict);
|
|
3797
|
-
if (!keys.includes(dbName)) {
|
|
3798
|
-
return Promise.reject(`Query: No available connection for ${dbName}`);
|
|
3799
|
-
}
|
|
3800
|
-
const mDB = this._dbDict[dbName];
|
|
3801
|
-
let ret = [];
|
|
3610
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
3802
3611
|
try {
|
|
3803
|
-
|
|
3804
|
-
return
|
|
3612
|
+
const queryResult = await database.selectSQL(statement, values);
|
|
3613
|
+
return { values: queryResult };
|
|
3805
3614
|
}
|
|
3806
3615
|
catch (err) {
|
|
3807
|
-
|
|
3616
|
+
throw new Error(`Query failed: ${err}`);
|
|
3808
3617
|
}
|
|
3809
3618
|
}
|
|
3810
3619
|
async isDBExists(options) {
|
|
3811
|
-
|
|
3812
|
-
if
|
|
3813
|
-
|
|
3814
|
-
|
|
3815
|
-
|
|
3816
|
-
keys = Object.keys(this._dbDict);
|
|
3817
|
-
if (!keys.includes(dbName)) {
|
|
3818
|
-
return Promise.reject('IsDBExists command failed: No available ' + 'connection for ' + dbName);
|
|
3819
|
-
}
|
|
3820
|
-
const isExists = this._uFile.isFileExists(dbName + 'SQLite.db');
|
|
3821
|
-
return Promise.resolve({
|
|
3822
|
-
result: isExists,
|
|
3823
|
-
});
|
|
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 };
|
|
3824
3625
|
}
|
|
3825
3626
|
async isDBOpen(options) {
|
|
3826
|
-
|
|
3827
|
-
|
|
3828
|
-
|
|
3829
|
-
}
|
|
3830
|
-
const dbName = options.database;
|
|
3831
|
-
keys = Object.keys(this._dbDict);
|
|
3832
|
-
if (!keys.includes(dbName)) {
|
|
3833
|
-
return Promise.reject('isDBOpen command failed: No available ' + 'connection for ' + dbName);
|
|
3834
|
-
}
|
|
3835
|
-
const mDB = this._dbDict[dbName];
|
|
3836
|
-
const isOpen = await mDB.isDBOpen();
|
|
3837
|
-
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 };
|
|
3838
3631
|
}
|
|
3839
3632
|
async isDatabase(options) {
|
|
3840
|
-
const
|
|
3841
|
-
|
|
3842
|
-
|
|
3843
|
-
}
|
|
3844
|
-
const dbName = options.database;
|
|
3845
|
-
const isExists = this._uFile.isFileExists(dbName + 'SQLite.db');
|
|
3846
|
-
return Promise.resolve({
|
|
3847
|
-
result: isExists,
|
|
3848
|
-
});
|
|
3633
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3634
|
+
const isExists = this.fileUtil.isFileExists(dbName + 'SQLite.db');
|
|
3635
|
+
return { result: isExists };
|
|
3849
3636
|
}
|
|
3850
3637
|
async isTableExists(options) {
|
|
3851
|
-
|
|
3852
|
-
|
|
3853
|
-
|
|
3854
|
-
}
|
|
3855
|
-
const dbName = options.database;
|
|
3856
|
-
if (!keys.includes('table')) {
|
|
3857
|
-
return Promise.reject('Must provide a table name');
|
|
3858
|
-
}
|
|
3859
|
-
const tableName = options.table;
|
|
3860
|
-
keys = Object.keys(this._dbDict);
|
|
3861
|
-
if (!keys.includes(dbName)) {
|
|
3862
|
-
return Promise.reject('isTableExists command failed: No available ' +
|
|
3863
|
-
'connection for ' +
|
|
3864
|
-
dbName);
|
|
3865
|
-
}
|
|
3866
|
-
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);
|
|
3867
3641
|
try {
|
|
3868
|
-
const
|
|
3869
|
-
return
|
|
3642
|
+
const isTableExistsResult = await database.isTableExists(tableName);
|
|
3643
|
+
return { result: isTableExistsResult };
|
|
3870
3644
|
}
|
|
3871
3645
|
catch (err) {
|
|
3872
|
-
|
|
3646
|
+
throw new Error(`isTableExists: ${err}`);
|
|
3873
3647
|
}
|
|
3874
3648
|
}
|
|
3875
3649
|
async deleteDatabase(options) {
|
|
3876
|
-
|
|
3877
|
-
|
|
3878
|
-
return Promise.reject('Must provide a database name');
|
|
3879
|
-
}
|
|
3880
|
-
const dbName = options.database;
|
|
3881
|
-
keys = Object.keys(this._dbDict);
|
|
3882
|
-
if (!keys.includes(dbName)) {
|
|
3883
|
-
return Promise.reject('deleteDatabase: No available connection for ' + `${dbName}`);
|
|
3884
|
-
}
|
|
3885
|
-
const mDB = this._dbDict[dbName];
|
|
3650
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3651
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
3886
3652
|
try {
|
|
3887
|
-
await
|
|
3888
|
-
return
|
|
3653
|
+
await database.deleteDB(dbName + 'SQLite.db');
|
|
3654
|
+
return;
|
|
3889
3655
|
}
|
|
3890
3656
|
catch (err) {
|
|
3891
|
-
|
|
3657
|
+
throw new Error(`Delete: ${err}`);
|
|
3892
3658
|
}
|
|
3893
3659
|
}
|
|
3894
3660
|
async isJsonValid(options) {
|
|
3895
|
-
const
|
|
3896
|
-
|
|
3897
|
-
|
|
3898
|
-
}
|
|
3899
|
-
const jsonStrObj = options.jsonstring;
|
|
3900
|
-
const jsonObj = JSON.parse(jsonStrObj);
|
|
3901
|
-
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);
|
|
3902
3664
|
if (!isValid) {
|
|
3903
|
-
|
|
3665
|
+
throw new Error('Stringify Json Object not Valid');
|
|
3904
3666
|
}
|
|
3905
3667
|
else {
|
|
3906
|
-
return
|
|
3668
|
+
return { result: true };
|
|
3907
3669
|
}
|
|
3908
3670
|
}
|
|
3909
3671
|
async importFromJson(options) {
|
|
3910
3672
|
var _a, _b;
|
|
3911
|
-
const
|
|
3912
|
-
|
|
3913
|
-
|
|
3914
|
-
}
|
|
3915
|
-
const jsonStrObj = options.jsonstring;
|
|
3916
|
-
const jsonObj = JSON.parse(jsonStrObj);
|
|
3917
|
-
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);
|
|
3918
3676
|
if (!isValid) {
|
|
3919
|
-
|
|
3677
|
+
throw new Error('Must provide a valid JsonSQLite Object');
|
|
3920
3678
|
}
|
|
3921
3679
|
const vJsonObj = jsonObj;
|
|
3922
3680
|
const dbName = `${vJsonObj.database}SQLite.db`;
|
|
3923
|
-
const
|
|
3681
|
+
const targetDbVersion = (_a = vJsonObj.version) !== null && _a !== void 0 ? _a : 1;
|
|
3924
3682
|
const mode = vJsonObj.mode;
|
|
3925
3683
|
const overwrite = (_b = vJsonObj.overwrite) !== null && _b !== void 0 ? _b : false;
|
|
3926
3684
|
// const encrypted: boolean = vJsonObj.encrypted ?? false;
|
|
3927
3685
|
// const mode: string = encrypted ? 'secret' : 'no-encryption';
|
|
3928
3686
|
// Create the database
|
|
3929
|
-
const
|
|
3930
|
-
/*encrypted, mode, */
|
|
3687
|
+
const database = new Database_1.Database(dbName,
|
|
3688
|
+
/*encrypted, mode, */
|
|
3689
|
+
targetDbVersion, {});
|
|
3931
3690
|
try {
|
|
3932
3691
|
if (overwrite && mode === 'full') {
|
|
3933
|
-
const isExists = this.
|
|
3692
|
+
const isExists = this.fileUtil.isFileExists(dbName);
|
|
3934
3693
|
if (isExists) {
|
|
3935
|
-
await this.
|
|
3694
|
+
await this.fileUtil.deleteFileName(dbName);
|
|
3936
3695
|
}
|
|
3937
3696
|
}
|
|
3938
3697
|
// Open the database
|
|
3939
|
-
await
|
|
3940
|
-
const tableList = await
|
|
3698
|
+
await database.open();
|
|
3699
|
+
const tableList = await database.getTableList();
|
|
3941
3700
|
if (mode === 'full' && tableList.length > 0) {
|
|
3942
|
-
const
|
|
3943
|
-
if (
|
|
3944
|
-
|
|
3701
|
+
const currentVersion = await database.getVersion();
|
|
3702
|
+
if (targetDbVersion < currentVersion) {
|
|
3703
|
+
throw new Error(`ImportFromJson: Cannot import a version lower than ${currentVersion}`);
|
|
3945
3704
|
}
|
|
3946
|
-
if (
|
|
3947
|
-
return
|
|
3705
|
+
if (currentVersion === targetDbVersion) {
|
|
3706
|
+
return { changes: { changes: 0 } };
|
|
3948
3707
|
}
|
|
3949
3708
|
}
|
|
3950
3709
|
// Import the JsonSQLite Object
|
|
3951
|
-
const changes = await
|
|
3710
|
+
const changes = await database.importJson(vJsonObj);
|
|
3952
3711
|
// Close the database
|
|
3953
|
-
await
|
|
3954
|
-
return
|
|
3712
|
+
await database.close();
|
|
3713
|
+
return { changes: { changes: changes } };
|
|
3955
3714
|
}
|
|
3956
3715
|
catch (err) {
|
|
3957
|
-
|
|
3716
|
+
throw new Error(`ImportFromJson: ${err}`);
|
|
3958
3717
|
}
|
|
3959
3718
|
}
|
|
3960
3719
|
async exportToJson(options) {
|
|
3961
|
-
|
|
3962
|
-
|
|
3963
|
-
|
|
3964
|
-
}
|
|
3965
|
-
if (!keys.includes('jsonexportmode')) {
|
|
3966
|
-
return Promise.reject('Must provide a json export mode');
|
|
3967
|
-
}
|
|
3968
|
-
const dbName = options.database;
|
|
3969
|
-
const exportMode = options.jsonexportmode;
|
|
3970
|
-
keys = Object.keys(this._dbDict);
|
|
3971
|
-
if (!keys.includes(dbName)) {
|
|
3972
|
-
return Promise.reject('exportToJson: No available connection for ' + `${dbName}`);
|
|
3973
|
-
}
|
|
3974
|
-
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);
|
|
3975
3723
|
try {
|
|
3976
|
-
const
|
|
3977
|
-
const
|
|
3978
|
-
if (
|
|
3979
|
-
|
|
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}`);
|
|
3980
3728
|
}
|
|
3981
3729
|
else {
|
|
3982
|
-
return
|
|
3730
|
+
return { export: exportJsonResult };
|
|
3983
3731
|
}
|
|
3984
3732
|
}
|
|
3985
3733
|
catch (err) {
|
|
3986
|
-
|
|
3734
|
+
throw new Error(`exportToJson: ${err}`);
|
|
3987
3735
|
}
|
|
3988
3736
|
}
|
|
3989
3737
|
async createSyncTable(options) {
|
|
3990
|
-
|
|
3991
|
-
|
|
3992
|
-
return Promise.reject('Must provide a database name');
|
|
3993
|
-
}
|
|
3994
|
-
const dbName = options.database;
|
|
3995
|
-
keys = Object.keys(this._dbDict);
|
|
3996
|
-
if (!keys.includes(dbName)) {
|
|
3997
|
-
return Promise.reject('CreateSyncTable: No available connection for ' + `${dbName}`);
|
|
3998
|
-
}
|
|
3999
|
-
const mDB = this._dbDict[dbName];
|
|
3738
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3739
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
4000
3740
|
try {
|
|
4001
|
-
const
|
|
4002
|
-
return
|
|
3741
|
+
const createTableSyncResult = await database.createSyncTable();
|
|
3742
|
+
return {
|
|
3743
|
+
changes: { changes: createTableSyncResult }
|
|
3744
|
+
};
|
|
4003
3745
|
}
|
|
4004
3746
|
catch (err) {
|
|
4005
|
-
|
|
3747
|
+
throw new Error(`createSyncTable: ${err}`);
|
|
4006
3748
|
}
|
|
4007
3749
|
}
|
|
4008
3750
|
async setSyncDate(options) {
|
|
4009
|
-
|
|
4010
|
-
|
|
4011
|
-
|
|
4012
|
-
}
|
|
4013
|
-
if (!keys.includes('syncdate')) {
|
|
4014
|
-
return Promise.reject('Must provide a synchronization date');
|
|
4015
|
-
}
|
|
4016
|
-
const dbName = options.database;
|
|
4017
|
-
const syncDate = options.syncdate;
|
|
4018
|
-
keys = Object.keys(this._dbDict);
|
|
4019
|
-
if (!keys.includes(dbName)) {
|
|
4020
|
-
return Promise.reject(`SetSyncDate: No available connection for ${dbName}`);
|
|
4021
|
-
}
|
|
4022
|
-
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);
|
|
4023
3754
|
try {
|
|
4024
|
-
await
|
|
4025
|
-
return
|
|
3755
|
+
await database.setSyncDate(syncDate);
|
|
3756
|
+
return;
|
|
4026
3757
|
}
|
|
4027
3758
|
catch (err) {
|
|
4028
|
-
|
|
3759
|
+
throw new Error(`SetSyncDate: ${err}`);
|
|
4029
3760
|
}
|
|
4030
3761
|
}
|
|
4031
3762
|
async getSyncDate(options) {
|
|
4032
|
-
|
|
4033
|
-
|
|
4034
|
-
return Promise.reject('Must provide a database name');
|
|
4035
|
-
}
|
|
4036
|
-
const dbName = options.database;
|
|
4037
|
-
keys = Object.keys(this._dbDict);
|
|
4038
|
-
if (!keys.includes(dbName)) {
|
|
4039
|
-
return Promise.reject(`GetSyncDate: No available connection for ${dbName}`);
|
|
4040
|
-
}
|
|
4041
|
-
const mDB = this._dbDict[dbName];
|
|
3763
|
+
const dbName = this.getOptionValue(options, 'database');
|
|
3764
|
+
const database = this.getDatabaseConnectionOrThrowError(dbName);
|
|
4042
3765
|
try {
|
|
4043
|
-
const ret = await
|
|
3766
|
+
const ret = await database.getSyncDate();
|
|
4044
3767
|
return Promise.resolve(ret);
|
|
4045
3768
|
}
|
|
4046
3769
|
catch (err) {
|
|
4047
|
-
|
|
3770
|
+
throw new Error(`GetSyncDate: ${err}`);
|
|
4048
3771
|
}
|
|
4049
3772
|
}
|
|
4050
3773
|
async addUpgradeStatement(options) {
|
|
4051
|
-
|
|
4052
|
-
|
|
4053
|
-
|
|
4054
|
-
|
|
4055
|
-
if (!
|
|
4056
|
-
|
|
4057
|
-
|
|
4058
|
-
|
|
4059
|
-
|
|
4060
|
-
|
|
4061
|
-
|
|
4062
|
-
|
|
4063
|
-
|
|
4064
|
-
|
|
4065
|
-
|
|
4066
|
-
|
|
4067
|
-
return Promise.reject('ugrade.fromVersion must be a number');
|
|
4068
|
-
}
|
|
4069
|
-
const upgVDict = {};
|
|
4070
|
-
upgVDict[upgrade.fromVersion] = upgrade;
|
|
4071
|
-
this._versionUpgrades[dbName] = upgVDict;
|
|
4072
|
-
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;
|
|
4073
3790
|
}
|
|
4074
3791
|
async copyFromAssets(options) {
|
|
4075
|
-
const
|
|
4076
|
-
const mOverwrite = keys.includes('overwrite') ? options.overwrite : true;
|
|
3792
|
+
const overwrite = this.getOptionValue(options, 'overwrite', false);
|
|
4077
3793
|
// check if the assets/database folder exists
|
|
4078
|
-
const assetsDbPath = this.
|
|
4079
|
-
const
|
|
4080
|
-
if (
|
|
3794
|
+
const assetsDbPath = this.fileUtil.getAssetsDatabasesPath();
|
|
3795
|
+
const pathExists = this.fileUtil.isPathExists(assetsDbPath);
|
|
3796
|
+
if (pathExists) {
|
|
4081
3797
|
// get the database files
|
|
4082
|
-
const dbList = await this.
|
|
3798
|
+
const dbList = await this.fileUtil.getFileList(assetsDbPath);
|
|
4083
3799
|
// loop through the database files
|
|
4084
3800
|
dbList.forEach(async (db) => {
|
|
4085
3801
|
if (db.substring(db.length - 3) === '.db') {
|
|
4086
3802
|
// for each copy the file to the Application database folder
|
|
4087
|
-
await this.
|
|
3803
|
+
await this.fileUtil.copyFromAssetToDatabase(db, overwrite);
|
|
4088
3804
|
}
|
|
4089
3805
|
if (db.substring(db.length - 4) === '.zip') {
|
|
4090
|
-
await this.
|
|
3806
|
+
await this.fileUtil.unzipDatabase(db, overwrite);
|
|
4091
3807
|
}
|
|
4092
3808
|
});
|
|
4093
|
-
return
|
|
3809
|
+
return;
|
|
4094
3810
|
}
|
|
4095
3811
|
else {
|
|
4096
|
-
|
|
3812
|
+
throw new Error('CopyFromAssets: assets/databases folder does not exist');
|
|
4097
3813
|
}
|
|
4098
3814
|
}
|
|
4099
3815
|
async getDatabaseList() {
|
|
4100
3816
|
// get the database folder
|
|
4101
|
-
const pathDatabase = this.
|
|
3817
|
+
const pathDatabase = this.fileUtil.getDatabasesPath();
|
|
4102
3818
|
// get the list of databases
|
|
4103
|
-
const files = await this.
|
|
3819
|
+
const files = await this.fileUtil.getFileList(pathDatabase);
|
|
4104
3820
|
if (files.length > 0) {
|
|
4105
|
-
return
|
|
3821
|
+
return { values: files };
|
|
4106
3822
|
}
|
|
4107
3823
|
else {
|
|
4108
|
-
|
|
3824
|
+
throw new Error(`isTableExists: No databases found`);
|
|
4109
3825
|
}
|
|
4110
3826
|
}
|
|
4111
|
-
async getMigratableDbList(options) {
|
|
4112
|
-
console.log('getCordovaDbList', options);
|
|
4113
|
-
return Promise.reject('Method not implemented.');
|
|
4114
|
-
}
|
|
4115
|
-
async addSQLiteSuffix(options) {
|
|
4116
|
-
console.log(`${JSON.stringify(options)}`);
|
|
4117
|
-
throw new Error('Method not implemented.');
|
|
4118
|
-
}
|
|
4119
|
-
async deleteOldDatabases(options) {
|
|
4120
|
-
console.log(`${JSON.stringify(options)}`);
|
|
4121
|
-
throw new Error('Method not implemented.');
|
|
4122
|
-
}
|
|
4123
3827
|
async checkConnectionsConsistency(options) {
|
|
4124
|
-
const
|
|
4125
|
-
|
|
4126
|
-
|
|
4127
|
-
}
|
|
4128
|
-
const dbNames = options.dbNames;
|
|
4129
|
-
const ret = {};
|
|
4130
|
-
ret.result = false;
|
|
3828
|
+
const dbNames = this.getOptionValue(options, 'dbNames');
|
|
3829
|
+
const checkConsistencyResult = {};
|
|
3830
|
+
checkConsistencyResult.result = false;
|
|
4131
3831
|
try {
|
|
4132
|
-
let inConnectionsSet = new Set(Object.keys(this.
|
|
3832
|
+
let inConnectionsSet = new Set(Object.keys(this.databases));
|
|
4133
3833
|
const outConnectionSet = new Set(dbNames);
|
|
4134
3834
|
if (outConnectionSet.size === 0) {
|
|
4135
|
-
await this.resetDbDict(Object.keys(this.
|
|
4136
|
-
return Promise.resolve(
|
|
3835
|
+
await this.resetDbDict(Object.keys(this.databases));
|
|
3836
|
+
return Promise.resolve(checkConsistencyResult);
|
|
4137
3837
|
}
|
|
4138
3838
|
if (inConnectionsSet.size < outConnectionSet.size) {
|
|
4139
|
-
await this.resetDbDict(Object.keys(this.
|
|
4140
|
-
return Promise.resolve(
|
|
3839
|
+
await this.resetDbDict(Object.keys(this.databases));
|
|
3840
|
+
return Promise.resolve(checkConsistencyResult);
|
|
4141
3841
|
}
|
|
4142
3842
|
if (inConnectionsSet.size > outConnectionSet.size) {
|
|
4143
3843
|
for (const key of inConnectionsSet) {
|
|
@@ -4148,25 +3848,25 @@ class CapacitorSQLite {
|
|
|
4148
3848
|
}
|
|
4149
3849
|
}
|
|
4150
3850
|
}
|
|
4151
|
-
inConnectionsSet = new Set(Object.keys(this.
|
|
3851
|
+
inConnectionsSet = new Set(Object.keys(this.databases));
|
|
4152
3852
|
if (inConnectionsSet.size === outConnectionSet.size) {
|
|
4153
|
-
const
|
|
4154
|
-
if (
|
|
4155
|
-
|
|
4156
|
-
return
|
|
3853
|
+
const symmetricDifferenceSet = await this.symmetricDifference(inConnectionsSet, outConnectionSet);
|
|
3854
|
+
if (symmetricDifferenceSet.size === 0) {
|
|
3855
|
+
checkConsistencyResult.result = true;
|
|
3856
|
+
return checkConsistencyResult;
|
|
4157
3857
|
}
|
|
4158
3858
|
else {
|
|
4159
|
-
await this.resetDbDict(Object.keys(this.
|
|
4160
|
-
return
|
|
3859
|
+
await this.resetDbDict(Object.keys(this.databases));
|
|
3860
|
+
return checkConsistencyResult;
|
|
4161
3861
|
}
|
|
4162
3862
|
}
|
|
4163
3863
|
else {
|
|
4164
|
-
await this.resetDbDict(Object.keys(this.
|
|
4165
|
-
return
|
|
3864
|
+
await this.resetDbDict(Object.keys(this.databases));
|
|
3865
|
+
return checkConsistencyResult;
|
|
4166
3866
|
}
|
|
4167
3867
|
}
|
|
4168
3868
|
catch (err) {
|
|
4169
|
-
|
|
3869
|
+
throw new Error(`CheckConnectionsConsistency: ${err}`);
|
|
4170
3870
|
}
|
|
4171
3871
|
}
|
|
4172
3872
|
async resetDbDict(keys) {
|
|
@@ -4178,7 +3878,7 @@ class CapacitorSQLite {
|
|
|
4178
3878
|
}
|
|
4179
3879
|
}
|
|
4180
3880
|
catch (err) {
|
|
4181
|
-
|
|
3881
|
+
throw new Error(`ResetDbDict: ${err}`);
|
|
4182
3882
|
}
|
|
4183
3883
|
}
|
|
4184
3884
|
async symmetricDifference(setA, setB) {
|
|
@@ -4191,7 +3891,95 @@ class CapacitorSQLite {
|
|
|
4191
3891
|
difference.add(elem);
|
|
4192
3892
|
}
|
|
4193
3893
|
}
|
|
4194
|
-
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.');
|
|
4195
3983
|
}
|
|
4196
3984
|
}
|
|
4197
3985
|
exports.CapacitorSQLite = src.CapacitorSQLite = CapacitorSQLite;
|