@capacitor-community/sqlite 3.3.3-1 → 3.3.3-2

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.
@@ -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,6 +26,9 @@ 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)
28
32
  }
29
33
  // swiftlint:disable file_length
30
34
  // swiftlint:disable type_body_length
@@ -39,6 +43,70 @@ class UtilsFile {
39
43
  return exists && isDir.boolValue
40
44
  }
41
45
 
46
+ class func createDirCopyDB(url: URL) throws {
47
+ do {
48
+ var dirUrl = url
49
+ let dirPath: String = url.path
50
+ if !UtilsFile.isDirExist(dirPath: dirPath) {
51
+ // create the directory
52
+ try FileManager.default
53
+ .createDirectory(at: dirUrl, withIntermediateDirectories: true)
54
+ // exclude the directory from iCloud Backup
55
+ try UtilsFile.setExcludeFromiCloudBackup(&dirUrl,
56
+ isExcluded: true)
57
+ // move all SQLite database from Documents folder
58
+ // to this new directory
59
+ try UtilsFile.moveAllDBSQLite(dirUrl: dirUrl)
60
+ }
61
+ } catch UtilsFileError.getFolderURLFailed(let message) {
62
+ throw UtilsFileError.createDirFailed(message: message)
63
+ } catch UtilsFileError.moveAllDBSQLiteFailed(let message) {
64
+ throw UtilsFileError.createDirFailed(message: message)
65
+ } catch let error {
66
+ var msg: String = "createDir command failed :"
67
+ msg.append(" \(error.localizedDescription)")
68
+ throw UtilsFileError.createDirFailed(message: msg)
69
+ }
70
+
71
+ }
72
+
73
+ // MARK: - moveAllDBSQLite
74
+
75
+ class func moveAllDBSQLite(dirUrl: URL) throws {
76
+ // get the db list from Documents folder
77
+ do {
78
+ let databaseURL: URL = try UtilsFile
79
+ .getDatabasesUrl().absoluteURL
80
+ let fileList: [String] = try UtilsFile
81
+ .getFileList(path: databaseURL.path,
82
+ ext: "SQLite.db")
83
+ for file in fileList {
84
+ let fromFileURL: URL = databaseURL
85
+ .appendingPathComponent(file).absoluteURL
86
+ let toFileURL = dirUrl
87
+ .appendingPathComponent(file).absoluteURL
88
+ let retB: Bool = try UtilsFile
89
+ .moveFile(pathName: fromFileURL.path,
90
+ toPathName: toFileURL.path, overwrite: true)
91
+ if !retB {
92
+ let msg: String = "moveFile command failed :"
93
+ throw UtilsFileError.moveAllDBSQLiteFailed(message: msg)
94
+ }
95
+ }
96
+
97
+ } catch UtilsFileError.getFileListFailed {
98
+ let msg: String = "getFileList command failed :"
99
+ throw UtilsFileError.moveAllDBSQLiteFailed(message: msg)
100
+ } catch UtilsFileError.moveFileFailed {
101
+ let msg: String = "moveFile command failed :"
102
+ throw UtilsFileError.moveAllDBSQLiteFailed(message: msg)
103
+ } catch let error {
104
+ var msg: String = "moveAllDBSQLite command failed :"
105
+ msg.append(" \(error.localizedDescription)")
106
+ throw UtilsFileError.moveAllDBSQLiteFailed(message: msg)
107
+ }
108
+
109
+ }
42
110
  // MARK: - IsFileExist
43
111
 
44
112
  class func isFileExist(filePath: String) -> Bool {
@@ -49,11 +117,12 @@ class UtilsFile {
49
117
  }
50
118
  return ret
51
119
  }
