@capacitor-community/sqlite 3.3.2 → 3.3.3-4

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 (37) hide show
  1. package/CHANGELOG.md +47 -0
  2. package/README.md +28 -2
  3. package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLite.java +105 -7
  4. package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLitePlugin.java +126 -4
  5. package/android/src/main/java/com/getcapacitor/community/database/sqlite/RetHandler.java +48 -0
  6. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java +75 -47
  7. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/JsonIndex.java +5 -4
  8. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsFile.java +9 -0
  9. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsNCDatabase.java +26 -0
  10. package/dist/esm/definitions.d.ts +131 -2
  11. package/dist/esm/definitions.js +74 -0
  12. package/dist/esm/definitions.js.map +1 -1
  13. package/dist/esm/web.d.ts +6 -1
  14. package/dist/esm/web.js +19 -0
  15. package/dist/esm/web.js.map +1 -1
  16. package/dist/plugin.cjs.js +93 -0
  17. package/dist/plugin.cjs.js.map +1 -1
  18. package/dist/plugin.js +93 -0
  19. package/dist/plugin.js.map +1 -1
  20. package/electron/dist/plugin.js +20 -1
  21. package/electron/dist/plugin.js.map +1 -1
  22. package/ios/Plugin/CapacitorSQLite.swift +139 -20
  23. package/ios/Plugin/CapacitorSQLitePlugin.m +5 -0
  24. package/ios/Plugin/CapacitorSQLitePlugin.swift +269 -64
  25. package/ios/Plugin/Database.swift +63 -28
  26. package/ios/Plugin/ImportExportJson/ImportFromJson.swift +2 -1
  27. package/ios/Plugin/ImportExportJson/JsonSQLite.swift +1 -1
  28. package/ios/Plugin/ReturnHandler.swift +25 -0
  29. package/ios/Plugin/SqliteConfig.swift +10 -0
  30. package/ios/Plugin/Utils/UtilsEncryption.swift +10 -7
  31. package/ios/Plugin/Utils/UtilsFile.swift +195 -33
  32. package/ios/Plugin/Utils/UtilsMigrate.swift +11 -44
  33. package/ios/Plugin/Utils/UtilsNCDatabase.swift +31 -0
  34. package/ios/Plugin/Utils/UtilsSQLCipher.swift +29 -24
  35. package/ios/Plugin/Utils/UtilsSecret.swift +22 -8
  36. package/ios/Plugin/Utils/UtilsUpgrade.swift +6 -2
  37. package/package.json +5 -5
