@capacitor-community/sqlite 5.0.5-2 → 5.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.
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLite.java +118 -156
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLitePlugin.java +81 -249
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/NotificationCenter.java +1 -1
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/RetHandler.java +0 -12
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java +184 -40
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ExportToJson.java +141 -135
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ImportFromJson.java +2 -1
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/UtilsEncryption.java +111 -0
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsBiometric.java +0 -4
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsFile.java +30 -18
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsMigrate.java +12 -4
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsNCDatabase.java +4 -1
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsSQLCipher.java +8 -6
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsSQLite.java +14 -28
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsSecret.java +3 -4
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsUpgrade.java +0 -1
- package/dist/esm/definitions.d.ts +91 -4
- package/dist/esm/definitions.js +79 -19
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/web.d.ts +3 -1
- package/dist/esm/web.js +8 -0
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +87 -19
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +87 -19
- package/dist/plugin.js.map +1 -1
- package/electron/dist/plugin.js +333 -148
- package/electron/dist/plugin.js.map +1 -1
- package/electron/rollup.config.js +2 -0
- package/ios/Plugin/CapacitorSQLite.swift +125 -92
- package/ios/Plugin/CapacitorSQLitePlugin.swift +6 -3
- package/ios/Plugin/Database.swift +45 -19
- package/ios/Plugin/ImportExportJson/ExportToJson.swift +10 -5
- package/ios/Plugin/ImportExportJson/ImportData.swift +434 -0
- package/ios/Plugin/ImportExportJson/ImportFromJson.swift +47 -59
- package/ios/Plugin/ImportExportJson/JsonSQLite.swift +7 -0
- package/ios/Plugin/Utils/UtilsDownloadFromHTTP.swift +61 -61
- package/ios/Plugin/Utils/UtilsDrop.swift +2 -1
- package/ios/Plugin/Utils/UtilsJson.swift +123 -1
- package/ios/Plugin/Utils/UtilsSQLCipher.swift +254 -23
- package/ios/Plugin/Utils/UtilsUpgrade.swift +0 -1
- package/package.json +2 -2
- package/src/definitions.ts +171 -18
- package/src/web.ts +10 -0
package/electron/dist/plugin.js
CHANGED
|
@@ -11,6 +11,8 @@ var require$$5 = require('jszip');
|
|
|
11
11
|
var require$$6 = require('electron');
|
|
12
12
|
var require$$1$1 = require('better-sqlite3-multiple-ciphers');
|
|
13
13
|
var require$$3$1 = require('electron-json-storage');
|
|
14
|
+
var require$$1$2 = require('crypto');
|
|
15
|
+
var require$$2$1 = require('crypto-js');
|
|
14
16
|
|
|
15
17
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
16
18
|
|
|
@@ -23,6 +25,8 @@ var require$$5__default = /*#__PURE__*/_interopDefaultLegacy(require$$5);
|
|
|
23
25
|
var require$$6__default = /*#__PURE__*/_interopDefaultLegacy(require$$6);
|
|
24
26
|
var require$$1__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$1$1);
|
|
25
27
|
var require$$3__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$3$1);
|
|
28
|
+
var require$$1__default$2 = /*#__PURE__*/_interopDefaultLegacy(require$$1$2);
|
|
29
|
+
var require$$2__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$2$1);
|
|
26
30
|
|
|
27
31
|
var src = {};
|
|
28
32
|
|
|
@@ -84,33 +88,32 @@ class UtilsFile {
|
|
|
84
88
|
else {
|
|
85
89
|
this.capConfig = JSON.parse(this.NodeFs.readFileSync(this.Path.join(this.appPath, 'capacitor.config.json')).toString());
|
|
86
90
|
}
|
|
87
|
-
this.isEncryption = this.capConfig.plugins.CapacitorSQLite
|
|
91
|
+
this.isEncryption = this.capConfig.plugins.CapacitorSQLite
|
|
92
|
+
.electronIsEncryption
|
|
88
93
|
? this.capConfig.plugins.CapacitorSQLite.electronIsEncryption
|
|
89
94
|
: false;
|
|
90
95
|
this.osType = this.Os.type();
|
|
91
96
|
switch (this.osType) {
|
|
92
97
|
case 'Darwin':
|
|
93
|
-
this.pathDB =
|
|
94
|
-
this.capConfig.plugins.CapacitorSQLite.electronMacLocation
|
|
95
|
-
|
|
96
|
-
: 'Databases';
|
|
98
|
+
this.pathDB = this.capConfig.plugins.CapacitorSQLite.electronMacLocation
|
|
99
|
+
? this.capConfig.plugins.CapacitorSQLite.electronMacLocation
|
|
100
|
+
: 'Databases';
|
|
97
101
|
break;
|
|
98
102
|
case 'Linux':
|
|
99
|
-
this.pathDB =
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
+
this.pathDB = this.capConfig.plugins.CapacitorSQLite
|
|
104
|
+
.electronLinuxLocation
|
|
105
|
+
? this.capConfig.plugins.CapacitorSQLite.electronLinuxLocation
|
|
106
|
+
: 'Databases';
|
|
103
107
|
break;
|
|
104
108
|
case 'Windows_NT':
|
|
105
|
-
this.pathDB =
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
+
this.pathDB = this.capConfig.plugins.CapacitorSQLite
|
|
110
|
+
.electronWindowsLocation
|
|
111
|
+
? this.capConfig.plugins.CapacitorSQLite.electronWindowsLocation
|
|
112
|
+
: 'Databases';
|
|
109
113
|
break;
|
|
110
114
|
default:
|
|
111
115
|
console.log('other operating system');
|
|
112
116
|
}
|
|
113
|
-
console.log(`&&& Databases path: ${this.pathDB}`);
|
|
114
117
|
}
|
|
115
118
|
/**
|
|
116
119
|
* Get isEncryption from config
|
|
@@ -670,13 +673,13 @@ class UtilsSQLite {
|
|
|
670
673
|
let mDB;
|
|
671
674
|
if (!readonly) {
|
|
672
675
|
mDB = new this.BCSQLite3(pathDB, {
|
|
673
|
-
verbose: console.log,
|
|
676
|
+
// verbose: console.log,
|
|
674
677
|
fileMustExist: false,
|
|
675
678
|
});
|
|
676
679
|
}
|
|
677
680
|
else {
|
|
678
681
|
mDB = new this.BCSQLite3(pathDB, {
|
|
679
|
-
verbose: console.log,
|
|
682
|
+
// verbose: console.log,
|
|
680
683
|
readonly: true,
|
|
681
684
|
fileMustExist: true,
|
|
682
685
|
});
|
|
@@ -799,7 +802,7 @@ class UtilsSQLite {
|
|
|
799
802
|
*/
|
|
800
803
|
changePassword(pathDB, password, newpassword) {
|
|
801
804
|
let mDB;
|
|
802
|
-
const msg =
|
|
805
|
+
const msg = 'ChangePassword';
|
|
803
806
|
try {
|
|
804
807
|
mDB = this.openOrCreateDatabase(pathDB, password, false);
|
|
805
808
|
this.pragmaReKey(mDB, password, newpassword);
|
|
@@ -944,16 +947,16 @@ class UtilsSQLite {
|
|
|
944
947
|
*/
|
|
945
948
|
execute(mDB, sql, fromJson) {
|
|
946
949
|
const result = { changes: 0, lastId: -1 };
|
|
947
|
-
const msg =
|
|
950
|
+
const msg = 'Execute';
|
|
948
951
|
let changes = -1;
|
|
949
952
|
let lastId = -1;
|
|
950
953
|
let initChanges = -1;
|
|
951
954
|
try {
|
|
952
955
|
initChanges = this.dbChanges(mDB);
|
|
953
956
|
let sqlStmt = sql;
|
|
954
|
-
if (sql.toLowerCase().includes('DELETE FROM'.toLowerCase())
|
|
955
|
-
|
|
956
|
-
|
|
957
|
+
if (sql.toLowerCase().includes('DELETE FROM'.toLowerCase()) ||
|
|
958
|
+
sql.toLowerCase().includes('INSERT INTO'.toLowerCase()) ||
|
|
959
|
+
sql.toLowerCase().includes('UPDATE'.toLowerCase())) {
|
|
957
960
|
sqlStmt = this.checkStatements(mDB, sql, fromJson);
|
|
958
961
|
}
|
|
959
962
|
this.execDB(mDB, sqlStmt);
|
|
@@ -972,8 +975,8 @@ class UtilsSQLite {
|
|
|
972
975
|
// split the statements in an array of statement
|
|
973
976
|
let sqlStmt = sql.replace(/\n/g, '');
|
|
974
977
|
// deal with trigger
|
|
975
|
-
sqlStmt = sqlStmt.replace(/end;/g,
|
|
976
|
-
sqlStmt = sqlStmt.replace(/;END;/g,
|
|
978
|
+
sqlStmt = sqlStmt.replace(/end;/g, 'END;');
|
|
979
|
+
sqlStmt = sqlStmt.replace(/;END;/g, '&END;');
|
|
977
980
|
const sqlStmts = sqlStmt.split(';');
|
|
978
981
|
const resArr = [];
|
|
979
982
|
// loop through the statement
|
|
@@ -986,12 +989,11 @@ class UtilsSQLite {
|
|
|
986
989
|
switch (method) {
|
|
987
990
|
case 'CREATE':
|
|
988
991
|
if (rStmt.includes('&END')) {
|
|
989
|
-
rStmt = rStmt.replace(/&END/g,
|
|
992
|
+
rStmt = rStmt.replace(/&END/g, ';END');
|
|
990
993
|
}
|
|
991
994
|
break;
|
|
992
995
|
case 'DELETE':
|
|
993
|
-
if (!fromJson &&
|
|
994
|
-
stmt.toLowerCase().includes('WHERE'.toLowerCase())) {
|
|
996
|
+
if (!fromJson && stmt.toLowerCase().includes('WHERE'.toLowerCase())) {
|
|
995
997
|
const whereStmt = this.cleanStatement(`${stmt.trim()}`);
|
|
996
998
|
rStmt = this.deleteSQL(mDB, whereStmt, []);
|
|
997
999
|
}
|
|
@@ -1007,8 +1009,7 @@ class UtilsSQLite {
|
|
|
1007
1009
|
}
|
|
1008
1010
|
break;
|
|
1009
1011
|
case 'SELECT':
|
|
1010
|
-
if (!fromJson &&
|
|
1011
|
-
stmt.toLowerCase().includes('WHERE'.toLowerCase())) {
|
|
1012
|
+
if (!fromJson && stmt.toLowerCase().includes('WHERE'.toLowerCase())) {
|
|
1012
1013
|
rStmt = this.cleanStatement(`${stmt.trim()}`);
|
|
1013
1014
|
}
|
|
1014
1015
|
break;
|
|
@@ -1040,10 +1041,10 @@ class UtilsSQLite {
|
|
|
1040
1041
|
* @param set
|
|
1041
1042
|
* @param fromJson
|
|
1042
1043
|
*/
|
|
1043
|
-
executeSet(mDB, set, fromJson) {
|
|
1044
|
-
const ret = { changes: 0, lastId: -1 };
|
|
1044
|
+
executeSet(mDB, set, fromJson, returnMode) {
|
|
1045
|
+
const ret = { changes: 0, lastId: -1, values: [] };
|
|
1045
1046
|
let result = { changes: 0, lastId: -1 };
|
|
1046
|
-
const msg =
|
|
1047
|
+
const msg = 'ExecuteSet';
|
|
1047
1048
|
for (let i = 0; i < set.length; i++) {
|
|
1048
1049
|
const statement = 'statement' in set[i] ? set[i].statement : null;
|
|
1049
1050
|
const values = 'values' in set[i] && set[i].values.length > 0 ? set[i].values : [];
|
|
@@ -1056,22 +1057,28 @@ class UtilsSQLite {
|
|
|
1056
1057
|
if (Array.isArray(values[0])) {
|
|
1057
1058
|
for (const val of values) {
|
|
1058
1059
|
const mVal = this.replaceUndefinedByNull(val);
|
|
1059
|
-
result = this.prepareRun(mDB, statement, mVal, fromJson);
|
|
1060
|
+
result = this.prepareRun(mDB, statement, mVal, fromJson, returnMode);
|
|
1060
1061
|
ret.changes += result.changes;
|
|
1061
1062
|
ret.lastId = result.lastId;
|
|
1063
|
+
const keys = Object.keys(result);
|
|
1064
|
+
if (keys.includes('values') && result.values.length > 0) {
|
|
1065
|
+
ret.values.push(result.values);
|
|
1066
|
+
}
|
|
1062
1067
|
}
|
|
1063
1068
|
}
|
|
1064
1069
|
else {
|
|
1065
1070
|
if (values.length > 0) {
|
|
1066
1071
|
const mVal = this.replaceUndefinedByNull(values);
|
|
1067
|
-
result = this.prepareRun(mDB, statement, mVal, fromJson);
|
|
1068
|
-
ret.changes += result.changes;
|
|
1069
|
-
ret.lastId = result.lastId;
|
|
1072
|
+
result = this.prepareRun(mDB, statement, mVal, fromJson, returnMode);
|
|
1070
1073
|
}
|
|
1071
1074
|
else {
|
|
1072
|
-
result = this.prepareRun(mDB, statement, [], fromJson);
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
+
result = this.prepareRun(mDB, statement, [], fromJson, returnMode);
|
|
1076
|
+
}
|
|
1077
|
+
ret.changes += result.changes;
|
|
1078
|
+
ret.lastId = result.lastId;
|
|
1079
|
+
const keys = Object.keys(result);
|
|
1080
|
+
if (keys.includes('values') && result.values.length > 0) {
|
|
1081
|
+
ret.values.push(result.values);
|
|
1075
1082
|
}
|
|
1076
1083
|
}
|
|
1077
1084
|
}
|
|
@@ -1089,9 +1096,9 @@ class UtilsSQLite {
|
|
|
1089
1096
|
* @param values
|
|
1090
1097
|
* @param fromJson
|
|
1091
1098
|
*/
|
|
1092
|
-
prepareRun(mDB, statement, values, fromJson) {
|
|
1099
|
+
prepareRun(mDB, statement, values, fromJson, returnMode) {
|
|
1093
1100
|
const result = { changes: 0, lastId: -1 };
|
|
1094
|
-
const msg =
|
|
1101
|
+
const msg = 'PrepareRun';
|
|
1095
1102
|
const stmtType = statement
|
|
1096
1103
|
.replace(/\n/g, '')
|
|
1097
1104
|
.trim()
|
|
@@ -1114,9 +1121,16 @@ class UtilsSQLite {
|
|
|
1114
1121
|
mVal.push(null);
|
|
1115
1122
|
}
|
|
1116
1123
|
}
|
|
1117
|
-
const ret = this.runExec(mDB, sqlStmt, mVal);
|
|
1118
|
-
|
|
1119
|
-
|
|
1124
|
+
const ret = this.runExec(mDB, sqlStmt, mVal, returnMode);
|
|
1125
|
+
if (ret.values != null) {
|
|
1126
|
+
result.values = ret.values;
|
|
1127
|
+
result.changes = ret.changes;
|
|
1128
|
+
result.lastId = ret.lastInsertRowid;
|
|
1129
|
+
}
|
|
1130
|
+
else {
|
|
1131
|
+
result.changes = ret.changes;
|
|
1132
|
+
result.lastId = ret.lastInsertRowid;
|
|
1133
|
+
}
|
|
1120
1134
|
return result;
|
|
1121
1135
|
}
|
|
1122
1136
|
catch (err) {
|
|
@@ -1124,17 +1138,59 @@ class UtilsSQLite {
|
|
|
1124
1138
|
throw new Error(`${msg} ${errmsg}`);
|
|
1125
1139
|
}
|
|
1126
1140
|
}
|
|
1127
|
-
runExec(mDB, stmt, values = []) {
|
|
1141
|
+
runExec(mDB, stmt, values = [], returnMode) {
|
|
1142
|
+
let result = { changes: 0, lastInsertRowid: -1, values: [] };
|
|
1128
1143
|
const msg = 'runExec: ';
|
|
1129
1144
|
try {
|
|
1130
1145
|
const cStmt = this.cleanStatement(stmt);
|
|
1131
|
-
const
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1146
|
+
const params = this.getStmtAndNames(cStmt, returnMode);
|
|
1147
|
+
switch (params.mMode) {
|
|
1148
|
+
case 'one': {
|
|
1149
|
+
const iniChanges = this.dbChanges(mDB);
|
|
1150
|
+
if (values.length === 0) {
|
|
1151
|
+
const value = mDB.prepare(params.stmt).get();
|
|
1152
|
+
result.values.push(value);
|
|
1153
|
+
result.lastInsertRowid = this.getLastId(mDB);
|
|
1154
|
+
}
|
|
1155
|
+
else {
|
|
1156
|
+
const lowerId = this.getLastId(mDB) + 1;
|
|
1157
|
+
const statement = mDB.prepare(params.stmt);
|
|
1158
|
+
const res = statement.run(values);
|
|
1159
|
+
result.lastInsertRowid = res.lastInsertRowid;
|
|
1160
|
+
const sql = `SELECT ${params.names} FROM ${params.tableName} WHERE rowid = ${lowerId};`;
|
|
1161
|
+
const value = this.queryOne(mDB, sql, []);
|
|
1162
|
+
result.values.push(value);
|
|
1163
|
+
}
|
|
1164
|
+
result.changes = this.dbChanges(mDB) - iniChanges;
|
|
1165
|
+
break;
|
|
1166
|
+
}
|
|
1167
|
+
case 'all': {
|
|
1168
|
+
const iniChanges = this.dbChanges(mDB);
|
|
1169
|
+
if (values.length === 0) {
|
|
1170
|
+
result.values = mDB.prepare(params.stmt).all();
|
|
1171
|
+
result.lastInsertRowid = this.getLastId(mDB);
|
|
1172
|
+
}
|
|
1173
|
+
else {
|
|
1174
|
+
const lowerId = this.getLastId(mDB) + 1;
|
|
1175
|
+
const statement = mDB.prepare(params.stmt);
|
|
1176
|
+
const res = statement.run(values);
|
|
1177
|
+
const upperId = res.lastInsertRowid;
|
|
1178
|
+
const sql = `SELECT ${params.names} FROM ${params.tableName} WHERE rowid BETWEEN ${lowerId} AND ${upperId};`;
|
|
1179
|
+
result.values = this.queryAll(mDB, sql, []);
|
|
1180
|
+
result.lastInsertRowid = res.lastInsertRowid;
|
|
1181
|
+
}
|
|
1182
|
+
result.changes = this.dbChanges(mDB) - iniChanges;
|
|
1183
|
+
break;
|
|
1184
|
+
}
|
|
1185
|
+
default: {
|
|
1186
|
+
const statement = mDB.prepare(params.stmt);
|
|
1187
|
+
if (values != null && values.length > 0) {
|
|
1188
|
+
result = statement.run(values);
|
|
1189
|
+
}
|
|
1190
|
+
else {
|
|
1191
|
+
result = statement.run();
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1138
1194
|
}
|
|
1139
1195
|
return result;
|
|
1140
1196
|
}
|
|
@@ -1169,7 +1225,7 @@ class UtilsSQLite {
|
|
|
1169
1225
|
*/
|
|
1170
1226
|
deleteSQL(mDB, statement, values) {
|
|
1171
1227
|
let sqlStmt = statement;
|
|
1172
|
-
const msg =
|
|
1228
|
+
const msg = 'DeleteSQL';
|
|
1173
1229
|
try {
|
|
1174
1230
|
const isLast = this.isLastModified(mDB, true);
|
|
1175
1231
|
const isDel = this.isSqlDeleted(mDB, true);
|
|
@@ -1201,7 +1257,7 @@ class UtilsSQLite {
|
|
|
1201
1257
|
* @returns
|
|
1202
1258
|
*/
|
|
1203
1259
|
findReferencesAndUpdate(mDB, tableName, whereStmt, values) {
|
|
1204
|
-
const msg =
|
|
1260
|
+
const msg = 'FindReferencesAndUpdate';
|
|
1205
1261
|
try {
|
|
1206
1262
|
const references = this.getReferences(mDB, tableName);
|
|
1207
1263
|
if (references.length <= 0) {
|
|
@@ -1252,7 +1308,7 @@ class UtilsSQLite {
|
|
|
1252
1308
|
}
|
|
1253
1309
|
}
|
|
1254
1310
|
}
|
|
1255
|
-
const ret = this.runExec(mDB, stmt, selValues);
|
|
1311
|
+
const ret = this.runExec(mDB, stmt, selValues, 'no');
|
|
1256
1312
|
const lastId = ret.lastInsertRowid;
|
|
1257
1313
|
if (lastId == -1) {
|
|
1258
1314
|
const msg = `UPDATE sql_deleted failed for references table: ${refTable}`;
|
|
@@ -1349,7 +1405,7 @@ class UtilsSQLite {
|
|
|
1349
1405
|
return whereStmt;
|
|
1350
1406
|
}
|
|
1351
1407
|
getReferences(mDB, tableName) {
|
|
1352
|
-
const msg =
|
|
1408
|
+
const msg = 'GetReferences';
|
|
1353
1409
|
const sqlStmt = 'SELECT sql FROM sqlite_master ' +
|
|
1354
1410
|
"WHERE sql LIKE('%FOREIGN KEY%') AND sql LIKE('%REFERENCES%') AND " +
|
|
1355
1411
|
"sql LIKE('%" +
|
|
@@ -1391,7 +1447,7 @@ class UtilsSQLite {
|
|
|
1391
1447
|
* @param values
|
|
1392
1448
|
*/
|
|
1393
1449
|
queryAll(mDB, sql, values) {
|
|
1394
|
-
const msg =
|
|
1450
|
+
const msg = 'QueryAll';
|
|
1395
1451
|
try {
|
|
1396
1452
|
const cSql = this.cleanStatement(sql);
|
|
1397
1453
|
const stmt = mDB.prepare(cSql);
|
|
@@ -1419,7 +1475,7 @@ class UtilsSQLite {
|
|
|
1419
1475
|
* @param values
|
|
1420
1476
|
*/
|
|
1421
1477
|
queryOne(mDB, sql, values) {
|
|
1422
|
-
const msg =
|
|
1478
|
+
const msg = 'QueryOne';
|
|
1423
1479
|
try {
|
|
1424
1480
|
const cSql = this.cleanStatement(sql);
|
|
1425
1481
|
const stmt = mDB.prepare(cSql);
|
|
@@ -1442,7 +1498,7 @@ class UtilsSQLite {
|
|
|
1442
1498
|
* @param mDb
|
|
1443
1499
|
*/
|
|
1444
1500
|
getTablesNames(mDb) {
|
|
1445
|
-
const msg =
|
|
1501
|
+
const msg = 'getTablesNames';
|
|
1446
1502
|
let sql = 'SELECT name FROM sqlite_master WHERE ';
|
|
1447
1503
|
sql += "type='table' AND name NOT LIKE 'sync_table' ";
|
|
1448
1504
|
sql += "AND name NOT LIKE '_temp_%' ";
|
|
@@ -1466,7 +1522,7 @@ class UtilsSQLite {
|
|
|
1466
1522
|
* @param mDb
|
|
1467
1523
|
*/
|
|
1468
1524
|
getViewsNames(mDb) {
|
|
1469
|
-
const msg =
|
|
1525
|
+
const msg = 'GetViewsNames';
|
|
1470
1526
|
let sql = 'SELECT name FROM sqlite_master WHERE ';
|
|
1471
1527
|
sql += "type='view' AND name NOT LIKE 'sqlite_%' ";
|
|
1472
1528
|
sql += 'ORDER BY rootpage DESC;';
|
|
@@ -1489,7 +1545,7 @@ class UtilsSQLite {
|
|
|
1489
1545
|
* @param isOpen
|
|
1490
1546
|
*/
|
|
1491
1547
|
isLastModified(mDB, isOpen) {
|
|
1492
|
-
const msg =
|
|
1548
|
+
const msg = 'IsLastModified';
|
|
1493
1549
|
if (!isOpen) {
|
|
1494
1550
|
throw new Error(`${msg} database not opened`);
|
|
1495
1551
|
}
|
|
@@ -1515,7 +1571,7 @@ class UtilsSQLite {
|
|
|
1515
1571
|
* @param isOpen
|
|
1516
1572
|
*/
|
|
1517
1573
|
isSqlDeleted(mDB, isOpen) {
|
|
1518
|
-
const msg =
|
|
1574
|
+
const msg = 'IsSqlDeleted';
|
|
1519
1575
|
if (!isOpen) {
|
|
1520
1576
|
throw new Error(`${msg} database not opened`);
|
|
1521
1577
|
}
|
|
@@ -1536,10 +1592,11 @@ class UtilsSQLite {
|
|
|
1536
1592
|
}
|
|
1537
1593
|
}
|
|
1538
1594
|
getJournalMode(mDB) {
|
|
1539
|
-
const msg =
|
|
1595
|
+
const msg = 'getJournalMode';
|
|
1540
1596
|
try {
|
|
1541
|
-
const retMode = mDB.pragma('journal_mode')
|
|
1542
|
-
|
|
1597
|
+
const retMode = mDB.pragma('journal_mode');
|
|
1598
|
+
console.log(`journal_mode: ${retMode[0].journal_mode}`);
|
|
1599
|
+
return retMode[0].journal_mode;
|
|
1543
1600
|
}
|
|
1544
1601
|
catch (err) {
|
|
1545
1602
|
const errmsg = err.message ? err.message : err;
|
|
@@ -1547,7 +1604,7 @@ class UtilsSQLite {
|
|
|
1547
1604
|
}
|
|
1548
1605
|
}
|
|
1549
1606
|
async isDatabaseEncrypted(dbName) {
|
|
1550
|
-
const msg =
|
|
1607
|
+
const msg = 'isDatabaseEncrypted';
|
|
1551
1608
|
try {
|
|
1552
1609
|
const isExists = this.fileUtil.isFileExists(dbName);
|
|
1553
1610
|
if (isExists) {
|
|
@@ -1565,7 +1622,10 @@ class UtilsSQLite {
|
|
|
1565
1622
|
}
|
|
1566
1623
|
async isDBEncrypted(filePath) {
|
|
1567
1624
|
try {
|
|
1568
|
-
const retStr = await this.fileUtil.readFileAsPromise(filePath, {
|
|
1625
|
+
const retStr = await this.fileUtil.readFileAsPromise(filePath, {
|
|
1626
|
+
start: 0,
|
|
1627
|
+
end: 12,
|
|
1628
|
+
});
|
|
1569
1629
|
if (retStr === 'SQLite format')
|
|
1570
1630
|
return false;
|
|
1571
1631
|
else
|
|
@@ -1581,7 +1641,7 @@ class UtilsSQLite {
|
|
|
1581
1641
|
* @param tableName
|
|
1582
1642
|
*/
|
|
1583
1643
|
getTableColumnNamesTypes(mDB, tableName) {
|
|
1584
|
-
const msg =
|
|
1644
|
+
const msg = 'getTableColumnNamesTypes';
|
|
1585
1645
|
try {
|
|
1586
1646
|
const infos = mDB.pragma(`table_info('${tableName}')`);
|
|
1587
1647
|
const retNames = [];
|
|
@@ -1598,22 +1658,21 @@ class UtilsSQLite {
|
|
|
1598
1658
|
}
|
|
1599
1659
|
}
|
|
1600
1660
|
cleanStatement(stmt) {
|
|
1601
|
-
let sql =
|
|
1661
|
+
let sql = '';
|
|
1602
1662
|
if (stmt.toLowerCase().includes('INSERT INTO'.toLowerCase()) ||
|
|
1603
1663
|
stmt.toLowerCase().includes('SELECT'.toLowerCase()) ||
|
|
1604
1664
|
stmt.toLowerCase().includes('UPDATE'.toLowerCase()) ||
|
|
1605
1665
|
stmt.toLowerCase().includes('DELETE FROM'.toLowerCase())) {
|
|
1606
1666
|
// check for JSON string
|
|
1607
1667
|
sql = this.dealJsonString(stmt);
|
|
1608
|
-
sql = sql.replaceAll("
|
|
1609
|
-
sql = sql.replaceAll(
|
|
1668
|
+
sql = sql.replaceAll('"', "'");
|
|
1669
|
+
sql = sql.replaceAll('§', '"');
|
|
1610
1670
|
}
|
|
1611
1671
|
else {
|
|
1612
1672
|
sql = stmt;
|
|
1613
1673
|
}
|
|
1614
1674
|
return sql;
|
|
1615
1675
|
}
|
|
1616
|
-
;
|
|
1617
1676
|
findIndex(str, char) {
|
|
1618
1677
|
const a = [];
|
|
1619
1678
|
for (let i = str.length; i--;)
|
|
@@ -1626,11 +1685,54 @@ class UtilsSQLite {
|
|
|
1626
1685
|
const oJ = this.findIndex(stmt, '{');
|
|
1627
1686
|
const eJ = this.findIndex(stmt, '}');
|
|
1628
1687
|
for (let i = 0; i < oJ.length; i++) {
|
|
1629
|
-
const g =
|
|
1688
|
+
const g = retStmt.substring(oJ[i] + 1, eJ[i]).replaceAll('"', '§');
|
|
1630
1689
|
retStmt = retStmt.substring(0, oJ[i] + 1) + g + retStmt.substring(eJ[i]);
|
|
1631
1690
|
}
|
|
1632
1691
|
return retStmt;
|
|
1633
1692
|
}
|
|
1693
|
+
getStmtAndNames(stmt, returnMode) {
|
|
1694
|
+
const retObj = {};
|
|
1695
|
+
const mStmt = stmt;
|
|
1696
|
+
if (mStmt.toUpperCase().includes('RETURNING') &&
|
|
1697
|
+
(returnMode === 'all' || returnMode === 'one')) {
|
|
1698
|
+
retObj.tableName = this.getTableName(mStmt);
|
|
1699
|
+
retObj.mMode = returnMode;
|
|
1700
|
+
const idx = mStmt.toUpperCase().indexOf('RETURNING') + 9;
|
|
1701
|
+
const names = mStmt.substring(idx).trim();
|
|
1702
|
+
retObj.names = names.slice(-1) === ';' ? names.slice(0, -1) : names;
|
|
1703
|
+
retObj.stmt = mStmt;
|
|
1704
|
+
}
|
|
1705
|
+
else {
|
|
1706
|
+
retObj.mMode = 'no';
|
|
1707
|
+
if (mStmt.toUpperCase().includes('RETURNING')) {
|
|
1708
|
+
const idx = mStmt.toUpperCase().indexOf('RETURNING');
|
|
1709
|
+
retObj.stmt = mStmt.slice(0, idx).trim() + ';';
|
|
1710
|
+
}
|
|
1711
|
+
else {
|
|
1712
|
+
retObj.stmt = mStmt;
|
|
1713
|
+
}
|
|
1714
|
+
}
|
|
1715
|
+
return retObj;
|
|
1716
|
+
}
|
|
1717
|
+
getTableName(sqlStatement) {
|
|
1718
|
+
const patterns = {
|
|
1719
|
+
insert: /INSERT\s+INTO\s+(\w+)/i,
|
|
1720
|
+
delete: /DELETE\s+FROM\s+(\w+)/i,
|
|
1721
|
+
update: /UPDATE\s+(\w+)/i,
|
|
1722
|
+
select: /SELECT.*\s+FROM\s+(\w+)/i,
|
|
1723
|
+
};
|
|
1724
|
+
let tableName = null;
|
|
1725
|
+
Object.keys(patterns).some((key) => {
|
|
1726
|
+
const pattern = patterns[key];
|
|
1727
|
+
const match = pattern.exec(sqlStatement);
|
|
1728
|
+
if (match) {
|
|
1729
|
+
tableName = match[1];
|
|
1730
|
+
return true; // Stop iterating through patterns
|
|
1731
|
+
}
|
|
1732
|
+
return false;
|
|
1733
|
+
});
|
|
1734
|
+
return tableName;
|
|
1735
|
+
}
|
|
1634
1736
|
}
|
|
1635
1737
|
utilsSQLite.UtilsSQLite = UtilsSQLite;
|
|
1636
1738
|
|
|
@@ -1861,7 +1963,7 @@ class UtilsJson {
|
|
|
1861
1963
|
if (stmt.substring(0, 6).toUpperCase() === 'DELETE') {
|
|
1862
1964
|
row = [];
|
|
1863
1965
|
}
|
|
1864
|
-
results = this.sqliteUtil.prepareRun(mDB, stmt, row, true);
|
|
1966
|
+
results = this.sqliteUtil.prepareRun(mDB, stmt, row, true, 'no');
|
|
1865
1967
|
lastId = results.lastId;
|
|
1866
1968
|
if (lastId < 0) {
|
|
1867
1969
|
throw new Error(`${msg} lastId < 0`);
|
|
@@ -1893,8 +1995,7 @@ class UtilsJson {
|
|
|
1893
1995
|
if (row.length != tColNames.length ||
|
|
1894
1996
|
row.length === 0 ||
|
|
1895
1997
|
tColNames.length === 0) {
|
|
1896
|
-
throw new Error(`${msg} Table ${tableName} ` +
|
|
1897
|
-
`values row ${j} not correct length`);
|
|
1998
|
+
throw new Error(`${msg} Table ${tableName} ` + `values row ${j} not correct length`);
|
|
1898
1999
|
}
|
|
1899
2000
|
try {
|
|
1900
2001
|
const retisIdExists = this.isIdExists(mDB, tableName, tColNames[0], row[0]);
|
|
@@ -2521,8 +2622,7 @@ class ExportToJson {
|
|
|
2521
2622
|
break;
|
|
2522
2623
|
}
|
|
2523
2624
|
default: {
|
|
2524
|
-
errmsg =
|
|
2525
|
-
`${msg} expMode ${sqlObj.mode} not defined`;
|
|
2625
|
+
errmsg = `${msg} expMode ${sqlObj.mode} not defined`;
|
|
2526
2626
|
break;
|
|
2527
2627
|
}
|
|
2528
2628
|
}
|
|
@@ -2566,7 +2666,7 @@ class ExportToJson {
|
|
|
2566
2666
|
}
|
|
2567
2667
|
}
|
|
2568
2668
|
getLastExportDate(mDb) {
|
|
2569
|
-
const msg =
|
|
2669
|
+
const msg = 'GetLastExportDate';
|
|
2570
2670
|
let retDate = -1;
|
|
2571
2671
|
try {
|
|
2572
2672
|
// get the last sync date
|
|
@@ -2589,7 +2689,7 @@ class ExportToJson {
|
|
|
2589
2689
|
* @returns
|
|
2590
2690
|
*/
|
|
2591
2691
|
setLastExportDate(mDb, lastExportedDate) {
|
|
2592
|
-
const msg =
|
|
2692
|
+
const msg = 'SetLastExportDate';
|
|
2593
2693
|
try {
|
|
2594
2694
|
const isTable = this.jsonUtil.isTableExists(mDb, true, 'sync_table');
|
|
2595
2695
|
if (!isTable) {
|
|
@@ -2597,7 +2697,7 @@ class ExportToJson {
|
|
|
2597
2697
|
}
|
|
2598
2698
|
const sDate = Math.round(new Date(lastExportedDate).getTime() / 1000);
|
|
2599
2699
|
let stmt = '';
|
|
2600
|
-
if (
|
|
2700
|
+
if (this.getLastExportDate(mDb) > 0) {
|
|
2601
2701
|
stmt = `UPDATE sync_table SET sync_date = ${sDate} WHERE id = 2;`;
|
|
2602
2702
|
}
|
|
2603
2703
|
else {
|
|
@@ -2619,7 +2719,7 @@ class ExportToJson {
|
|
|
2619
2719
|
}
|
|
2620
2720
|
}
|
|
2621
2721
|
delExportedRows(mDb) {
|
|
2622
|
-
const msg =
|
|
2722
|
+
const msg = 'DelExportedRows';
|
|
2623
2723
|
let lastExportDate;
|
|
2624
2724
|
try {
|
|
2625
2725
|
// check if synchronization table exists
|
|
@@ -2642,7 +2742,7 @@ class ExportToJson {
|
|
|
2642
2742
|
// define the delete statement
|
|
2643
2743
|
const delStmt = `DELETE FROM ${table}
|
|
2644
2744
|
WHERE sql_deleted = 1 AND last_modified < ${lastExportDate};`;
|
|
2645
|
-
const results = this.sqliteUtil.prepareRun(mDb, delStmt, [], true);
|
|
2745
|
+
const results = this.sqliteUtil.prepareRun(mDb, delStmt, [], true, 'no');
|
|
2646
2746
|
if (results.lastId < 0) {
|
|
2647
2747
|
throw new Error(`${msg} lastId < 0`);
|
|
2648
2748
|
}
|
|
@@ -2681,7 +2781,7 @@ class ExportToJson {
|
|
|
2681
2781
|
* @param mDb
|
|
2682
2782
|
*/
|
|
2683
2783
|
getSyncDate(mDb) {
|
|
2684
|
-
const msg =
|
|
2784
|
+
const msg = 'GetSyncDate';
|
|
2685
2785
|
let retDate = -1;
|
|
2686
2786
|
// get the last sync date
|
|
2687
2787
|
const stmt = `SELECT sync_date FROM sync_table WHERE id = ?;`;
|
|
@@ -3240,7 +3340,7 @@ class UtilsDrop {
|
|
|
3240
3340
|
statements.push(stmt);
|
|
3241
3341
|
}
|
|
3242
3342
|
for (const stmt of statements) {
|
|
3243
|
-
const results = this.sqliteUtil.prepareRun(db, stmt, [], false);
|
|
3343
|
+
const results = this.sqliteUtil.prepareRun(db, stmt, [], false, 'no');
|
|
3244
3344
|
if (results.lastId < 0) {
|
|
3245
3345
|
throw new Error(`${msg}: lastId < 0`);
|
|
3246
3346
|
}
|
|
@@ -3268,7 +3368,7 @@ class UtilsDrop {
|
|
|
3268
3368
|
// drop views
|
|
3269
3369
|
this.dropElements(db, 'view');
|
|
3270
3370
|
// vacuum the database
|
|
3271
|
-
this.sqliteUtil.prepareRun(db, 'VACUUM;', [], false);
|
|
3371
|
+
this.sqliteUtil.prepareRun(db, 'VACUUM;', [], false, 'no');
|
|
3272
3372
|
return;
|
|
3273
3373
|
}
|
|
3274
3374
|
catch (err) {
|
|
@@ -3448,55 +3548,20 @@ class ImportFromJson {
|
|
|
3448
3548
|
}
|
|
3449
3549
|
importFromJson.ImportFromJson = ImportFromJson;
|
|
3450
3550
|
|
|
3451
|
-
var
|
|
3452
|
-
|
|
3453
|
-
Object.defineProperty(utilsEncryption, "__esModule", { value: true });
|
|
3454
|
-
utilsEncryption.UtilsEncryption = void 0;
|
|
3455
|
-
const utilsFile_1$3 = utilsFile;
|
|
3456
|
-
const utilsSQLite_1$4 = utilsSQLite;
|
|
3457
|
-
class UtilsEncryption {
|
|
3458
|
-
constructor() {
|
|
3459
|
-
this.fileUtil = new utilsFile_1$3.UtilsFile();
|
|
3460
|
-
this.sqliteUtil = new utilsSQLite_1$4.UtilsSQLite();
|
|
3461
|
-
}
|
|
3462
|
-
/**
|
|
3463
|
-
* EncryptDatabase
|
|
3464
|
-
* @param pathDB
|
|
3465
|
-
* @param password
|
|
3466
|
-
*/
|
|
3467
|
-
async encryptDatabase(pathDB, password) {
|
|
3468
|
-
const msg = 'EncryptDatabase: ';
|
|
3469
|
-
const retB = this.fileUtil.isPathExists(pathDB);
|
|
3470
|
-
if (retB) {
|
|
3471
|
-
try {
|
|
3472
|
-
const mDB = await this.sqliteUtil.openOrCreateDatabase(pathDB, '', false);
|
|
3473
|
-
this.sqliteUtil.pragmaReKey(mDB, '', password);
|
|
3474
|
-
this.sqliteUtil.closeDB(mDB);
|
|
3475
|
-
return Promise.resolve();
|
|
3476
|
-
}
|
|
3477
|
-
catch (err) {
|
|
3478
|
-
return Promise.reject(new Error(`${msg} ${err.message} `));
|
|
3479
|
-
}
|
|
3480
|
-
}
|
|
3481
|
-
else {
|
|
3482
|
-
return Promise.reject(new Error(`${msg}file path ${pathDB} ` + 'does not exist'));
|
|
3483
|
-
}
|
|
3484
|
-
}
|
|
3485
|
-
}
|
|
3486
|
-
utilsEncryption.UtilsEncryption = UtilsEncryption;
|
|
3551
|
+
var utilsJsonEncryption = {};
|
|
3487
3552
|
|
|
3488
3553
|
var utilsSecret = {};
|
|
3489
3554
|
|
|
3490
3555
|
Object.defineProperty(utilsSecret, "__esModule", { value: true });
|
|
3491
3556
|
utilsSecret.UtilsSecret = void 0;
|
|
3492
3557
|
const GlobalSQLite_1$2 = GlobalSQLite$1;
|
|
3493
|
-
const utilsFile_1$
|
|
3494
|
-
const utilsSQLite_1$
|
|
3558
|
+
const utilsFile_1$3 = utilsFile;
|
|
3559
|
+
const utilsSQLite_1$4 = utilsSQLite;
|
|
3495
3560
|
class UtilsSecret {
|
|
3496
3561
|
constructor() {
|
|
3497
3562
|
this.globalUtil = new GlobalSQLite_1$2.GlobalSQLite();
|
|
3498
|
-
this.sqliteUtil = new utilsSQLite_1$
|
|
3499
|
-
this.fileUtil = new utilsFile_1$
|
|
3563
|
+
this.sqliteUtil = new utilsSQLite_1$4.UtilsSQLite();
|
|
3564
|
+
this.fileUtil = new utilsFile_1$3.UtilsFile();
|
|
3500
3565
|
this.storage = require$$3__default$1["default"];
|
|
3501
3566
|
}
|
|
3502
3567
|
isSecretStored() {
|
|
@@ -3512,7 +3577,7 @@ class UtilsSecret {
|
|
|
3512
3577
|
throw new Error(`setEncryptSecret: passphrase already stored`);
|
|
3513
3578
|
}
|
|
3514
3579
|
else {
|
|
3515
|
-
oldpassphrase = this.globalUtil != null ? this.globalUtil.secret :
|
|
3580
|
+
oldpassphrase = this.globalUtil != null ? this.globalUtil.secret : '';
|
|
3516
3581
|
if (oldpassphrase.length <= 0) {
|
|
3517
3582
|
throw new Error(`setEncryptSecret: globalUtil is null`);
|
|
3518
3583
|
}
|
|
@@ -3547,7 +3612,7 @@ class UtilsSecret {
|
|
|
3547
3612
|
oldpassphrase = this.globalUtil.secret;
|
|
3548
3613
|
}
|
|
3549
3614
|
// check if some databases were encrypted with the oldpassphrase
|
|
3550
|
-
this.changeDatabaseSecret(oldpassphrase,
|
|
3615
|
+
this.changeDatabaseSecret(oldpassphrase, '').then(() => {
|
|
3551
3616
|
this.removePassphrase();
|
|
3552
3617
|
});
|
|
3553
3618
|
}
|
|
@@ -3590,12 +3655,12 @@ class UtilsSecret {
|
|
|
3590
3655
|
const data = this.storage.getSync('userData');
|
|
3591
3656
|
const keys = Object.keys(data);
|
|
3592
3657
|
if (data == null || keys.length <= 0)
|
|
3593
|
-
return
|
|
3658
|
+
return '';
|
|
3594
3659
|
if (Object.keys(data).includes('passphrase')) {
|
|
3595
3660
|
return data.passphrase;
|
|
3596
3661
|
}
|
|
3597
3662
|
else {
|
|
3598
|
-
return
|
|
3663
|
+
return '';
|
|
3599
3664
|
}
|
|
3600
3665
|
}
|
|
3601
3666
|
setPassphrase(passphrase) {
|
|
@@ -3617,6 +3682,103 @@ class UtilsSecret {
|
|
|
3617
3682
|
}
|
|
3618
3683
|
utilsSecret.UtilsSecret = UtilsSecret;
|
|
3619
3684
|
|
|
3685
|
+
Object.defineProperty(utilsJsonEncryption, "__esModule", { value: true });
|
|
3686
|
+
utilsJsonEncryption.UtilsJsonEncryption = void 0;
|
|
3687
|
+
const utilsSecret_1$2 = utilsSecret;
|
|
3688
|
+
class UtilsJsonEncryption {
|
|
3689
|
+
constructor() {
|
|
3690
|
+
this.fileSecret = new utilsSecret_1$2.UtilsSecret();
|
|
3691
|
+
this.SALT = 'jeep_capacitor_sqlite';
|
|
3692
|
+
this.Crypto = require$$1__default$2["default"];
|
|
3693
|
+
this.CryptoJS = require$$2__default$1["default"];
|
|
3694
|
+
}
|
|
3695
|
+
/**
|
|
3696
|
+
* deriveKeyFromPassphrase
|
|
3697
|
+
* Function to derive a symmetric key from passphrase and salt using PBKDF2
|
|
3698
|
+
* @param passphrase
|
|
3699
|
+
* @param salt
|
|
3700
|
+
* @returns
|
|
3701
|
+
*/
|
|
3702
|
+
deriveKeyFromPassphrase(passphrase, salt) {
|
|
3703
|
+
const iterations = 10000; // Recommended number of iterations for PBKDF2
|
|
3704
|
+
const keyLength = 32;
|
|
3705
|
+
const key = this.Crypto.pbkdf2Sync(passphrase, salt, iterations, keyLength, 'sha256');
|
|
3706
|
+
const keyHex = Buffer.from(key).toString('hex');
|
|
3707
|
+
return keyHex;
|
|
3708
|
+
}
|
|
3709
|
+
/**
|
|
3710
|
+
* encryptJSONObject
|
|
3711
|
+
* Function to encrypt JSON object with AES and return as Base64
|
|
3712
|
+
* @param jsonObj
|
|
3713
|
+
* @returns
|
|
3714
|
+
*/
|
|
3715
|
+
encryptJSONObject(jsonObj) {
|
|
3716
|
+
const jsonString = JSON.stringify(jsonObj);
|
|
3717
|
+
// get the passphrase
|
|
3718
|
+
const passphrase = this.fileSecret.getPassphrase();
|
|
3719
|
+
// derived a combined key from passphrase and salt
|
|
3720
|
+
const key = this.deriveKeyFromPassphrase(passphrase, this.SALT);
|
|
3721
|
+
const encrypted = this.CryptoJS.AES.encrypt(jsonString, key).toString();
|
|
3722
|
+
const encryptedBase64 = Buffer.from(encrypted).toString('base64');
|
|
3723
|
+
return encryptedBase64;
|
|
3724
|
+
}
|
|
3725
|
+
/**
|
|
3726
|
+
* decryptJSONObject
|
|
3727
|
+
* Function to decrypt AES encrypted JSON object from Base64
|
|
3728
|
+
* @param encryptedBase64
|
|
3729
|
+
* @returns
|
|
3730
|
+
*/
|
|
3731
|
+
decryptJSONObject(encryptedBase64) {
|
|
3732
|
+
const encryptedData = Buffer.from(encryptedBase64, 'base64').toString();
|
|
3733
|
+
// get the passphrase
|
|
3734
|
+
const passphrase = this.fileSecret.getPassphrase();
|
|
3735
|
+
// derived a combined key from passphrase and salt
|
|
3736
|
+
const key = this.deriveKeyFromPassphrase(passphrase, this.SALT);
|
|
3737
|
+
const bytes = this.CryptoJS.AES.decrypt(encryptedData, key);
|
|
3738
|
+
const decryptedString = bytes.toString(this.CryptoJS.enc.Utf8);
|
|
3739
|
+
const decryptedObj = JSON.parse(decryptedString);
|
|
3740
|
+
return decryptedObj;
|
|
3741
|
+
}
|
|
3742
|
+
}
|
|
3743
|
+
utilsJsonEncryption.UtilsJsonEncryption = UtilsJsonEncryption;
|
|
3744
|
+
|
|
3745
|
+
var utilsEncryption = {};
|
|
3746
|
+
|
|
3747
|
+
Object.defineProperty(utilsEncryption, "__esModule", { value: true });
|
|
3748
|
+
utilsEncryption.UtilsEncryption = void 0;
|
|
3749
|
+
const utilsFile_1$2 = utilsFile;
|
|
3750
|
+
const utilsSQLite_1$3 = utilsSQLite;
|
|
3751
|
+
class UtilsEncryption {
|
|
3752
|
+
constructor() {
|
|
3753
|
+
this.fileUtil = new utilsFile_1$2.UtilsFile();
|
|
3754
|
+
this.sqliteUtil = new utilsSQLite_1$3.UtilsSQLite();
|
|
3755
|
+
}
|
|
3756
|
+
/**
|
|
3757
|
+
* EncryptDatabase
|
|
3758
|
+
* @param pathDB
|
|
3759
|
+
* @param password
|
|
3760
|
+
*/
|
|
3761
|
+
async encryptDatabase(pathDB, password) {
|
|
3762
|
+
const msg = 'EncryptDatabase: ';
|
|
3763
|
+
const retB = this.fileUtil.isPathExists(pathDB);
|
|
3764
|
+
if (retB) {
|
|
3765
|
+
try {
|
|
3766
|
+
const mDB = await this.sqliteUtil.openOrCreateDatabase(pathDB, '', false);
|
|
3767
|
+
this.sqliteUtil.pragmaReKey(mDB, '', password);
|
|
3768
|
+
this.sqliteUtil.closeDB(mDB);
|
|
3769
|
+
return Promise.resolve();
|
|
3770
|
+
}
|
|
3771
|
+
catch (err) {
|
|
3772
|
+
return Promise.reject(new Error(`${msg} ${err.message} `));
|
|
3773
|
+
}
|
|
3774
|
+
}
|
|
3775
|
+
else {
|
|
3776
|
+
return Promise.reject(new Error(`${msg}file path ${pathDB} ` + 'does not exist'));
|
|
3777
|
+
}
|
|
3778
|
+
}
|
|
3779
|
+
}
|
|
3780
|
+
utilsEncryption.UtilsEncryption = UtilsEncryption;
|
|
3781
|
+
|
|
3620
3782
|
var utilsUpgrade = {};
|
|
3621
3783
|
|
|
3622
3784
|
Object.defineProperty(utilsUpgrade, "__esModule", { value: true });
|
|
@@ -3688,13 +3850,15 @@ const GlobalSQLite_1$1 = GlobalSQLite$1;
|
|
|
3688
3850
|
const exportToJson_1 = exportToJson;
|
|
3689
3851
|
const importFromJson_1 = importFromJson;
|
|
3690
3852
|
const utilsJson_1$1 = utilsJson;
|
|
3853
|
+
const utilsJsonEncryption_1$1 = utilsJsonEncryption;
|
|
3691
3854
|
const utilsEncryption_1 = utilsEncryption;
|
|
3692
3855
|
const utilsFile_1$1 = utilsFile;
|
|
3693
3856
|
const utilsSQLite_1$1 = utilsSQLite;
|
|
3694
3857
|
const utilsSecret_1$1 = utilsSecret;
|
|
3695
3858
|
const utilsUpgrade_1 = utilsUpgrade;
|
|
3696
3859
|
class Database {
|
|
3697
|
-
constructor(dbName, encrypted, mode, version, readonly, upgDict, globalUtil) {
|
|
3860
|
+
constructor(dbName, encrypted, mode, version, isEncryption, readonly, upgDict, globalUtil) {
|
|
3861
|
+
this.jsonEncryptUtil = new utilsJsonEncryption_1$1.UtilsJsonEncryption();
|
|
3698
3862
|
this.fileUtil = new utilsFile_1$1.UtilsFile();
|
|
3699
3863
|
this.sqliteUtil = new utilsSQLite_1$1.UtilsSQLite();
|
|
3700
3864
|
this.jsonUtil = new utilsJson_1$1.UtilsJson();
|
|
@@ -3708,6 +3872,7 @@ class Database {
|
|
|
3708
3872
|
this.dbName = dbName;
|
|
3709
3873
|
this._encrypted = encrypted;
|
|
3710
3874
|
this._mode = mode;
|
|
3875
|
+
this._isEncryption = isEncryption;
|
|
3711
3876
|
this.version = version;
|
|
3712
3877
|
this.readonly = readonly;
|
|
3713
3878
|
this.upgradeVersionDict = upgDict;
|
|
@@ -3716,6 +3881,7 @@ class Database {
|
|
|
3716
3881
|
this.globalUtil = globalUtil ? globalUtil : new GlobalSQLite_1$1.GlobalSQLite();
|
|
3717
3882
|
if (this.pathDB.length === 0)
|
|
3718
3883
|
throw new Error('Could not generate a path to ' + dbName);
|
|
3884
|
+
console.log(`&&& Databases path: ${this.pathDB}`);
|
|
3719
3885
|
}
|
|
3720
3886
|
/**
|
|
3721
3887
|
* IsDBOpen
|
|
@@ -4039,7 +4205,7 @@ class Database {
|
|
|
4039
4205
|
* @param values: string[]
|
|
4040
4206
|
* @returns Promise<{changes:number, lastId:number}>
|
|
4041
4207
|
*/
|
|
4042
|
-
runSQL(statement, values, transaction) {
|
|
4208
|
+
runSQL(statement, values, transaction, returnMode) {
|
|
4043
4209
|
this.ensureDatabaseIsOpen();
|
|
4044
4210
|
try {
|
|
4045
4211
|
// start a transaction
|
|
@@ -4053,7 +4219,7 @@ class Database {
|
|
|
4053
4219
|
throw new Error(`RunSQL: ${err}`);
|
|
4054
4220
|
}
|
|
4055
4221
|
try {
|
|
4056
|
-
const results = this.sqliteUtil.prepareRun(this.database, statement, values, false);
|
|
4222
|
+
const results = this.sqliteUtil.prepareRun(this.database, statement, values, false, returnMode);
|
|
4057
4223
|
if (results.lastId < 0) {
|
|
4058
4224
|
if (transaction) {
|
|
4059
4225
|
this.sqliteUtil.rollbackTransaction(this.database, this._isDbOpen);
|
|
@@ -4078,7 +4244,7 @@ class Database {
|
|
|
4078
4244
|
* @param set: any[]
|
|
4079
4245
|
* @returns Promise<{changes:number, lastId:number}>
|
|
4080
4246
|
*/
|
|
4081
|
-
execSet(set, transaction) {
|
|
4247
|
+
execSet(set, transaction, returnMode) {
|
|
4082
4248
|
this.ensureDatabaseIsOpen();
|
|
4083
4249
|
let results = { changes: 0, lastId: -1 };
|
|
4084
4250
|
try {
|
|
@@ -4093,7 +4259,7 @@ class Database {
|
|
|
4093
4259
|
throw new Error(`ExecSet: ${err}`);
|
|
4094
4260
|
}
|
|
4095
4261
|
try {
|
|
4096
|
-
results = this.sqliteUtil.executeSet(this.database, set, false);
|
|
4262
|
+
results = this.sqliteUtil.executeSet(this.database, set, false, returnMode);
|
|
4097
4263
|
if (transaction) {
|
|
4098
4264
|
this.sqliteUtil.commitTransaction(this.database, this._isDbOpen);
|
|
4099
4265
|
}
|
|
@@ -4174,13 +4340,21 @@ class Database {
|
|
|
4174
4340
|
if (isTable) {
|
|
4175
4341
|
this.exportToJsonUtil.setLastExportDate(this.database, new Date().toISOString());
|
|
4176
4342
|
}
|
|
4177
|
-
|
|
4343
|
+
let jsonResult = this.exportToJsonUtil.createExportObject(this.database, inJson);
|
|
4178
4344
|
const keys = Object.keys(jsonResult);
|
|
4179
4345
|
if (keys.length === 0) {
|
|
4180
4346
|
const msg = `ExportJson: return Object is empty ` + `No data to synchronize`;
|
|
4181
4347
|
throw new Error(msg);
|
|
4182
4348
|
}
|
|
4183
|
-
|
|
4349
|
+
let isValid = this.jsonUtil.isJsonSQLite(jsonResult);
|
|
4350
|
+
if (this._encrypted && this._isEncryption) {
|
|
4351
|
+
jsonResult.overwrite = true;
|
|
4352
|
+
jsonResult.encrypted = true;
|
|
4353
|
+
const base64Str = this.jsonEncryptUtil.encryptJSONObject(jsonResult);
|
|
4354
|
+
jsonResult = {};
|
|
4355
|
+
jsonResult.expData = base64Str;
|
|
4356
|
+
isValid = true;
|
|
4357
|
+
}
|
|
4184
4358
|
if (isValid) {
|
|
4185
4359
|
return jsonResult;
|
|
4186
4360
|
}
|
|
@@ -4208,6 +4382,7 @@ exports.CapacitorSQLite = src.CapacitorSQLite = void 0;
|
|
|
4208
4382
|
const GlobalSQLite_1 = GlobalSQLite$1;
|
|
4209
4383
|
const Database_1 = Database$1;
|
|
4210
4384
|
const utilsJson_1 = utilsJson;
|
|
4385
|
+
const utilsJsonEncryption_1 = utilsJsonEncryption;
|
|
4211
4386
|
const utilsFile_1 = utilsFile;
|
|
4212
4387
|
const utilsSQLite_1 = utilsSQLite;
|
|
4213
4388
|
const utilsSecret_1 = utilsSecret;
|
|
@@ -4217,6 +4392,7 @@ class CapacitorSQLite {
|
|
|
4217
4392
|
this.databases = {};
|
|
4218
4393
|
this.fileUtil = new utilsFile_1.UtilsFile();
|
|
4219
4394
|
this.jsonUtil = new utilsJson_1.UtilsJson();
|
|
4395
|
+
this.jsonEncryptUtil = new utilsJsonEncryption_1.UtilsJsonEncryption();
|
|
4220
4396
|
this.sqliteUtil = new utilsSQLite_1.UtilsSQLite();
|
|
4221
4397
|
this.secretUtil = new utilsSecret_1.UtilsSecret();
|
|
4222
4398
|
this.globalUtil = new GlobalSQLite_1.GlobalSQLite();
|
|
@@ -4250,7 +4426,7 @@ class CapacitorSQLite {
|
|
|
4250
4426
|
upgrades = this.versionUpgrades[dbName];
|
|
4251
4427
|
}
|
|
4252
4428
|
const connName = readonly ? 'RO_' + dbName : 'RW_' + dbName;
|
|
4253
|
-
const databaseConnection = new Database_1.Database(dbName + 'SQLite.db', encrypted, inMode, version, readonly, upgrades, this.globalUtil);
|
|
4429
|
+
const databaseConnection = new Database_1.Database(dbName + 'SQLite.db', encrypted, inMode, version, this.isEncryption, readonly, upgrades, this.globalUtil);
|
|
4254
4430
|
this.databases[connName] = databaseConnection;
|
|
4255
4431
|
return;
|
|
4256
4432
|
}
|
|
@@ -4389,6 +4565,7 @@ class CapacitorSQLite {
|
|
|
4389
4565
|
const setOfStatements = this.getOptionValue(options, 'set');
|
|
4390
4566
|
const transaction = this.getOptionValue(options, 'transaction', true);
|
|
4391
4567
|
const readonly = options.readonly ? options.readonly : false;
|
|
4568
|
+
const returnMode = options.returnMode ? options.returnMode : 'no';
|
|
4392
4569
|
const connName = 'RW_' + dbName;
|
|
4393
4570
|
const database = this.getDatabaseConnectionOrThrowError(connName);
|
|
4394
4571
|
for (const sStmt of setOfStatements) {
|
|
@@ -4402,7 +4579,7 @@ class CapacitorSQLite {
|
|
|
4402
4579
|
throw new Error(`ExecuteSet failed: ${msg}`);
|
|
4403
4580
|
}
|
|
4404
4581
|
try {
|
|
4405
|
-
const execSetResult = database.execSet(setOfStatements, transaction);
|
|
4582
|
+
const execSetResult = database.execSet(setOfStatements, transaction, returnMode);
|
|
4406
4583
|
if (execSetResult.lastId < 0) {
|
|
4407
4584
|
throw new Error(`ExecuteSet failed changes <0`);
|
|
4408
4585
|
}
|
|
@@ -4425,6 +4602,7 @@ class CapacitorSQLite {
|
|
|
4425
4602
|
const values = this.getOptionValue(options, 'values', []);
|
|
4426
4603
|
const transaction = this.getOptionValue(options, 'transaction', true);
|
|
4427
4604
|
const readonly = options.readonly ? options.readonly : false;
|
|
4605
|
+
const returnMode = options.returnMode ? options.returnMode : 'no';
|
|
4428
4606
|
const connName = 'RW_' + dbName;
|
|
4429
4607
|
const database = this.getDatabaseConnectionOrThrowError(connName);
|
|
4430
4608
|
if (database.isDBOpen()) {
|
|
@@ -4433,7 +4611,7 @@ class CapacitorSQLite {
|
|
|
4433
4611
|
throw new Error(`Run failed: ${msg}`);
|
|
4434
4612
|
}
|
|
4435
4613
|
try {
|
|
4436
|
-
const runResult = database.runSQL(statement, values, transaction);
|
|
4614
|
+
const runResult = database.runSQL(statement, values, transaction, returnMode);
|
|
4437
4615
|
return { changes: runResult };
|
|
4438
4616
|
}
|
|
4439
4617
|
catch (err) {
|
|
@@ -4545,7 +4723,14 @@ class CapacitorSQLite {
|
|
|
4545
4723
|
}
|
|
4546
4724
|
async importFromJson(options) {
|
|
4547
4725
|
const jsonString = this.getOptionValue(options, 'jsonstring');
|
|
4548
|
-
|
|
4726
|
+
let jsonObj = JSON.parse(jsonString);
|
|
4727
|
+
let inMode = 'no-encryption';
|
|
4728
|
+
const key = 'expData';
|
|
4729
|
+
if (key in jsonObj) {
|
|
4730
|
+
// Decrypt the data
|
|
4731
|
+
inMode = 'secret';
|
|
4732
|
+
jsonObj = this.jsonEncryptUtil.decryptJSONObject(jsonObj.expData);
|
|
4733
|
+
}
|
|
4549
4734
|
const isValid = this.jsonUtil.isJsonSQLite(jsonObj);
|
|
4550
4735
|
if (!isValid) {
|
|
4551
4736
|
throw new Error('Must provide a valid JsonSQLite Object');
|
|
@@ -4555,12 +4740,12 @@ class CapacitorSQLite {
|
|
|
4555
4740
|
const targetDbVersion = vJsonObj.version ?? 1;
|
|
4556
4741
|
const overwrite = vJsonObj.overwrite ?? false;
|
|
4557
4742
|
const encrypted = vJsonObj.encrypted ?? false;
|
|
4558
|
-
const mode = vJsonObj.mode ?? '
|
|
4743
|
+
const mode = vJsonObj.mode ?? 'full';
|
|
4559
4744
|
if (!this.isEncryption && encrypted) {
|
|
4560
4745
|
throw new Error('Must set electronIsEncryption = true in capacitor.config.ts');
|
|
4561
4746
|
}
|
|
4562
4747
|
// Create the database
|
|
4563
|
-
const database = new Database_1.Database(dbName, encrypted,
|
|
4748
|
+
const database = new Database_1.Database(dbName, encrypted, inMode, targetDbVersion, this.isEncryption, false, {}, this.globalUtil);
|
|
4564
4749
|
try {
|
|
4565
4750
|
if (overwrite && mode === 'full') {
|
|
4566
4751
|
const isExists = this.fileUtil.isFileExists(dbName);
|
|
@@ -4583,7 +4768,7 @@ class CapacitorSQLite {
|
|
|
4583
4768
|
// Import the JsonSQLite Object
|
|
4584
4769
|
const changes = await database.importJson(vJsonObj);
|
|
4585
4770
|
// Close the database
|
|
4586
|
-
|
|
4771
|
+
database.dbClose();
|
|
4587
4772
|
return { changes: { changes: changes } };
|
|
4588
4773
|
}
|
|
4589
4774
|
catch (err) {
|
|
@@ -4598,7 +4783,7 @@ class CapacitorSQLite {
|
|
|
4598
4783
|
const database = this.getDatabaseConnectionOrThrowError(connName);
|
|
4599
4784
|
if (database.isDBOpen()) {
|
|
4600
4785
|
try {
|
|
4601
|
-
const exportJsonResult =
|
|
4786
|
+
const exportJsonResult = database.exportJson(exportMode);
|
|
4602
4787
|
const resultKeys = Object.keys(exportJsonResult);
|
|
4603
4788
|
if (resultKeys.includes('message')) {
|
|
4604
4789
|
throw new Error(`exportToJson: ${exportJsonResult.message}`);
|
|
@@ -4859,7 +5044,7 @@ class CapacitorSQLite {
|
|
|
4859
5044
|
if (!isEncrypt) {
|
|
4860
5045
|
throw new Error(`setEncryptionSecret: Not available electronIsEncryption = false in capacitor.config.ts`);
|
|
4861
5046
|
}
|
|
4862
|
-
const passphrase = options.passphrase ? options.passphrase :
|
|
5047
|
+
const passphrase = options.passphrase ? options.passphrase : '';
|
|
4863
5048
|
if (passphrase.length <= 0) {
|
|
4864
5049
|
throw new Error(`setEncryptionSecret: You must give a passphrase`);
|
|
4865
5050
|
}
|
|
@@ -4883,14 +5068,14 @@ class CapacitorSQLite {
|
|
|
4883
5068
|
throw new Error(`changeEncryptionSecret: Not available electronIsEncryption = false in capacitor.config.ts`);
|
|
4884
5069
|
}
|
|
4885
5070
|
const oldsecret = this.secretUtil.getPassphrase();
|
|
4886
|
-
const oldpassphrase = options.oldpassphrase ? options.oldpassphrase :
|
|
5071
|
+
const oldpassphrase = options.oldpassphrase ? options.oldpassphrase : '';
|
|
4887
5072
|
if (oldpassphrase.length <= 0) {
|
|
4888
5073
|
throw new Error(`changeEncryptionSecret: You must give the oldpassphrase`);
|
|
4889
5074
|
}
|
|
4890
5075
|
if (oldpassphrase !== oldsecret) {
|
|
4891
5076
|
throw new Error(`changeEncryptionSecret: the given oldpassphrase is wrong`);
|
|
4892
5077
|
}
|
|
4893
|
-
const passphrase = options.passphrase ? options.passphrase :
|
|
5078
|
+
const passphrase = options.passphrase ? options.passphrase : '';
|
|
4894
5079
|
if (passphrase.length <= 0) {
|
|
4895
5080
|
throw new Error(`changetEncryptionSecret: You must give a passphrase`);
|
|
4896
5081
|
}
|
|
@@ -4938,7 +5123,7 @@ class CapacitorSQLite {
|
|
|
4938
5123
|
if (!isEncrypt) {
|
|
4939
5124
|
throw new Error(`checkEncryptionSecret: Not available electronIsEncryption = false in capacitor.config.ts`);
|
|
4940
5125
|
}
|
|
4941
|
-
const passphrase = options.passphrase ? options.passphrase :
|
|
5126
|
+
const passphrase = options.passphrase ? options.passphrase : '';
|
|
4942
5127
|
if (passphrase.length <= 0) {
|
|
4943
5128
|
throw new Error(`checkEncryptionSecret: You must give a passphrase`);
|
|
4944
5129
|
}
|