@capacitor-community/sqlite 3.3.3-1 → 3.3.3-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 (32) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/README.md +22 -2
  3. package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLite.java +22 -0
  4. package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLitePlugin.java +25 -1
  5. package/android/src/main/java/com/getcapacitor/community/database/sqlite/RetHandler.java +25 -1
  6. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java +10 -1
  7. package/dist/esm/definitions.d.ts +20 -0
  8. package/dist/esm/definitions.js +11 -0
  9. package/dist/esm/definitions.js.map +1 -1
  10. package/dist/esm/web.d.ts +2 -1
  11. package/dist/esm/web.js +3 -0
  12. package/dist/esm/web.js.map +1 -1
  13. package/dist/plugin.cjs.js +14 -0
  14. package/dist/plugin.cjs.js.map +1 -1
  15. package/dist/plugin.js +14 -0
  16. package/dist/plugin.js.map +1 -1
  17. package/electron/dist/plugin.js +3 -0
  18. package/electron/dist/plugin.js.map +1 -1
  19. package/ios/Plugin/CapacitorSQLite.swift +738 -552
  20. package/ios/Plugin/CapacitorSQLitePlugin.m +1 -0
  21. package/ios/Plugin/CapacitorSQLitePlugin.swift +166 -72
  22. package/ios/Plugin/Database.swift +29 -11
  23. package/ios/Plugin/ReturnHandler.swift +12 -0
  24. package/ios/Plugin/SqliteConfig.swift +10 -0
  25. package/ios/Plugin/Utils/UtilsEncryption.swift +10 -7
  26. package/ios/Plugin/Utils/UtilsFile.swift +194 -33
  27. package/ios/Plugin/Utils/UtilsMigrate.swift +11 -44
  28. package/ios/Plugin/Utils/UtilsNCDatabase.swift +2 -2
  29. package/ios/Plugin/Utils/UtilsSQLCipher.swift +29 -24
  30. package/ios/Plugin/Utils/UtilsSecret.swift +22 -8
  31. package/ios/Plugin/Utils/UtilsUpgrade.swift +6 -2
  32. package/package.json +1 -1
