@capacitor-community/sqlite 4.0.0-0 → 4.1.0-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.
@@ -1341,6 +1341,32 @@ enum CapacitorSQLiteError: Error {
1341
1341
  throw CapacitorSQLiteError.failed(message: initMessage)
1342
1342
  }
1343
1343
  }
1344
+
1345
+ // MARK: - moveDatabasesAndAddSuffix
1346
+
1347
+ @objc func moveDatabasesAndAddSuffix(_ folderPath: String, dbList: [String]) throws {
1348
+ if isInit {
1349
+ do {
1350
+ try UtilsMigrate
1351
+ .moveDatabasesAndAddSuffix(databaseLocation: databaseLocation,
1352
+ folderPath: folderPath,
1353
+ dbList: dbList)
1354
+ return
1355
+ } catch UtilsMigrateError.moveDatabasesAndAddSuffix(let message) {
1356
+ var msg: String = "moveDatabasesAndAddSuffix:"
1357
+ msg.append(" \(message)")
1358
+ throw CapacitorSQLiteError.failed(message: msg)
1359
+
1360
+ } catch let error {
1361
+ var msg: String = "moveDatabasesAndAddSuffix:"
1362
+ msg.append(" \(error)")
1363
+ throw CapacitorSQLiteError.failed(message: msg)
1364
+ }
1365
+ } else {
1366
+ throw CapacitorSQLiteError.failed(message: initMessage)
1367
+ }
1368
+ }
1369
+
1344
1370
  class func getDatabaseName(dbName: String) -> String {
1345
1371
  var retName: String = dbName
1346
1372
  if !retName.contains("/") {
@@ -38,6 +38,7 @@ CAP_PLUGIN(CapacitorSQLitePlugin, "CapacitorSQLite",
38
38
  CAP_PLUGIN_METHOD(getMigratableDbList, CAPPluginReturnPromise);
39
39
  CAP_PLUGIN_METHOD(addSQLiteSuffix, CAPPluginReturnPromise);
40
40
  CAP_PLUGIN_METHOD(deleteOldDatabases, CAPPluginReturnPromise);
41
+ CAP_PLUGIN_METHOD(moveDatabasesAndAddSuffix, CAPPluginReturnPromise);
41
42
  CAP_PLUGIN_METHOD(checkConnectionsConsistency, CAPPluginReturnPromise);
42
43
  CAP_PLUGIN_METHOD(isSecretStored, CAPPluginReturnPromise);
43
44
  CAP_PLUGIN_METHOD(setEncryptionSecret, CAPPluginReturnPromise);
@@ -549,6 +549,35 @@ public class CapacitorSQLitePlugin: CAPPlugin {
549
549
  }
550
550
  }
551
551
 
552
+ // MARK: - moveDatabasesAndAddSuffix
553
+
554
+ @objc func moveDatabasesAndAddSuffix(_ call: CAPPluginCall) {
555
+ let folderPath: String = call.getString("folderPath") ?? "default"
556
+ let dbJsList: JSArray = call.getArray("dbNameList") ?? []
557
+ var dbList: [String] = []
558
+ if dbJsList.count > 0 {
559
+ for dbName in dbJsList {
560
+ if let name = dbName as? String {
561
+ dbList.append(name)
562
+ }
563
+ }
564
+ }
565
+ do {
566
+ try implementation?.moveDatabasesAndAddSuffix(folderPath, dbList: dbList)
567
+ retHandler.rResult(call: call)
568
+ return
569
+ } catch CapacitorSQLiteError.failed(let message) {
570
+ let msg = "moveDatabasesAndAddSuffix: \(message)"
571
+ retHandler.rResult(call: call, message: msg)
572
+ return
573
+ } catch let error {
574
+ retHandler.rResult(
575
+ call: call,
576
+ message: "moveDatabasesAndAddSuffix: \(error)")
577
+ return
578
+ }
579
+ }
580
+
552
581
  // MARK: - Execute
553
582
 
554
583
  @objc func execute(_ call: CAPPluginCall) {
@@ -16,7 +16,6 @@ extension String {
16
16
  searchStartIndex = self.index(self.startIndex, offsetBy: fIdx)
17
17
  }
18
18
  }
19
-
20
19
 
21
20
  while searchStartIndex < self.endIndex,
22
21
  let range = self.range(of: string, options: .caseInsensitive, range: searchStartIndex..<self.endIndex),
@@ -819,6 +819,7 @@ class ExportToJson {
819
819
  // MARK: - ExportToJson - CreateSchema
820
820
 
821
821
  // swiftlint:disable function_body_length
822
+ // swiftlint:disable cyclomatic_complexity
822
823
  class func createSchema(stmt: String) throws -> [[String: String]] {
823
824
  var retSchema: [[String: String]] = []
824
825
  // get the sqlStmt between the parenthesis sqlStmt
@@ -836,14 +837,8 @@ class ExportToJson {
836
837
  var row = rstr.split(separator: " ", maxSplits: 1)
837
838
  if row.count == 2 {
838
839
  var columns: [String: String] = [:]
839
- if String(row[0]).uppercased() != "FOREIGN" && String(row[0]).uppercased() != "CONSTRAINT" {
840
- columns["column"] = String(row[0])
841
- } else if String(row[0]).uppercased() == "CONSTRAINT" {
842
- let tRow = row[1].split(separator: " ", maxSplits: 1)
843
- row[0] = tRow[0]
844
- columns["constraint"] = String(row[0])
845
- row[1] = tRow[1]
846
- } else {
840
+ switch String(row[0]).uppercased() {
841
+ case "FOREIGN":
847
842
  guard let oPar = rstr.firstIndex(of: "(")
848
843
  else {
849
844
  var msg: String = "Create Schema "
@@ -860,9 +855,45 @@ class ExportToJson {
860
855
  }
861
856
  row[0] = rstr[rstr.index(
862
857
  after: oPar)..<cPar]
863
- row[1] = rstr[rstr.index(
864
- cPar, offsetBy: 2)..<rstr.endIndex]
858
+ row[1] = rstr[rstr.index(cPar,
859
+ offsetBy: 2)..<rstr.endIndex]
865
860
  columns["foreignkey"] = String(row[0])
861
+ .replacingOccurrences(of: "§",
862
+ with: ",")
863
+ .replacingOccurrences(of: ", ",
864
+ with: ",")
865
+ case "PRIMARY":
866
+ guard let oPar = rstr.firstIndex(of: "(")
867
+ else {
868
+ var msg: String = "Create Schema "
869
+ msg.append("PRIMARY KEY no '('")
870
+ throw ExportToJsonError
871
+ .createSchema(message: msg)
872
+ }
873
+ guard let cPar = rstr.firstIndex(of: ")")
874
+ else {
875
+ var msg: String = "Create Schema "
876
+ msg.append("PRIMARY KEY no ')'")
877
+ throw ExportToJsonError
878
+ .createSchema(message: msg)
879
+ }
880
+ row[0] = rstr[rstr.index(
881
+ after: oPar)..<cPar]
882
+ row[1] = rstr[rstr.index(rstr.startIndex,
883
+ offsetBy: 0)..<rstr.endIndex]
884
+ columns["constraint"] = "CPK_" + String(row[0])
885
+ .replacingOccurrences(of: "§",
886
+ with: "_")
887
+ .replacingOccurrences(of: "_ ",
888
+ with: "_")
889
+ case "CONSTRAINT":
890
+ let tRow = row[1].split(separator: " ",
891
+ maxSplits: 1)
892
+ row[0] = tRow[0]
893
+ columns["constraint"] = String(row[0])
894
+ row[1] = tRow[1]
895
+ default:
896
+ columns["column"] = String(row[0])
866
897
  }
867
898
  columns["value"] = String(row[1]).replacingOccurrences(of: "§", with: ",")
868
899
  retSchema.append(columns)
@@ -886,6 +917,7 @@ class ExportToJson {
886
917
  }
887
918
  return retSchema
888
919
  }
920
+ // swiftlint:enable cyclomatic_complexity
889
921
  // swiftlint:enable function_body_length
890
922
 
891
923
  // MARK: - ExportToJson - CreateIndexes
@@ -149,7 +149,7 @@ class UtilsFile {
149
149
  dbPathURL = try UtilsFile.getApplicationURL().absoluteURL
150
150
  } else if first[0] == "Library" {
151
151
  dbPathURL = try UtilsFile.getLibraryURL().absoluteURL
152
- } else if first[0].caseInsensitiveCompare("cache") == .orderedSame {
152
+ } else if first[0].caseInsensitiveCompare("cache") == .orderedSame {
153
153
  dbPathURL = try UtilsFile.getCacheURL().absoluteURL
154
154
  } else if first[0] == "Documents" || first[0] == "default" {
155
155
  dbPathURL = databaseURL
@@ -241,7 +241,7 @@ class UtilsFile {
241
241
  throw UtilsFileError.getApplicationPathFailed
242
242
  }
243
243
  }
244
-
244
+
245
245
  // MARK: - getCacheURL
246
246
 
247
247
  class func getCacheURL() throws -> URL {
@@ -9,6 +9,7 @@ enum UtilsMigrateError: Error {
9
9
  case addSQLiteSuffix(message: String)
10
10
  case getMigratableList(message: String)
11
11
  case deleteOldDatabases(message: String)
12
+ case moveDatabasesAndAddSuffix(message: String)
12
13
  }
13
14
 
14
15
  class UtilsMigrate {
@@ -192,4 +193,75 @@ class UtilsMigrate {
192
193
  // swiftlint:enable cyclomatic_complexity
193
194
  // swiftlint:enable function_body_length
194
195
 
196
+ // MARK: - moveDatabasesAndAddSuffix
197
+
198
+ // swiftlint:disable function_body_length
199
+ // swiftlint:disable cyclomatic_complexity
200
+ class func moveDatabasesAndAddSuffix(databaseLocation: String, folderPath: String,
201
+ dbList: [String]) throws {
202
+ var fromFile: String = ""
203
+ var toFile: String = ""
204
+ do {
205
+ let databaseURL: URL = try UtilsFile
206
+ .getFolderURL(folderPath: databaseLocation)
207
+ let dbPathURL: URL = try UtilsFile
208
+ .getFolderURL(folderPath: folderPath)
209
+ var isDir = ObjCBool(true)
210
+ if FileManager.default.fileExists(atPath: dbPathURL.relativePath,
211
+ isDirectory: &isDir) &&
212
+ isDir.boolValue {
213
+ var mDbList: [String]
214
+ if dbList.count > 0 {
215
+ mDbList = try UtilsFile
216
+ .getFileList(path: dbPathURL.relativePath, ext: nil)
217
+ } else {
218
+ mDbList = try UtilsFile
219
+ .getFileList(path: dbPathURL.relativePath, ext: "db")
220
+ }
221
+ for file: String in mDbList {
222
+ if !file.contains("SQLite.db") {
223
+ fromFile = file
224
+ toFile = ""
225
+ if dbList.count > 0 {
226
+ if dbList.contains(fromFile) {
227
+ if String(file.suffix(3)) == ".db" {
228
+ toFile = file
229
+ .replacingOccurrences(of: ".db", with: "SQLite.db")
230
+ } else {
231
+ toFile = file + "SQLite.db"
232
+ }
233
+ }
234
+ } else {
235
+ toFile = file
236
+ .replacingOccurrences(of: ".db", with: "SQLite.db")
237
+ }
238
+ if !toFile.isEmpty {
239
+ let uFrom: URL = dbPathURL.appendingPathComponent(fromFile)
240
+ let uTo: URL = databaseURL.appendingPathComponent(toFile)
241
+ try UtilsFile.moveFile(pathName: uFrom.path, toPathName: uTo.path, overwrite: true)
242
+ }
243
+ }
244
+ }
245
+ return
246
+ } else {
247
+ var msg: String = "moveDatabasesAndAddSuffix command failed :"
248
+ msg.append(" Folder '\(dbPathURL.absoluteString)' not found")
249
+ throw UtilsMigrateError.moveDatabasesAndAddSuffix(message: msg)
250
+ }
251
+ } catch UtilsFileError.getDatabasesURLFailed {
252
+ throw UtilsMigrateError
253
+ .moveDatabasesAndAddSuffix(message: "getDatabasesURLFailed")
254
+ } catch UtilsFileError.getFolderURLFailed(let message) {
255
+ throw UtilsMigrateError.moveDatabasesAndAddSuffix(message: message)
256
+ } catch UtilsFileError.getFileListFailed {
257
+ throw UtilsMigrateError.moveDatabasesAndAddSuffix(message: "getFileListFailed")
258
+ } catch let error {
259
+ var msg: String = "moveDatabasesAndAddSuffix command failed :"
260
+ msg.append(" \(error.localizedDescription)")
261
+ throw UtilsMigrateError.moveDatabasesAndAddSuffix(message: msg)
262
+ }
263
+ }
264
+ // swiftlint:enable cyclomatic_complexity
265
+ // swiftlint:enable function_body_length
266
+
195
267
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capacitor-community/sqlite",
3
- "version": "4.0.0-0",
3
+ "version": "4.1.0-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": "^4.0.0",
59
- "@capacitor/core": "^4.0.0",
58
+ "@capacitor/android": "^4.1.0",
59
+ "@capacitor/core": "^4.1.0",
60
60
  "@capacitor/docgen": "^0.0.17",
61
- "@capacitor/ios": "^4.0.0",
61
+ "@capacitor/ios": "^4.1.0",
62
62
  "@ionic/eslint-config": "^0.3.0",
63
63
  "@ionic/prettier-config": "^1.0.1",
64
64
  "@ionic/swiftlint-config": "^1.1.2",
@@ -93,6 +93,6 @@
93
93
  }
94
94
  },
95
95
  "dependencies": {
96
- "jeep-sqlite": "^1.5.4"
96
+ "jeep-sqlite": "^1.5.6"
97
97
  }
98
98
  }