@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.
@@ -17,6 +17,8 @@ export default {
17
17
  'electron',
18
18
  'electron-json-storage',
19
19
  'better-sqlite3-multiple-ciphers',
20
+ 'crypto',
21
+ 'crypto-js',
20
22
  'path',
21
23
  'fs',
22
24
  'os',
@@ -1022,112 +1022,140 @@ enum CapacitorSQLiteError: Error {
1022
1022
  throws -> [String: Int] {
1023
1023
  if isInit {
1024
1024
  var mDb: Database
1025
- if let data = ("["+parsingData+"]").data(using: .utf8) {
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
- let encrypted: Bool = jsonSQLite[0].encrypted
1036
- var overwrite: Bool = false
1037
- if let mOverwrite = jsonSQLite[0].overwrite {
1038
- overwrite = mOverwrite
1039
- }
1040
- let mode: String = jsonSQLite[0].mode
1041
- let inMode: String = encrypted ? "secret"
1042
- : "no-encryption"
1043
- let version: Int = jsonSQLite[0].version
1044
- var dbName: String = CapacitorSQLite.getDatabaseName(
1045
- dbName: jsonSQLite[0].database
1046
- )
1047
- dbName.append("SQLite.db")
1048
- // open the database
1049
- do {
1050
- mDb = try Database(
1051
- databaseLocation: databaseLocation, databaseName: dbName,
1052
- encrypted: encrypted, isEncryption: isEncryption,
1053
- account: account,
1054
- mode: inMode, version: version, readonly: false,
1055
- vUpgDict: [:])
1056
- if overwrite && mode == "full" {
1057
- let isExists = UtilsFile
1058
- .isFileExist(databaseLocation: databaseLocation,
1059
- fileName: dbName)
1060
- if isExists {
1061
- _ = try UtilsFile
1062
- .deleteFile(fileName: dbName,
1063
- databaseLocation: databaseLocation)
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
- // check if the database as some tables
1077
- do {
1078
- let tableList: [String] = try mDb.getTableNames()
1079
- if mode == "full" && tableList.count > 0 {
1080
- let curVersion = try mDb.getVersion()
1081
- if version < curVersion {
1082
- var msg: String = "ImportFromJson: Cannot import a "
1083
- msg += "version lower than \(curVersion)"
1084
- throw CapacitorSQLiteError.failed(message: msg)
1085
- }
1086
- if curVersion == version {
1087
- var res: [String: Int] = [:]
1088
- res["changes"] = 0
1089
- return res
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
- } catch DatabaseError.getTableNames(let message) {
1094
- throw CapacitorSQLiteError.failed(message: message)
1095
- } catch let error {
1096
- let msg: String = "\(error)"
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
- // import from Json Object
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
- if let result = res["changes"] {
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
- throw CapacitorSQLiteError.failed(message: message)
1154
+ msg.append(" \(message)")
1155
+ throw CapacitorSQLiteError.failed(message: msg)
1127
1156
  }
1128
- } else {
1129
- let msg: String = "Stringify Json Object not Valid"
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(jsonSQLite: JsonSQLite)
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 jsonSQLite.tables.count > 0 {
639
+ if importData.tables.count > 0 {
631
640
  changes = try ImportFromJson
632
641
  .createDatabaseSchema(mDB: self,
633
- jsonSQLite: jsonSQLite)
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
- jsonSQLite: jsonSQLite)
649
+ tables: importData.tables,
650
+ mode: importData.mode)
639
651
  }
640
652
  }
641
- if let mViews = jsonSQLite.views {
653
+ if let mViews = importData.views {
642
654
  if mViews.count > 0 {
643
655
  changes += try ImportFromJson
644
656
  .createViews(mDB: self, views: mViews)