@capacitor-community/sqlite 3.4.2-4 → 3.4.3-1

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) {
@@ -72,9 +79,10 @@ class ExportToJson {
72
79
  // get the view's name
73
80
  var stmtV: String = "SELECT name,sql FROM sqlite_master WHERE "
74
81
  stmtV.append("type = 'view' AND name NOT LIKE 'sqlite_%';")
75
- let resViews = try UtilsSQLCipher.querySQL(
82
+ var resViews = try UtilsSQLCipher.querySQL(
76
83
  mDB: mDB, sql: stmtV, values: [])
77
- if resViews.count > 0 {
84
+ if resViews.count > 1 {
85
+ resViews.removeFirst()
78
86
  views = try ExportToJson
79
87
  .getViews(mDB: mDB,
80
88
  resViews: resViews)
@@ -85,9 +93,10 @@ class ExportToJson {
85
93
  var query: String = "SELECT name,sql FROM sqlite_master WHERE "
86
94
  query.append("type = 'table' AND name NOT LIKE 'sqlite_%' ")
87
95
  query.append("AND name NOT LIKE 'sync_table';")
88
- let resTables = try UtilsSQLCipher.querySQL(
96
+ var resTables = try UtilsSQLCipher.querySQL(
89
97
  mDB: mDB, sql: query, values: [])
90
- if resTables.count > 0 {
98
+ if resTables.count > 1 {
99
+ resTables.removeFirst()
91
100
  let isExists: Bool = try UtilsJson.isTableExists(
92
101
  mDB: mDB, tableName: "sync_table")
93
102
  if !isExists && expMode == "partial" {
@@ -528,9 +537,10 @@ class ExportToJson {
528
537
  var ret: Int64 = -1
529
538
  let query: String = "SELECT sync_date FROM sync_table;"
530
539
  do {
531
- let resSyncDate = try UtilsSQLCipher.querySQL(
540
+ var resSyncDate = try UtilsSQLCipher.querySQL(
532
541
  mDB: mDB, sql: query, values: [])
533
- if resSyncDate.count > 0 {
542
+ if resSyncDate.count > 1 {
543
+ resSyncDate.removeFirst()
534
544
  guard let res: Int64 = resSyncDate[0]["sync_date"] as?
535
545
  Int64 else {
536
546
  throw ExportToJsonError.getSyncDate(
@@ -548,6 +558,7 @@ class ExportToJson {
548
558
  // MARK: - ExportToJson - GetTablesModified
549
559
 
550
560
  // swiftlint:disable function_body_length
561
+ // swiftlint:disable cyclomatic_complexity
551
562
  class func getTablesModified(mDB: Database,
552
563
  tables: [[String: Any]],
553
564
  syncDate: Int64)
@@ -569,6 +580,9 @@ class ExportToJson {
569
580
  do {
570
581
  var resQuery = try UtilsSQLCipher.querySQL(
571
582
  mDB: mDB, sql: query, values: [])
583
+ if resQuery.count > 1 {
584
+ resQuery.removeFirst()
585
+ }
572
586
  if resQuery.count != 1 {
573
587
  break
574
588
  } else {
@@ -585,6 +599,9 @@ class ExportToJson {
585
599
  query.append("\(syncDate);")
586
600
  resQuery = try UtilsSQLCipher.querySQL(
587
601
  mDB: mDB, sql: query, values: [])
602
+ if resQuery.count > 1 {
603
+ resQuery.removeFirst()
604
+ }
588
605
  if resQuery.count != 1 {
589
606
  break
590
607
  } else {
@@ -618,6 +635,8 @@ class ExportToJson {
618
635
  }
619
636
  return retObj
620
637
  }
638
+ // swiftlint:enable cyclomatic_complexity
639
+ // swiftlint:enable function_body_length
621
640
 
622
641
  // MARK: - ExportToJson - ModEmbeddedParentheses
623
642
 
@@ -751,9 +770,10 @@ class ExportToJson {
751
770
  query.append("type = 'index' AND tbl_name = '\(tableName)' ")
752
771
  query.append("AND sql NOTNULL;")
753
772
  do {
754
- let resIndexes = try UtilsSQLCipher.querySQL(
773
+ var resIndexes = try UtilsSQLCipher.querySQL(
755
774
  mDB: mDB, sql: query, values: [])
756
- if resIndexes.count > 0 {
775
+ if resIndexes.count > 1 {
776
+ resIndexes.removeFirst()
757
777
  for ipos in 0..<resIndexes.count {
758
778
  var row: [String: String] = [:]
759
779
  let keys: [String] = Array(resIndexes[ipos].keys)
@@ -837,9 +857,10 @@ class ExportToJson {
837
857
  query.append("type = 'trigger' AND tbl_name = '\(tableName)' ")
838
858
  query.append("AND sql NOTNULL;")
839
859
  do {
840
- let resTriggers = try UtilsSQLCipher.querySQL(
860
+ var resTriggers = try UtilsSQLCipher.querySQL(
841
861
  mDB: mDB, sql: query, values: [])
842
- if resTriggers.count > 0 {
862
+ if resTriggers.count > 1 {
863
+ resTriggers.removeFirst()
843
864
  for ipos in 0..<resTriggers.count {
844
865
  var row: [String: String] = [:]
845
866
  let keys: [String] = Array(resTriggers[ipos].keys)
@@ -952,9 +973,10 @@ class ExportToJson {
952
973
 
953
974
  var retValues: [[Any]] = []
954
975
  do {
955
- let resValues = try UtilsSQLCipher.querySQL(
976
+ var resValues = try UtilsSQLCipher.querySQL(
956
977
  mDB: mDB, sql: query, values: [])
957
- if resValues.count > 0 {
978
+ if resValues.count > 1 {
979
+ resValues.removeFirst()
958
980
  for ipos in 0..<resValues.count {
959
981
  var row: [Any] = []
960
982
  do {
@@ -981,14 +1003,19 @@ class ExportToJson {
981
1003
 
982
1004
  // MARK: - ExportToJson - CreateRowValues
983
1005
 
1006
+ // swiftlint:disable function_body_length
1007
+ // swiftlint:disable cyclomatic_complexity
984
1008
  class func createRowValues(values: [[String: Any]], pos: Int,
985
1009
  names: [String],
986
1010
  types: [String] ) throws -> [Any] {
987
1011
  var row: [Any] = []
988
1012
  for jpos in 0..<names.count {
989
-
990
- // if types[jpos] == "INTEGER" {
991
- 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
+ ) {
992
1019
  guard let val = values[pos][names[jpos]] as? String
993
1020
  else {
994
1021
  throw ExportToJsonError.createValues(
@@ -1002,14 +1029,29 @@ class ExportToJson {
1002
1029
  message: "Error value must be NSNull")
1003
1030
  }
1004
1031
  row.append(val)
1005
- } 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())) {
1006
1035
  guard let val = values[pos][names[jpos]] as? Int64
1007
1036
  else {
1008
1037
  throw ExportToJsonError.createValues(
1009
1038
  message: "Error value must be Int64")
1010
1039
  }
1011
1040
  row.append(val)
1012
- } 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())) {
1013
1055
  guard let val = values[pos][names[jpos]] as? Double
1014
1056
  else {
1015
1057
  throw ExportToJsonError.createValues(
@@ -1025,6 +1067,8 @@ class ExportToJson {
1025
1067
  }
1026
1068
  return row
1027
1069
  }
1070
+ // swiftlint:enable cyclomatic_complexity
1071
+ // swiftlint:enable function_body_length
1028
1072
 
1029
1073
  }
1030
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
@@ -24,10 +24,10 @@ class UtilsBinding {
24
24
  value: value, idx: idx)
25
25
  idx += 1
26
26
  } else {
27
- message = "Error: querySQL bind failed "
27
+ message = "Error: bindValues bind failed "
28
28
  }
29
29
  } catch let error as NSError {
30
- message = "Error: querySQL bind failed "
30
+ message = "Error: bindValues bind failed "
31
31
  message.append("\(error.localizedDescription)")
32
32
  }
33
33
  if message.count > 0 { break }
@@ -33,8 +33,9 @@ class UtilsDrop {
33
33
  query.append("AND name NOT LIKE 'sqlite_%' ")
34
34
  query.append("ORDER BY rootpage DESC;")
35
35
  do {
36
- let resQuery = try mDB.selectSQL(sql: query, values: [])
36
+ var resQuery = try mDB.selectSQL(sql: query, values: [])
37
37
  if resQuery.count > 0 {
38
+ resQuery.removeFirst()
38
39
  for ipos in 0..<resQuery.count {
39
40
  if let mName = resQuery[ipos]["name"] as? String {
40
41
  names.append("\(mName)")
@@ -81,8 +82,9 @@ class UtilsDrop {
81
82
  query.append("type='view' AND name NOT LIKE 'sqlite_%' ")
82
83
  query.append("ORDER BY rootpage DESC;")
83
84
  do {
84
- let resQuery = try mDB.selectSQL(sql: query, values: [])
85
+ var resQuery = try mDB.selectSQL(sql: query, values: [])
85
86
  if resQuery.count > 0 {
87
+ resQuery.removeFirst()
86
88
  for ipos in 0..<resQuery.count {
87
89
  if let mName = resQuery[ipos]["name"] as? String {
88
90
  names.append("\(mName)")
@@ -128,8 +130,9 @@ class UtilsDrop {
128
130
  var query: String = "SELECT name FROM sqlite_master WHERE "
129
131
  query.append("type='index' AND name NOT LIKE 'sqlite_%';")
130
132
  do {
131
- let resQuery = try mDB.selectSQL(sql: query, values: [])
133
+ var resQuery = try mDB.selectSQL(sql: query, values: [])
132
134
  if resQuery.count > 0 {
135
+ resQuery.removeFirst()
133
136
  for ipos in 0..<resQuery.count {
134
137
  if let mName = resQuery[ipos]["name"] as? String {
135
138
  indexes.append("\(mName)")
@@ -174,8 +177,9 @@ class UtilsDrop {
174
177
  var query: String = "SELECT name FROM sqlite_master WHERE "
175
178
  query.append("type='trigger';")
176
179
  do {
177
- let resQuery = try mDB.selectSQL(sql: query, values: [])
180
+ var resQuery = try mDB.selectSQL(sql: query, values: [])
178
181
  if resQuery.count > 0 {
182
+ resQuery.removeFirst()
179
183
  for ipos in 0..<resQuery.count {
180
184
  if let mName = resQuery[ipos]["name"] as? String {
181
185
  triggers.append("\(mName)")
@@ -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
 
@@ -38,9 +40,14 @@ class UtilsJson {
38
40
  query.append(tableName)
39
41
  query.append("';")
40
42
  do {
41
- let resQuery: [Any] = try UtilsSQLCipher
43
+ var resQuery: [Any] = try UtilsSQLCipher
42
44
  .querySQL(mDB: mDB, sql: query, values: [])
43
- if resQuery.count > 0 {ret = true}
45
+ if resQuery.count > 0 {
46
+ resQuery.removeFirst()
47
+ if resQuery.count == 1 {
48
+ ret = true
49
+ }
50
+ }
44
51
  } catch UtilsSQLCipherError.querySQL(let message) {
45
52
  throw UtilsJsonError.tableNotExists(message: message)
46
53
  }
@@ -89,9 +96,14 @@ class UtilsJson {
89
96
  query.append(viewName)
90
97
  query.append("';")
91
98
  do {
92
- let resQuery: [Any] = try UtilsSQLCipher
99
+ var resQuery: [Any] = try UtilsSQLCipher
93
100
  .querySQL(mDB: mDB, sql: query, values: [])
94
- if resQuery.count > 0 {ret = true}
101
+ if resQuery.count > 0 {
102
+ resQuery.removeFirst()
103
+ if resQuery.count == 1 {
104
+ ret = true
105
+ }
106
+ }
95
107
  } catch UtilsSQLCipherError.querySQL(let message) {
96
108
  throw UtilsJsonError.viewNotExists(message: message)
97
109
  }
@@ -109,8 +121,9 @@ class UtilsJson {
109
121
  query.append(tableName)
110
122
  query.append(");")
111
123
  do {
112
- let resQuery = try mDB.selectSQL(sql: query, values: [])
124
+ var resQuery = try mDB.selectSQL(sql: query, values: [])
113
125
  if resQuery.count > 0 {
126
+ resQuery.removeFirst()
114
127
  var names: [String] = []
115
128
  var types: [String] = []
116
129
  for ipos in 0..<resQuery.count {
@@ -190,9 +203,12 @@ class UtilsJson {
190
203
  query.append("\(key);")
191
204
  }
192
205
  do {
193
- let resQuery = try mDB.selectSQL(sql: query, values: [])
194
- if resQuery.count == 1 {
195
- ret = true
206
+ var resQuery = try mDB.selectSQL(sql: query, values: [])
207
+ if resQuery.count > 1 {
208
+ resQuery.removeFirst()
209
+ if resQuery.count == 1 {
210
+ ret = true
211
+ }
196
212
  }
197
213
  } catch DatabaseError.selectSQL(let message) {
198
214
  throw UtilsJsonError.isIdExists(
@@ -224,6 +240,70 @@ class UtilsJson {
224
240
  return retArray
225
241
  }
226
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
+ }
227
307
  // MARK: - ImportFromJson - SetNameForUpdate
228
308
 
229
309
  class func setNameForUpdate(names: [String]) -> String {
@@ -360,3 +440,4 @@ class UtilsJson {
360
440
 
361
441
  }
362
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"
@@ -266,11 +245,12 @@ class UtilsSQLCipher {
266
245
 
267
246
  let sqltr: String = "PRAGMA user_version;"
268
247
  do {
269
- let resVersion = try UtilsSQLCipher.querySQL(mDB: mDB,
248
+ var resVersion = try UtilsSQLCipher.querySQL(mDB: mDB,
270
249
  sql: sqltr,
271
250
  values: [])
272
- if resVersion.count > 0 {
273
- guard let res: Int64 = resVersion[1]["user_version"]
251
+ if resVersion.count > 1 {
252
+ resVersion.removeFirst()
253
+ guard let res: Int64 = resVersion[0]["user_version"]
274
254
  as? Int64 else {
275
255
  throw UtilsSQLCipherError.getVersion(
276
256
  message: "Error get version failed")
@@ -504,13 +484,14 @@ class UtilsSQLCipher {
504
484
  // MARK: - FetchColumnInfo
505
485
 
506
486
  // swiftlint:disable function_body_length
487
+ // swiftlint:disable cyclomatic_complexity
507
488
  class func fetchColumnInfo(handle: OpaquePointer?)
508
489
  throws -> [[String: Any]] {
509
490
  var result: [[String: Any]] = []
510
491
  var columnCount: Int32 = 0
511
492
  var columnNames: [String] = []
512
493
  var columnData: [String: Any] = [:]
513
-
494
+
514
495
  while sqlite3_step(handle) == SQLITE_ROW {
515
496
  columnCount = sqlite3_column_count(handle)
516
497
  var rowData: [String: Any] = [:]
@@ -567,6 +548,7 @@ class UtilsSQLCipher {
567
548
  }
568
549
  return result
569
550
  }
551
+ // swiftlint:enable cyclomatic_complexity
570
552
  // swiftlint:enable function_body_length
571
553
 
572
554
  // MARK: - dbChanges
@@ -22,7 +22,7 @@ enum UtilsUpgradeError: Error {
22
22
  case executeStatementProcessFailed(message: String)
23
23
  case executeSetProcessFailed(message: String)
24
24
  }
25
-
25
+ // swiftlint:disable file_length
26
26
  // swiftlint:disable type_body_length
27
27
  class UtilsUpgrade {
28
28
  let utilsDrop: UtilsDrop = UtilsDrop()
@@ -88,6 +88,13 @@ class UtilsUpgrade {
88
88
  if statement.count > 0 {
89
89
  _ = try executeStatementProcess(mDB: mDB,
90
90
  statement: statement)
91
+ // -> Drop _temp_tables
92
+ _ = try UtilsDrop
93
+ .dropTempTables(mDB: mDB,
94
+ alterTables: self.alterTables)
95
+ // -> Do some cleanup
96
+ self.alterTables = [:]
97
+ self.commonColumns = [:]
91
98
 
92
99
  // here we assume that the Set contains only
93
100
  // - the data for new tables as INSERT statements
@@ -116,6 +123,25 @@ class UtilsUpgrade {
116
123
  throw UtilsUpgradeError.onUpgradeFailed(message: msg)
117
124
  } catch UtilsUpgradeError.executeStatementProcessFailed(
118
125
  let message) {
126
+ var msg: String = message
127
+ do {
128
+ // -> Drop _temp_tables
129
+ _ = try UtilsDrop
130
+ .dropTempTables(mDB: mDB,
131
+ alterTables: self.alterTables)
132
+ // -> Do some cleanup
133
+ self.alterTables = [:]
134
+ self.commonColumns = [:]
135
+
136
+ } catch UtilsDropError.dropTempTablesFailed(let message) {
137
+ msg += ": \(message)"
138
+ throw UtilsUpgradeError.onUpgradeFailed(
139
+ message: message)
140
+ }
141
+
142
+ throw UtilsUpgradeError.onUpgradeFailed(
143
+ message: message)
144
+ } catch UtilsDropError.dropTempTablesFailed(let message) {
119
145
  throw UtilsUpgradeError.onUpgradeFailed(
120
146
  message: message)
121
147
  } catch UtilsUpgradeError.executeSetProcessFailed(
@@ -194,6 +220,7 @@ class UtilsUpgrade {
194
220
  throws {
195
221
  var changes: Int = -1
196
222
  do {
223
+
197
224
  // -> backup all existing tables "tableName" in
198
225
  // "temp_tableName"
199
226
  _ = try backupTables(mDB: mDB)
@@ -216,13 +243,6 @@ class UtilsUpgrade {
216
243
  // -> Update the new table's data from old table's data
217
244
  _ = try updateNewTablesData(mDB: mDB)
218
245
 
219
- // -> Drop _temp_tables
220
- _ = try UtilsDrop
221
- .dropTempTables(mDB: mDB,
222
- alterTables: self.alterTables)
223
- // -> Do some cleanup
224
- self.alterTables = [:]
225
- self.commonColumns = [:]
226
246
  } catch UtilsUpgradeError.backupTablesFailed(let message) {
227
247
  throw UtilsUpgradeError.executeStatementProcessFailed(
228
248
  message: message)
@@ -243,9 +263,6 @@ class UtilsUpgrade {
243
263
  let message) {
244
264
  throw UtilsUpgradeError.executeStatementProcessFailed(
245
265
  message: message)
246
- } catch UtilsDropError.dropTempTablesFailed(let message) {
247
- throw UtilsUpgradeError.executeStatementProcessFailed(
248
- message: message)
249
266
  }
250
267
 
251
268
  }
@@ -276,9 +293,12 @@ class UtilsUpgrade {
276
293
  columnNames = try getTableColumnNames(mDB: mDB,
277
294
  tableName: tableName)
278
295
  alterTables["\(tableName)"] = columnNames
296
+ let tmpTable = "_temp_\(tableName)"
297
+ let delStmt: String = "DROP TABLE IF EXISTS \(tmpTable);"
298
+ _ = try mDB.runSQL(sql: delStmt, values: [])
279
299
  // prefix the table with _temp_
280
300
  var stmt: String = "ALTER TABLE \(tableName) RENAME "
281
- stmt.append("TO _temp_\(tableName);")
301
+ stmt.append("TO \(tmpTable);")
282
302
  let retRun: [String: Int64] = try mDB.runSQL(
283
303
  sql: stmt, values: [])
284
304
  guard let changes: Int64 = retRun["changes"] else {
@@ -307,9 +327,10 @@ class UtilsUpgrade {
307
327
  var retNames: [String] = []
308
328
  let query: String = "PRAGMA table_info('\(tableName)');"
309
329
  do {
310
- let resColumns: [[String: Any]] = try
330
+ var resColumns: [[String: Any]] = try
311
331
  mDB.selectSQL(sql: query, values: [])
312
332
  if resColumns.count > 0 {
333
+ resColumns.removeFirst()
313
334
  for rColumn in resColumns {
314
335
  guard let columnName: String = rColumn["name"] as?
315
336
  String else {
@@ -391,3 +412,4 @@ class UtilsUpgrade {
391
412
  }
392
413
  }
393
414
  // swiftlint:enable type_body_length
415
+ // swiftlint:enable file_length
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capacitor-community/sqlite",
3
- "version": "3.4.2-4",
3
+ "version": "3.4.3-1",
4
4
  "description": "Community plugin for native & electron SQLite databases",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
@@ -55,10 +55,10 @@
55
55
  "prepublishOnly": "npm run build && npm run build-electron && npm run docgen"
56
56
  },
57
57
  "devDependencies": {
58
- "@capacitor/android": "^3.4.2",
59
- "@capacitor/core": "^3.4.2",
58
+ "@capacitor/android": "^3.4.3",
59
+ "@capacitor/core": "^3.4.3",
60
60
  "@capacitor/docgen": "^0.0.17",
61
- "@capacitor/ios": "^3.4.2",
61
+ "@capacitor/ios": "^3.4.3",
62
62
  "@ionic/eslint-config": "^0.3.0",
63
63
  "@ionic/prettier-config": "^1.0.1",
64
64
  "@ionic/swiftlint-config": "^1.1.2",
@@ -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.3"
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.2"
97
97
  }
98
98
  }