@@ -17,17 +17,19 @@ class UtilsEncryption {
17
17
  // MARK: - EncryptDatabase
18
18
 
19
19
  // swiftlint:disable function_body_length
20
- class func encryptDatabase(filePath: String, password: String)
21
- throws -> Bool {
20
+ class func encryptDatabase(databaseLocation: String, filePath: String,
21
+ password: String) throws -> Bool {
22
22
  var ret: Bool = false
23
23
  var oDB: OpaquePointer?
24
24
  do {
25
25
  if UtilsFile.isFileExist(filePath: filePath) {
26
26
  do {
27
- let tempPath: String = try UtilsFile.getFilePath(
28
- fileName: "temp.db")
27
+ let tempPath: String = try UtilsFile
28
+ .getFilePath(databaseLocation: databaseLocation,
29
+ fileName: "temp.db")
29
30
  try UtilsFile.renameFile(filePath: filePath,
30
- toFilePath: tempPath)
31
+ toFilePath: tempPath,
32
+ databaseLocation: databaseLocation)
31
33
  oDB = try UtilsSQLCipher
32
34
  .openOrCreateDatabase(filename: tempPath,
33
35
  password: "",
@@ -43,8 +45,9 @@ class UtilsEncryption {
43
45
  stmt.append("DETACH DATABASE encrypted;")
44
46
  if sqlite3_exec(oDB, stmt, nil, nil, nil) ==
45
47
  SQLITE_OK {
46
- try _ = UtilsFile.deleteFile(
47
- fileName: "temp.db")
48
+ try _ = UtilsFile
49
+ .deleteFile(fileName: "temp.db",
50
+ databaseLocation: databaseLocation)
48
51
  ret = true
49
52
  }
50
53
  // close the db
@@ -12,6 +12,7 @@ import ZIPFoundation
12
12
  enum UtilsFileError: Error {
13
13
  case getFilePathFailed
14
14
  case copyFileFailed
15
+ case moveFileFailed
15
16
  case renameFileFailed
16
17
  case deleteFileFailed
17
18
  case getAssetsDatabasesPathFailed
@@ -25,11 +26,45 @@ enum UtilsFileError: Error {
25
26
  case copyFromAssetToDatabaseFailed(message: String)
26
27
  case unzipFromAssetToDatabaseFailed(message: String)
27
28
  case copyFromNamesFailed
29
+ case getFolderURLFailed(message: String)
30
+ case createDirFailed(message: String)
31
+ case moveAllDBSQLiteFailed(message: String)
32
+ case createDatabaseLocationFailed(message: String)
28
33
  }
29
34
  // swiftlint:disable file_length
30
35
  // swiftlint:disable type_body_length
31
36
  class UtilsFile {
32
37
 
38
+ class func createDatabaseLocation(location: String) throws {
39
+ do {
40
+ var dirUrl: URL = try UtilsFile
41
+ .getFolderURL(folderPath: location)
42
+ let dirPath: String = dirUrl.path
43
+ if !UtilsFile.isDirExist(dirPath: dirPath) {
44
+ // create the directory
45
+ try FileManager.default
46
+ .createDirectory(at: dirUrl, withIntermediateDirectories: true)
47
+ // exclude the directory from iCloud Backup
48
+ try UtilsFile.setExcludeFromiCloudBackup(&dirUrl,
49
+ isExcluded: true)
50
+ // move all existing dbs from "Documents" to location folder
51
+ if location.prefix(9) != "Documents" &&
52
+ location.prefix(7) != "default" {
53
+
54
+ try UtilsFile.moveAllDBSQLite(dirUrl: dirUrl)
55
+ }
56
+ }
57
+ } catch UtilsFileError.getFolderURLFailed(let message) {
58
+ throw UtilsFileError.createDatabaseLocationFailed(message: message)
59
+ } catch UtilsFileError.moveAllDBSQLiteFailed(let message) {
60
+ throw UtilsFileError.createDatabaseLocationFailed(message: message)
61
+ } catch let error {
62
+ var msg: String = "CreateDatabaseLocation command failed :"
63
+ msg.append(" \(error.localizedDescription)")
64
+ throw UtilsFileError.createDatabaseLocationFailed(message: msg)
65
+ }
66
+ }
67
+
33
68
  // MARK: - IsFileExist
34
69
 
35
70
  class func isDirExist(dirPath: String) -> Bool {
@@ -39,6 +74,43 @@ class UtilsFile {
39
74
  return exists && isDir.boolValue
40
75
  }
41
76
 
77
+ // MARK: - moveAllDBSQLite
78
+
79
+ class func moveAllDBSQLite(dirUrl: URL) throws {
80
+ // get the db list from Documents folder
81
+ do {
82
+ let databaseURL: URL = try UtilsFile
83
+ .getDatabasesUrl().absoluteURL
84
+ let fileList: [String] = try UtilsFile
85
+ .getFileList(path: databaseURL.path,
86
+ ext: "SQLite.db")
87
+ for file in fileList {
88
+ let fromFileURL: URL = databaseURL
89
+ .appendingPathComponent(file).absoluteURL
90
+ let toFileURL = dirUrl
91
+ .appendingPathComponent(file).absoluteURL
92
+ let retB: Bool = try UtilsFile
93
+ .moveFile(pathName: fromFileURL.path,
94
+ toPathName: toFileURL.path, overwrite: true)
95
+ if !retB {
96
+ let msg: String = "moveFile command failed :"
97
+ throw UtilsFileError.moveAllDBSQLiteFailed(message: msg)
98
+ }
99
+ }
100
+
101
+ } catch UtilsFileError.getFileListFailed {
102
+ let msg: String = "getFileList command failed :"
103
+ throw UtilsFileError.moveAllDBSQLiteFailed(message: msg)
104
+ } catch UtilsFileError.moveFileFailed {
105
+ let msg: String = "moveFile command failed :"
106
+ throw UtilsFileError.moveAllDBSQLiteFailed(message: msg)
107
+ } catch let error {
108
+ var msg: String = "moveAllDBSQLite command failed :"
109
+ msg.append(" \(error.localizedDescription)")
110
+ throw UtilsFileError.moveAllDBSQLiteFailed(message: msg)
111
+ }
112
+
113
+ }
42
114
  // MARK: - IsFileExist
43
115
 
44
116
  class func isFileExist(filePath: String) -> Bool {
@@ -49,11 +121,12 @@ class UtilsFile {
49
121
  }
50
122
  return ret
51
123
  }
52
- class func isFileExist(fileName: String) -> Bool {
124
+ class func isFileExist(databaseLocation: String, fileName: String) -> Bool {
53
125
  var ret: Bool = false
54
126
  do {
55
127
  let filePath: String =
56
128
  try UtilsFile.getFilePath(
129
+ databaseLocation: databaseLocation,
57
130
  fileName: fileName)
58
131
  ret = UtilsFile.isFileExist(filePath: filePath)
59
132
  return ret
@@ -64,14 +137,53 @@ class UtilsFile {
64
137
  }
65
138
  }
66
139
 
67
- // MARK: - GetFilePath
140
+ // MARK: - getFolderURL
68
141
 
69
- class func getFilePath(fileName: String) throws -> String {
142
+ class func getFolderURL(folderPath: String) throws -> URL {
70
143
  do {
71
- let url = try getDatabasesUrl()
72
- return url.appendingPathComponent("\(fileName)").path
144
+ let databaseURL = try UtilsFile.getDatabasesUrl().absoluteURL
145
+ var dbPathURL: URL
146
+ let first = folderPath.split(separator: "/", maxSplits: 1)
147
+ if first[0] == "Applications" {
148
+ dbPathURL = try UtilsFile.getApplicationURL().absoluteURL
149
+ } else if first[0] == "Library" {
150
+ dbPathURL = try UtilsFile.getLibraryURL().absoluteURL
151
+ } else if first[0] == "Documents" || first[0] == "default" {
152
+ dbPathURL = databaseURL
153
+ } else {
154
+ var msg: String = "getFolderURL command failed :"
155
+ msg.append(" Folder '\(first[0])' not allowed")
156
+ throw UtilsFileError.getFolderURLFailed(message: msg)
157
+ }
158
+ if first.count > 1 {
159
+ dbPathURL = dbPathURL
160
+ .appendingPathComponent(String(first[1])).absoluteURL
161
+ }
162
+ return dbPathURL
73
163
  } catch UtilsFileError.getDatabasesURLFailed {
74
- print("Error: getDatabasesUrl Failed")
164
+ throw UtilsFileError.getFolderURLFailed(message: "getDatabasesURLFailed")
165
+ } catch UtilsFileError.getApplicationURLFailed {
166
+ throw UtilsFileError
167
+ .getFolderURLFailed(message: "getApplicationURLFailed")
168
+ } catch let error {
169
+ var msg: String = "getFolderURL command failed :"
170
+ msg.append(" \(error.localizedDescription)")
171
+ throw UtilsFileError.getFolderURLFailed(message: msg)
172
+ }
173
+ }
174
+
175
+ // MARK: - GetFilePath
176
+
177
+ class func getFilePath(databaseLocation: String,
178
+ fileName: String) throws -> String {
179
+ do {
180
+ let url: URL = try UtilsFile
181
+ .getFolderURL(folderPath: databaseLocation)
182
+ let dbPath: String = url
183
+ .appendingPathComponent("\(fileName)").path
184
+ return dbPath
185
+ } catch UtilsFileError.getFolderURLFailed(let message) {
186
+ print("Error: getFilePath Failed \(message)")
75
187
  throw UtilsFileError.getFilePathFailed
76
188
  }
77
189
  }
@@ -157,7 +269,6 @@ class UtilsFile {
157
269
 
158
270
  class func getAssetsDatabasesPath() throws -> URL {
159
271
  if let appFolder = Bundle.main.resourceURL {
160
- print("getAssetsDatabasesPath appFolder \(appFolder)")
161
272
  return appFolder.appendingPathComponent("public/assets/databases")
162
273
  } else {
163
274
  print("Error: getAssetsDatabasePath did not find app folder")
@@ -230,16 +341,16 @@ class UtilsFile {
230
341
 
231
342
  // MARK: - CopyFromAssetToDatabase
232
343
 
233
- class func copyFromAssetToDatabase(fromDb: String, toDb: String,
234
- overwrite: Bool) throws {
344
+ class func copyFromAssetToDatabase(databaseLocation: String, fromDb: String,
345
+ toDb: String, overwrite: Bool) throws {
235
346
  do {
236
347
  let uAsset: URL = try getAssetsDatabasesPath()
237
348
  .appendingPathComponent(fromDb)
238
- let pAsset: String = uAsset.path
239
- let uDb: URL = try getDatabasesUrl()
349
+
350
+ let uDb: URL = try getFolderURL(folderPath: databaseLocation)
240
351
  .appendingPathComponent(toDb)
241
- let pDb: String = uDb.path
242
- let bRet: Bool = try copyFile(pathName: pAsset, toPathName: pDb,
352
+ let bRet: Bool = try copyFile(pathName: uAsset.path,
353
+ toPathName: uDb.path,
243
354
  overwrite: overwrite)
244
355
  if bRet {
245
356
  return
@@ -252,11 +363,9 @@ class UtilsFile {
252
363
  let msg = "Error: getAssetsDatabasesPath Failed"
253
364
  print("\(msg)")
254
365
  throw UtilsFileError.copyFromAssetToDatabaseFailed(message: msg)
255
- } catch UtilsFileError.getDatabasesURLFailed {
256
- let msg = "Error: getDatabasesUrl Failed"
257
- print("\(msg)")
258
-
259
- throw UtilsFileError.copyFromAssetToDatabaseFailed(message: msg)
366
+ } catch UtilsFileError.getFolderURLFailed(let message) {
367
+ print("Error: getFolderUrl Failed \(message)")
368
+ throw UtilsFileError.copyFromAssetToDatabaseFailed(message: message)
260
369
  } catch UtilsFileError.copyFileFailed {
261
370
  let msg = "Error: copyFile Failed"
262
371
  print("\(msg)")
@@ -270,7 +379,8 @@ class UtilsFile {
270
379
 
271
380
  }
272
381
 
273
- class func unzipFromAssetToDatabase(zip: String, overwrite: Bool) throws {
382
+ class func unzipFromAssetToDatabase(databaseLocation: String, zip: String,
383
+ overwrite: Bool) throws {
274
384
  do {
275
385
  let zipAsset: URL = try getAssetsDatabasesPath()
276
386
  .appendingPathComponent(zip)
@@ -279,10 +389,10 @@ class UtilsFile {
279
389
  print("\(msg)")
280
390
  throw UtilsFileError.unzipFromAssetToDatabaseFailed(message: msg)
281
391
  }
392
+ let uDb: URL = try getFolderURL(folderPath: databaseLocation)
282
393
  for entry in archive {
283
394
  let dbEntry = setPathSuffix(sDb: entry.path)
284
- let zipCopy: URL = try getDatabasesUrl()
285
- .appendingPathComponent(dbEntry)
395
+ let zipCopy: URL = uDb.appendingPathComponent(dbEntry)
286
396
  do {
287
397
  let isExist: Bool = isFileExist(filePath: zipCopy.path)
288
398
  if !isExist || overwrite {
@@ -302,10 +412,9 @@ class UtilsFile {
302
412
  let msg = "Error: getAssetsDatabasesPath Failed"
303
413
  print("\(msg)")
304
414
  throw UtilsFileError.unzipFromAssetToDatabaseFailed(message: msg)
305
- } catch UtilsFileError.getDatabasesURLFailed {
306
- let msg = "Error: getDatabasesUrl Failed"
307
- print("\(msg)")
308
- throw UtilsFileError.unzipFromAssetToDatabaseFailed(message: msg)
415
+ } catch UtilsFileError.getFolderURLFailed(let message) {
416
+ print("Error: getFolderUrl Failed \(message)")
417
+ throw UtilsFileError.unzipFromAssetToDatabaseFailed(message: message)
309
418
  } catch let error {
310
419
  let msg = "Error: \(error)"
311
420
  print("\(msg)")
@@ -313,6 +422,36 @@ class UtilsFile {
313
422
  }
314
423
  }
315
424
 
425
+ // MARK: - MoveFile
426
+
427
+ class func moveFile(pathName: String, toPathName: String, overwrite: Bool) throws -> Bool {
428
+ if pathName.count > 0 && toPathName.count > 0 {
429
+ let isPath = isFileExist(filePath: pathName)
430
+ if isPath {
431
+ do {
432
+ let isExist: Bool = isFileExist(filePath: toPathName)
433
+ if !isExist || overwrite {
434
+ if overwrite && isExist {
435
+ _ = try deleteFile(filePath: toPathName)
436
+ }
437
+ let fileManager = FileManager.default
438
+ try fileManager.moveItem(atPath: pathName,
439
+ toPath: toPathName)
440
+ }
441
+ return true
442
+ } catch let error {
443
+ print("Error: \(error)")
444
+ throw UtilsFileError.moveFileFailed
445
+ }
446
+ } else {
447
+ print("Error: MoveFile Failed pathName does not exist")
448
+ throw UtilsFileError.moveFileFailed
449
+ }
450
+ } else {
451
+ print("Error: MoveFile Failed paths count = 0")
452
+ throw UtilsFileError.moveFileFailed
453
+ }
454
+ }
316
455
  // MARK: - CopyFile
317
456
 
318
457
  class func copyFile(pathName: String, toPathName: String, overwrite: Bool) throws -> Bool {
@@ -346,12 +485,17 @@ class UtilsFile {
346
485
 
347
486
  // MARK: - CopyFile
348
487
 
349
- class func copyFile(fileName: String, toFileName: String)
488
+ class func copyFile(fileName: String, toFileName: String,
489
+ databaseLocation: String)
350
490
  throws -> Bool {
351
491
  var ret: Bool = false
352
492
  do {
353
- let fromPath: String = try getFilePath(fileName: fileName)
354
- let toPath: String = try getFilePath(fileName: toFileName)
493
+ let fromPath: String = try
494
+ getFilePath(databaseLocation: databaseLocation,
495
+ fileName: fileName)
496
+ let toPath: String = try
497
+ getFilePath(databaseLocation: databaseLocation,
498
+ fileName: toFileName)
355
499
  ret = try copyFile(pathName: fromPath, toPathName: toPath, overwrite: true)
356
500
  return ret
357
501
  } catch UtilsFileError.getFilePathFailed {
@@ -387,10 +531,13 @@ class UtilsFile {
387
531
 
388
532
  // MARK: - DeleteFile
389
533
 
390
- class func deleteFile(fileName: String) throws -> Bool {
534
+ class func deleteFile(fileName: String,
535
+ databaseLocation: String) throws -> Bool {
391
536
  var ret: Bool = false
392
537
  do {
393
- let filePath: String = try getFilePath(fileName: fileName)
538
+ let filePath: String = try
539
+ getFilePath(databaseLocation: databaseLocation,
540
+ fileName: fileName)
394
541
  ret = try deleteFile(filePath: filePath)
395
542
  } catch let error {
396
543
  print("Error: \(error)")
@@ -418,14 +565,15 @@ class UtilsFile {
418
565
 
419
566
  // MARK: - RenameFile
420
567
 
421
- class func renameFile (filePath: String, toFilePath: String)
422
- throws {
568
+ class func renameFile (filePath: String, toFilePath: String,
569
+ databaseLocation: String) throws {
423
570
  let fileManager = FileManager.default
424
571
  do {
425
572
  if isFileExist(filePath: toFilePath) {
426
573
  let fileName = URL(
427
574
  fileURLWithPath: toFilePath).lastPathComponent
428
- try _ = deleteFile(fileName: fileName)
575
+ try _ = deleteFile(fileName: fileName,
576
+ databaseLocation: databaseLocation)
429
577
  }
430
578
  try fileManager.moveItem(atPath: filePath,
431
579
  toPath: toFilePath)
@@ -434,6 +582,19 @@ class UtilsFile {
434
582
  throw UtilsFileError.renameFileFailed
435
583
  }
436
584
  }
585
+
586
+ class func setExcludeFromiCloudBackup(_ fileOrDirectoryURL: inout URL, isExcluded: Bool) throws {
587
+ var values = URLResourceValues()
588
+ values.isExcludedFromBackup = isExcluded
589
+ try fileOrDirectoryURL.setResourceValues(values)
590
+ }
591
+
592
+ class func getExcludeFromiCloudBackup(_ fileOrDirectoryURL: URL) throws -> Bool {
593
+ let keySet: Set<URLResourceKey> = [.isExcludedFromBackupKey]
594
+
595
+ return try
596
+ fileOrDirectoryURL.resourceValues(forKeys: keySet).isExcludedFromBackup ?? false
597
+ }
437
598
  }
438
599
  // swiftlint:enable type_body_length
439
600
  // swiftlint:enable file_length
@@ -9,7 +9,6 @@ enum UtilsMigrateError: Error {
9
9
  case addSQLiteSuffix(message: String)
10
10
  case getMigratableList(message: String)
11
11
  case deleteOldDatabases(message: String)
12
- case getFolderURL(message: String)
13
12
  }
14
13
 
15
14
  class UtilsMigrate {
@@ -19,7 +18,7 @@ class UtilsMigrate {
19
18
  class func getMigratableList(folderPath: String) throws -> [String] {
20
19
  var mDbList: [String] = []
21
20
  do {
22
- let dbPathURL: URL = try UtilsMigrate
21
+ let dbPathURL: URL = try UtilsFile
23
22
  .getFolderURL(folderPath: folderPath)
24
23
  var isDir = ObjCBool(true)
25
24
  if FileManager.default.fileExists(atPath: dbPathURL.relativePath,
@@ -34,9 +33,9 @@ class UtilsMigrate {
34
33
  throw UtilsMigrateError.getMigratableList(message: msg)
35
34
  }
36
35
 
37
- } catch UtilsFileError.getDatabasesURLFailed {
36
+ } catch UtilsFileError.getFolderURLFailed {
38
37
  throw UtilsMigrateError
39
- .getMigratableList(message: "getDatabasesURLFailed")
38
+ .getMigratableList(message: "getFolderURLFailed")
40
39
  } catch UtilsFileError.getFileListFailed {
41
40
  throw UtilsMigrateError.getMigratableList(message: "getFileListFailed")
42
41
  } catch let error {
@@ -50,12 +49,14 @@ class UtilsMigrate {
50
49
 
51
50
  // swiftlint:disable function_body_length
52
51
  // swiftlint:disable cyclomatic_complexity
53
- class func addSQLiteSuffix(folderPath: String, dbList: [String]) throws {
52
+ class func addSQLiteSuffix(databaseLocation: String, folderPath: String,
53
+ dbList: [String]) throws {
54
54
  var fromFile: String = ""
55
55
  var toFile: String = ""
56
56
  do {
57
- let databaseURL = try UtilsFile.getDatabasesUrl().absoluteURL
58
- let dbPathURL: URL = try UtilsMigrate
57
+ let databaseURL: URL = try UtilsFile
58
+ .getFolderURL(folderPath: databaseLocation)
59
+ let dbPathURL: URL = try UtilsFile
59
60
  .getFolderURL(folderPath: folderPath)
60
61
  var isDir = ObjCBool(true)
61
62
  if FileManager.default.fileExists(atPath: dbPathURL.relativePath,
@@ -106,7 +107,7 @@ class UtilsMigrate {
106
107
  } catch UtilsFileError.getDatabasesURLFailed {
107
108
  throw UtilsMigrateError
108
109
  .addSQLiteSuffix(message: "getDatabasesURLFailed")
109
- } catch UtilsMigrateError.getFolderURL(let message) {
110
+ } catch UtilsFileError.getFolderURLFailed(let message) {
110
111
  throw UtilsMigrateError.addSQLiteSuffix(message: message)
111
112
  } catch UtilsFileError.getFileListFailed {
112
113
  throw UtilsMigrateError.addSQLiteSuffix(message: "getFileListFailed")
@@ -129,7 +130,7 @@ class UtilsMigrate {
129
130
  // swiftlint:disable cyclomatic_complexity
130
131
  class func deleteOldDatabases(folderPath: String, dbList: [String]) throws {
131
132
  do {
132
- let dbPathURL: URL = try UtilsMigrate
133
+ let dbPathURL: URL = try UtilsFile
133
134
  .getFolderURL(folderPath: folderPath)
134
135
  var isDir = ObjCBool(true)
135
136
  if FileManager.default.fileExists(atPath: dbPathURL.relativePath,
@@ -176,7 +177,7 @@ class UtilsMigrate {
176
177
  } catch UtilsFileError.getDatabasesURLFailed {
177
178
  throw UtilsMigrateError
178
179
  .addSQLiteSuffix(message: "getDatabasesURLFailed")
179
- } catch UtilsMigrateError.getFolderURL(let message) {
180
+ } catch UtilsFileError.getFolderURLFailed(let message) {
180
181
  throw UtilsMigrateError.addSQLiteSuffix(message: message)
181
182
  } catch UtilsFileError.getFileListFailed {
182
183
  throw UtilsMigrateError.addSQLiteSuffix(message: "getFileListFailed")
@@ -191,38 +192,4 @@ class UtilsMigrate {
191
192
  // swiftlint:enable cyclomatic_complexity
192
193
  // swiftlint:enable function_body_length
193
194
 
194
- // MARK: - getFolderURL
195
- class func getFolderURL(folderPath: String) throws -> URL {
196
- do {
197
- let databaseURL = try UtilsFile.getDatabasesUrl().absoluteURL
198
- var dbPathURL: URL
199
- let first = folderPath.split(separator: "/", maxSplits: 1)
200
- if first[0] == "Applications" {
201
- dbPathURL = try UtilsFile.getApplicationURL().absoluteURL
202
- } else if first[0] == "Library" {
203
- dbPathURL = try UtilsFile.getLibraryURL().absoluteURL
204
- } else if first[0] == "Documents" || first[0] == "default" {
205
- dbPathURL = databaseURL
206
- } else {
207
- var msg: String = "addSQLiteSuffix command failed :"
208
- msg.append(" Folder '\(first[0])' not allowed")
209
- throw UtilsMigrateError.getFolderURL(message: msg)
210
- }
211
- if first.count > 1 {
212
- dbPathURL = dbPathURL
213
- .appendingPathComponent(String(first[1])).absoluteURL
214
- }
215
- return dbPathURL
216
- } catch UtilsFileError.getDatabasesURLFailed {
217
- throw UtilsMigrateError.getFolderURL(message: "getDatabasesURLFailed")
218
- } catch UtilsFileError.getApplicationURLFailed {
219
- throw UtilsMigrateError
220
- .getFolderURL(message: "getApplicationURLFailed")
221
- } catch let error {
222
- var msg: String = "getFolderURL command failed :"
223
- msg.append(" \(error.localizedDescription)")
224
- throw UtilsMigrateError.getFolderURL(message: msg)
225
- }
226
- }
227
-
228
195
  }
@@ -16,10 +16,10 @@ class UtilsNCDatabase {
16
16
 
17
17
  class func getNCDatabasePath(folderPath: String, database: String) throws -> String {
18
18
  do {
19
- let dbPathURL: URL = try UtilsMigrate
19
+ let dbPathURL: URL = try UtilsFile
20
20
  .getFolderURL(folderPath: folderPath)
21
21
  return dbPathURL.appendingPathComponent("\(database)").path
22
- } catch UtilsMigrateError.getFolderURL(let message) {
22
+ } catch UtilsFileError.getFolderURLFailed(let message) {
23
23
  throw UtilsNCDatabaseError.getNCDatabasePath(message: message)
24
24
  } catch let error {
25
25
  var msg: String = "getNCDatabasePath command failed :"
@@ -43,9 +43,12 @@ enum State: String {
43
43
  // swiftlint:disable type_body_length
44
44
  class UtilsSQLCipher {
45
45
 
46
- class func getDatabaseState(databaseName: String) -> State {
46
+ class func getDatabaseState(databaseLocation: String,
47
+ databaseName: String) -> State {
47
48
  do {
48
- let path: String = try UtilsFile.getFilePath(fileName: databaseName)
49
+ let path: String = try UtilsFile
50
+ .getFilePath(databaseLocation: databaseLocation,
51
+ fileName: databaseName)
49
52
  if UtilsFile.isFileExist(filePath: path) {
50
53
  do {
51
54
  try openDBNoPassword(dBPath: path)
@@ -586,17 +589,17 @@ class UtilsSQLCipher {
586
589
 
587
590
  // MARK: - DeleteDB
588
591
 
589
- class func deleteDB(databaseName: String) throws {
590
- if let dir = FileManager.default.urls(
591
- for: .documentDirectory,
592
- in: .userDomainMask).first {
592
+ class func deleteDB(databaseLocation: String, databaseName: String) throws {
593
+ do {
594
+ let dir: URL = try UtilsFile
595
+ .getFolderURL(folderPath: databaseLocation)
596
+
593
597
  let fileURL = dir.appendingPathComponent(databaseName)
594
598
  let isFileExists = FileManager.default.fileExists(
595
599
  atPath: fileURL.path)
596
600
  if isFileExists {
597
601
  do {
598
602
  try FileManager.default.removeItem(at: fileURL)
599
- print("Database \(databaseName) deleted")
600
603
  } catch let error {
601
604
  var msg: String = "Error: deleteDB: "
602
605
  msg.append(" \(error.localizedDescription)")
@@ -604,6 +607,9 @@ class UtilsSQLCipher {
604
607
  message: msg)
605
608
  }
606
609
  }
610
+ } catch UtilsFileError.getFolderURLFailed(let message) {
611
+ let msg = "Error: deleteDB: \(message)"
612
+ throw UtilsSQLCipherError.deleteDB(message: msg)
607
613
  }
608
614
  }
609
615
 
@@ -663,10 +669,11 @@ class UtilsSQLCipher {
663
669
 
664
670
  // MARK: - RestoreDB
665
671
 
666
- class func restoreDB(databaseName: String) throws {
667
- if let dir = FileManager.default.urls(
668
- for: .documentDirectory,
669
- in: .userDomainMask).first {
672
+ class func restoreDB(databaseLocation: String, databaseName: String) throws {
673
+ do {
674
+ let dir: URL = try UtilsFile
675
+ .getFolderURL(folderPath: databaseLocation)
676
+
670
677
  let fileURL = dir.appendingPathComponent(databaseName)
671
678
  let backupURL = dir
672
679
  .appendingPathComponent("backup-\(databaseName)")
@@ -678,13 +685,11 @@ class UtilsSQLCipher {
678
685
  if isFileExists {
679
686
  do {
680
687
  try FileManager.default.removeItem(at: fileURL)
681
- print("Database \(databaseName) deleted")
682
688
  try FileManager
683
689
  .default.copyItem(atPath: backupURL.path,
684
690
  toPath: fileURL.path)
685
691
  try FileManager.default
686
692
  .removeItem(at: backupURL)
687
- print("Database backup-\(databaseName) deleted")
688
693
  } catch {
689
694
  var msg = "Error: restoreDB : \(databaseName)"
690
695
  msg += " \(error)"
@@ -704,19 +709,20 @@ class UtilsSQLCipher {
704
709
  throw UtilsSQLCipherError.restoreDB(
705
710
  message: msg)
706
711
  }
707
- } else {
708
- let msg = "Error: restoreDB: FileManager.default.urls"
709
- throw UtilsSQLCipherError.restoreDB(
710
- message: msg)
712
+ } catch UtilsFileError.getFolderURLFailed(let message) {
713
+ let msg = "Error: restoreDB: \(message)"
714
+ throw UtilsSQLCipherError.restoreDB(message: msg)
711
715
  }
712
716
  }
713
717
 
714
718
  // MARK: - deleteBackupDB
715
719
 
716
- class func deleteBackupDB(databaseName: String) throws {
717
- if let dir = FileManager.default.urls(
718
- for: .documentDirectory,
719
- in: .userDomainMask).first {
720
+ class func deleteBackupDB(databaseLocation: String,
721
+ databaseName: String) throws {
722
+
723
+ do {
724
+ let dir: URL = try UtilsFile
725
+ .getFolderURL(folderPath: databaseLocation)
720
726
  let backupURL = dir
721
727
  .appendingPathComponent("backup-\(databaseName)")
722
728
  let isBackupExists = FileManager.default.fileExists(
@@ -725,7 +731,6 @@ class UtilsSQLCipher {
725
731
  do {
726
732
  try FileManager.default
727
733
  .removeItem(at: backupURL)
728
- print("Database backup-\(databaseName) deleted")
729
734
  } catch {
730
735
  var msg = "Error: deleteBackupDB : \(databaseName)"
731
736
  msg += " \(error)"
@@ -737,8 +742,8 @@ class UtilsSQLCipher {
737
742
  msg.append("backup-\(databaseName) does not exist")
738
743
  throw UtilsSQLCipherError.deleteBackupDB(message: msg)
739
744
  }
740
- } else {
741
- let msg = "Error: deleteBackupDB: FileManager.default.urls"
745
+ } catch UtilsFileError.getFolderURLFailed(let message) {
746
+ let msg = "Error: deleteBackupDB: \(message)"
742
747
  throw UtilsSQLCipherError.deleteBackupDB(
743
748
  message: msg)
744
749
  }