@@ -5,7 +5,19 @@ enum CapacitorSQLiteError: Error {
5
5
  // swiftlint:disable file_length
6
6
  // swiftlint:disable type_body_length
7
7
  @objc public class CapacitorSQLite: NSObject {
8
+ private var config: SqliteConfig
8
9
  private var dbDict: [String: Database] = [:]
10
+ private var databaseLocation: String
11
+
12
+ init(config: SqliteConfig) {
13
+ self.config = config
14
+ if let isLocation = config.iosDatabaseLocation {
15
+ self.databaseLocation = isLocation
16
+ } else {
17
+ self.databaseLocation = "Documents"
18
+ }
19
+ super.init()
20
+ }
9
21
 
10
22
  // MARK: - Echo
11
23
 
@@ -31,7 +43,9 @@ enum CapacitorSQLiteError: Error {
31
43
  // close all connections
32
44
  try closeAllConnections()
33
45
  // set encryption secret
34
- try UtilsSecret.setEncryptionSecret(passphrase: passphrase)
46
+ try UtilsSecret
47
+ .setEncryptionSecret(passphrase: passphrase,
48
+ databaseLocation: databaseLocation)
35
49
  return
36
50
  } catch UtilsSecretError.setEncryptionSecret(let message) {
37
51
  throw CapacitorSQLiteError.failed(message: message)
@@ -48,7 +62,10 @@ enum CapacitorSQLiteError: Error {
48
62
  // close all connections
49
63
  try closeAllConnections()
50
64
  // set encryption secret
51
- try UtilsSecret.changeEncryptionSecret(passphrase: passphrase, oldPassphrase: oldPassphrase)
65
+ try UtilsSecret
66
+ .changeEncryptionSecret(passphrase: passphrase,
67
+ oldPassphrase: oldPassphrase,
68
+ databaseLocation: databaseLocation)
52
69
  return
53
70
  } catch UtilsSecretError.changeEncryptionSecret(let message) {
54
71
  throw CapacitorSQLiteError.failed(message: message)
@@ -57,6 +74,68 @@ enum CapacitorSQLiteError: Error {
57
74
  }
58
75
 
59
76
  }
77
+ // MARK: - getNCDatabasePath
78
+
79
+ @objc public func getNCDatabasePath(_ folderPath: String, dbName: String ) throws -> String {
80
+ do {
81
+ let databasePath: String = try UtilsNCDatabase
82
+ .getNCDatabasePath(folderPath: folderPath,
83
+ database: dbName )
84
+ return databasePath
85
+ } catch let error {
86
+ throw CapacitorSQLiteError.failed(message: "\(error)")
87
+ }
88
+
89
+ }
90
+
91
+ // MARK: - CreateNCConnection
92
+
93
+ @objc public func createNCConnection(_ databasePath: String,
94
+ version: Int) throws {
95
+
96
+ // check if the connection already exists
97
+ let conn = dbDict[databasePath]
98
+ if conn != nil {
99
+ let msg = "Connection \(databasePath) already exists"
100
+ throw CapacitorSQLiteError.failed(message: msg)
101
+ }
102
+
103
+ do {
104
+ let isFileExists: Bool = UtilsFile
105
+ .isFileExist(filePath: databasePath)
106
+
107
+ if !isFileExists {
108
+ throw CapacitorSQLiteError.failed(message: "database \(databasePath) does not exist")
109
+ }
110
+ let mDb: Database = try Database(
111
+ databaseLocation: databaseLocation,
112
+ databaseName: databasePath,
113
+ encrypted: false, mode: "no-encryption", version: version,
114
+ vUpgDict: [:])
115
+ dbDict[databasePath] = mDb
116
+ return
117
+ } catch let error {
118
+ throw CapacitorSQLiteError.failed(message: "\(error)")
119
+ }
120
+ }
121
+
122
+ // MARK: - CloseNCConnection
123
+
124
+ @objc public func closeNCConnection(_ dbName: String) throws {
125
+ guard let mDb: Database = dbDict[dbName] else {
126
+ let msg = "Connection to \(dbName) not available"
127
+ throw CapacitorSQLiteError.failed(message: msg)
128
+ }
129
+ if mDb.isDBOpen() {
130
+ do {
131
+ try mDb.close()
132
+ } catch DatabaseError.close(let message) {
133
+ throw CapacitorSQLiteError.failed(message: message)
134
+ }
135
+ }
136
+ dbDict.removeValue(forKey: dbName)
137
+ return
138
+ }
60
139
 
61
140
  // MARK: - CreateConnection
62
141
 
@@ -75,6 +154,7 @@ enum CapacitorSQLiteError: Error {
75
154
 
76
155
  do {
77
156
  let mDb: Database = try Database(
157
+ databaseLocation: databaseLocation,
78
158
  databaseName: "\(mDbName)SQLite.db",
79
159
  encrypted: encrypted, mode: mode, version: version,
80
160
  vUpgDict: vUpgDict)
@@ -117,6 +197,22 @@ enum CapacitorSQLiteError: Error {
117
197
  }
118
198
  }
119
199
 
200
+ // MARK: - getUrl
201
+
202
+ @objc public func getUrl(_ dbName: String) throws -> String {
203
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
204
+ guard let mDb: Database = dbDict[mDbName] else {
205
+ let msg = "Connection to \(mDbName) not available"
206
+ throw CapacitorSQLiteError.failed(message: msg)
207
+ }
208
+ do {
209
+ let res: String = try mDb.getUrl()
210
+ return res
211
+ } catch DatabaseError.close(let message) {
212
+ throw CapacitorSQLiteError.failed(message: message)
213
+ }
214
+ }
215
+
120
216
  // MARK: - GetVersion
121
217
 
122
218
  @objc public func getVersion(_ dbName: String) throws -> NSNumber {
@@ -133,6 +229,7 @@ enum CapacitorSQLiteError: Error {
133
229
  throw CapacitorSQLiteError.failed(message: message)
134
230
  }
135
231
  }
232
+
136
233
  // MARK: - Close Connection
137
234
 
138
235
  @objc public func closeConnection(_ dbName: String) throws {
@@ -193,12 +290,26 @@ enum CapacitorSQLiteError: Error {
193
290
  throw CapacitorSQLiteError.failed(message: "\(error)")
194
291
  }
195
292
  }
293
+
196
294
  // MARK: - IsDatabase
197
295
 
198
296
  @objc public func isDatabase(_ dbName: String) throws -> NSNumber {
199
297
  let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
200
298
  let isFileExists: Bool = UtilsFile
201
- .isFileExist(fileName: mDbName + "SQLite.db")
299
+ .isFileExist(databaseLocation: databaseLocation,
300
+ fileName: mDbName + "SQLite.db")
301
+ if isFileExists {
302
+ return 1
303
+ } else {
304
+ return 0
305
+ }
306
+ }
307
+
308
+ // MARK: - IsNCDatabase
309
+
310
+ @objc public func isNCDatabase(_ databasePath: String) throws -> NSNumber {
311
+ let isFileExists: Bool = UtilsFile
312
+ .isFileExist(filePath: databasePath)
202
313
  if isFileExists {
203
314
  return 1
204
315
  } else {
@@ -244,7 +355,7 @@ enum CapacitorSQLiteError: Error {
244
355
  let msg = "Connection to \(mDbName) not available"
245
356
  throw CapacitorSQLiteError.failed(message: msg)
246
357
  }
247
- if mDb.isDBOpen() {
358
+ if !mDb.isNCDB() && mDb.isDBOpen() {
248
359
  do {
249
360
  var stmts = statements
250
361
  // remove carriage returns
@@ -268,7 +379,7 @@ enum CapacitorSQLiteError: Error {
268
379
  throw CapacitorSQLiteError.failed(message: msg)
269
380
  }
270
381
  } else {
271
- let msg = "Database \(mDbName) not opened"
382
+ let msg = "Database \(mDbName) not opened or in read-only"
272
383
  throw CapacitorSQLiteError.failed(message: msg)
273
384
  }
274
385
  }
@@ -283,7 +394,7 @@ enum CapacitorSQLiteError: Error {
283
394
  let msg = "Connection to \(mDbName) not available"
284
395
  throw CapacitorSQLiteError.failed(message: msg)
285
396
  }
286
- if mDb.isDBOpen() {
397
+ if !mDb.isNCDB() && mDb.isDBOpen() {
287
398
  do {
288
399
  let res = try mDb.execSet(set: set, transaction: transaction)
289
400
  return res
@@ -294,7 +405,7 @@ enum CapacitorSQLiteError: Error {
294
405
  throw CapacitorSQLiteError.failed(message: msg)
295
406
  }
296
407
  } else {
297
- let msg = "Database \(mDbName) not opened"
408
+ let msg = "Database \(mDbName) not opened or in read-only"
298
409
  throw CapacitorSQLiteError.failed(message: msg)
299
410
  }
300
411
  }
@@ -310,7 +421,7 @@ enum CapacitorSQLiteError: Error {
310
421
  let msg = "Connection to \(mDbName) not available"
311
422
  throw CapacitorSQLiteError.failed(message: msg)
312
423
  }
313
- if mDb.isDBOpen() {
424
+ if !mDb.isNCDB() && mDb.isDBOpen() {
314
425
  do {
315
426
  var val: [Any] = []
316
427
  if values.count > 0 {
@@ -343,7 +454,7 @@ enum CapacitorSQLiteError: Error {
343
454
  throw CapacitorSQLiteError.failed(message: msg)
344
455
  }
345
456
  } else {
346
- let msg = "Database \(mDbName) not opened"
457
+ let msg = "Database \(mDbName) not opened or in read-only"
347
458
  throw CapacitorSQLiteError.failed(message: msg)
348
459
  }
349
460
  }
@@ -384,7 +495,8 @@ enum CapacitorSQLiteError: Error {
384
495
  throw CapacitorSQLiteError.failed(message: msg)
385
496
  }
386
497
  let res: Bool = UtilsFile
387
- .isFileExist(fileName: "\(mDbName)SQLite.db")
498
+ .isFileExist(databaseLocation: databaseLocation,
499
+ fileName: "\(mDbName)SQLite.db")
388
500
  if res {
389
501
  return 1
390
502
  } else {
@@ -421,8 +533,7 @@ enum CapacitorSQLiteError: Error {
421
533
  if !mDb.isDBOpen() {
422
534
  try mDb.open()
423
535
  }
424
- let res: Bool = try mDb.deleteDB(
425
- databaseName: "\(mDbName)SQLite.db")
536
+ let res: Bool = try mDb.deleteDB(databaseName: "\(mDbName)SQLite.db")
426
537
  if res {
427
538
  return
428
539
  } else {
@@ -483,6 +594,7 @@ enum CapacitorSQLiteError: Error {
483
594
  // open the database
484
595
  do {
485
596
  mDb = try Database(
597
+ databaseLocation: databaseLocation,
486
598
  databaseName: dbName, encrypted: encrypted,
487
599
  mode: inMode, version: version, vUpgDict: [:])
488
600
  try mDb.open()
@@ -685,7 +797,7 @@ enum CapacitorSQLiteError: Error {
685
797
  let aPath: String = assetsDbPath.path
686
798
  let bRes: Bool = UtilsFile.isDirExist(dirPath: aPath)
687
799
  if bRes {
688
- // get the database files
800
+ // get the database files from assets
689
801
  let dbList: [String] = try UtilsFile
690
802
  .getFileList(path: aPath, ext: ".db")
691
803
  // loop through the database files
@@ -697,7 +809,8 @@ enum CapacitorSQLiteError: Error {
697
809
  // for each copy the file to the Application
698
810
  // database folder
699
811
  _ = try UtilsFile
700
- .copyFromAssetToDatabase(fromDb: mDb,
812
+ .copyFromAssetToDatabase(databaseLocation: databaseLocation,
813
+ fromDb: mDb,
701
814
  toDb: toDb, overwrite: overwrite)
702
815
  }
703
816
  // get the zip files
@@ -708,7 +821,8 @@ enum CapacitorSQLiteError: Error {
708
821
  // for each zip uncompress the file to the Application
709
822
  // database folder
710
823
  _ = try UtilsFile
711
- .unzipFromAssetToDatabase(zip: zip, overwrite: overwrite)
824
+ .unzipFromAssetToDatabase(databaseLocation: databaseLocation,
825
+ zip: zip, overwrite: overwrite)
712
826
  }
713
827
  return
714
828
  } else {
@@ -729,7 +843,7 @@ enum CapacitorSQLiteError: Error {
729
843
 
730
844
  @objc func getDatabaseList() throws -> [String] {
731
845
  do {
732
- let aPath: String = try UtilsFile.getDatabasesPath()
846
+ let aPath: String = try (UtilsFile.getFolderURL(folderPath: databaseLocation)).path
733
847
  // get the database files
734
848
  let dbList: [String] = try UtilsFile.getFileList(path: aPath, ext: ".db")
735
849
  return dbList
@@ -765,7 +879,9 @@ enum CapacitorSQLiteError: Error {
765
879
  @objc func addSQLiteSuffix(_ folderPath: String, dbList: [String]) throws {
766
880
 
767
881
  do {
768
- try UtilsMigrate.addSQLiteSuffix(folderPath: folderPath, dbList: dbList)
882
+ try UtilsMigrate.addSQLiteSuffix(databaseLocation: databaseLocation,
883
+ folderPath: folderPath,
884
+ dbList: dbList)
769
885
  return
770
886
  } catch UtilsMigrateError.addSQLiteSuffix(let message) {
771
887
  var msg: String = "addSQLiteSuffix:"
@@ -783,7 +899,8 @@ enum CapacitorSQLiteError: Error {
783
899
 
784
900
  @objc func deleteOldDatabases(_ folderPath: String, dbList: [String]) throws {
785
901
  do {
786
- try UtilsMigrate.deleteOldDatabases(folderPath: folderPath, dbList: dbList)
902
+ try UtilsMigrate
903
+ .deleteOldDatabases(folderPath: folderPath, dbList: dbList)
787
904
  return
788
905
  } catch UtilsMigrateError.deleteOldDatabases(let message) {
789
906
  var msg: String = "deleteOldDatabases:"
@@ -799,8 +916,10 @@ enum CapacitorSQLiteError: Error {
799
916
  }
800
917
  class func getDatabaseName(dbName: String) -> String {
801
918
  var retName: String = dbName
802
- if retName.suffix(3) == ".db" {
803
- retName = String(retName.dropLast(3))
919
+ if !retName.contains("/") {
920
+ if retName.suffix(3) == ".db" {
921
+ retName = String(retName.dropLast(3))
922
+ }
804
923
  }
805
924
  return retName
806
925
  }
@@ -7,8 +7,12 @@ CAP_PLUGIN(CapacitorSQLitePlugin, "CapacitorSQLite",
7
7
  CAP_PLUGIN_METHOD(echo, CAPPluginReturnPromise);
8
8
  CAP_PLUGIN_METHOD(createConnection, CAPPluginReturnPromise);
9
9
  CAP_PLUGIN_METHOD(closeConnection, CAPPluginReturnPromise);
10
+ CAP_PLUGIN_METHOD(createNCConnection, CAPPluginReturnPromise);
11
+ CAP_PLUGIN_METHOD(closeNCConnection, CAPPluginReturnPromise);
12
+ CAP_PLUGIN_METHOD(getNCDatabasePath, CAPPluginReturnPromise);
10
13
  CAP_PLUGIN_METHOD(open, CAPPluginReturnPromise);
11
14
  CAP_PLUGIN_METHOD(close, CAPPluginReturnPromise);
15
+ CAP_PLUGIN_METHOD(getUrl, CAPPluginReturnPromise);
12
16
  CAP_PLUGIN_METHOD(getVersion, CAPPluginReturnPromise);
13
17
  CAP_PLUGIN_METHOD(execute, CAPPluginReturnPromise);
14
18
  CAP_PLUGIN_METHOD(executeSet, CAPPluginReturnPromise);
@@ -26,6 +30,7 @@ CAP_PLUGIN(CapacitorSQLitePlugin, "CapacitorSQLite",
26
30
  CAP_PLUGIN_METHOD(addUpgradeStatement, CAPPluginReturnPromise);
27
31
  CAP_PLUGIN_METHOD(copyFromAssets, CAPPluginReturnPromise);
28
32
  CAP_PLUGIN_METHOD(isDatabase, CAPPluginReturnPromise);
33
+ CAP_PLUGIN_METHOD(isNCDatabase, CAPPluginReturnPromise);
29
34
  CAP_PLUGIN_METHOD(isTableExists, CAPPluginReturnPromise);
30
35
  CAP_PLUGIN_METHOD(getDatabaseList, CAPPluginReturnPromise);
31
36
  CAP_PLUGIN_METHOD(getMigratableDbList, CAPPluginReturnPromise);