@capacitor-community/sqlite 3.4.2-5 → 3.4.2

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.
@@ -28,9 +28,16 @@ enum ExportToJsonError: Error {
28
28
  case modEmbeddedParentheses(message: String)
29
29
  case getViews(message: String)
30
30
  }
31
-
31
+ var REALAFFINITY: [String] = ["REAL", "DOUBLE", "DOUBLE PRECISION", "FLOAT"]
32
+ var INTEGERAFFINITY: [String] = ["INTEGER", "INT", "TINYINT", "SMALLINT",
33
+ "MEDIUMINT", "BIGINT", "UNSIGNED BIG INT",
34
+ "INT2", "INT8"]
35
+ var TEXTAFFINITY: [String] = ["TEXT", "CHARACTER", "VARCHAR", "VARYING CHARACTER",
36
+ "NCHAR", "NATIVE CHARACTER", "NVARCHAR", "CLOB"]
37
+ var BLOBAFFINITY: [String] = ["BLOB"]
38
+ var NUMERICAFFINITY: [String] = ["NUMERIC", "DECIMAL", "BOOLEAN", "DATE",
39
+ "DATETIME"]
32
40
  class ExportToJson {
33
-
34
41
  // MARK: - JsonNotifications - NotifyExportProgressEvent
35
42
 
36
43
  class func notifyExportProgressEvent(msg: String) {
@@ -996,14 +1003,19 @@ class ExportToJson {
996
1003
 
997
1004
  // MARK: - ExportToJson - CreateRowValues
998
1005
 
1006
+ // swiftlint:disable function_body_length
1007
+ // swiftlint:disable cyclomatic_complexity
999
1008
  class func createRowValues(values: [[String: Any]], pos: Int,
1000
1009
  names: [String],
1001
1010
  types: [String] ) throws -> [Any] {
1002
1011
  var row: [Any] = []
1003
1012
  for jpos in 0..<names.count {
1004
-
1005
- // if types[jpos] == "INTEGER" {
1006
- if values[pos][names[jpos]] is String {
1013
+ if values[pos][names[jpos]] is String && (TEXTAFFINITY
1014
+ .contains(types[jpos].components(separatedBy: "(")[0]
1015
+ .uppercased())
1016
+ || BLOBAFFINITY.contains(types[jpos].uppercased())
1017
+ || NUMERICAFFINITY.contains(types[jpos].uppercased())
1018
+ ) {
1007
1019
  guard let val = values[pos][names[jpos]] as? String
1008
1020
  else {
1009
1021
  throw ExportToJsonError.createValues(
@@ -1017,14 +1029,29 @@ class ExportToJson {
1017
1029
  message: "Error value must be NSNull")
1018
1030
  }
1019
1031
  row.append(val)
1020
- } else if values[pos][names[jpos]] is Int64 {
1032
+ } else if values[pos][names[jpos]] is Int64 && (
1033
+ INTEGERAFFINITY.contains(types[jpos].uppercased()) ||
1034
+ NUMERICAFFINITY.contains(types[jpos].uppercased())) {
1021
1035
  guard let val = values[pos][names[jpos]] as? Int64
1022
1036
  else {
1023
1037
  throw ExportToJsonError.createValues(
1024
1038
  message: "Error value must be Int64")
1025
1039
  }
1026
1040
  row.append(val)
1027
- } else if values[pos][names[jpos]] is Double {
1041
+ } else if values[pos][names[jpos]] is Int64 && (
1042
+ REALAFFINITY.contains(types[jpos].uppercased()) ||
1043
+ NUMERICAFFINITY.contains(types[jpos]
1044
+ .components(separatedBy: "(")[0].uppercased())) {
1045
+ guard let val = values[pos][names[jpos]] as? Int64
1046
+ else {
1047
+ throw ExportToJsonError.createValues(
1048
+ message: "Error value must be double")
1049
+ }
1050
+ row.append(Double(val))
1051
+ } else if values[pos][names[jpos]] is Double && (
1052
+ REALAFFINITY.contains(types[jpos].uppercased()) ||
1053
+ NUMERICAFFINITY.contains(types[jpos]
1054
+ .components(separatedBy: "(")[0].uppercased())) {
1028
1055
  guard let val = values[pos][names[jpos]] as? Double
1029
1056
  else {
1030
1057
  throw ExportToJsonError.createValues(
@@ -1040,6 +1067,8 @@ class ExportToJson {
1040
1067
  }
1041
1068
  return row
1042
1069
  }
1070
+ // swiftlint:enable cyclomatic_complexity
1071
+ // swiftlint:enable function_body_length
1043
1072
 
1044
1073
  }
1045
1074
  // swiftlint:enable type_body_length
@@ -236,7 +236,7 @@ class ImportFromJson {
236
236
  stmt.append(" AFTER UPDATE ON ")
237
237
  stmt.append(tableName)
238
238
  stmt.append(" FOR EACH ROW ")
239
- stmt.append("WHEN NEW.last_modified <= OLD.last_modified ")
239
+ stmt.append("WHEN NEW.last_modified < OLD.last_modified ")
240
240
  stmt.append("BEGIN UPDATE ")
241
241
  stmt.append(tableName)
242
242
  stmt.append(" SET last_modified = (strftime('%s','now')) ")
@@ -409,6 +409,8 @@ class ImportFromJson {
409
409
  // Check row validity
410
410
  let row: [UncertainValue<String, Int, Double>] =
411
411
  mValues[jpos]
412
+ var isRun: Bool = true
413
+
412
414
  /* Remove types checking for allowing RDBMS Types
413
415
  do {
414
416
  try UtilsJson.checkRowValidity(
@@ -429,19 +431,26 @@ class ImportFromJson {
429
431
  jsonNamesTypes: jsonNamesTypes)
430
432
  let rowValues = UtilsJson.getValuesFromRow(
431
433
  rowValues: row)
432
- let lastId: Int64 = try UtilsSQLCipher.prepareSQL(
433
- mDB: mDB, sql: stmt, values: rowValues)
434
- if lastId < 0 {
435
- throw ImportFromJsonError.createTableData(
436
- message: "lastId < 0")
434
+ isRun = try UtilsJson.checkUpdate(mDB: mDB, stmt: stmt,
435
+ values: rowValues,
436
+ tableName: tableName,
437
+ names: jsonNamesTypes.names,
438
+ types: jsonNamesTypes.types)
439
+
440
+ if isRun {
441
+ let lastId: Int64 = try UtilsSQLCipher.prepareSQL(
442
+ mDB: mDB, sql: stmt, values: rowValues)
443
+ if lastId < 0 {
444
+ throw ImportFromJsonError.createTableData(
445
+ message: "lastId < 0")
446
+ }
437
447
  }
438
- } catch ImportFromJsonError.createRowStatement(
439
- let message) {
440
- throw ImportFromJsonError.createTableData(
441
- message: message)
448
+ } catch UtilsJsonError.checkUpdate(let message) {
449
+ throw ImportFromJsonError.createTableData(message: message)
450
+ } catch ImportFromJsonError.createRowStatement(let message) {
451
+ throw ImportFromJsonError.createTableData(message: message)
442
452
  } catch UtilsSQLCipherError.prepareSQL(let message) {
443
- throw ImportFromJsonError.createTableData(
444
- message: message)
453
+ throw ImportFromJsonError.createTableData(message: message)
445
454
  }
446
455
  }
447
456
  return
@@ -18,8 +18,10 @@ enum UtilsJsonError: Error {
18
18
  case validateTriggers(message: String)
19
19
  case validateViews(message: String)
20
20
  case isLastModified(message: String)
21
- }
21
+ case checkUpdate(message: String)
22
+ case checkValues(message: String)}
22
23
 
24
+ // swiftlint:disable file_length
23
25
  // swiftlint:disable type_body_length
24
26
  class UtilsJson {
25
27
 
@@ -238,6 +240,70 @@ class UtilsJson {
238
240
  return retArray
239
241
  }
240
242
 
243
+ // MARK: - ImportFromJson - CheckUpdate
244
+
245
+ // swiftlint:disable function_parameter_count
246
+ class func checkUpdate(mDB: Database, stmt: String, values: [Any],
247
+ tableName: String, names: [String],
248
+ types: [String]) throws -> Bool {
249
+ var isRun: Bool = true
250
+ if stmt.prefix(6) == "UPDATE" {
251
+ var query: String = "SELECT * FROM \(tableName) WHERE \(names[0]) = "
252
+ if type(of: values[0]) == String.self {
253
+ query.append("'\(values[0])';")
254
+ } else {
255
+ query.append("\(values[0]);")
256
+ }
257
+
258
+ do {
259
+ // create the table data
260
+ let resValues: [[Any]] = try ExportToJson
261
+ .createValues(mDB: mDB, query: query, names: names,
262
+ types: types)
263
+ if resValues.count > 0 {
264
+ isRun = try checkValues(values: values, nValues: resValues[0])
265
+ return isRun
266
+ } else {
267
+ let msg = "CheckUpdate statement returns nothing"
268
+ throw UtilsJsonError.checkUpdate(message: msg)
269
+ }
270
+ } catch ExportToJsonError.createValues(let message) {
271
+ throw UtilsJsonError.checkUpdate(message: message)
272
+ } catch UtilsJsonError.checkValues(let message) {
273
+ throw UtilsJsonError.checkUpdate(message: message)
274
+ }
275
+ }
276
+ return isRun
277
+ }
278
+ // swiftlint:enable function_parameter_count
279
+
280
+ // MARK: - ImportFromJson - CheckValues
281
+
282
+ class func checkValues(values: [Any], nValues: [Any]) throws -> Bool {
283
+ if values.count > 0 && nValues.count > 0
284
+ && values.count == nValues.count {
285
+ let valuesWithIndex = values.enumerated()
286
+ for (index, mValue) in valuesWithIndex {
287
+ let rValue = nValues[index]
288
+ if type(of: rValue) == Double.self {
289
+ if let dValue = rValue as? Double {
290
+ if let dValues = mValue as? Double {
291
+ if !dValue.isEqual(to: dValues) {
292
+ return true
293
+ }
294
+ }
295
+ }
296
+ } else if rValue as AnyObject !== values[index] as AnyObject {
297
+ return true
298
+ }
299
+ }
300
+ return false
301
+ } else {
302
+ let msg = "CheckValues Both arrays not the same length"
303
+ throw UtilsJsonError.checkValues(message: msg)
304
+ }
305
+
306
+ }
241
307
  // MARK: - ImportFromJson - SetNameForUpdate
242
308
 
243
309
  class func setNameForUpdate(names: [String]) -> String {
@@ -374,3 +440,4 @@ class UtilsJson {
374
440
 
375
441
  }
376
442
  // swiftlint:enable type_body_length
443
+ // swiftlint:enable file_length
@@ -159,27 +159,6 @@ class UtilsSQLCipher {
159
159
  let msg: String = "Cannot open the DB"
160
160
  throw UtilsSQLCipherError.openOrCreateDatabase(message: msg)
161
161
  }
162
-
163
- /* this should work but doe not sqlite3_key_v2 is not known
164
- if password.count > 0 {
165
- let nKey:Int32 = Int32(password.count)
166
- if sqlite3_key_v2(mDB!, filename, password, nKey) == SQLITE_OK {
167
- var stmt: String = "SELECT count(*) FROM "
168
- stmt.append("sqlite_master;")
169
- if sqlite3_exec(mDB, stmt, nil, nil, nil) !=
170
- SQLITE_OK {
171
- print("Unable to open a database \(filename)")
172
- throw UtilsSQLCipherError
173
- .openOrCreateDatabase(message: msg)
174
- }
175
- } else {
176
- print("Unable to open a database \(filename)")
177
- throw UtilsSQLCipherError
178
- .openOrCreateDatabase(message: msg)
179
- }
180
- }
181
- print("Successfully opened database \(filename)")
182
- */
183
162
  return mDB
184
163
  } else {
185
164
  let message: String = "open_v2 failed"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capacitor-community/sqlite",
3
- "version": "3.4.2-5",
3
+ "version": "3.4.2",
4
4
  "description": "Community plugin for native & electron SQLite databases",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
@@ -74,7 +74,7 @@
74
74
  "typescript": "~4.0.5"
75
75
  },
76
76
  "peerDependencies": {
77
- "@capacitor/core": "~3.4.1"
77
+ "@capacitor/core": "~3.4.2"
78
78
  },
79
79
  "prettier": "@ionic/prettier-config",
80
80
  "swiftlint": "@ionic/swiftlint-config",
@@ -93,6 +93,6 @@
93
93
  }
94
94
  },
95
95
  "dependencies": {
96
- "jeep-sqlite": "^1.4.0"
96
+ "jeep-sqlite": "^1.4.1"
97
97
  }
98
98
  }