@capacitor-community/sqlite 3.3.2 → 3.3.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.
Files changed (30) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +6 -0
  3. package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLite.java +83 -7
  4. package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLitePlugin.java +101 -3
  5. package/android/src/main/java/com/getcapacitor/community/database/sqlite/RetHandler.java +24 -0
  6. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java +67 -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 +111 -2
  11. package/dist/esm/definitions.js +63 -0
  12. package/dist/esm/definitions.js.map +1 -1
  13. package/dist/esm/web.d.ts +5 -1
  14. package/dist/esm/web.js +16 -0
  15. package/dist/esm/web.js.map +1 -1
  16. package/dist/plugin.cjs.js +79 -0
  17. package/dist/plugin.cjs.js.map +1 -1
  18. package/dist/plugin.js +79 -0
  19. package/dist/plugin.js.map +1 -1
  20. package/electron/dist/plugin.js +17 -1
  21. package/electron/dist/plugin.js.map +1 -1
  22. package/ios/Plugin/CapacitorSQLite.swift +85 -8
  23. package/ios/Plugin/CapacitorSQLitePlugin.m +4 -0
  24. package/ios/Plugin/CapacitorSQLitePlugin.swift +111 -0
  25. package/ios/Plugin/Database.swift +39 -22
  26. package/ios/Plugin/ImportExportJson/ImportFromJson.swift +2 -1
  27. package/ios/Plugin/ImportExportJson/JsonSQLite.swift +1 -1
  28. package/ios/Plugin/ReturnHandler.swift +13 -0
  29. package/ios/Plugin/Utils/UtilsNCDatabase.swift +31 -0
  30. package/package.json +5 -5
@@ -57,6 +57,67 @@ enum CapacitorSQLiteError: Error {
57
57
  }
58
58
 
59
59
  }
60
+ // MARK: - getNCDatabasePath
61
+
62
+ @objc public func getNCDatabasePath(_ folderPath: String, dbName: String ) throws -> String {
63
+ do {
64
+ let databasePath: String = try UtilsNCDatabase
65
+ .getNCDatabasePath(folderPath: folderPath,
66
+ database: dbName )
67
+ return databasePath
68
+ } catch let error {
69
+ throw CapacitorSQLiteError.failed(message: "\(error)")
70
+ }
71
+
72
+ }
73
+
74
+ // MARK: - CreateNCConnection
75
+
76
+ @objc public func createNCConnection(_ databasePath: String,
77
+ version: Int) throws {
78
+
79
+ // check if the connection already exists
80
+ let conn = dbDict[databasePath]
81
+ if conn != nil {
82
+ let msg = "Connection \(databasePath) already exists"
83
+ throw CapacitorSQLiteError.failed(message: msg)
84
+ }
85
+
86
+ do {
87
+ let isFileExists: Bool = UtilsFile
88
+ .isFileExist(filePath: databasePath)
89
+
90
+ if !isFileExists {
91
+ throw CapacitorSQLiteError.failed(message: "database \(databasePath) does not exist")
92
+ }
93
+ let mDb: Database = try Database(
94
+ databaseName: databasePath,
95
+ encrypted: false, mode: "no-encryption", version: version,
96
+ vUpgDict: [:])
97
+ dbDict[databasePath] = mDb
98
+ return
99
+ } catch let error {
100
+ throw CapacitorSQLiteError.failed(message: "\(error)")
101
+ }
102
+ }
103
+
104
+ // MARK: - CloseNCConnection
105
+
106
+ @objc public func closeNCConnection(_ dbName: String) throws {
107
+ guard let mDb: Database = dbDict[dbName] else {
108
+ let msg = "Connection to \(dbName) not available"
109
+ throw CapacitorSQLiteError.failed(message: msg)
110
+ }
111
+ if mDb.isDBOpen() {
112
+ do {
113
+ try mDb.close()
114
+ } catch DatabaseError.close(let message) {
115
+ throw CapacitorSQLiteError.failed(message: message)
116
+ }
117
+ }
118
+ dbDict.removeValue(forKey: dbName)
119
+ return
120
+ }
60
121
 
61
122
  // MARK: - CreateConnection
62
123
 
@@ -133,6 +194,7 @@ enum CapacitorSQLiteError: Error {
133
194
  throw CapacitorSQLiteError.failed(message: message)
134
195
  }
