@capacitor-community/sqlite 3.4.2 → 3.4.3-3

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.
Files changed (31) hide show
  1. package/README.md +42 -2
  2. package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLite.java +41 -2
  3. package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLitePlugin.java +26 -0
  4. package/android/src/main/java/com/getcapacitor/community/database/sqlite/NotificationCenter.java +1 -1
  5. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java +220 -7
  6. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ExportToJson.java +100 -3
  7. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ImportFromJson.java +37 -34
  8. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsUpgrade.java +8 -4
  9. package/dist/esm/definitions.d.ts +17 -1
  10. package/dist/esm/definitions.js +9 -17
  11. package/dist/esm/definitions.js.map +1 -1
  12. package/dist/esm/web.d.ts +20 -11
  13. package/dist/esm/web.js +288 -473
  14. package/dist/esm/web.js.map +1 -1
  15. package/dist/plugin.cjs.js +273 -466
  16. package/dist/plugin.cjs.js.map +1 -1
  17. package/dist/plugin.js +1036 -1229
  18. package/dist/plugin.js.map +1 -1
  19. package/electron/dist/plugin.js +1147 -1032
  20. package/electron/dist/plugin.js.map +1 -1
  21. package/ios/Plugin/CapacitorSQLite.swift +34 -1
  22. package/ios/Plugin/CapacitorSQLitePlugin.m +1 -0
  23. package/ios/Plugin/CapacitorSQLitePlugin.swift +25 -0
  24. package/ios/Plugin/Database.swift +29 -1
  25. package/ios/Plugin/Extensions/String.swift +8 -0
  26. package/ios/Plugin/ImportExportJson/ExportToJson.swift +154 -25
  27. package/ios/Plugin/ImportExportJson/ImportFromJson.swift +53 -27
  28. package/ios/Plugin/Utils/UtilsDrop.swift +2 -2
  29. package/ios/Plugin/Utils/UtilsSQLCipher.swift +277 -8
  30. package/ios/Plugin/Utils/UtilsUpgrade.swift +33 -13
  31. package/package.json +6 -6
