@capacitor-community/sqlite 5.0.5 → 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 +9 -0
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java +9 -0
- 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/UtilsSecret.java +3 -3
- package/dist/esm/definitions.d.ts +66 -0
- package/dist/esm/definitions.js +37 -0
- 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 +45 -0
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +45 -0
- package/dist/plugin.js.map +1 -1
- package/electron/dist/plugin.js +138 -52
- package/electron/dist/plugin.js.map +1 -1
- package/electron/rollup.config.js +2 -0
- package/ios/Plugin/CapacitorSQLite.swift +117 -88
- package/ios/Plugin/Database.swift +17 -5
- package/ios/Plugin/ImportExportJson/ImportData.swift +434 -0
- package/ios/Plugin/ImportExportJson/ImportFromJson.swift +43 -57
- package/ios/Plugin/ImportExportJson/JsonSQLite.swift +7 -0
- package/ios/Plugin/Utils/UtilsJson.swift +123 -1
- package/ios/Plugin/Utils/UtilsSQLCipher.swift +0 -1
- package/package.json +1 -1
- package/src/definitions.ts +104 -0
- package/src/web.ts +10 -0
|
@@ -1022,112 +1022,140 @@ enum CapacitorSQLiteError: Error {
|
|
|
1022
1022
|
throws -> [String: Int] {
|
|
1023
1023
|
if isInit {
|
|
1024
1024
|
var mDb: Database
|
|
1025
|
-
|
|
1025
|
+
var importData: ImportData
|
|
1026
|
+
// check if json object is encrypted
|
|
1027
|
+
if parsingData.contains("expData") {
|
|
1028
|
+
guard let data = ("["+parsingData+"]")
|
|
1029
|
+
.data(using: .utf8) else {
|
|
1030
|
+
let msg: String = "Stringify Encrypted Json Object " +
|
|
1031
|
+
"not Valid"
|
|
1032
|
+
throw CapacitorSQLiteError.failed(message: msg)
|
|
1033
|
+
}
|
|
1034
|
+
var encryptJson: [EncryptJson]
|
|
1035
|
+
do {
|
|
1036
|
+
encryptJson = try JSONDecoder()
|
|
1037
|
+
.decode([EncryptJson].self, from: data)
|
|
1038
|
+
let dict: [String: Any] = try
|
|
1039
|
+
UtilsJson.decryptBase64ToDictionary(
|
|
1040
|
+
encryptJson[0].expData,
|
|
1041
|
+
forAccount: account)
|
|
1042
|
+
importData = ImportData(jsonDict: dict)
|
|
1043
|
+
} catch let error {
|
|
1044
|
+
var msg: String = "Encrypted Json Object not Valid "
|
|
1045
|
+
msg.append("\(error)")
|
|
1046
|
+
throw CapacitorSQLiteError.failed(message: msg)
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
} else {
|
|
1050
|
+
|
|
1051
|
+
guard let data = ("["+parsingData+"]")
|
|
1052
|
+
.data(using: .utf8) else {
|
|
1053
|
+
let msg: String = "Stringify Json Object not Valid"
|
|
1054
|
+
throw CapacitorSQLiteError.failed(message: msg)
|
|
1055
|
+
}
|
|
1026
1056
|
var jsonSQLite: [JsonSQLite]
|
|
1027
1057
|
do {
|
|
1028
1058
|
jsonSQLite = try JSONDecoder()
|
|
1029
1059
|
.decode([JsonSQLite].self, from: data)
|
|
1060
|
+
importData = ImportData(jsonSQLite: jsonSQLite[0])
|
|
1061
|
+
jsonSQLite = []
|
|
1030
1062
|
} catch let error {
|
|
1031
1063
|
var msg: String = "Stringify Json Object not Valid "
|
|
1032
1064
|
msg.append("\(error)")
|
|
1033
1065
|
throw CapacitorSQLiteError.failed(message: msg)
|
|
1034
1066
|
}
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
}
|
|
1067
|
+
|
|
1068
|
+
}
|
|
1069
|
+
let encrypted: Bool = importData.encrypted
|
|
1070
|
+
let overwrite: Bool = importData.overwrite ?
|
|
1071
|
+
importData.overwrite : false
|
|
1072
|
+
let mode: String = importData.mode
|
|
1073
|
+
let inMode: String = encrypted ? "secret"
|
|
1074
|
+
: "no-encryption"
|
|
1075
|
+
let version: Int = importData.version
|
|
1076
|
+
var dbName: String = CapacitorSQLite.getDatabaseName(
|
|
1077
|
+
dbName: importData.database
|
|
1078
|
+
)
|
|
1079
|
+
dbName.append("SQLite.db")
|
|
1080
|
+
// open the database
|
|
1081
|
+
do {
|
|
1082
|
+
mDb = try Database(
|
|
1083
|
+
databaseLocation: databaseLocation, databaseName: dbName,
|
|
1084
|
+
encrypted: encrypted, isEncryption: isEncryption,
|
|
1085
|
+
account: account,
|
|
1086
|
+
mode: inMode, version: version, readonly: false,
|
|
1087
|
+
vUpgDict: [:])
|
|
1088
|
+
if overwrite && mode == "full" {
|
|
1089
|
+
let isExists = UtilsFile
|
|
1090
|
+
.isFileExist(databaseLocation: databaseLocation,
|
|
1091
|
+
fileName: dbName)
|
|
1092
|
+
if isExists {
|
|
1093
|
+
_ = try UtilsFile
|
|
1094
|
+
.deleteFile(fileName: dbName,
|
|
1095
|
+
databaseLocation: databaseLocation)
|
|
1065
1096
|
}
|
|
1066
|
-
try mDb.open()
|
|
1067
|
-
} catch UtilsFileError.deleteFileFailed {
|
|
1068
|
-
let message = "Delete Database failed"
|
|
1069
|
-
throw CapacitorSQLiteError.failed(message: message)
|
|
1070
|
-
} catch DatabaseError.open(let message) {
|
|
1071
|
-
throw CapacitorSQLiteError.failed(message: message)
|
|
1072
|
-
} catch let error {
|
|
1073
|
-
let msg: String = "\(error)"
|
|
1074
|
-
throw CapacitorSQLiteError.failed(message: msg)
|
|
1075
1097
|
}
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1098
|
+
try mDb.open()
|
|
1099
|
+
} catch UtilsFileError.deleteFileFailed {
|
|
1100
|
+
let message = "Delete Database failed"
|
|
1101
|
+
throw CapacitorSQLiteError.failed(message: message)
|
|
1102
|
+
} catch DatabaseError.open(let message) {
|
|
1103
|
+
throw CapacitorSQLiteError.failed(message: message)
|
|
1104
|
+
} catch let error {
|
|
1105
|
+
let msg: String = "\(error)"
|
|
1106
|
+
throw CapacitorSQLiteError.failed(message: msg)
|
|
1107
|
+
}
|
|
1108
|
+
// check if the database as some tables
|
|
1109
|
+
do {
|
|
1110
|
+
let tableList: [String] = try mDb.getTableNames()
|
|
1111
|
+
if mode == "full" && tableList.count > 0 {
|
|
1112
|
+
let curVersion = try mDb.getVersion()
|
|
1113
|
+
if version < curVersion {
|
|
1114
|
+
var msg: String = "ImportFromJson: Cannot import a "
|
|
1115
|
+
msg += "version lower than \(curVersion)"
|
|
1116
|
+
throw CapacitorSQLiteError.failed(message: msg)
|
|
1117
|
+
}
|
|
1118
|
+
if curVersion == version {
|
|
1119
|
+
var res: [String: Int] = [:]
|
|
1120
|
+
res["changes"] = 0
|
|
1121
|
+
return res
|
|
1091
1122
|
}
|
|
1123
|
+
}
|
|
1092
1124
|
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1125
|
+
} catch DatabaseError.getTableNames(let message) {
|
|
1126
|
+
throw CapacitorSQLiteError.failed(message: message)
|
|
1127
|
+
} catch let error {
|
|
1128
|
+
let msg: String = "\(error)"
|
|
1129
|
+
throw CapacitorSQLiteError.failed(message: msg)
|
|
1130
|
+
}
|
|
1131
|
+
// import from Json Object
|
|
1132
|
+
do {
|
|
1133
|
+
let res: [String: Int] = try mDb
|
|
1134
|
+
.importFromJson(importData: importData)
|
|
1135
|
+
try mDb.close()
|
|
1136
|
+
if let result = res["changes"] {
|
|
1137
|
+
if result < 0 {
|
|
1138
|
+
let msg: String = "changes < 0"
|
|
1139
|
+
throw CapacitorSQLiteError
|
|
1140
|
+
.failed(message: msg)
|
|
1141
|
+
} else {
|
|
1142
|
+
return res
|
|
1143
|
+
}
|
|
1144
|
+
} else {
|
|
1145
|
+
let msg: String = "changes not found"
|
|
1097
1146
|
throw CapacitorSQLiteError.failed(message: msg)
|
|
1098
1147
|
}
|
|
1099
|
-
|
|
1148
|
+
} catch DatabaseError.importFromJson(let message) {
|
|
1149
|
+
var msg = message
|
|
1100
1150
|
do {
|
|
1101
|
-
let res: [String: Int] = try mDb
|
|
1102
|
-
.importFromJson(jsonSQLite: jsonSQLite[0])
|
|
1103
1151
|
try mDb.close()
|
|
1104
|
-
|
|
1105
|
-
if result < 0 {
|
|
1106
|
-
let msg: String = "changes < 0"
|
|
1107
|
-
throw CapacitorSQLiteError
|
|
1108
|
-
.failed(message: msg)
|
|
1109
|
-
} else {
|
|
1110
|
-
return res
|
|
1111
|
-
}
|
|
1112
|
-
} else {
|
|
1113
|
-
let msg: String = "changes not found"
|
|
1114
|
-
throw CapacitorSQLiteError.failed(message: msg)
|
|
1115
|
-
}
|
|
1116
|
-
} catch DatabaseError.importFromJson(let message) {
|
|
1117
|
-
var msg = message
|
|
1118
|
-
do {
|
|
1119
|
-
try mDb.close()
|
|
1120
|
-
throw CapacitorSQLiteError.failed(message: msg)
|
|
1121
|
-
} catch DatabaseError.close(let message) {
|
|
1122
|
-
msg.append(" \(message)")
|
|
1123
|
-
throw CapacitorSQLiteError.failed(message: msg)
|
|
1124
|
-
}
|
|
1152
|
+
throw CapacitorSQLiteError.failed(message: msg)
|
|
1125
1153
|
} catch DatabaseError.close(let message) {
|
|
1126
|
-
|
|
1154
|
+
msg.append(" \(message)")
|
|
1155
|
+
throw CapacitorSQLiteError.failed(message: msg)
|
|
1127
1156
|
}
|
|
1128
|
-
}
|
|
1129
|
-
|
|
1130
|
-
throw CapacitorSQLiteError.failed(message: msg)
|
|
1157
|
+
} catch DatabaseError.close(let message) {
|
|
1158
|
+
throw CapacitorSQLiteError.failed(message: message)
|
|
1131
1159
|
}
|
|
1132
1160
|
} else {
|
|
1133
1161
|
throw CapacitorSQLiteError.failed(message: initMessage)
|
|
@@ -1156,7 +1184,8 @@ enum CapacitorSQLiteError: Error {
|
|
|
1156
1184
|
var msg: String = "return Object is empty "
|
|
1157
1185
|
msg.append("No data to synchronize")
|
|
1158
1186
|
throw CapacitorSQLiteError.failed(message: msg)
|
|
1159
|
-
|
|
1187
|
+
} else if res.count == 1 && res.contains(where: { $0.key == "expData" }) {
|
|
1188
|
+
return res
|
|
1160
1189
|
} else if res.count == 5 || res.count == 6 ||
|
|
1161
1190
|
res.count == 7 {
|
|
1162
1191
|
return res
|
|
@@ -594,6 +594,15 @@ class Database {
|
|
|
594
594
|
"expMode": expMode, "version": dbVersion]
|
|
595
595
|
retObj = try ExportToJson
|
|
596
596
|
.createExportObject(mDB: self, data: data)
|
|
597
|
+
if isEncryption && encrypted {
|
|
598
|
+
retObj["overwrite"] = true
|
|
599
|
+
let base64Str = try UtilsJson.encryptDictionaryToBase64(
|
|
600
|
+
retObj,
|
|
601
|
+
forAccount: account)
|
|
602
|
+
retObj = [:]
|
|
603
|
+
retObj["expData"] = base64Str
|
|
604
|
+
}
|
|
605
|
+
|
|
597
606
|
} catch UtilsJsonError.tableNotExists(let message) {
|
|
598
607
|
throw DatabaseError.exportToJson(message: message)
|
|
599
608
|
} catch ExportToJsonError.setLastExportDate(let message) {
|
|
@@ -617,7 +626,7 @@ class Database {
|
|
|
617
626
|
|
|
618
627
|
// MARK: - ImportFromJson
|
|
619
628
|
|
|
620
|
-
func importFromJson(
|
|
629
|
+
func importFromJson(importData: ImportData)
|
|
621
630
|
throws -> [String: Int] {
|
|
622
631
|
var changes: Int = 0
|
|
623
632
|
|
|
@@ -627,18 +636,21 @@ class Database {
|
|
|
627
636
|
try UtilsSQLCipher
|
|
628
637
|
.setForeignKeyConstraintsEnabled(mDB: self,
|
|
629
638
|
toggle: false)
|
|
630
|
-
if
|
|
639
|
+
if importData.tables.count > 0 {
|
|
631
640
|
changes = try ImportFromJson
|
|
632
641
|
.createDatabaseSchema(mDB: self,
|
|
633
|
-
|
|
642
|
+
tables: importData.tables,
|
|
643
|
+
mode: importData.mode,
|
|
644
|
+
version: importData.version)
|
|
634
645
|
if changes != -1 {
|
|
635
646
|
// Create the Database Data
|
|
636
647
|
changes += try ImportFromJson
|
|
637
648
|
.createDatabaseData(mDB: self,
|
|
638
|
-
|
|
649
|
+
tables: importData.tables,
|
|
650
|
+
mode: importData.mode)
|
|
639
651
|
}
|
|
640
652
|
}
|
|
641
|
-
if let mViews =
|
|
653
|
+
if let mViews = importData.views {
|
|
642
654
|
if mViews.count > 0 {
|
|
643
655
|
changes += try ImportFromJson
|
|
644
656
|
.createViews(mDB: self, views: mViews)
|