@capacitor-community/sqlite 5.0.5-1 → 5.0.5

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 (35) hide show
  1. package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLite.java +109 -156
  2. package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLitePlugin.java +81 -249
  3. package/android/src/main/java/com/getcapacitor/community/database/sqlite/NotificationCenter.java +1 -1
  4. package/android/src/main/java/com/getcapacitor/community/database/sqlite/RetHandler.java +0 -12
  5. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java +175 -40
  6. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ExportToJson.java +141 -135
  7. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ImportFromJson.java +2 -1
  8. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsBiometric.java +0 -4
  9. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsFile.java +30 -18
  10. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsMigrate.java +12 -4
  11. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsNCDatabase.java +4 -1
  12. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsSQLCipher.java +8 -6
  13. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsSQLite.java +14 -28
  14. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsSecret.java +0 -1
  15. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsUpgrade.java +0 -1
  16. package/dist/esm/definitions.d.ts +25 -4
  17. package/dist/esm/definitions.js +42 -19
  18. package/dist/esm/definitions.js.map +1 -1
  19. package/dist/plugin.cjs.js +42 -19
  20. package/dist/plugin.cjs.js.map +1 -1
  21. package/dist/plugin.js +42 -19
  22. package/dist/plugin.js.map +1 -1
  23. package/electron/dist/plugin.js +197 -125
  24. package/electron/dist/plugin.js.map +1 -1
  25. package/ios/Plugin/CapacitorSQLite.swift +8 -4
  26. package/ios/Plugin/CapacitorSQLitePlugin.swift +6 -3
  27. package/ios/Plugin/Database.swift +28 -14
  28. package/ios/Plugin/ImportExportJson/ExportToJson.swift +10 -5
  29. package/ios/Plugin/ImportExportJson/ImportFromJson.swift +4 -2
  30. package/ios/Plugin/Utils/UtilsDownloadFromHTTP.swift +61 -61
  31. package/ios/Plugin/Utils/UtilsDrop.swift +2 -1
  32. package/ios/Plugin/Utils/UtilsSQLCipher.swift +255 -23
  33. package/ios/Plugin/Utils/UtilsUpgrade.swift +0 -1
  34. package/package.json +2 -2
  35. package/src/definitions.ts +67 -18