52
- class func isFileExist(fileName: String) -> Bool {
120
+ class func isFileExist(databaseLocation: String, fileName: String) -> Bool {
53
121
  var ret: Bool = false
54
122
  do {
55
123
  let filePath: String =
56
124
  try UtilsFile.getFilePath(
125
+ databaseLocation: databaseLocation,
57
126
  fileName: fileName)
58
127
  ret = UtilsFile.isFileExist(filePath: filePath)
59
128
  return ret
@@ -64,14 +133,58 @@ class UtilsFile {
64
133
  }
65
134
  }
66
135
 
67
- // MARK: - GetFilePath
136
+ // MARK: - getFolderURL
68
137
 
69
- class func getFilePath(fileName: String) throws -> String {
138
+ class func getFolderURL(folderPath: String) throws -> URL {
70
139
  do {
71
- let url = try getDatabasesUrl()
72
- return url.appendingPathComponent("\(fileName)").path
140
+ let databaseURL = try UtilsFile.getDatabasesUrl().absoluteURL
141
+ var dbPathURL: URL
142
+ let first = folderPath.split(separator: "/", maxSplits: 1)
143
+ if first[0] == "Applications" {
144
+ dbPathURL = try UtilsFile.getApplicationURL().absoluteURL
145
+ } else if first[0] == "Library" {
146
+ dbPathURL = try UtilsFile.getLibraryURL().absoluteURL
147
+ } else if first[0] == "Documents" || first[0] == "default" {
148
+ dbPathURL = databaseURL
149
+ } else {
150
+ var msg: String = "getFolderURL command failed :"
151
+ msg.append(" Folder '\(first[0])' not allowed")
152
+ throw UtilsFileError.getFolderURLFailed(message: msg)
153
+ }
154
+ if first.count > 1 {
155
+ dbPathURL = dbPathURL
156
+ .appendingPathComponent(String(first[1])).absoluteURL
157
+ }
158
+ return dbPathURL
73
159
  } catch UtilsFileError.getDatabasesURLFailed {
74
- print("Error: getDatabasesUrl Failed")
160
+ throw UtilsFileError.getFolderURLFailed(message: "getDatabasesURLFailed")
161
+ } catch UtilsFileError.getApplicationURLFailed {
162
+ throw UtilsFileError
163
+ .getFolderURLFailed(message: "getApplicationURLFailed")
164
+ } catch let error {
165
+ var msg: String = "getFolderURL command failed :"
166
+ msg.append(" \(error.localizedDescription)")
167
+ throw UtilsFileError.getFolderURLFailed(message: msg)
168
+ }
169
+ }
170
+
171
+ // MARK: - GetFilePath
172
+
173
+ class func getFilePath(databaseLocation: String,
174
+ fileName: String) throws -> String {
175
+ do {
176
+ let url: URL = try UtilsFile
177
+ .getFolderURL(folderPath: databaseLocation)
178
+ if databaseLocation.prefix(9) != "Documents" &&
179
+ databaseLocation.prefix(7) != "default" {
180
+ // create the directories if they do not exists
181
+ try UtilsFile.createDirCopyDB(url: url)
182
+ }
183
+ let dbPath: String = url
184
+ .appendingPathComponent("\(fileName)").path
185
+ return dbPath
186
+ } catch UtilsFileError.getFolderURLFailed(let message) {
187
+ print("Error: getFilePath Failed \(message)")
75
188
  throw UtilsFileError.getFilePathFailed
76
189
  }
77
190
  }
@@ -157,7 +270,6 @@ class UtilsFile {
157
270
 
158
271
  class func getAssetsDatabasesPath() throws -> URL {
159
272
  if let appFolder = Bundle.main.resourceURL {
160
- print("getAssetsDatabasesPath appFolder \(appFolder)")
161
273
  return appFolder.appendingPathComponent("public/assets/databases")
162
274
  } else {
163
275
  print("Error: getAssetsDatabasePath did not find app folder")
@@ -230,16 +342,16 @@ class UtilsFile {
230
342
 
231
343
  // MARK: - CopyFromAssetToDatabase
232
344
 
233
- class func copyFromAssetToDatabase(fromDb: String, toDb: String,
234
- overwrite: Bool) throws {
345
+ class func copyFromAssetToDatabase(databaseLocation: String, fromDb: String,
346
+ toDb: String, overwrite: Bool) throws {
235
347
  do {
236
348
  let uAsset: URL = try getAssetsDatabasesPath()
237
349
  .appendingPathComponent(fromDb)
238
- let pAsset: String = uAsset.path
239
- let uDb: URL = try getDatabasesUrl()
350
+
351
+ let uDb: URL = try getFolderURL(folderPath: databaseLocation)
240
352
  .appendingPathComponent(toDb)
241
- let pDb: String = uDb.path
242
- let bRet: Bool = try copyFile(pathName: pAsset, toPathName: pDb,
353
+ let bRet: Bool = try copyFile(pathName: uAsset.path,
354
+ toPathName: uDb.path,
243
355
  overwrite: overwrite)
244
356
  if bRet {
245
357
  return
@@ -252,11 +364,9 @@ class UtilsFile {
252
364
  let msg = "Error: getAssetsDatabasesPath Failed"
253
365
  print("\(msg)")
254
366
  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)
367
+ } catch UtilsFileError.getFolderURLFailed(let message) {
368
+ print("Error: getFolderUrl Failed \(message)")
369
+ throw UtilsFileError.copyFromAssetToDatabaseFailed(message: message)
260
370
  } catch UtilsFileError.copyFileFailed {
261
371
  let msg = "Error: copyFile Failed"
262
372
  print("\(msg)")
@@ -270,7 +380,8 @@ class UtilsFile {
270
380
 
271
381
  }
272
382
 
273
- class func unzipFromAssetToDatabase(zip: String, overwrite: Bool) throws {
383
+ class func unzipFromAssetToDatabase(databaseLocation: String, zip: String,
384
+ overwrite: Bool) throws {
274
385
  do {
275
386
  let zipAsset: URL = try getAssetsDatabasesPath()
276
387
  .appendingPathComponent(zip)
@@ -279,10 +390,10 @@ class UtilsFile {
279
390
  print("\(msg)")
280
391
  throw UtilsFileError.unzipFromAssetToDatabaseFailed(message: msg)
281
392
  }
393
+ let uDb: URL = try getFolderURL(folderPath: databaseLocation)
282
394
  for entry in archive {
283
395
  let dbEntry = setPathSuffix(sDb: entry.path)
284
- let zipCopy: URL = try getDatabasesUrl()
285
- .appendingPathComponent(dbEntry)
396
+ let zipCopy: URL = uDb.appendingPathComponent(dbEntry)
286
397
  do {
287
398
  let isExist: Bool = isFileExist(filePath: zipCopy.path)
288
399
  if !isExist || overwrite {
@@ -302,10 +413,9 @@ class UtilsFile {
302
413
  let msg = "Error: getAssetsDatabasesPath Failed"
303
414
  print("\(msg)")
304
415
  throw UtilsFileError.unzipFromAssetToDatabaseFailed(message: msg)
305
- } catch UtilsFileError.getDatabasesURLFailed {
306
- let msg = "Error: getDatabasesUrl Failed"
307
- print("\(msg)")
308
- throw UtilsFileError.unzipFromAssetToDatabaseFailed(message: msg)
416
+ } catch UtilsFileError.getFolderURLFailed(let message) {
417
+ print("Error: getFolderUrl Failed \(message)")
418
+ throw UtilsFileError.unzipFromAssetToDatabaseFailed(message: message)
309
419
  } catch let error {
310
420
  let msg = "Error: \(error)"
311
421
  print("\(msg)")
@@ -313,6 +423,36 @@ class UtilsFile {
313
423
  }
314
424
  }
315
425
 
426
+ // MARK: - MoveFile
427
+
428
+ class func moveFile(pathName: String, toPathName: String, overwrite: Bool) throws -> Bool {
429
+ if pathName.count > 0 && toPathName.count > 0 {
430
+ let isPath = isFileExist(filePath: pathName)
431
+ if isPath {
432
+ do {
433
+ let isExist: Bool = isFileExist(filePath: toPathName)
434
+ if !isExist || overwrite {
435
+ if overwrite && isExist {
436
+ _ = try deleteFile(filePath: toPathName)
437
+ }
438
+ let fileManager = FileManager.default
439
+ try fileManager.moveItem(atPath: pathName,
440
+ toPath: toPathName)
441
+ }
442
+ return true
443
+ } catch let error {
444
+ print("Error: \(error)")
445
+ throw UtilsFileError.moveFileFailed
446
+ }
447
+ } else {
448
+ print("Error: MoveFile Failed pathName does not exist")
449
+ throw UtilsFileError.moveFileFailed
450
+ }
451
+ } else {
452
+ print("Error: MoveFile Failed paths count = 0")
453
+ throw UtilsFileError.moveFileFailed
454
+ }
455
+ }
316
456
  // MARK: - CopyFile
317
457
 
318
458
  class func copyFile(pathName: String, toPathName: String, overwrite: Bool) throws -> Bool {
@@ -346,12 +486,17 @@ class UtilsFile {
346
486
 
347
487
  // MARK: - CopyFile
348
488
 
349
- class func copyFile(fileName: String, toFileName: String)
489
+ class func copyFile(fileName: String, toFileName: String,
490
+ databaseLocation: String)
350
491
  throws -> Bool {
351
492
  var ret: Bool = false
352
493
  do {
353
- let fromPath: String = try getFilePath(fileName: fileName)
354
- let toPath: String = try getFilePath(fileName: toFileName)
494
+ let fromPath: String = try
495
+ getFilePath(databaseLocation: databaseLocation,
496
+ fileName: fileName)
497
+ let toPath: String = try
498
+ getFilePath(databaseLocation: databaseLocation,
499
+ fileName: toFileName)
355
500
  ret = try copyFile(pathName: fromPath, toPathName: toPath, overwrite: true)
356
501
  return ret
357
502
  } catch UtilsFileError.getFilePathFailed {
@@ -387,10 +532,13 @@ class UtilsFile {
387
532
 
388
533
  // MARK: - DeleteFile
389
534
 
390
- class func deleteFile(fileName: String) throws -> Bool {
535
+ class func deleteFile(fileName: String,
536
+ databaseLocation: String) throws -> Bool {
391
537
  var ret: Bool = false
392
538
  do {
393
- let filePath: String = try getFilePath(fileName: fileName)
539
+ let filePath: String = try
540
+ getFilePath(databaseLocation: databaseLocation,
541
+ fileName: fileName)
394
542
  ret = try deleteFile(filePath: filePath)
395
543
  } catch let error {
396
544
  print("Error: \(error)")
@@ -418,14 +566,15 @@ class UtilsFile {
418
566
 
419
567
  // MARK: - RenameFile
420
568
 
421
- class func renameFile (filePath: String, toFilePath: String)
422
- throws {
569
+ class func renameFile (filePath: String, toFilePath: String,
570
+ databaseLocation: String) throws {
423
571
  let fileManager = FileManager.default
424
572
  do {
425
573
  if isFileExist(filePath: toFilePath) {
426
574
  let fileName = URL(
427
575
  fileURLWithPath: toFilePath).lastPathComponent
428
- try _ = deleteFile(fileName: fileName)
576
+ try _ = deleteFile(fileName: fileName,
577
+ databaseLocation: databaseLocation)
429
578
  }
430
579
  try fileManager.moveItem(atPath: filePath,
431
580
  toPath: toFilePath)
@@ -434,6 +583,19 @@ class UtilsFile {
434
583
  throw UtilsFileError.renameFileFailed
435
584
  }
436
585
  }
586
+
587
+ class func setExcludeFromiCloudBackup(_ fileOrDirectoryURL: inout URL, isExcluded: Bool) throws {
588
+ var values = URLResourceValues()
589
+ values.isExcludedFromBackup = isExcluded
590
+ try fileOrDirectoryURL.setResourceValues(values)
591
+ }
592
+
593
+ class func getExcludeFromiCloudBackup(_ fileOrDirectoryURL: URL) throws -> Bool {
594
+ let keySet: Set<URLResourceKey> = [.isExcludedFromBackupKey]
595
+
596
+ return try
597
+ fileOrDirectoryURL.resourceValues(forKeys: keySet).isExcludedFromBackup ?? false
598
+ }
437
599
  }
438
600
  // swiftlint:enable type_body_length
439
601
  // 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
  }