135
196
  }
197
+
136
198
  // MARK: - Close Connection
137
199
 
138
200
  @objc public func closeConnection(_ dbName: String) throws {
@@ -193,6 +255,7 @@ enum CapacitorSQLiteError: Error {
193
255
  throw CapacitorSQLiteError.failed(message: "\(error)")
194
256
  }
195
257
  }
258
+
196
259
  // MARK: - IsDatabase
197
260
 
198
261
  @objc public func isDatabase(_ dbName: String) throws -> NSNumber {
@@ -206,6 +269,18 @@ enum CapacitorSQLiteError: Error {
206
269
  }
207
270
  }
208
271
 
272
+ // MARK: - IsNCDatabase
273
+
274
+ @objc public func isNCDatabase(_ databasePath: String) throws -> NSNumber {
275
+ let isFileExists: Bool = UtilsFile
276
+ .isFileExist(filePath: databasePath)
277
+ if isFileExists {
278
+ return 1
279
+ } else {
280
+ return 0
281
+ }
282
+ }
283
+
209
284
  // MARK: - IsTableExists
210
285
 
211
286
  @objc public func isTableExists(_ dbName: String, tableName: String) throws -> NSNumber {
@@ -244,7 +319,7 @@ enum CapacitorSQLiteError: Error {
244
319
  let msg = "Connection to \(mDbName) not available"
245
320
  throw CapacitorSQLiteError.failed(message: msg)
246
321
  }
247
- if mDb.isDBOpen() {
322
+ if !mDb.isNCDB() && mDb.isDBOpen() {
248
323
  do {
249
324
  var stmts = statements
250
325
  // remove carriage returns
@@ -268,7 +343,7 @@ enum CapacitorSQLiteError: Error {
268
343
  throw CapacitorSQLiteError.failed(message: msg)
269
344
  }
270
345
  } else {
271
- let msg = "Database \(mDbName) not opened"
346
+ let msg = "Database \(mDbName) not opened or in read-only"
272
347
  throw CapacitorSQLiteError.failed(message: msg)
273
348
  }
274
349
  }
@@ -283,7 +358,7 @@ enum CapacitorSQLiteError: Error {
283
358
  let msg = "Connection to \(mDbName) not available"
284
359
  throw CapacitorSQLiteError.failed(message: msg)
285
360
  }
286
- if mDb.isDBOpen() {
361
+ if !mDb.isNCDB() && mDb.isDBOpen() {
287
362
  do {
288
363
  let res = try mDb.execSet(set: set, transaction: transaction)
289
364
  return res
@@ -294,7 +369,7 @@ enum CapacitorSQLiteError: Error {
294
369
  throw CapacitorSQLiteError.failed(message: msg)
295
370
  }
296
371
  } else {
297
- let msg = "Database \(mDbName) not opened"
372
+ let msg = "Database \(mDbName) not opened or in read-only"
298
373
  throw CapacitorSQLiteError.failed(message: msg)
299
374
  }
300
375
  }
@@ -310,7 +385,7 @@ enum CapacitorSQLiteError: Error {
310
385
  let msg = "Connection to \(mDbName) not available"
311
386
  throw CapacitorSQLiteError.failed(message: msg)
312
387
  }
313
- if mDb.isDBOpen() {
388
+ if !mDb.isNCDB() && mDb.isDBOpen() {
314
389
  do {
315
390
  var val: [Any] = []
316
391
  if values.count > 0 {
@@ -343,7 +418,7 @@ enum CapacitorSQLiteError: Error {
343
418
  throw CapacitorSQLiteError.failed(message: msg)
344
419
  }
345
420
  } else {
346
- let msg = "Database \(mDbName) not opened"
421
+ let msg = "Database \(mDbName) not opened or in read-only"
347
422
  throw CapacitorSQLiteError.failed(message: msg)
348
423
  }
349
424
  }
@@ -799,8 +874,10 @@ enum CapacitorSQLiteError: Error {
799
874
  }
800
875
  class func getDatabaseName(dbName: String) -> String {
801
876
  var retName: String = dbName
802
- if retName.suffix(3) == ".db" {
803
- retName = String(retName.dropLast(3))
877
+ if !retName.contains("/") {
878
+ if retName.suffix(3) == ".db" {
879
+ retName = String(retName.dropLast(3))
880
+ }
804
881
  }
805
882
  return retName
806
883
  }
@@ -7,6 +7,9 @@ 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);
12
15
  CAP_PLUGIN_METHOD(getVersion, CAPPluginReturnPromise);
@@ -26,6 +29,7 @@ CAP_PLUGIN(CapacitorSQLitePlugin, "CapacitorSQLite",
26
29
  CAP_PLUGIN_METHOD(addUpgradeStatement, CAPPluginReturnPromise);
27
30
  CAP_PLUGIN_METHOD(copyFromAssets, CAPPluginReturnPromise);
28
31
  CAP_PLUGIN_METHOD(isDatabase, CAPPluginReturnPromise);
32
+ CAP_PLUGIN_METHOD(isNCDatabase, CAPPluginReturnPromise);
29
33
  CAP_PLUGIN_METHOD(isTableExists, CAPPluginReturnPromise);
30
34
  CAP_PLUGIN_METHOD(getDatabaseList, CAPPluginReturnPromise);
31
35
  CAP_PLUGIN_METHOD(getMigratableDbList, CAPPluginReturnPromise);
@@ -966,6 +966,117 @@ public class CapacitorSQLitePlugin: CAPPlugin {
966
966
  }
967
967
  }
968
968
 
969
+ // MARK: - getNCDatabasePath
970
+
971
+ @objc func getNCDatabasePath(_ call: CAPPluginCall) {
972
+ guard let folderPath = call.options["path"] as? String else {
973
+ retHandler.rPath(call: call, ret: "",
974
+ message: "getNCDatabasePath: Must provide a folder path")
975
+ return
976
+ }
977
+ guard let dbName = call.options["database"] as? String else {
978
+ retHandler.rPath(call: call, ret: "",
979
+ message: "getNCDatabasePath: Must provide a database name")
980
+ return
981
+ }
982
+ do {
983
+
984
+ let path: String = try implementation.getNCDatabasePath(folderPath,
985
+ dbName: dbName)
986
+ retHandler.rPath(call: call, ret: path)
987
+ return
988
+ } catch CapacitorSQLiteError.failed(let message) {
989
+ let msg = "getNCDatabasePath: \(message)"
990
+ retHandler.rPath(call: call, ret: "", message: msg)
991
+ return
992
+ } catch let error {
993
+ retHandler.rPath(call: call, ret: "",
994
+ message: "getNCDatabasePath: \(error)")
995
+ return
996
+ }
997
+ }
998
+
999
+ // MARK: - CreateNCConnection
1000
+
1001
+ @objc func createNCConnection(_ call: CAPPluginCall) {
1002
+ guard let dbPath = call.options["databasePath"] as? String else {
1003
+ retHandler.rResult(
1004
+ call: call,
1005
+ message: "CreateNCConnection: Must provide a database path")
1006
+ return
1007
+ }
1008
+ let version: Int = call.getInt("version") ?? 1
1009
+ do {
1010
+ try implementation.createNCConnection(dbPath,
1011
+ version: version)
1012
+ retHandler.rResult(call: call)
1013
+ return
1014
+ } catch CapacitorSQLiteError.failed(let message) {
1015
+ let msg = "CreateNCConnection: \(message)"
1016
+ retHandler.rResult(call: call, message: msg)
1017
+ return
1018
+ } catch let error {
1019
+ retHandler.rResult(
1020
+ call: call,
1021
+ message: "CreateNCConnection: \(error)")
1022
+ return
1023
+ }
1024
+ }
1025
+
1026
+ // MARK: - CloseNCConnection
1027
+
1028
+ @objc func closeNCConnection(_ call: CAPPluginCall) {
1029
+ guard let dbPath = call.options["databasePath"] as? String else {
1030
+ retHandler.rResult(
1031
+ call: call,
1032
+ message: "CloseNCConnection: Must provide a database path")
1033
+ return
1034
+ }
1035
+ do {
1036
+ try implementation.closeNCConnection(dbPath)
1037
+ retHandler.rResult(call: call)
1038
+ return
1039
+ } catch CapacitorSQLiteError.failed(let message) {
1040
+ let msg = "CloseNCConnection: \(message)"
1041
+ retHandler.rResult(call: call, message: msg)
1042
+ return
1043
+ } catch let error {
1044
+ retHandler.rResult(
1045
+ call: call,
1046
+ message: "CloseNCConnection: \(error)")
1047
+ return
1048
+ }
1049
+ }
1050
+
1051
+ // MARK: - IsNCDatabase
1052
+
1053
+ @objc func isNCDatabase(_ call: CAPPluginCall) {
1054
+ guard let dbPath = call.options["databasePath"] as? String else {
1055
+ retHandler.rResult(
1056
+ call: call, ret: false,
1057
+ message: "isNCDatabase: Must provide a database path")
1058
+ return
1059
+ }
1060
+ do {
1061
+ let res = try implementation.isNCDatabase(dbPath)
1062
+ var bRes: Bool = false
1063
+ if res == 1 {
1064
+ bRes = true
1065
+ }
1066
+ retHandler.rResult(call: call, ret: bRes)
1067
+ } catch CapacitorSQLiteError.failed(let message) {
1068
+ let msg = "isNCDatabase: \(message)"
1069
+ retHandler.rResult(call: call, message: msg)
1070
+ return
1071
+ } catch let error {
1072
+ retHandler.rResult(
1073
+ call: call,
1074
+ message: "isNCDatabase: \(error)")
1075
+ return
1076
+ }
1077
+
1078
+ }
1079
+
969
1080
  // MARK: - Add Observers
970
1081
 
971
1082
  @objc func addObserversToNotificationCenter() {
@@ -37,6 +37,8 @@ class Database {
37
37
  var mDb: OpaquePointer?
38
38
  let globalData: GlobalSQLite = GlobalSQLite()
39
39
  let uUpg: UtilsUpgrade = UtilsUpgrade()
40
+ var readOnly: Bool = false
41
+ var ncDB: Bool = false
40
42
 
41
43
  // MARK: - Init
42
44
  init(databaseName: String, encrypted: Bool,
@@ -48,12 +50,19 @@ class Database {
48
50
  self.dbName = databaseName
49
51
  self.mode = mode
50
52
  self.vUpgDict = vUpgDict
51
- do {
52
- self.path = try UtilsFile.getFilePath(
53
- fileName: databaseName)
54
- } catch UtilsFileError.getFilePathFailed {
55
- throw DatabaseError.filePath(
56
- message: "Could not generate the file path")
53
+ if databaseName.contains("/") &&
54
+ databaseName.suffix(9) != "SQLite.db" {
55
+ self.readOnly = true
56
+ self.path = databaseName
57
+ self.ncDB = true
58
+ } else {
59
+ do {
60
+ self.path = try UtilsFile.getFilePath(
61
+ fileName: databaseName)
62
+ } catch UtilsFileError.getFilePathFailed {
63
+ throw DatabaseError.filePath(
64
+ message: "Could not generate the file path")
65
+ }
57
66
  }
58
67
  print("database path \(self.path)")
59
68
  }
@@ -64,6 +73,12 @@ class Database {
64
73
  return isOpen
65
74
  }
66
75
 
76
+ // MARK: - isNCDB
77
+
78
+ func isNCDB () -> Bool {
79
+ return ncDB
80
+ }
81
+
67
82
  // MARK: - Open
68
83
 
69
84
  // swiftlint:disable cyclomatic_complexity
@@ -92,27 +107,29 @@ class Database {
92
107
  mDb = try UtilsSQLCipher
93
108
  .openOrCreateDatabase(filename: path,
94
109
  password: password,
95
- readonly: false)
110
+ readonly: self.readOnly)
96
111
  isOpen = true
97
112
  // PRAGMA foreign_keys = ON;
98
113
  try UtilsSQLCipher
99
114
  .setForeignKeyConstraintsEnabled(mDB: self,
100
115
  toggle: true)
101
- var curVersion: Int = try UtilsSQLCipher
102
- .getVersion(mDB: self)
103
- if curVersion == 0 {
104
- try UtilsSQLCipher.setVersion(mDB: self, version: 1)
105
- curVersion = try UtilsSQLCipher.getVersion(mDB: self)
106
- }
107
- if dbVersion > curVersion {
108
- if vUpgDict.count > 0 {
109
- _ = try uUpg.onUpgrade(mDB: self, upgDict: vUpgDict,
110
- dbName: dbName,
111
- currentVersion: curVersion,
112
- targetVersion: dbVersion)
113
- try UtilsSQLCipher.deleteBackupDB(databaseName: dbName)
114
- } else {
115
- try UtilsSQLCipher.setVersion(mDB: self, version: dbVersion)
116
+ if !ncDB {
117
+ var curVersion: Int = try UtilsSQLCipher
118
+ .getVersion(mDB: self)
119
+ if curVersion == 0 {
120
+ try UtilsSQLCipher.setVersion(mDB: self, version: 1)
121
+ curVersion = try UtilsSQLCipher.getVersion(mDB: self)
122
+ }
123
+ if dbVersion > curVersion {
124
+ if vUpgDict.count > 0 {
125
+ _ = try uUpg.onUpgrade(mDB: self, upgDict: vUpgDict,
126
+ dbName: dbName,
127
+ currentVersion: curVersion,
128
+ targetVersion: dbVersion)
129
+ try UtilsSQLCipher.deleteBackupDB(databaseName: dbName)
130
+ } else {
131
+ try UtilsSQLCipher.setVersion(mDB: self, version: dbVersion)
132
+ }
116
133
  }
117
134
  }
118
135
  } catch UtilsSQLCipherError.openOrCreateDatabase(let message) {
@@ -245,7 +245,8 @@ class ImportFromJson {
245
245
  var statements: [String] = []
246
246
  for jpos in 0..<mIndexes.count {
247
247
  var mUnique: String = ""
248
- if let mMode = mIndexes[jpos].mode {
248
+ if var mMode = mIndexes[jpos].mode {
249
+ mMode = mMode.uppercased()
249
250
  if mMode == "UNIQUE" {
250
251
  mUnique = mMode + " "
251
252
  }
@@ -108,7 +108,7 @@ public struct JsonIndex: Codable {
108
108
  print("value: \(value) ")
109
109
  if let mMode = mode {
110
110
  if mMode.count > 0 {
111
- print("mode: \(mMode) ")
111
+ print("mode: \(mMode.uppercased()) ")
112
112
  }
113
113
  }
114
114
  }
@@ -93,4 +93,17 @@ class ReturnHandler {
93
93
  return
94
94
  }
95
95
  }
96
+
97
+ // MARK: - rPath
98
+
99
+ func rPath(call: CAPPluginCall, ret: String,
100
+ message: String? = nil) {
101
+ if let intMessage = message {
102
+ call.reject(intMessage)
103
+ return
104
+ } else {
105
+ call.resolve(["path": ret])
106
+ return
107
+ }
108
+ }
96
109
  }
@@ -0,0 +1,31 @@
1
+ //
2
+ // UtilsNCDatabase.swift
3
+ // CapacitorCommunitySqlite
4
+ //
5
+ // Created by Quéau Jean Pierre on 17/12/2021.
6
+ //
7
+
8
+ import Foundation
9
+ enum UtilsNCDatabaseError: Error {
10
+ case getNCDatabasePath(message: String)
11
+ }
12
+
13
+ class UtilsNCDatabase {
14
+
15
+ // MARK: - getNCDatabasePath
16
+
17
+ class func getNCDatabasePath(folderPath: String, database: String) throws -> String {
18
+ do {
19
+ let dbPathURL: URL = try UtilsMigrate
20
+ .getFolderURL(folderPath: folderPath)
21
+ return dbPathURL.appendingPathComponent("\(database)").path
22
+ } catch UtilsMigrateError.getFolderURL(let message) {
23
+ throw UtilsNCDatabaseError.getNCDatabasePath(message: message)
24
+ } catch let error {
25
+ var msg: String = "getNCDatabasePath command failed :"
26
+ msg.append(" \(error.localizedDescription)")
27
+ throw UtilsNCDatabaseError.getNCDatabasePath(message: msg)
28
+ }
29
+
30
+ }
31
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capacitor-community/sqlite",
3
- "version": "3.3.2",
3
+ "version": "3.3.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.3.2",
59
- "@capacitor/core": "3.3.2",
58
+ "@capacitor/android": "^3.3.3",
59
+ "@capacitor/core": "3.3.3",
60
60
  "@capacitor/docgen": "^0.0.17",
61
- "@capacitor/ios": "^3.3.2",
61
+ "@capacitor/ios": "^3.3.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.3.2"
77
+ "@capacitor/core": "^3.3.3"
78
78
  },
79
79
  "prettier": "@ionic/prettier-config",
80
80
  "swiftlint": "@ionic/swiftlint-config",