@@ -735,7 +735,7 @@ enum CapacitorSQLiteError: Error {
735
735
  // MARK: - ExecuteSet
736
736
 
737
737
  @objc func executeSet(_ dbName: String, set: [[String: Any]],
738
- transaction: Bool, readonly: Bool)
738
+ transaction: Bool, readonly: Bool, returnMode: String)
739
739
  throws -> [String: Any] {
740
740
  if isInit {
741
741
  let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
@@ -750,7 +750,8 @@ enum CapacitorSQLiteError: Error {
750
750
  }
751
751
  if !mDb.isNCDB() && mDb.isDBOpen() {
752
752
  do {
753
- let res = try mDb.execSet(set: set, transaction: transaction)
753
+ let res = try mDb.execSet(set: set, transaction: transaction,
754
+ returnMode: returnMode)
754
755
  return res
755
756
  } catch DatabaseError.execSet(let message) {
756
757
  throw CapacitorSQLiteError.failed(message: message)
@@ -771,8 +772,9 @@ enum CapacitorSQLiteError: Error {
771
772
 
772
773
  // swiftlint:disable function_body_length
773
774
  // swiftlint:disable cyclomatic_complexity
775
+ // swiftlint:disable function_parameter_count
774
776
  @objc func run(_ dbName: String, statement: String, values: [Any],
775
- transaction: Bool, readonly: Bool)
777
+ transaction: Bool, readonly: Bool, returnMode: String)
776
778
  throws -> [String: Any] {
777
779
  if isInit {
778
780
  let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
@@ -834,7 +836,8 @@ enum CapacitorSQLiteError: Error {
834
836
  }
835
837
  }
836
838
  let res = try mDb.runSQL(sql: statement, values: val,
837
- transaction: transaction)
839
+ transaction: transaction,
840
+ returnMode: returnMode)
838
841
  return res
839
842
  } catch DatabaseError.runSQL(let message) {
840
843
  throw CapacitorSQLiteError.failed(message: message)
@@ -850,6 +853,7 @@ enum CapacitorSQLiteError: Error {
850
853
  throw CapacitorSQLiteError.failed(message: initMessage)
851
854
  }
852
855
  }
856
+ // swiftlint:enable function_parameter_count
853
857
  // swiftlint:enable cyclomatic_complexity
854
858
  // swiftlint:enable function_body_length
855
859
 
@@ -767,10 +767,12 @@ public class CapacitorSQLitePlugin: CAPPlugin {
767
767
  }
768
768
  let transaction: Bool = call.getBool("transaction") ?? true
769
769
  let readOnly: Bool = call.getBool("readonly") ?? false
770
+ let returnMode: String = call.getString("returnMode") ?? "no"
770
771
  do {
771
772
  if let res = try implementation?.executeSet(dbName, set: set,
772
773
  transaction: transaction,
773
- readonly: readOnly) {
774
+ readonly: readOnly,
775
+ returnMode: returnMode) {
774
776
  retHandler.rChanges(call: call, ret: res)
775
777
  return
776
778
  } else {
@@ -823,13 +825,15 @@ public class CapacitorSQLitePlugin: CAPPlugin {
823
825
  }
824
826
  let transaction: Bool = call.getBool("transaction") ?? true
825
827
  let readOnly: Bool = call.getBool("readonly") ?? false
828
+ let returnMode: String = call.getString("returnMode") ?? "no"
826
829
  do {
827
830
  if let res = try
828
831
  implementation?.run(dbName,
829
832
  statement: statement,
830
833
  values: values,
831
834
  transaction: transaction,
832
- readonly: readOnly) {
835
+ readonly: readOnly,
836
+ returnMode: returnMode) {
833
837
  retHandler.rChanges(call: call, ret: res)
834
838
  return
835
839
  } else {
@@ -1414,7 +1418,6 @@ public class CapacitorSQLitePlugin: CAPPlugin {
1414
1418
  return
1415
1419
  }
1416
1420
  DispatchQueue.global(qos: .background).async {
1417
-
1418
1421
  do {
1419
1422
  try self.implementation?.getFromHTTPRequest(call, url: url)
1420
1423
  DispatchQueue.main.async {
@@ -297,13 +297,17 @@ class Database {
297
297
 
298
298
  // MARK: - ExecSet
299
299
 
300
- func execSet(set: [[String: Any]], transaction: Bool = true) throws -> [String: Int64] {
300
+ // swiftlint:disable function_body_length
301
+ func execSet(set: [[String: Any]], transaction: Bool = true,
302
+ returnMode: String = "no") throws -> [String: Any] {
301
303
  var msg: String = "Failed in execSet : "
302
304
  let initChanges = UtilsSQLCipher.dbChanges(mDB: mDb)
303
305
  var changes: Int = -1
304
306
  var lastId: Int64 = -1
305
- var changesDict: [String: Int64] = ["lastId": lastId,
306
- "changes": Int64(changes)]
307
+ var response: [[String: Any]] = []
308
+ var changesDict: [String: Any] = ["lastId": lastId,
309
+ "changes": changes,
310
+ "values": [[:]]]
307
311
 
308
312
  // Start a transaction
309
313
  if transaction {
@@ -316,12 +320,15 @@ class Database {
316
320
  }
317
321
  // Execute the query
318
322
  do {
319
- lastId = try UtilsSQLCipher
320
- .executeSet(mDB: self, set: set)
323
+ let resp = try UtilsSQLCipher
324
+ .executeSet(mDB: self, set: set, returnMode: returnMode)
325
+ lastId = resp.0
326
+ response = resp.1
321
327
  changes = UtilsSQLCipher
322
328
  .dbChanges(mDB: mDb) - initChanges
323
- changesDict["changes"] = Int64(changes)
329
+ changesDict["changes"] = changes
324
330
  changesDict["lastId"] = lastId
331
+ changesDict["values"] = response
325
332
 
326
333
  } catch UtilsSQLCipherError
327
334
  .executeSet(let message) {
@@ -352,12 +359,13 @@ class Database {
352
359
 
353
360
  // MARK: - RunSQL
354
361
 
355
- func runSQL(sql: String, values: [Any], transaction: Bool = true) throws -> [String: Int64] {
362
+ func runSQL(sql: String, values: [Any], transaction: Bool = true,
363
+ returnMode: String = "no") throws -> [String: Any] {
356
364
  var msg: String = "Failed in runSQL : "
357
365
  var changes: Int = -1
358
366
  var lastId: Int64 = -1
367
+ var response: [[String: Any]] = []
359
368
  let initChanges = UtilsSQLCipher.dbChanges(mDB: mDb)
360
-
361
369
  // Start a transaction
362
370
  if transaction {
363
371
  do {
@@ -370,9 +378,11 @@ class Database {
370
378
  }
371
379
  // Execute the query
372
380
  do {
373
- lastId = try UtilsSQLCipher
381
+ let resp = try UtilsSQLCipher
374
382
  .prepareSQL(mDB: self, sql: sql, values: values,
375
- fromJson: false)
383
+ fromJson: false, returnMode: returnMode)
384
+ lastId = resp.0
385
+ response = resp.1
376
386
  } catch UtilsSQLCipherError.prepareSQL(let message) {
377
387
  if transaction {
378
388
  do {
@@ -400,8 +410,9 @@ class Database {
400
410
  }
401
411
  }
402
412
  changes = UtilsSQLCipher.dbChanges(mDB: mDb) - initChanges
403
- let result: [String: Int64] = ["changes": Int64(changes),
404
- "lastId": lastId]
413
+ let result: [String: Any] = ["changes": changes,
414
+ "lastId": lastId,
415
+ "values": response]
405
416
  return result
406
417
  }
407
418
 
@@ -511,6 +522,8 @@ class Database {
511
522
 
512
523
  func setSyncDate(syncDate: String ) throws -> Bool {
513
524
  var retBool: Bool = false
525
+ var lastId: Int64 = -1
526
+ var resp: [String: Any] = [:]
514
527
  do {
515
528
  let isExists: Bool = try UtilsJson.isTableExists(
516
529
  mDB: self, tableName: "sync_table")
@@ -524,8 +537,9 @@ class Database {
524
537
  let syncTime: Int = Int(date.timeIntervalSince1970)
525
538
  var stmt: String = "UPDATE sync_table SET sync_date = "
526
539
  stmt.append("\(syncTime) WHERE id = 1;")
527
- let retRun = try runSQL(sql: stmt, values: [])
528
- if let lastId: Int64 = retRun["lastId"] {
540
+ resp = try runSQL(sql: stmt, values: [])
541
+ if let mLastId: Int64 = resp["lastId"] as? Int64 {
542
+ lastId = mLastId
529
543
  if lastId != -1 {
530
544
  retBool = true
531
545
  }
@@ -88,6 +88,7 @@ class ExportToJson {
88
88
  // MARK: - ExportToJson - SetLastExportDate
89
89
 
90
90
  class func setLastExportDate(mDB: Database, sTime: Int) throws {
91
+ var lastId: Int64 = -1
91
92
  do {
92
93
  let isExists: Bool = try UtilsJson.isTableExists(
93
94
  mDB: mDB, tableName: "sync_table")
@@ -103,8 +104,10 @@ class ExportToJson {
103
104
  } else {
104
105
  stmt = "INSERT INTO sync_table (sync_date) VALUES (\(sTime));"
105
106
  }
106
- let lastId: Int64 = try UtilsSQLCipher.prepareSQL(
107
- mDB: mDB, sql: stmt, values: [], fromJson: false)
107
+ let resp = try UtilsSQLCipher.prepareSQL(
108
+ mDB: mDB, sql: stmt, values: [], fromJson: false,
109
+ returnMode: "no")
110
+ lastId = resp.0
108
111
  if lastId < 0 {
109
112
  throw ExportToJsonError.setLastExportDate(
110
113
  message: "lastId < 0")
@@ -149,9 +152,11 @@ class ExportToJson {
149
152
  // define the delete statement
150
153
  let delStmt = "DELETE FROM \(table) WHERE sql_deleted = 1 " +
151
154
  "AND last_modified < \(lastExportDate);"
152
- lastId = try UtilsSQLCipher.prepareSQL(mDB: mDB, sql: delStmt,
153
- values: [],
154
- fromJson: true)
155
+ let resp = try UtilsSQLCipher.prepareSQL(mDB: mDB, sql: delStmt,
156
+ values: [],
157
+ fromJson: true,
158
+ returnMode: "no")
159
+ lastId = resp.0
155
160
  if lastId < 0 {
156
161
  let msg = "DelExportedRows: lastId < 0"
157
162
  throw ExportToJsonError.delExportedRows(message: msg)
@@ -384,6 +384,7 @@ class ImportFromJson {
384
384
  mDB: Database, mode: String,
385
385
  mValues: [[UncertainValue<String, Int, Double>]],
386
386
  tableName: String) throws {
387
+ var lastId: Int64 = -1
387
388
  // Check if table exists
388
389
  do {
389
390
  let isTab: Bool = try UtilsJson
@@ -443,8 +444,9 @@ class ImportFromJson {
443
444
  if stmt.prefix(6) == "DELETE" {
444
445
  rowValues = []
445
446
  }
446
- let lastId: Int64 = try UtilsSQLCipher.prepareSQL(
447
- mDB: mDB, sql: stmt, values: rowValues, fromJson: true)
447
+ let resp = try UtilsSQLCipher.prepareSQL(
448
+ mDB: mDB, sql: stmt, values: rowValues, fromJson: true, returnMode: "no")
449
+ lastId = resp.0
448
450
  if lastId < 0 {
449
451
  throw ImportFromJsonError.createTableData(
450
452
  message: "lastId < 0")
@@ -39,80 +39,80 @@ class UtilsDownloadFromHTTP {
39
39
  isDirectory: false
40
40
  )
41
41
 
42
- let task = URLSession.shared.downloadTask(with: mUrl) {
43
- (tempURL, response, error) in
44
- // Early exit on error
45
- guard let tempURL = tempURL else {
46
- let msg = "\(String(describing: error?.localizedDescription))"
47
- print("\(msg)")
48
- completion(.failure(UtilsDownloadError.downloadFromHTTPFailed))
49
- return
50
- }
51
- if let httpResponse = response as? HTTPURLResponse {
52
- switch httpResponse.statusCode {
53
- case 200:
54
- do {
55
- // Remove any existing document at file
56
- if FileManager.default.fileExists(
57
- atPath: fileCacheURL.path) {
58
- try FileManager.default.removeItem(
59
- atPath: fileCacheURL.path)
60
- }
61
-
62
- // Copy the tempURL to file
63
- try FileManager.default.copyItem(
64
- at: tempURL,
65
- to: fileCacheURL
66
- )
67
- // Delete the tempUrl file
68
- try FileManager.default.removeItem(at: tempURL)
69
- let dbURL = try UtilsFile.getDatabaseLocationURL(
70
- databaseLocation: databaseLocation)
71
- if isZip {
72
- // get the zip files
73
- let zipList: [String] = try UtilsFile
74
- .getFileList(path: cacheURL.path,
75
- ext: ".zip")
76
- // loop through the database files
77
- for zip in zipList {
78
- _ = try UtilsFile.unzipToDatabase(
79
- fromURL: cacheURL,
80
- databaseLocation: tmp,
81
- zip: zip,
82
- overwrite: true)
42
+ let task = URLSession.shared
43
+ .downloadTask(with: mUrl) {(tempURL, response, error) in
44
+ // Early exit on error
45
+ guard let tempURL = tempURL else {
46
+ let msg = "\(String(describing: error?.localizedDescription))"
47
+ print("\(msg)")
48
+ completion(.failure(UtilsDownloadError.downloadFromHTTPFailed))
49
+ return
50
+ }
51
+ if let httpResponse = response as? HTTPURLResponse {
52
+ switch httpResponse.statusCode {
53
+ case 200:
54
+ do {
55
+ // Remove any existing document at file
56
+ if FileManager.default.fileExists(
57
+ atPath: fileCacheURL.path) {
58
+ try FileManager.default.removeItem(
59
+ atPath: fileCacheURL.path)
83
60
  }
84
- // Delete the zip file
85
- try FileManager.default.removeItem(
86
- at: fileCacheURL)
87
61
 
62
+ // Copy the tempURL to file
63
+ try FileManager.default.copyItem(
64
+ at: tempURL,
65
+ to: fileCacheURL
66
+ )
67
+ // Delete the tempUrl file
68
+ try FileManager.default.removeItem(at: tempURL)
69
+ let dbURL = try UtilsFile.getDatabaseLocationURL(
70
+ databaseLocation: databaseLocation)
71
+ if isZip {
72
+ // get the zip files
73
+ let zipList: [String] = try UtilsFile
74
+ .getFileList(path: cacheURL.path,
75
+ ext: ".zip")
76
+ // loop through the database files
77
+ for zip in zipList {
78
+ _ = try UtilsFile.unzipToDatabase(
79
+ fromURL: cacheURL,
80
+ databaseLocation: tmp,
81
+ zip: zip,
82
+ overwrite: true)
83
+ }
84
+ // Delete the zip file
85
+ try FileManager.default.removeItem(
86
+ at: fileCacheURL)
87
+
88
+ }
89
+ try UtilsFile.moveAllDBSQLite(
90
+ fromURL: cacheURL,
91
+ dirUrl: dbURL)
92
+ completion(.success(true))
93
+ return
88
94
  }
89
- try UtilsFile.moveAllDBSQLite(
90
- fromURL: cacheURL,
91
- dirUrl: dbURL)
92
- completion(.success(true))
93
- return
94
- }
95
95
 
96
- // Handle potential file system errors
97
- catch let error {
98
- let msg = "\(error.localizedDescription)"
96
+ // Handle potential file system errors
97
+ catch let error {
98
+ let msg = "\(error.localizedDescription)"
99
+ print("\(msg)")
100
+ completion(.failure(UtilsDownloadError.downloadFromHTTPFailed))
101
+ return
102
+ }
103
+ default:
104
+ let msg = "Download: GET resquest not successful. http status code \(httpResponse.statusCode)"
99
105
  print("\(msg)")
100
106
  completion(.failure(UtilsDownloadError.downloadFromHTTPFailed))
101
107
  return
102
108
  }
103
- default:
104
- let msg = "Download: GET resquest not successful. http status code \(httpResponse.statusCode)"
109
+ } else {
110
+ let msg = "Download: not a valid http response"
105
111
  print("\(msg)")
106
112
  completion(.failure(UtilsDownloadError.downloadFromHTTPFailed))
107
113
  return
108
114
  }
109
- } else {
110
- let msg = "Download: not a valid http response"
111
- print("\(msg)")
112
- completion(.failure(UtilsDownloadError.downloadFromHTTPFailed))
113
- return
114
115
  }
115
- }
116
116
  // Start the download
117
117
  task.resume()
118
118
  }
@@ -233,7 +233,8 @@ class UtilsDrop {
233
233
  retChanges = try self.dropViews(mDB: mDB)
234
234
  if changes >= 0 {
235
235
  _ = try UtilsSQLCipher.prepareSQL(mDB: mDB, sql: "VACUUM;",
236
- values: [], fromJson: false)
236
+ values: [], fromJson: false,
237
+ returnMode: "no")
237
238
  changes = UtilsSQLCipher.dbChanges(mDB: mDB.mDb) -
238
239
  initChanges
239
240
  }