@@ -959,7 +959,13 @@ enum CapacitorSQLiteError: Error {
959
959
  do {
960
960
  let res: [String: Any] = try
961
961
  mDb.exportToJson(expMode: expMode)
962
- if res.count == 5 || res.count == 6 || res.count == 7 {
962
+ if res.count == 0 {
963
+ var msg: String = "return Object is empty "
964
+ msg.append("No data to synchronize")
965
+ throw CapacitorSQLiteError.failed(message: msg)
966
+
967
+ } else if res.count == 5 || res.count == 6 ||
968
+ res.count == 7 {
963
969
  return res
964
970
  } else {
965
971
  var msg: String = "return Object is not a "
@@ -981,6 +987,33 @@ enum CapacitorSQLiteError: Error {
981
987
  }
982
988
  }
983
989
 
990
+ // MARK: - deleteExportedRows
991
+
992
+ @objc func deleteExportedRows(_ dbName: String) throws {
993
+ if isInit {
994
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
995
+ guard let mDb: Database = dbDict[mDbName] else {
996
+ let msg = "Connection to \(mDbName) not available"
997
+ throw CapacitorSQLiteError.failed(message: msg)
998
+ }
999
+ if mDb.isDBOpen() {
1000
+ do {
1001
+ try mDb.deleteExportedRows()
1002
+ } catch DatabaseError.deleteExportedRows(let message) {
1003
+ throw CapacitorSQLiteError.failed(message: message)
1004
+ } catch let error {
1005
+ let msg: String = "\(error)"
1006
+ throw CapacitorSQLiteError.failed(message: msg)
1007
+ }
1008
+ } else {
1009
+ let msg = "Database \(mDbName) not opened"
1010
+ throw CapacitorSQLiteError.failed(message: msg)
1011
+ }
1012
+ } else {
1013
+ throw CapacitorSQLiteError.failed(message: initMessage)
1014
+ }
1015
+ }
1016
+
984
1017
  // MARK: - createSyncTable
985
1018
 
986
1019
  @objc func createSyncTable(_ dbName: String) throws -> NSNumber {
@@ -24,6 +24,7 @@ CAP_PLUGIN(CapacitorSQLitePlugin, "CapacitorSQLite",
24
24
  CAP_PLUGIN_METHOD(importFromJson, CAPPluginReturnPromise);
25
25
  CAP_PLUGIN_METHOD(isJsonValid, CAPPluginReturnPromise);
26
26
  CAP_PLUGIN_METHOD(exportToJson, CAPPluginReturnPromise);
27
+ CAP_PLUGIN_METHOD(deleteExportedRows, CAPPluginReturnPromise);
27
28
  CAP_PLUGIN_METHOD(createSyncTable, CAPPluginReturnPromise);
28
29
  CAP_PLUGIN_METHOD(setSyncDate, CAPPluginReturnPromise);
29
30
  CAP_PLUGIN_METHOD(getSyncDate, CAPPluginReturnPromise);
@@ -937,6 +937,31 @@ public class CapacitorSQLitePlugin: CAPPlugin {
937
937
 
938
938
  }
939
939
 
940
+ // MARK: - DeleteExportedRows
941
+
942
+ @objc func deleteExportedRows(_ call: CAPPluginCall) {
943
+ guard let dbName = call.options["database"]
944
+ as? String else {
945
+ let msg = "DeleteExportedRows: Must provide a database name"
946
+ retHandler.rResult(call: call, message: msg)
947
+ return
948
+ }
949
+ do {
950
+ try implementation?.deleteExportedRows(dbName)
951
+ retHandler.rResult(call: call)
952
+ return
953
+ } catch CapacitorSQLiteError.failed(let message) {
954
+ let msg = "exportToJson: \(message)"
955
+ retHandler.rResult(call: call, message: msg)
956
+ return
957
+ } catch let error {
958
+ let msg = "exportToJson: \(error)"
959
+ retHandler.rResult(call: call, message: msg)
960
+ return
961
+ }
962
+
963
+ }
964
+
940
965
  // MARK: - CreateSyncTable
941
966
 
942
967
  @objc func createSyncTable(_ call: CAPPluginCall) {
@@ -24,6 +24,7 @@ enum DatabaseError: Error {
24
24
  case exportToJson(message: String)
25
25
  case importFromJson(message: String)
26
26
  case getTableNames(message: String)
27
+ case deleteExportedRows(message: String)
27
28
  }
28
29
  // swiftlint:disable file_length
29
30
  // swiftlint:disable type_body_length
@@ -360,7 +361,8 @@ class Database {
360
361
  // Execute the query
361
362
  do {
362
363
  lastId = try UtilsSQLCipher
363
- .prepareSQL(mDB: self, sql: sql, values: values)
364
+ .prepareSQL(mDB: self, sql: sql, values: values,
365
+ fromJson: false)
364
366
  } catch UtilsSQLCipherError.prepareSQL(let message) {
365
367
  if transaction {
366
368
  do {
@@ -551,17 +553,35 @@ class Database {
551
553
  var retObj: [String: Any] = [:]
552
554
 
553
555
  do {
556
+ let date = Date()
557
+ let syncTime: Int = Int(date.timeIntervalSince1970)
558
+ // Set the last exported date
559
+ try ExportToJson.setLastExportDate(mDB: self, sTime: syncTime)
560
+ // Launch the export process
554
561
  let data: [String: Any] = [
555
562
  "dbName": dbName, "encrypted": self.encrypted,
556
563
  "expMode": expMode, "version": dbVersion]
557
564
  retObj = try ExportToJson
558
565
  .createExportObject(mDB: self, data: data)
566
+ } catch ExportToJsonError.setLastExportDate(let message) {
567
+ throw DatabaseError.exportToJson(message: message)
559
568
  } catch ExportToJsonError.createExportObject(let message) {
560
569
  throw DatabaseError.exportToJson(message: message)
561
570
  }
562
571
  return retObj
563
572
  }
564
573
 
574
+ // MARK: - DeleteExportedRows()
575
+
576
+ func deleteExportedRows() throws {
577
+
578
+ do {
579
+ try ExportToJson.delExportedRows(mDB: self)
580
+ } catch ExportToJsonError.delExportedRows(let message) {
581
+ throw DatabaseError.exportToJson(message: message)
582
+ }
583
+ }
584
+
565
585
  // MARK: - ImportFromJson
566
586
 
567
587
  func importFromJson(jsonSQLite: JsonSQLite)
@@ -570,6 +590,10 @@ class Database {
570
590
 
571
591
  // Create the Database Schema
572
592
  do {
593
+ // PRAGMA foreign_keys = OFF;
594
+ try UtilsSQLCipher
595
+ .setForeignKeyConstraintsEnabled(mDB: self,
596
+ toggle: false)
573
597
  if jsonSQLite.tables.count > 0 {
574
598
  changes = try ImportFromJson
575
599
  .createDatabaseSchema(mDB: self,
@@ -587,6 +611,10 @@ class Database {
587
611
  .createViews(mDB: self, views: mViews)
588
612
  }
589
613
  }
614
+ // PRAGMA foreign_keys = ON;
615
+ try UtilsSQLCipher
616
+ .setForeignKeyConstraintsEnabled(mDB: self,
617
+ toggle: true)
590
618
 
591
619
  return ["changes": changes]
592
620
  } catch ImportFromJsonError.createDatabaseSchema(let message) {
@@ -27,4 +27,12 @@ extension String {
27
27
  let stopIndex = self.index(self.startIndex, offsetBy: toIdx)
28
28
  return self[startIndex..<stopIndex]
29
29
  }
30
+ public func deletingPrefix(_ prefix: String) -> String {
31
+ guard self.hasPrefix(prefix) else { return self }
32
+ return String(self.dropFirst(prefix.count))
33
+ }
34
+ public func trimmingLeadingAndTrailingSpaces(using characterSet: CharacterSet = .whitespacesAndNewlines) -> String {
35
+ return trimmingCharacters(in: characterSet)
36
+ }
37
+
30
38
  }
@@ -27,6 +27,10 @@ enum ExportToJsonError: Error {
27
27
  case createRowValues(message: String)
28
28
  case modEmbeddedParentheses(message: String)
29
29
  case getViews(message: String)
30
+ case setLastExportDate(message: String)
31
+ case getLastExportDate(message: String)
32
+ case delExportedRows(message: String)
33
+
30
34
  }
31
35
  var REALAFFINITY: [String] = ["REAL", "DOUBLE", "DOUBLE PRECISION", "FLOAT"]
32
36
  var INTEGERAFFINITY: [String] = ["INTEGER", "INT", "TINYINT", "SMALLINT",
@@ -47,6 +51,124 @@ class ExportToJson {
47
51
  userInfo: vId)
48
52
  }
49
53
 
54
+ // MARK: - ExportToJson - GetLastExportDate
55
+
56
+ class func getLastExportDate(mDB: Database) throws -> Int64 {
57
+ var ret: Int64 = -1
58
+ let query: String = "SELECT sync_date FROM sync_table WHERE id = 2;"
59
+ do {
60
+ let isExists: Bool = try UtilsJson.isTableExists(
61
+ mDB: mDB, tableName: "sync_table")
62
+ if isExists {
63
+ var resSyncDate = try UtilsSQLCipher.querySQL(
64
+ mDB: mDB, sql: query, values: [])
65
+ if resSyncDate.count > 1 {
66
+ resSyncDate.removeFirst()
67
+ guard let res: Int64 = resSyncDate[0]["sync_date"] as?
68
+ Int64 else {
69
+ throw ExportToJsonError.getLastExportDate(
70
+ message: "Error get sync date failed")
71
+ }
72
+ if res > 0 {ret = res}
73
+ }
74
+ } else {
75
+ let msg = "No sync_table available"
76
+ throw ExportToJsonError.getLastExportDate(message: msg)
77
+ }
78
+ } catch UtilsJsonError.tableNotExists(let message) {
79
+ throw ExportToJsonError.getLastExportDate(message: message)
80
+ } catch UtilsSQLCipherError.querySQL(let message) {
81
+ throw ExportToJsonError.getLastExportDate(
82
+ message: "Error get last export date failed : \(message)")
83
+ }
84
+ return ret
85
+
86
+ }
87
+
88
+ // MARK: - ExportToJson - SetLastExportDate
89
+
90
+ class func setLastExportDate(mDB: Database, sTime: Int) throws {
91
+ do {
92
+ let isExists: Bool = try UtilsJson.isTableExists(
93
+ mDB: mDB, tableName: "sync_table")
94
+ if !isExists {
95
+ let msg = "No sync_table available"
96
+ throw ExportToJsonError.setLastExportDate(message: msg)
97
+ }
98
+ var stmt: String = ""
99
+ let res = try getLastExportDate(mDB: mDB)
100
+ if res > 0 {
101
+ stmt = "UPDATE sync_table SET sync_date = \(sTime) " +
102
+ "WHERE id = 2;"
103
+ } else {
104
+ stmt = "INSERT INTO sync_table (sync_date) VALUES (\(sTime));"
105
+ }
106
+ let lastId: Int64 = try UtilsSQLCipher.prepareSQL(
107
+ mDB: mDB, sql: stmt, values: [], fromJson: false)
108
+ if lastId < 0 {
109
+ throw ExportToJsonError.setLastExportDate(
110
+ message: "lastId < 0")
111
+ }
112
+ return
113
+ } catch UtilsSQLCipherError.prepareSQL(let message) { throw ExportToJsonError.setLastExportDate(message: message)
114
+ } catch UtilsJsonError.tableNotExists(let message) {
115
+ throw ExportToJsonError.setLastExportDate(message: message)
116
+ } catch ExportToJsonError.getLastExportDate(let message) {
117
+ throw ExportToJsonError.setLastExportDate(message: message)
118
+ }
119
+
120
+ }
121
+
122
+ // MARK: - ExportToJson - DelExportedRows
123
+
124
+ class func delExportedRows(mDB: Database) throws {
125
+ do {
126
+ // check if synchronization table exists
127
+ let isExists: Bool = try UtilsJson.isTableExists(
128
+ mDB: mDB, tableName: "sync_table")
129
+ if !isExists {
130
+ let msg = "No sync_table available"
131
+ throw ExportToJsonError.delExportedRows(message: msg)
132
+ }
133
+ // get the last export date
134
+ let lastExportDate = try getLastExportDate(mDB: mDB)
135
+ if lastExportDate < 0 {
136
+ let msg = "No last exported date available"
137
+ throw ExportToJsonError.delExportedRows(message: msg)
138
+ }
139
+
140
+ // get the table' name list
141
+ let tableList: [String] = try mDB.getTableNames()
142
+ if tableList.count == 0 {
143
+ let msg = "No table's names returned"
144
+ throw ExportToJsonError.delExportedRows(message: msg)
145
+ }
146
+ // Loop through the tables
147
+ for table in tableList {
148
+ var lastId: Int64 = -1
149
+ // define the delete statement
150
+ let delStmt = "DELETE FROM \(table) WHERE sql_deleted = 1 " +
151
+ "AND last_modified < \(lastExportDate);"
152
+ lastId = try UtilsSQLCipher.prepareSQL(mDB: mDB, sql: delStmt,
153
+ values: [],
154
+ fromJson: true)
155
+ if lastId < 0 {
156
+ let msg = "DelExportedRows: lastId < 0"
157
+ throw ExportToJsonError.delExportedRows(message: msg)
158
+ }
159
+ }
160
+ return
161
+ } catch UtilsJsonError.tableNotExists(let message) {
162
+ throw ExportToJsonError.delExportedRows(message: message)
163
+ } catch ExportToJsonError.getLastExportDate(let message) {
164
+ throw ExportToJsonError.delExportedRows(message: message)
165
+ } catch DatabaseError.getTableNames(let message) {
166
+ throw ExportToJsonError.delExportedRows(message: message)
167
+ } catch UtilsSQLCipherError.prepareSQL(let message) {
168
+ throw ExportToJsonError.delExportedRows(message: message)
169
+ }
170
+ }
171
+
50
172
  // MARK: - ExportToJson - CreateExportObject
51
173
 
52
174
  // swiftlint:disable function_body_length
@@ -434,17 +556,17 @@ class ExportToJson {
434
556
  table: table)
435
557
  guard let isSch: Bool = result["isSchema"] as? Bool
436
558
  else {
437
- throw ExportToJsonError.getTablesFull(
559
+ throw ExportToJsonError.getTablesPartial(
438
560
  message: "Error did not find isSchema")
439
561
  }
440
562
  guard let isIdxes: Bool = result["isIndexes"] as?
441
563
  Bool else {
442
- throw ExportToJsonError.getTablesFull(
564
+ throw ExportToJsonError.getTablesPartial(
443
565
  message: "Error did not find isIndexes")
444
566
  }
445
567
  guard let retTable: [String: Any] = result["table"]
446
568
  as? [String: Any] else {
447
- throw ExportToJsonError.getTablesFull(
569
+ throw ExportToJsonError.getTablesPartial(
448
570
  message: "Error did not find table")
449
571
  }
450
572
  isSchema = isSch
@@ -459,18 +581,18 @@ class ExportToJson {
459
581
  let query: String = modTables[tableName] == "Create"
460
582
  ? "SELECT * FROM \(tableName);"
461
583
  : "SELECT * FROM \(tableName) WHERE last_modified" +
462
- " > \(syncDate);"
584
+ " >= \(syncDate);"
463
585
  result = try ExportToJson
464
586
  .getValues(mDB: mDB, stmt: query,
465
587
  table: table)
466
588
  guard let isValues: Bool = result["isValues"] as? Bool
467
589
  else {
468
- throw ExportToJsonError.getTablesFull(
590
+ throw ExportToJsonError.getTablesPartial(
469
591
  message: "Error did not find isValues")
470
592
  }
471
593
  guard let retTable1: [String: Any] = result["table"]
472
594
  as? [String: Any] else {
473
- throw ExportToJsonError.getTablesFull(
595
+ throw ExportToJsonError.getTablesPartial(
474
596
  message: "Error did not find table")
475
597
  }
476
598
  table = retTable1
@@ -478,15 +600,13 @@ class ExportToJson {
478
600
  var tableKeys: [String] = []
479
601
  tableKeys.append(contentsOf: table.keys)
480
602
 
481
- if tableKeys.count <= 1 ||
482
- (!isSchema && !isIndexes && !isValues) {
483
- throw ExportToJsonError.getTablesPartial(
484
- message: "Error table \(tableName) is not a jsonTable")
603
+ if tableKeys.count >= 1 &&
604
+ (isSchema || isIndexes || isValues) {
605
+ tables.append(table)
606
+ msg = "Partial: Table \(tableName) data export completed " +
607
+ "\(iTable)/\(resTables.count) ..."
608
+ notifyExportProgressEvent(msg: msg)
485
609
  }
486
- tables.append(table)
487
- msg = "Partial: Table \(tableName) data export completed " +
488
- "\(iTable)/\(resTables.count) ..."
489
- notifyExportProgressEvent(msg: msg)
490
610
 
491
611
  }
492
612
  } catch ExportToJsonError.getSchemaIndexes(let message) {
@@ -535,19 +655,28 @@ class ExportToJson {
535
655
 
536
656
  class func getSyncDate(mDB: Database) throws -> Int64 {
537
657
  var ret: Int64 = -1
538
- let query: String = "SELECT sync_date FROM sync_table;"
658
+ let query: String = "SELECT sync_date FROM sync_table WHERE id = 1;"
539
659
  do {
540
- var resSyncDate = try UtilsSQLCipher.querySQL(
541
- mDB: mDB, sql: query, values: [])
542
- if resSyncDate.count > 1 {
543
- resSyncDate.removeFirst()
544
- guard let res: Int64 = resSyncDate[0]["sync_date"] as?
545
- Int64 else {
546
- throw ExportToJsonError.getSyncDate(
547
- message: "Error get sync date failed")
660
+ let isExists: Bool = try UtilsJson.isTableExists(
661
+ mDB: mDB, tableName: "sync_table")
662
+ if isExists {
663
+ var resSyncDate = try UtilsSQLCipher.querySQL(
664
+ mDB: mDB, sql: query, values: [])
665
+ if resSyncDate.count > 1 {
666
+ resSyncDate.removeFirst()
667
+ guard let res: Int64 = resSyncDate[0]["sync_date"] as?
668
+ Int64 else {
669
+ throw ExportToJsonError.getSyncDate(
670
+ message: "Error get sync date failed")
671
+ }
672
+ if res > 0 {ret = res}
548
673
  }
549
- if res > 0 {ret = res}
674
+ } else {
675
+ throw ExportToJsonError.getSyncDate(
676
+ message: "Error no sync_table available")
550
677
  }
678
+ } catch UtilsJsonError.tableNotExists(let message) {
679
+ throw ExportToJsonError.getSyncDate(message: message)
551
680
  } catch UtilsSQLCipherError.querySQL(let message) {
552
681
  throw ExportToJsonError.getSyncDate(
553
682
  message: "Error get sync date failed : \(message)")
@@ -595,7 +724,7 @@ class ExportToJson {
595
724
  .getTablesModified(message: msg)
596
725
  }
597
726
  query = "SELECT count(*) AS count FROM \(tableName) "
598
- query.append("WHERE last_modified > ")
727
+ query.append("WHERE last_modified >= ")
599
728
  query.append("\(syncDate);")
600
729
  resQuery = try UtilsSQLCipher.querySQL(
601
730
  mDB: mDB, sql: query, values: [])
@@ -46,15 +46,9 @@ class ImportFromJson {
46
46
  try UtilsSQLCipher.setVersion(mDB: mDB,
47
47
  version: version)
48
48
  if jsonSQLite.mode == "full" {
49
- try UtilsSQLCipher
50
- .setForeignKeyConstraintsEnabled(mDB: mDB,
51
- toggle: false)
52
49
  // Drop All Tables, Indexes and Triggers
53
50
  try _ = UtilsDrop.dropAll(mDB: mDB)
54
51
  }
55
- try UtilsSQLCipher
56
- .setForeignKeyConstraintsEnabled(mDB: mDB,
57
- toggle: true)
58
52
  // create database schema
59
53
  changes = try ImportFromJson
60
54
  .createSchema(mDB: mDB,
@@ -429,7 +423,7 @@ class ImportFromJson {
429
423
  .createRowStatement(mDB: mDB, data: data,
430
424
  row: row,
431
425
  jsonNamesTypes: jsonNamesTypes)
432
- let rowValues = UtilsJson.getValuesFromRow(
426
+ var rowValues = UtilsJson.getValuesFromRow(
433
427
  rowValues: row)
434
428
  isRun = try UtilsJson.checkUpdate(mDB: mDB, stmt: stmt,
435
429
  values: rowValues,
@@ -438,8 +432,11 @@ class ImportFromJson {
438
432
  types: jsonNamesTypes.types)
439
433
 
440
434
  if isRun {
435
+ if stmt.prefix(6) == "DELETE" {
436
+ rowValues = []
437
+ }
441
438
  let lastId: Int64 = try UtilsSQLCipher.prepareSQL(
442
- mDB: mDB, sql: stmt, values: rowValues)
439
+ mDB: mDB, sql: stmt, values: rowValues, fromJson: true)
443
440
  if lastId < 0 {
444
441
  throw ImportFromJsonError.createTableData(
445
442
  message: "lastId < 0")
@@ -460,6 +457,7 @@ class ImportFromJson {
460
457
  // MARK: - ImportFromJson - createRowStatement
461
458
 
462
459
  // swiftlint:disable function_body_length
460
+ // swiftlint:disable cyclomatic_complexity
463
461
  class func createRowStatement(
464
462
  mDB: Database,
465
463
  data: [String: Any],
@@ -508,31 +506,59 @@ class ImportFromJson {
508
506
  stmt = "INSERT INTO \(tableName) (\(nameString)) VALUES "
509
507
  stmt.append("(\(questionMarkString));")
510
508
  } else {
511
- // Update
512
- let setString: String = UtilsJson.setNameForUpdate(
513
- names: jsonNamesTypes.names)
514
- if setString.count == 0 {
515
- var message: String = "importFromJson: Table "
516
- message.append("\(tableName) values row ")
517
- message.append("\(pos) not set to String")
518
- throw ImportFromJsonError.createRowStatement(
519
- message: message)
509
+ var isUpdate: Bool = true
510
+ let idxDelete: Int = jsonNamesTypes
511
+ .names.firstIndex(where: {$0 == "sql_deleted"}) ?? -1
512
+ if idxDelete >= 0 {
513
+ if let delValue = row[idxDelete].value as? Int {
514
+ if delValue == 1 {
515
+ isUpdate = false
516
+ stmt = "DELETE FROM \(tableName) WHERE "
517
+ if let rwValue = row[0].value as? String {
518
+ stmt += "\(jsonNamesTypes.names[0]) = '\(rwValue)';"
519
+ } else if let rwValue = row[0].value as? Int {
520
+ stmt += "\(jsonNamesTypes.names[0]) = \(rwValue);"
521
+ } else {
522
+ var msg: String = "importFromJson: Table "
523
+ msg.append("\(tableName) values row[0]does not exist")
524
+ throw ImportFromJsonError.createRowStatement(
525
+ message: message)
526
+ }
527
+ }
528
+ }
520
529
  }
530
+ if isUpdate {
531
+ // Update
532
+ let setString: String = UtilsJson.setNameForUpdate(
533
+ names: jsonNamesTypes.names)
534
+ if setString.count == 0 {
535
+ var message: String = "importFromJson: Table "
536
+ message.append("\(tableName) values row ")
537
+ message.append("\(pos) not set to String")
538
+ throw ImportFromJsonError.createRowStatement(
539
+ message: message)
540
+ }
521
541
 
522
- stmt = "UPDATE \(tableName) SET \(setString) WHERE "
523
- if let rwValue = row[0].value as? String {
524
- stmt += "\(jsonNamesTypes.names[0]) = '\(rwValue)';"
525
- } else if let rwValue = row[0].value as? Int {
526
- stmt += "\(jsonNamesTypes.names[0]) = \(rwValue);"
527
- } else {
528
- var msg: String = "importFromJson: Table "
529
- msg.append("\(tableName) values row[0]does not exist")
530
- throw ImportFromJsonError.createRowStatement(
531
- message: message)
542
+ stmt = "UPDATE \(tableName) SET \(setString) WHERE "
543
+ if let rwValue = row[0].value as? String {
544
+ stmt += "\(jsonNamesTypes.names[0]) = '\(rwValue)';"
545
+ } else if let rwValue = row[0].value as? Int {
546
+ stmt += "\(jsonNamesTypes.names[0]) = \(rwValue);"
547
+ } else {
548
+ var msg: String = "importFromJson: Table "
549
+ msg.append("\(tableName) values row[0]does not exist")
550
+ throw ImportFromJsonError.createRowStatement(
551
+ message: message)
552
+ }
532
553
  }
533
554
  }
534
555
  return stmt
535
556
  }
557
+ // swiftlint:enable cyclomatic_complexity
558
+ // swiftlint:enable function_body_length
559
+
560
+ // MARK: - ImportFromJson - createViews
561
+
536
562
  // swiftlint:disable function_body_length
537
563
  class func createViews(mDB: Database, views: [JsonView]) throws -> Int {
538
564
  var changes: Int = 0
@@ -232,8 +232,8 @@ class UtilsDrop {
232
232
  changes += retChanges
233
233
  retChanges = try self.dropViews(mDB: mDB)
234
234
  if changes >= 0 {
235
- _ = try UtilsSQLCipher.prepareSQL(mDB: mDB,
236
- sql: "VACUUM;", values: [])
235
+ _ = try UtilsSQLCipher.prepareSQL(mDB: mDB, sql: "VACUUM;",
236
+ values: [], fromJson: false)
237
237
  changes = UtilsSQLCipher.dbChanges(mDB: mDB.mDb) -
238
238
  initChanges
239
239
  }