@capacitor-community/sqlite 4.1.0-5 → 4.1.0-6

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.
@@ -58,14 +58,9 @@ class UtilsSQLite {
58
58
  if (password.length > 0) {
59
59
  await this.setCipherPragma(mDB, password);
60
60
  }
61
- */
61
+ */
62
62
  // set Foreign Keys On
63
63
  await this.setForeignKeyConstraintsEnabled(mDB, true);
64
- // Check Version
65
- const curVersion = await this.getVersion(mDB);
66
- if (curVersion === 0) {
67
- await this.setVersion(mDB, 1);
68
- }
69
64
  }
70
65
  catch (err) {
71
66
  return Promise.reject(msg + `${err}`);
@@ -1692,11 +1687,11 @@ utilsJson.UtilsJson = UtilsJson;
1692
1687
  Object.defineProperty(exportToJson, "__esModule", { value: true });
1693
1688
  exportToJson.ExportToJson = void 0;
1694
1689
  const utilsSQLite_1$4 = utilsSQLite;
1695
- const utilsJson_1$4 = utilsJson;
1690
+ const utilsJson_1$3 = utilsJson;
1696
1691
  class ExportToJson {
1697
1692
  constructor() {
1698
1693
  this.sqliteUtil = new utilsSQLite_1$4.UtilsSQLite();
1699
- this.jsonUtil = new utilsJson_1$4.UtilsJson();
1694
+ this.jsonUtil = new utilsJson_1$3.UtilsJson();
1700
1695
  }
1701
1696
  /**
1702
1697
  * CreateExportObject
@@ -2514,14 +2509,14 @@ utilsDrop.UtilsDrop = UtilsDrop;
2514
2509
 
2515
2510
  Object.defineProperty(importFromJson, "__esModule", { value: true });
2516
2511
  importFromJson.ImportFromJson = void 0;
2517
- const utilsDrop_1$1 = utilsDrop;
2512
+ const utilsDrop_1 = utilsDrop;
2518
2513
  const utilsSQLite_1$2 = utilsSQLite;
2519
- const utilsJson_1$3 = utilsJson;
2514
+ const utilsJson_1$2 = utilsJson;
2520
2515
  class ImportFromJson {
2521
2516
  constructor() {
2522
- this.jsonUtil = new utilsJson_1$3.UtilsJson();
2517
+ this.jsonUtil = new utilsJson_1$2.UtilsJson();
2523
2518
  this.sqliteUtil = new utilsSQLite_1$2.UtilsSQLite();
2524
- this.dropUtil = new utilsDrop_1$1.UtilsDrop();
2519
+ this.dropUtil = new utilsDrop_1.UtilsDrop();
2525
2520
  }
2526
2521
  /**
2527
2522
  * CreateDatabaseSchema
@@ -3098,18 +3093,10 @@ var utilsUpgrade = {};
3098
3093
 
3099
3094
  Object.defineProperty(utilsUpgrade, "__esModule", { value: true });
3100
3095
  utilsUpgrade.UtilsUpgrade = void 0;
3101
- const utilsJson_1$2 = utilsJson;
3102
- const utilsDrop_1 = utilsDrop;
3103
- const utilsFile_1$2 = utilsFile;
3104
3096
  const utilsSQLite_1$1 = utilsSQLite;
3105
3097
  class UtilsUpgrade {
3106
3098
  constructor() {
3107
3099
  this.sqliteUtil = new utilsSQLite_1$1.UtilsSQLite();
3108
- this.fileUtil = new utilsFile_1$2.UtilsFile();
3109
- this.dropUtil = new utilsDrop_1.UtilsDrop();
3110
- this.jsonUtil = new utilsJson_1$2.UtilsJson();
3111
- this._alterTables = {};
3112
- this._commonColumns = {};
3113
3100
  }
3114
3101
  /**
3115
3102
  * OnUpgrade
@@ -3119,300 +3106,50 @@ class UtilsUpgrade {
3119
3106
  * @param curVersion
3120
3107
  * @param targetVersion
3121
3108
  */
3122
- async onUpgrade(mDB, vUpgDict, dbName, curVersion, targetVersion) {
3123
- const upgrade = vUpgDict[curVersion];
3124
- if (upgrade != null) {
3125
- const keys = Object.keys(upgrade);
3126
- if (!keys.includes('toVersion')) {
3127
- return Promise.reject('onUpgrade: toVersion not given');
3128
- }
3129
- const toVersion = upgrade.toVersion;
3130
- if (!keys.includes('statement')) {
3131
- return Promise.reject('onUpgrade: statement not given');
3132
- }
3133
- const statement = upgrade.statement;
3134
- let set = [];
3135
- if (keys.includes('set')) {
3136
- set = upgrade.set;
3137
- }
3138
- if (targetVersion < toVersion) {
3139
- let msg = 'Error: version mistmatch ';
3140
- msg += 'Upgrade Statement would upgrade to ';
3141
- msg += `version ${toVersion} , but target version `;
3142
- msg += `is ${targetVersion} for database ${dbName}`;
3143
- msg += ` and version ${curVersion}`;
3144
- return Promise.reject(`onUpgrade: ${msg}`);
3145
- }
3146
- try {
3147
- // set Foreign Keys Off
3148
- await this.sqliteUtil.setForeignKeyConstraintsEnabled(mDB, false);
3149
- await this.fileUtil.copyFileName(dbName, `backup-${dbName}`);
3150
- const initChanges = await this.sqliteUtil.dbChanges(mDB);
3151
- // Here we assume that all table schemas are given
3152
- // in the upgrade statement
3153
- if (statement.length > 0) {
3154
- await this.executeStatementProcess(mDB, statement);
3155
- // Here we assume that the Set contains only
3156
- // - the data for new tables
3157
- // as INSERT statements
3158
- // - the data for new columns in existing tables
3159
- // as UPDATE statements
3160
- if (set.length > 0) {
3161
- await this.executeSetProcess(mDB, set, toVersion);
3162
- }
3163
- }
3164
- // set Foreign Keys On
3165
- await this.sqliteUtil.setForeignKeyConstraintsEnabled(mDB, true);
3166
- const changes = (await this.sqliteUtil.dbChanges(mDB)) - initChanges;
3167
- return Promise.resolve(changes);
3168
- }
3169
- catch (err) {
3170
- return Promise.reject(`onUpgrade: ${err}`);
3171
- }
3172
- }
3173
- else {
3174
- return Promise.reject('onUpgrade: upgrade not found');
3175
- }
3176
- }
3177
- /**
3178
- * ExecuteStatementProcess
3179
- * @param mDB
3180
- * @param statement
3181
- */
3182
- async executeStatementProcess(mDB, statement) {
3183
- try {
3184
- // -> backup all existing tables "tableName" in
3185
- // "temp_tableName"
3186
- await this.backupTables(mDB);
3187
- // -> Drop all Indexes
3188
- await this.dropUtil.dropElements(mDB, 'index');
3189
- // -> Drop all Triggers
3190
- await this.dropUtil.dropElements(mDB, 'trigger');
3191
- // -> Create new tables from upgrade.statement
3192
- const changes = await this.sqliteUtil.execute(mDB, statement, false);
3193
- if (changes < 0) {
3194
- return Promise.reject('ExecuteStatementProcess: ' + 'changes < 0');
3195
- }
3196
- // -> Create the list of table's common fields
3197
- await this.findCommonColumns(mDB);
3198
- // -> Update the new table's data from old table's data
3199
- if (Object.keys(this._commonColumns).length > 0) {
3200
- await this.updateNewTablesData(mDB);
3201
- }
3202
- return Promise.resolve();
3203
- }
3204
- catch (err) {
3205
- return Promise.reject(`ExecuteStatementProcess: ${err}`);
3206
- }
3207
- finally {
3208
- // -> Drop _temp_tables
3209
- await this.dropUtil.dropTempTables(mDB, this._alterTables);
3210
- // -> Do some cleanup
3211
- this._alterTables = {};
3212
- this._commonColumns = {};
3213
- }
3214
- }
3215
- /**
3216
- * ExecuteSetProcess
3217
- * @param mDB
3218
- * @param set
3219
- * @param toVersion
3220
- */
3221
- async executeSetProcess(mDB, set, toVersion) {
3222
- try {
3223
- // -> load new data
3224
- const lastId = await this.sqliteUtil.executeSet(mDB, set, false);
3225
- if (lastId < 0) {
3226
- return Promise.reject('ExecuteSetProcess: lastId ' + '< 0');
3227
- }
3228
- // -> update database version
3229
- await this.sqliteUtil.setVersion(mDB, toVersion);
3230
- // -> update syncDate if any
3231
- const retB = await this.jsonUtil.isTableExists(mDB, true, 'sync_table');
3232
- if (retB) {
3233
- const sDate = Math.round(new Date().getTime() / 1000);
3234
- let stmt = 'UPDATE sync_table SET ';
3235
- stmt += `sync_date = ${sDate} WHERE id = 1;`;
3236
- const changes = await this.sqliteUtil.execute(mDB, stmt, false);
3237
- if (changes < 0) {
3238
- return Promise.reject('ExecuteSetProcess: changes ' + '< 0');
3109
+ async onUpgrade(mDB, vUpgDict, curVersion, targetVersion) {
3110
+ const sortedKeys = Object.keys(vUpgDict)
3111
+ .map(item => parseInt(item))
3112
+ .sort();
3113
+ for (const versionKey of sortedKeys) {
3114
+ if (versionKey > curVersion && versionKey <= targetVersion) {
3115
+ const statements = vUpgDict[versionKey].statements;
3116
+ if (statements.length === 0) {
3117
+ return Promise.reject('onUpgrade: statements not given');
3239
3118
  }
3240
- }
3241
- return Promise.resolve();
3242
- }
3243
- catch (err) {
3244
- return Promise.reject(`ExecuteSetProcess: ${err}`);
3245
- }
3246
- }
3247
- /**
3248
- * BackupTables
3249
- * @param mDB
3250
- */
3251
- async backupTables(mDB) {
3252
- const msg = 'BackupTables: ';
3253
- try {
3254
- const tables = await this.sqliteUtil.getTablesNames(mDB);
3255
- for (const table of tables) {
3256
3119
  try {
3257
- await this.backupTable(mDB, table);
3120
+ // set Foreign Keys Off
3121
+ await this.sqliteUtil.setForeignKeyConstraintsEnabled(mDB, false);
3122
+ const initChanges = await this.sqliteUtil.dbChanges(mDB);
3123
+ await this.executeStatementsProcess(mDB, statements);
3124
+ await this.sqliteUtil.setVersion(mDB, versionKey);
3125
+ // set Foreign Keys On
3126
+ await this.sqliteUtil.setForeignKeyConstraintsEnabled(mDB, true);
3127
+ const changes = (await this.sqliteUtil.dbChanges(mDB)) - initChanges;
3128
+ return Promise.resolve(changes);
3258
3129
  }
3259
3130
  catch (err) {
3260
- return Promise.reject(`${msg}table ${table}: ` + `${err}`);
3131
+ return Promise.reject(`onUpgrade: ${err}`);
3261
3132
  }
3262
3133
  }
3263
- return Promise.resolve();
3264
- }
3265
- catch (err) {
3266
- return Promise.reject(`BackupTables: ${err}`);
3267
3134
  }
3268
3135
  }
3269
3136
  /**
3270
- * BackupTable
3137
+ * ExecuteStatementProcess
3271
3138
  * @param mDB
3272
- * @param table
3139
+ * @param statements
3273
3140
  */
3274
- async backupTable(mDB, table) {
3141
+ async executeStatementsProcess(mDB, statements) {
3275
3142
  try {
3276
- // start a transaction
3277
3143
  await this.sqliteUtil.beginTransaction(mDB, true);
3278
- // get the table's column names
3279
- const colNames = await this.getTableColumnNames(mDB, table);
3280
- this._alterTables[`${table}`] = colNames;
3281
- const tmpTable = `_temp_${table}`;
3282
- // Drop the tmpTable if exists
3283
- const delStmt = `DROP TABLE IF EXISTS ${tmpTable};`;
3284
- await this.sqliteUtil.prepareRun(mDB, delStmt, [], false);
3285
- // prefix the table with _temp_
3286
- let stmt = `ALTER TABLE ${table} RENAME `;
3287
- stmt += `TO ${tmpTable};`;
3288
- const lastId = await this.sqliteUtil.prepareRun(mDB, stmt, [], false);
3289
- if (lastId < 0) {
3290
- let msg = 'BackupTable: lastId < 0';
3291
- try {
3292
- await this.sqliteUtil.rollbackTransaction(mDB, true);
3293
- }
3294
- catch (err) {
3295
- msg += `: ${err}`;
3296
- }
3297
- return Promise.reject(`${msg}`);
3298
- }
3299
- else {
3300
- try {
3301
- await this.sqliteUtil.commitTransaction(mDB, true);
3302
- }
3303
- catch (err) {
3304
- return Promise.reject('BackupTable: ' + `${err}`);
3305
- }
3306
- }
3307
- return Promise.resolve();
3308
- }
3309
- catch (err) {
3310
- return Promise.reject(`BackupTable: ${err}`);
3311
- }
3312
- }
3313
- /**
3314
- * GetTableColumnNames
3315
- * @param mDB
3316
- * @param tableName
3317
- */
3318
- async getTableColumnNames(mDB, tableName) {
3319
- let resQuery = [];
3320
- const retNames = [];
3321
- const query = `PRAGMA table_info('${tableName}');`;
3322
- try {
3323
- resQuery = await this.sqliteUtil.queryAll(mDB, query, []);
3324
- if (resQuery.length > 0) {
3325
- for (const query of resQuery) {
3326
- retNames.push(query.name);
3327
- }
3328
- }
3329
- return Promise.resolve(retNames);
3330
- }
3331
- catch (err) {
3332
- return Promise.reject('GetTableColumnNames: ' + `${err}`);
3333
- }
3334
- }
3335
- /**
3336
- * FindCommonColumns
3337
- * @param mDB
3338
- */
3339
- async findCommonColumns(mDB) {
3340
- try {
3341
- // Get new table list
3342
- const tables = await this.sqliteUtil.getTablesNames(mDB);
3343
- if (tables.length === 0) {
3344
- return Promise.reject('FindCommonColumns: get ' + "table's names failed");
3345
- }
3346
- for (const table of tables) {
3347
- // get the column's name
3348
- const tableNames = await this.getTableColumnNames(mDB, table);
3349
- // find the common columns
3350
- const keys = Object.keys(this._alterTables);
3351
- if (keys.includes(table)) {
3352
- this._commonColumns[table] = this.arraysIntersection(this._alterTables[table], tableNames);
3353
- }
3144
+ for (const statement of statements) {
3145
+ await this.sqliteUtil.execute(mDB, statement, false);
3354
3146
  }
3147
+ await this.sqliteUtil.commitTransaction(mDB, true);
3355
3148
  return Promise.resolve();
3356
3149
  }
3357
3150
  catch (err) {
3358
- return Promise.reject(`FindCommonColumns: ${err}`);
3359
- }
3360
- }
3361
- /**
3362
- * ArraysIntersection
3363
- * @param a1
3364
- * @param a2
3365
- */
3366
- arraysIntersection(a1, a2) {
3367
- if (a1 != null && a2 != null) {
3368
- const first = new Set(a1);
3369
- const second = new Set(a2);
3370
- return [...first].filter(item => second.has(item));
3371
- }
3372
- else {
3373
- return [];
3374
- }
3375
- }
3376
- /**
3377
- * UpdateNewTablesData
3378
- * @param mDB
3379
- */
3380
- async updateNewTablesData(mDB) {
3381
- try {
3382
- // start a transaction
3383
- await this.sqliteUtil.beginTransaction(mDB, true);
3384
- const statements = [];
3385
- const keys = Object.keys(this._commonColumns);
3386
- keys.forEach(key => {
3387
- const columns = this._commonColumns[key].join(',');
3388
- let stmt = `INSERT INTO ${key} `;
3389
- stmt += `(${columns}) `;
3390
- stmt += `SELECT ${columns} FROM _temp_${key};`;
3391
- statements.push(stmt);
3392
- });
3393
- const changes = await this.sqliteUtil.execute(mDB, statements.join('\n'), false);
3394
- if (changes < 0) {
3395
- let msg = 'updateNewTablesData: ' + 'changes < 0';
3396
- try {
3397
- await this.sqliteUtil.rollbackTransaction(mDB, true);
3398
- }
3399
- catch (err) {
3400
- msg += `: ${err}`;
3401
- }
3402
- return Promise.reject(`${msg}`);
3403
- }
3404
- else {
3405
- try {
3406
- await this.sqliteUtil.commitTransaction(mDB, true);
3407
- return Promise.resolve();
3408
- }
3409
- catch (err) {
3410
- return Promise.reject('updateNewTablesData: ' + `${err}`);
3411
- }
3412
- }
3413
- }
3414
- catch (err) {
3415
- return Promise.reject('updateNewTablesData: ' + `${err}`);
3151
+ await this.sqliteUtil.rollbackTransaction(mDB, true);
3152
+ return Promise.reject(`ExecuteStatementProcess: ${err}`);
3416
3153
  }
3417
3154
  }
3418
3155
  }
@@ -3496,8 +3233,9 @@ class Database {
3496
3233
  if (this.version > curVersion &&
3497
3234
  Object.keys(this.upgradeVersionDict).length > 0) {
3498
3235
  try {
3236
+ await this.fileUtil.copyFileName(this.dbName, `backup-${this.dbName}`);
3499
3237
  // execute the upgrade flow process
3500
- await this.upgradeUtil.onUpgrade(this.database, this.upgradeVersionDict, this.dbName, curVersion, this.version);
3238
+ await this.upgradeUtil.onUpgrade(this.database, this.upgradeVersionDict, curVersion, this.version);
3501
3239
  // delete the backup database
3502
3240
  await this.fileUtil.deleteFileName(`backup-${this.dbName}`);
3503
3241
  }
@@ -4279,16 +4017,15 @@ class CapacitorSQLite {
4279
4017
  const upgrades = this.getOptionValue(options, 'upgrade');
4280
4018
  const firstUpgrade = upgrades[0];
4281
4019
  const versionUpgradeKeys = Object.keys(firstUpgrade);
4282
- if (!versionUpgradeKeys.includes('fromVersion') ||
4283
- !versionUpgradeKeys.includes('toVersion') ||
4284
- !versionUpgradeKeys.includes('statement')) {
4020
+ if (!versionUpgradeKeys.includes('toVersion') ||
4021
+ !versionUpgradeKeys.includes('statements')) {
4285
4022
  throw new Error('Must provide an upgrade capSQLiteVersionUpgrade Object');
4286
4023
  }
4287
- if (typeof firstUpgrade.fromVersion != 'number') {
4288
- throw new Error('upgrade.fromVersion must be a number');
4024
+ if (typeof firstUpgrade.toVersion != 'number') {
4025
+ throw new Error('upgrade.toVersion must be a number');
4289
4026
  }
4290
4027
  const upgradeVersionDict = {};
4291
- upgradeVersionDict[firstUpgrade.fromVersion] = firstUpgrade;
4028
+ upgradeVersionDict[firstUpgrade.toVersion] = firstUpgrade;
4292
4029
  this.versionUpgrades[dbName] = upgradeVersionDict;
4293
4030
  return;
4294
4031
  }