@capacitor-community/sqlite 3.4.1-1 → 3.4.1-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.
@@ -453,18 +453,27 @@ class Database {
453
453
  let isExists: Bool = try UtilsJson.isTableExists(
454
454
  mDB: self, tableName: "sync_table")
455
455
  if !isExists {
456
- let date = Date()
457
- let syncTime: Int = Int(date.timeIntervalSince1970)
458
- var stmt: String = "CREATE TABLE IF NOT EXISTS "
459
- stmt.append("sync_table (")
460
- stmt.append("id INTEGER PRIMARY KEY NOT NULL,")
461
- stmt.append("sync_date INTEGER);")
462
- stmt.append("INSERT INTO sync_table (sync_date) ")
463
- stmt.append("VALUES ('\(syncTime)');")
464
- retObj = try executeSQL(sql: stmt)
456
+ // check if there are tables with last_modified column
457
+ let isLastModified: Bool = try UtilsJson.isLastModified(mDB: self)
458
+ if isLastModified {
459
+ let date = Date()
460
+ let syncTime: Int = Int(date.timeIntervalSince1970)
461
+ var stmt: String = "CREATE TABLE IF NOT EXISTS "
462
+ stmt.append("sync_table (")
463
+ stmt.append("id INTEGER PRIMARY KEY NOT NULL,")
464
+ stmt.append("sync_date INTEGER);")
465
+ stmt.append("INSERT INTO sync_table (sync_date) ")
466
+ stmt.append("VALUES ('\(syncTime)');")
467
+ retObj = try executeSQL(sql: stmt)
468
+ } else {
469
+ let msg = "No last_modified column in tables"
470
+ throw DatabaseError.createSyncTable(message: msg)
471
+ }
465
472
  } else {
466
473
  retObj = 0
467
474
  }
475
+ } catch UtilsJsonError.isLastModified(let message) {
476
+ throw DatabaseError.createSyncTable(message: message)
468
477
  } catch UtilsJsonError.tableNotExists(let message) {
469
478
  throw DatabaseError.createSyncTable(message: message)
470
479
  } catch DatabaseError.executeSQL(let message) {
@@ -478,6 +487,11 @@ class Database {
478
487
  func setSyncDate(syncDate: String ) throws -> Bool {
479
488
  var retBool: Bool = false
480
489
  do {
490
+ let isExists: Bool = try UtilsJson.isTableExists(
491
+ mDB: self, tableName: "sync_table")
492
+ if !isExists {
493
+ throw DatabaseError.createSyncDate(message: "No sync_table available")
494
+ }
481
495
  let dateFormatter = DateFormatter()
482
496
  dateFormatter.locale = Locale(identifier: "en_US_POSIX")
483
497
  dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
@@ -494,6 +508,8 @@ class Database {
494
508
  } else {
495
509
  throw DatabaseError.createSyncDate(message: "wrong syncDate")
496
510
  }
511
+ } catch UtilsJsonError.tableNotExists(let message) {
512
+ throw DatabaseError.createSyncDate(message: message)
497
513
  } catch DatabaseError.runSQL(let message) {
498
514
  throw DatabaseError.createSyncDate(message: message)
499
515
  }
@@ -505,7 +521,14 @@ class Database {
505
521
  func getSyncDate( ) throws -> Int64 {
506
522
  var syncDate: Int64 = 0
507
523
  do {
524
+ let isExists: Bool = try UtilsJson.isTableExists(
525
+ mDB: self, tableName: "sync_table")
526
+ if !isExists {
527
+ throw DatabaseError.getSyncDate(message: "No sync_table available")
528
+ }
508
529
  syncDate = try ExportToJson.getSyncDate(mDB: self)
530
+ } catch UtilsJsonError.tableNotExists(let message) {
531
+ throw DatabaseError.getSyncDate(message: message)
509
532
  } catch ExportToJsonError.getSyncDate(let message) {
510
533
  throw DatabaseError.getSyncDate(message: message)
511
534
  }
@@ -88,6 +88,12 @@ class ExportToJson {
88
88
  let resTables = try UtilsSQLCipher.querySQL(
89
89
  mDB: mDB, sql: query, values: [])
90
90
  if resTables.count > 0 {
91
+ let isExists: Bool = try UtilsJson.isTableExists(
92
+ mDB: mDB, tableName: "sync_table")
93
+ if !isExists && expMode == "partial" {
94
+ throw ExportToJsonError.createExportObject(message: "No sync_table available")
95
+ }
96
+
91
97
  switch expMode {
92
98
  case "partial" :
93
99
  tables = try ExportToJson
@@ -102,6 +108,8 @@ class ExportToJson {
102
108
  message: "expMode \(expMode) not defined")
103
109
  }
104
110
  }
111
+ } catch UtilsJsonError.tableNotExists(let message) {
112
+ throw ExportToJsonError.createExportObject(message: message)
105
113
  } catch UtilsSQLCipherError.querySQL(let message) {
106
114
  throw ExportToJsonError.createExportObject(
107
115
  message: "Error get table's names failed : \(message)")
@@ -194,12 +194,16 @@ class ImportFromJson {
194
194
  -> [String] {
195
195
  var statements: [String] = []
196
196
  var stmt: String
197
+ var isLastModified: Bool = false
197
198
  stmt = "CREATE TABLE IF NOT EXISTS "
198
199
  stmt.append(tableName)
199
200
  stmt.append(" (")
200
201
  for jpos in 0..<mSchema.count {
201
202
  if let jSchColumn = mSchema[jpos].column {
202
203
  if jSchColumn.count > 0 {
204
+ if jSchColumn == "last_modified" {
205
+ isLastModified = true
206
+ }
203
207
  stmt.append(jSchColumn)
204
208
  }
205
209
  }
@@ -221,20 +225,22 @@ class ImportFromJson {
221
225
  }
222
226
  stmt.append(");")
223
227
  statements.append(stmt)
224
- // create trigger last_modified associated with the table
225
- let triggerName: String = tableName + "_trigger_last_modified"
226
- stmt = "CREATE TRIGGER IF NOT EXISTS "
227
- stmt.append(triggerName)
228
- stmt.append(" AFTER UPDATE ON ")
229
- stmt.append(tableName)
230
- stmt.append(" FOR EACH ROW ")
231
- stmt.append("WHEN NEW.last_modified <= OLD.last_modified ")
232
- stmt.append("BEGIN UPDATE ")
233
- stmt.append(tableName)
234
- stmt.append(" SET last_modified = (strftime('%s','now')) ")
235
- stmt.append("WHERE id=OLD.id; ")
236
- stmt.append("END;")
237
- statements.append(stmt)
228
+ if isLastModified {
229
+ // create trigger last_modified associated with the table
230
+ let triggerName: String = tableName + "_trigger_last_modified"
231
+ stmt = "CREATE TRIGGER IF NOT EXISTS "
232
+ stmt.append(triggerName)
233
+ stmt.append(" AFTER UPDATE ON ")
234
+ stmt.append(tableName)
235
+ stmt.append(" FOR EACH ROW ")
236
+ stmt.append("WHEN NEW.last_modified <= OLD.last_modified ")
237
+ stmt.append("BEGIN UPDATE ")
238
+ stmt.append(tableName)
239
+ stmt.append(" SET last_modified = (strftime('%s','now')) ")
240
+ stmt.append("WHERE id=OLD.id; ")
241
+ stmt.append("END;")
242
+ statements.append(stmt)
243
+ }
238
244
  return statements
239
245
  }
240
246
 
@@ -17,6 +17,7 @@ enum UtilsJsonError: Error {
17
17
  case validateIndexes(message: String)
18
18
  case validateTriggers(message: String)
19
19
  case validateViews(message: String)
20
+ case isLastModified(message: String)
20
21
  }
21
22
 
22
23
  // swiftlint:disable type_body_length
@@ -46,6 +47,33 @@ class UtilsJson {
46
47
  return ret
47
48
  }
48
49
 
50
+ // MARK: - ImportFromJson - IsLastModified
51
+
52
+ class func isLastModified(mDB: Database) throws -> Bool {
53
+ var msg: String = "Error LastModified: "
54
+ if !mDB.isDBOpen() {
55
+ msg.append("Database not opened")
56
+ throw UtilsJsonError.isLastModified(message: msg)
57
+ }
58
+ var ret: Bool = false
59
+ do {
60
+ let tableList: [String] = try UtilsDrop.getTablesNames(mDB: mDB)
61
+ for table in tableList {
62
+ let namesTypes: JsonNamesTypes = try getTableColumnNamesTypes(mDB: mDB,
63
+ tableName: table)
64
+ if namesTypes.names.contains("last_modified") {
65
+ ret = true
66
+ break
67
+ }
68
+ }
69
+ } catch UtilsJsonError.getTableColumnNamesTypes(let message) {
70
+ throw UtilsJsonError.isLastModified(message: message)
71
+ } catch UtilsDropError.getTablesNamesFailed(let message) {
72
+ throw UtilsJsonError.isLastModified(message: message)
73
+ }
74
+ return ret
75
+ }
76
+
49
77
  // MARK: - ImportFromJson - IsViewExists
50
78
 
51
79
  class func isViewExists(mDB: Database, viewName: String)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capacitor-community/sqlite",
3
- "version": "3.4.1-1",
3
+ "version": "3.4.1-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",
@@ -91,5 +91,8 @@
91
91
  "electron": {
92
92
  "src": "electron"
93
93
  }
94
+ },
95
+ "dependencies": {
96
+ "jeep-sqlite": "^1.3.7"
94
97
  }
95
98
  }