@capacitor-community/sqlite 5.2.3 → 5.2.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.
@@ -112,52 +112,50 @@ enum CapacitorSQLiteError: Error {
112
112
  // MARK: - IsSecretStored
113
113
 
114
114
  @objc public func isSecretStored() throws -> NSNumber {
115
- if isInit {
116
- if isEncryption {
117
- do {
118
- let isSecretExists: Bool = try UtilsSecret.isPassphrase(
119
- account: account)
120
- if isSecretExists {
121
- return 1
122
- } else {
123
- return 0
124
- }
125
- } catch UtilsSecretError.prefixPassphrase(let message) {
126
- throw CapacitorSQLiteError.failed(message: message)
127
- }
115
+ guard isInit else {
116
+ throw CapacitorSQLiteError.failed(message: initMessage)
117
+ }
118
+
119
+ guard isEncryption else {
120
+ throw CapacitorSQLiteError.failed(message: "No Encryption set in capacitor.config")
121
+ }
122
+ do {
123
+ let isSecretExists: Bool = try UtilsSecret.isPassphrase(
124
+ account: account)
125
+ if isSecretExists {
126
+ return 1
128
127
  } else {
129
- throw CapacitorSQLiteError.failed(message: "No Encryption set in capacitor.config")
128
+ return 0
130
129
  }
131
- } else {
132
- throw CapacitorSQLiteError.failed(message: initMessage)
130
+ } catch UtilsSecretError.prefixPassphrase(let message) {
131
+ throw CapacitorSQLiteError.failed(message: message)
133
132
  }
134
133
  }
135
134
 
136
135
  // MARK: - SetEncryptionSecret
137
136
 
138
137
  @objc public func setEncryptionSecret(passphrase: String) throws {
139
- if isInit {
140
- if isEncryption {
141
- do {
142
- // close all connections
143
- try closeAllConnections()
144
- // set encryption secret
145
- try UtilsSecret
146
- .setEncryptionSecret(prefix: prefixKeychain,
147
- passphrase: passphrase,
148
- databaseLocation: databaseLocation)
149
- return
150
- } catch UtilsSecretError.setEncryptionSecret(let message) {
151
- throw CapacitorSQLiteError.failed(message: message)
152
- } catch let error {
153
- throw CapacitorSQLiteError.failed(message: "\(error)")
154
- }
155
- } else {
156
- throw CapacitorSQLiteError.failed(message: "No Encryption set in capacitor.config")
157
- }
158
- } else {
138
+ guard isInit else {
159
139
  throw CapacitorSQLiteError.failed(message: initMessage)
160
140
  }
141
+
142
+ guard isEncryption else {
143
+ throw CapacitorSQLiteError.failed(message: "No Encryption set in capacitor.config")
144
+ }
145
+ do {
146
+ // close all connections
147
+ try closeAllConnections()
148
+ // set encryption secret
149
+ try UtilsSecret
150
+ .setEncryptionSecret(prefix: prefixKeychain,
151
+ passphrase: passphrase,
152
+ databaseLocation: databaseLocation)
153
+ return
154
+ } catch UtilsSecretError.setEncryptionSecret(let message) {
155
+ throw CapacitorSQLiteError.failed(message: message)
156
+ } catch let error {
157
+ throw CapacitorSQLiteError.failed(message: "\(error)")
158
+ }
161
159
  }
162
160
 
163
161
  // MARK: - ChangeEncryptionSecret
@@ -166,68 +164,59 @@ enum CapacitorSQLiteError: Error {
166
164
  // swiftlint:disable no_space_in_method_call
167
165
  @objc public func changeEncryptionSecret(call: CAPPluginCall, passphrase: String,
168
166
  oldPassphrase: String) throws {
169
- if isInit {
170
- if isEncryption {
171
- self.call = call
172
- let retHandler: ReturnHandler = ReturnHandler()
173
- do {
174
- // close all connections
175
- try closeAllConnections()
176
- if isBiometricAuth {
177
- let dbLocation = databaseLocation
178
- let mPrefixKeychain = prefixKeychain
179
- bioIdAuth.authenticateUser { [weak self] message in
180
- if let message = message {
181
- self?.intBioMessage = message
182
- return
183
- } else {
184
- do {
185
- try UtilsSecret.changeEncryptionSecret(
186
- prefix: mPrefixKeychain,
187
- passphrase: passphrase,
188
- oldPassphrase: oldPassphrase,
189
- databaseLocation: dbLocation)
190
- retHandler.rResult(call: call)
191
- return
192
- } catch UtilsSecretError.changeEncryptionSecret(let message) {
193
- let msg = "ChangeEncryptionSecret: \(message)"
194
- retHandler.rResult(call: call, message: msg)
195
- return
196
- } catch let error {
197
- retHandler.rResult(
198
- call: call,
199
- message: "ChangeEncryptionSecret: \(error.localizedDescription)")
200
- return
201
- }
202
- }
203
- }
167
+ guard isInit else {
168
+ throw CapacitorSQLiteError.failed(message: initMessage)
169
+ }
170
+
171
+ guard isEncryption else {
172
+ throw CapacitorSQLiteError.failed(message: "No Encryption set in capacitor.config")
173
+ }
174
+ self.call = call
175
+ let retHandler: ReturnHandler = ReturnHandler()
176
+ do {
177
+ // close all connections
178
+ try closeAllConnections()
179
+ if isBiometricAuth {
180
+ let dbLocation = databaseLocation
181
+ let mPrefixKeychain = prefixKeychain
182
+ bioIdAuth.authenticateUser { [weak self] message in
183
+ if let message = message {
184
+ self?.intBioMessage = message
204
185
  } else {
205
- // set encryption secret
206
- try UtilsSecret
207
- .changeEncryptionSecret(prefix: prefixKeychain,
208
- passphrase: passphrase,
209
- oldPassphrase: oldPassphrase,
210
- databaseLocation: databaseLocation)
211
- retHandler.rResult(call: call)
212
- return
186
+ do {
187
+ try UtilsSecret.changeEncryptionSecret(
188
+ prefix: mPrefixKeychain,
189
+ passphrase: passphrase,
190
+ oldPassphrase: oldPassphrase,
191
+ databaseLocation: dbLocation)
192
+ retHandler.rResult(call: call)
193
+ } catch UtilsSecretError.changeEncryptionSecret(let message) {
194
+ let msg = "ChangeEncryptionSecret: \(message)"
195
+ retHandler.rResult(call: call, message: msg)
196
+ } catch let error {
197
+ retHandler.rResult(
198
+ call: call,
199
+ message: "ChangeEncryptionSecret: \(error.localizedDescription)")
200
+ }
213
201
  }
214
- } catch UtilsSecretError.changeEncryptionSecret(let message) {
215
- let msg = "ChangeEncryptionSecret: \(message)"
216
- retHandler.rResult(call: call, message: msg)
217
- return
218
- } catch let error {
219
- retHandler.rResult(
220
- call: call,
221
- message: "ChangeEncryptionSecret: \(error.localizedDescription)")
222
- return
223
202
  }
224
203
  } else {
225
- throw CapacitorSQLiteError.failed(message: "No Encryption set in capacitor.config")
226
- }
227
- } else {
228
- throw CapacitorSQLiteError.failed(message: initMessage)
204
+ // set encryption secret
205
+ try UtilsSecret
206
+ .changeEncryptionSecret(prefix: prefixKeychain,
207
+ passphrase: passphrase,
208
+ oldPassphrase: oldPassphrase,
209
+ databaseLocation: databaseLocation)
210
+ retHandler.rResult(call: call)
211
+ }
212
+ } catch UtilsSecretError.changeEncryptionSecret(let message) {
213
+ let msg = "ChangeEncryptionSecret: \(message)"
214
+ retHandler.rResult(call: call, message: msg)
215
+ } catch let error {
216
+ retHandler.rResult(
217
+ call: call,
218
+ message: "ChangeEncryptionSecret: \(error.localizedDescription)")
229
219
  }
230
-
231
220
  }
232
221
  // swiftlint:enable no_space_in_method_call
233
222
  // swiftlint:enable function_body_length
@@ -235,130 +224,123 @@ enum CapacitorSQLiteError: Error {
235
224
  // MARK: - ClearEncryptionSecret
236
225
 
237
226
  @objc public func clearEncryptionSecret() throws {
238
- if isInit {
239
- if isEncryption {
240
- do {
241
- // close all connections
242
- try closeAllConnections()
243
- // set encryption secret
244
- try UtilsSecret
245
- .clearEncryptionSecret(prefix: prefixKeychain,
246
- databaseLocation: databaseLocation)
247
- return
248
- } catch UtilsSecretError.clearEncryptionSecret(let message) {
249
- throw CapacitorSQLiteError.failed(message: message)
250
- } catch let error {
251
- throw CapacitorSQLiteError.failed(message: "\(error)")
252
- }
253
- } else {
254
- throw CapacitorSQLiteError.failed(message: "No Encryption set in capacitor.config")
255
- }
256
- } else {
227
+ guard isInit else {
257
228
  throw CapacitorSQLiteError.failed(message: initMessage)
258
229
  }
230
+ guard isEncryption else {
231
+ throw CapacitorSQLiteError.failed(message: "No Encryption set in capacitor.config")
232
+ }
233
+ do {
234
+ // close all connections
235
+ try closeAllConnections()
236
+ // set encryption secret
237
+ try UtilsSecret
238
+ .clearEncryptionSecret(prefix: prefixKeychain,
239
+ databaseLocation: databaseLocation)
240
+ return
241
+ } catch UtilsSecretError.clearEncryptionSecret(let message) {
242
+ throw CapacitorSQLiteError.failed(message: message)
243
+ } catch let error {
244
+ throw CapacitorSQLiteError.failed(message: "\(error)")
245
+ }
259
246
  }
260
247
 
261
248
  // MARK: - CheckEncryptionSecret
262
249
 
263
250
  @objc public func checkEncryptionSecret(passphrase: String) throws -> NSNumber {
264
- if isInit {
265
- if isEncryption {
266
- do {
267
- // close all connections
268
- try closeAllConnections()
269
- // check encryption secret
270
- let res: NSNumber = try UtilsSecret
271
- .checkEncryptionSecret(prefix: prefixKeychain,
272
- passphrase: passphrase)
273
- return res
274
- } catch UtilsSecretError.checkEncryptionSecret(let message) {
275
- throw CapacitorSQLiteError.failed(message: message)
276
- } catch let error {
277
- throw CapacitorSQLiteError.failed(message: "\(error)")
278
- }
279
- } else {
280
- throw CapacitorSQLiteError.failed(message: "No Encryption set in capacitor.config")
281
- }
282
- } else {
251
+ guard isInit else {
283
252
  throw CapacitorSQLiteError.failed(message: initMessage)
284
253
  }
254
+
255
+ guard isEncryption else {
256
+ throw CapacitorSQLiteError.failed(message: "No Encryption set in capacitor.config")
257
+ }
258
+ do {
259
+ // close all connections
260
+ try closeAllConnections()
261
+ // check encryption secret
262
+ let res: NSNumber = try UtilsSecret
263
+ .checkEncryptionSecret(prefix: prefixKeychain,
264
+ passphrase: passphrase)
265
+ return res
266
+ } catch UtilsSecretError.checkEncryptionSecret(let message) {
267
+ throw CapacitorSQLiteError.failed(message: message)
268
+ } catch let error {
269
+ throw CapacitorSQLiteError.failed(message: "\(error)")
270
+ }
285
271
  }
286
272
 
287
273
  // MARK: - getNCDatabasePath
288
274
 
289
275
  @objc public func getNCDatabasePath(_ folderPath: String, dbName: String ) throws -> String {
290
- if isInit {
291
- do {
292
- let databasePath: String = try UtilsNCDatabase
293
- .getNCDatabasePath(folderPath: folderPath,
294
- database: dbName )
295
- return databasePath
296
- } catch let error {
297
- throw CapacitorSQLiteError.failed(message: "\(error)")
298
- }
299
- } else {
276
+ guard isInit else {
300
277
  throw CapacitorSQLiteError.failed(message: initMessage)
301
278
  }
279
+ do {
280
+ let databasePath: String = try UtilsNCDatabase
281
+ .getNCDatabasePath(folderPath: folderPath,
282
+ database: dbName )
283
+ return databasePath
284
+ } catch let error {
285
+ throw CapacitorSQLiteError.failed(message: "\(error)")
286
+ }
302
287
  }
303
288
 
304
289
  // MARK: - CreateNCConnection
305
290
 
306
291
  @objc public func createNCConnection(_ databasePath: String,
307
292
  version: Int) throws {
308
- if isInit {
309
-
310
- // check if the connection already exists
311
- let connName: String = "RO_\(databasePath)"
312
- let conn = dbDict[connName]
313
- if conn != nil {
314
- let msg = "Connection \(databasePath) already exists"
315
- throw CapacitorSQLiteError.failed(message: msg)
316
- }
293
+ guard isInit else {
294
+ throw CapacitorSQLiteError.failed(message: initMessage)
295
+ }
296
+ // check if the connection already exists
297
+ let connName: String = "RO_\(databasePath)"
298
+ let conn = dbDict[connName]
299
+ if conn != nil {
300
+ let msg = "Connection \(databasePath) already exists"
301
+ throw CapacitorSQLiteError.failed(message: msg)
302
+ }
317
303
 
318
- do {
319
- let isFileExists: Bool = UtilsFile
320
- .isFileExist(filePath: databasePath)
304
+ do {
305
+ let isFileExists: Bool = UtilsFile
306
+ .isFileExist(filePath: databasePath)
321
307
 
322
- if !isFileExists {
323
- throw CapacitorSQLiteError.failed(message: "database \(databasePath) does not exist")
324
- }
325
- let mDb: Database = try Database(
326
- databaseLocation: databaseLocation,
327
- databaseName: databasePath,
328
- encrypted: false, isEncryption: isEncryption, account: account,
329
- mode: "no-encryption", version: version, readonly: true,
330
- vUpgDict: [:])
331
- dbDict[connName] = mDb
332
- return
333
- } catch let error {
334
- throw CapacitorSQLiteError.failed(message: "\(error)")
308
+ if !isFileExists {
309
+ throw CapacitorSQLiteError.failed(message: "database \(databasePath) does not exist")
335
310
  }
336
- } else {
337
- throw CapacitorSQLiteError.failed(message: initMessage)
311
+ let mDb: Database = try Database(
312
+ databaseLocation: databaseLocation,
313
+ databaseName: databasePath,
314
+ encrypted: false, isEncryption: isEncryption, account: account,
315
+ mode: "no-encryption", version: version, readonly: true,
316
+ vUpgDict: [:])
317
+ dbDict[connName] = mDb
318
+ return
319
+ } catch let error {
320
+ throw CapacitorSQLiteError.failed(message: "\(error)")
338
321
  }
339
322
  }
340
323
 
341
324
  // MARK: - CloseNCConnection
342
325
 
343
326
  @objc public func closeNCConnection(_ dbName: String) throws {
344
- if isInit {
345
- let connName: String = "RO_\(dbName)"
346
- guard let mDb: Database = dbDict[connName] else {
347
- let msg = "Connection to \(dbName) not available"
348
- throw CapacitorSQLiteError.failed(message: msg)
349
- }
350
- if mDb.isDBOpen() {
351
- do {
352
- try mDb.close()
353
- } catch DatabaseError.close(let message) {
354
- throw CapacitorSQLiteError.failed(message: message)
355
- }
356
- }
357
- dbDict.removeValue(forKey: connName)
358
- return
359
- } else {
327
+ guard isInit else {
360
328
  throw CapacitorSQLiteError.failed(message: initMessage)
361
329
  }
330
+ let connName: String = "RO_\(dbName)"
331
+ guard let mDb: Database = dbDict[connName] else {
332
+ let msg = "Connection to \(dbName) not available"
333
+ throw CapacitorSQLiteError.failed(message: msg)
334
+ }
335
+ if mDb.isDBOpen() {
336
+ do {
337
+ try mDb.close()
338
+ } catch DatabaseError.close(let message) {
339
+ throw CapacitorSQLiteError.failed(message: message)
340
+ }
341
+ }
342
+ dbDict.removeValue(forKey: connName)
343
+ return
362
344
  }
363
345
 
364
346
  // MARK: - CreateConnection
@@ -370,287 +352,275 @@ enum CapacitorSQLiteError: Error {
370
352
  version: Int,
371
353
  vUpgDict: [Int: [String: Any]],
372
354
  readonly: Bool) throws {
373
- if isInit {
374
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
375
- // check if the connection already exists
376
- let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
377
- let conn = dbDict[connName]
378
- if conn != nil {
379
- let msg = "Connection \(mDbName) already exists"
380
- throw CapacitorSQLiteError.failed(message: msg)
381
- }
382
- if encrypted && !isEncryption {
383
- let msg = "Database cannot be encrypted as 'No Encryption' set in capacitor.config"
384
- throw CapacitorSQLiteError.failed(message: msg)
385
- }
386
- do {
387
- let mDb: Database = try Database(
388
- databaseLocation: databaseLocation,
389
- databaseName: "\(mDbName)SQLite.db",
390
- encrypted: encrypted, isEncryption: isEncryption, account: account,
391
- mode: mode, version: version, readonly: readonly,
392
- vUpgDict: vUpgDict)
393
-
394
- dbDict[connName] = mDb
395
- return
396
- } catch let error {
397
- throw CapacitorSQLiteError.failed(message: "\(error)")
398
- }
399
- } else {
355
+ guard isInit else {
400
356
  throw CapacitorSQLiteError.failed(message: initMessage)
401
357
  }
358
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
359
+ // check if the connection already exists
360
+ let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
361
+ let conn = dbDict[connName]
362
+ if conn != nil {
363
+ let msg = "Connection \(mDbName) already exists"
364
+ throw CapacitorSQLiteError.failed(message: msg)
365
+ }
366
+ if encrypted && !isEncryption {
367
+ let msg = "Database cannot be encrypted as 'No Encryption' set in capacitor.config"
368
+ throw CapacitorSQLiteError.failed(message: msg)
369
+ }
370
+ do {
371
+ let mDb: Database = try Database(
372
+ databaseLocation: databaseLocation,
373
+ databaseName: "\(mDbName)SQLite.db",
374
+ encrypted: encrypted, isEncryption: isEncryption, account: account,
375
+ mode: mode, version: version, readonly: readonly,
376
+ vUpgDict: vUpgDict)
377
+
378
+ dbDict[connName] = mDb
379
+ return
380
+ } catch let error {
381
+ throw CapacitorSQLiteError.failed(message: "\(error)")
382
+ }
402
383
  }
403
384
  // swiftlint:enable function_parameter_count
404
385
 
405
386
  // MARK: - Open
406
387
 
407
388
  @objc public func open(_ dbName: String, readonly: Bool) throws {
408
- if isInit {
409
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
410
- let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
411
- guard let mDb: Database = dbDict[connName] else {
412
- let msg = "Connection to \(mDbName) not available"
413
- throw CapacitorSQLiteError.failed(message: msg)
414
- }
415
- do {
416
- try mDb.open()
417
- return
418
- } catch DatabaseError.open(let message) {
419
- throw CapacitorSQLiteError.failed(message: message)
420
- }
421
- } else {
389
+ guard isInit else {
422
390
  throw CapacitorSQLiteError.failed(message: initMessage)
423
391
  }
392
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
393
+ let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
394
+ guard let mDb: Database = dbDict[connName] else {
395
+ let msg = "Connection to \(mDbName) not available"
396
+ throw CapacitorSQLiteError.failed(message: msg)
397
+ }
398
+ do {
399
+ try mDb.open()
400
+ return
401
+ } catch DatabaseError.open(let message) {
402
+ throw CapacitorSQLiteError.failed(message: message)
403
+ }
424
404
  }
425
405
 
426
406
  // MARK: - Close
427
407
 
428
408
  @objc public func close(_ dbName: String, readonly: Bool) throws {
429
- if isInit {
430
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
431
- let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
432
- guard let mDb: Database = dbDict[connName] else {
433
- let msg = "Connection to \(mDbName) not available"
434
- throw CapacitorSQLiteError.failed(message: msg)
435
- }
436
- do {
437
- try mDb.close()
438
- return
439
- } catch DatabaseError.close(let message) {
440
- throw CapacitorSQLiteError.failed(message: message)
441
- }
442
- } else {
409
+ guard isInit else {
443
410
  throw CapacitorSQLiteError.failed(message: initMessage)
444
411
  }
412
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
413
+ let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
414
+ guard let mDb: Database = dbDict[connName] else {
415
+ let msg = "Connection to \(mDbName) not available"
416
+ throw CapacitorSQLiteError.failed(message: msg)
417
+ }
418
+ do {
419
+ try mDb.close()
420
+ return
421
+ } catch DatabaseError.close(let message) {
422
+ throw CapacitorSQLiteError.failed(message: message)
423
+ }
445
424
  }
446
-
425
+
447
426
  // MARK: - BeginTransaction
448
427
 
449
428
  @objc public func beginTransaction(_ dbName: String) throws -> [String: Any] {
450
- if isInit {
451
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
452
- let connName: String = "RW_\(mDbName)"
453
- guard let mDb: Database = dbDict[connName] else {
454
- let msg = "Connection to \(mDbName) not available"
455
- throw CapacitorSQLiteError.failed(message: msg)
456
- }
457
- if !mDb.isNCDB() && mDb.isDBOpen() {
458
- do {
459
- let res = try mDb.beginTransaction()
460
- return ["changes": res]
461
- } catch DatabaseError.beginTransaction(let message) {
462
- throw CapacitorSQLiteError.failed(message: message)
463
- } catch let error {
464
- let msg: String = "\(error)"
465
- throw CapacitorSQLiteError.failed(message: msg)
466
- }
467
- } else {
468
- let msg = "Database \(mDbName) not opened or in read-only"
429
+ guard isInit else {
430
+ throw CapacitorSQLiteError.failed(message: initMessage)
431
+ }
432
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
433
+ let connName: String = "RW_\(mDbName)"
434
+ guard let mDb: Database = dbDict[connName] else {
435
+ let msg = "Connection to \(mDbName) not available"
436
+ throw CapacitorSQLiteError.failed(message: msg)
437
+ }
438
+ if !mDb.isNCDB() && mDb.isDBOpen() {
439
+ do {
440
+ let res = try mDb.beginTransaction()
441
+ return ["changes": res]
442
+ } catch DatabaseError.beginTransaction(let message) {
443
+ throw CapacitorSQLiteError.failed(message: message)
444
+ } catch let error {
445
+ let msg: String = "\(error)"
469
446
  throw CapacitorSQLiteError.failed(message: msg)
470
447
  }
471
448
  } else {
472
- throw CapacitorSQLiteError.failed(message: initMessage)
449
+ let msg = "Database \(mDbName) not opened or in read-only"
450
+ throw CapacitorSQLiteError.failed(message: msg)
473
451
  }
474
452
  }
475
-
453
+
476
454
  // MARK: - CommitTransaction
477
455
 
478
456
  @objc public func commitTransaction(_ dbName: String) throws -> [String: Any] {
479
- if isInit {
480
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
481
- let connName: String = "RW_\(mDbName)"
482
- guard let mDb: Database = dbDict[connName] else {
483
- let msg = "Connection to \(mDbName) not available"
484
- throw CapacitorSQLiteError.failed(message: msg)
485
- }
486
- if !mDb.isNCDB() && mDb.isDBOpen() {
487
- do {
488
- let res = try mDb.commitTransaction()
489
- return ["changes": res]
490
- } catch DatabaseError.commitTransaction(let message) {
491
- throw CapacitorSQLiteError.failed(message: message)
492
- } catch let error {
493
- let msg: String = "\(error)"
494
- throw CapacitorSQLiteError.failed(message: msg)
495
- }
496
- } else {
497
- let msg = "Database \(mDbName) not opened or in read-only"
457
+ guard isInit else {
458
+ throw CapacitorSQLiteError.failed(message: initMessage)
459
+ }
460
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
461
+ let connName: String = "RW_\(mDbName)"
462
+ guard let mDb: Database = dbDict[connName] else {
463
+ let msg = "Connection to \(mDbName) not available"
464
+ throw CapacitorSQLiteError.failed(message: msg)
465
+ }
466
+ if !mDb.isNCDB() && mDb.isDBOpen() {
467
+ do {
468
+ let res = try mDb.commitTransaction()
469
+ return ["changes": res]
470
+ } catch DatabaseError.commitTransaction(let message) {
471
+ throw CapacitorSQLiteError.failed(message: message)
472
+ } catch let error {
473
+ let msg: String = "\(error)"
498
474
  throw CapacitorSQLiteError.failed(message: msg)
499
475
  }
500
476
  } else {
501
- throw CapacitorSQLiteError.failed(message: initMessage)
477
+ let msg = "Database \(mDbName) not opened or in read-only"
478
+ throw CapacitorSQLiteError.failed(message: msg)
502
479
  }
503
480
  }
504
-
481
+
505
482
  // MARK: - RollbackTransaction
506
483
 
507
484
  @objc public func rollbackTransaction(_ dbName: String)
508
- throws -> [String: Any] {
509
- if isInit {
510
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
511
- let connName: String = "RW_\(mDbName)"
512
- guard let mDb: Database = dbDict[connName] else {
513
- let msg = "Connection to \(mDbName) not available"
514
- throw CapacitorSQLiteError.failed(message: msg)
515
- }
516
- if !mDb.isNCDB() && mDb.isDBOpen() {
517
- do {
518
- let res = try mDb.rollbackTransaction()
519
- return ["changes": res]
520
- } catch DatabaseError.rollbackTransaction(let message) {
521
- throw CapacitorSQLiteError.failed(message: message)
522
- } catch let error {
523
- let msg: String = "\(error)"
524
- throw CapacitorSQLiteError.failed(message: msg)
525
- }
526
- } else {
527
- let msg = "Database \(mDbName) not opened or in read-only"
485
+ throws -> [String: Any] {
486
+ guard isInit else {
487
+ throw CapacitorSQLiteError.failed(message: initMessage)
488
+ }
489
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
490
+ let connName: String = "RW_\(mDbName)"
491
+ guard let mDb: Database = dbDict[connName] else {
492
+ let msg = "Connection to \(mDbName) not available"
493
+ throw CapacitorSQLiteError.failed(message: msg)
494
+ }
495
+ if !mDb.isNCDB() && mDb.isDBOpen() {
496
+ do {
497
+ let res = try mDb.rollbackTransaction()
498
+ return ["changes": res]
499
+ } catch DatabaseError.rollbackTransaction(let message) {
500
+ throw CapacitorSQLiteError.failed(message: message)
501
+ } catch let error {
502
+ let msg: String = "\(error)"
528
503
  throw CapacitorSQLiteError.failed(message: msg)
529
504
  }
530
505
  } else {
531
- throw CapacitorSQLiteError.failed(message: initMessage)
506
+ let msg = "Database \(mDbName) not opened or in read-only"
507
+ throw CapacitorSQLiteError.failed(message: msg)
532
508
  }
533
509
  }
534
510
 
535
511
  // MARK: - IsTransactionActive
536
512
 
537
513
  @objc public func isTransactionActive(_ dbName: String) throws -> NSNumber {
538
- if isInit {
539
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
540
- let connName: String = "RW_\(mDbName)"
541
- guard let mDb: Database = dbDict[connName] else {
542
- let msg = "Connection to \(mDbName) not available"
543
- throw CapacitorSQLiteError.failed(message: msg)
544
- }
545
- if !mDb.isNCDB() && mDb.isDBOpen() {
514
+ guard isInit else {
515
+ throw CapacitorSQLiteError.failed(message: initMessage)
516
+ }
517
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
518
+ let connName: String = "RW_\(mDbName)"
519
+ guard let mDb: Database = dbDict[connName] else {
520
+ let msg = "Connection to \(mDbName) not available"
521
+ throw CapacitorSQLiteError.failed(message: msg)
522
+ }
523
+ if !mDb.isNCDB() && mDb.isDBOpen() {
546
524
 
547
- do {
548
- let isAvail = try mDb.isAvailTrans()
549
- if isAvail {
550
- return 1
551
- } else {
552
- return 0
553
- }
554
- } catch DatabaseError.isAvailTrans(let message) {
555
- throw CapacitorSQLiteError.failed(message: message)
525
+ do {
526
+ let isAvail = try mDb.isAvailTrans()
527
+ if isAvail {
528
+ return 1
529
+ } else {
530
+ return 0
556
531
  }
557
- } else {
558
- let msg = "Database \(mDbName) not opened or in read-only"
559
- throw CapacitorSQLiteError.failed(message: msg)
532
+ } catch DatabaseError.isAvailTrans(let message) {
533
+ throw CapacitorSQLiteError.failed(message: message)
560
534
  }
561
535
  } else {
562
- throw CapacitorSQLiteError.failed(message: initMessage)
536
+ let msg = "Database \(mDbName) not opened or in read-only"
537
+ throw CapacitorSQLiteError.failed(message: msg)
563
538
  }
564
539
  }
565
540
 
566
541
  // MARK: - getUrl
567
542
 
568
543
  @objc public func getUrl(_ dbName: String, readonly: Bool) throws -> String {
569
- if isInit {
570
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
571
- let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
572
- guard let mDb: Database = dbDict[connName] else {
573
- let msg = "Connection to \(mDbName) not available"
574
- throw CapacitorSQLiteError.failed(message: msg)
575
- }
576
- let res: String = mDb.getUrl()
577
- return res
578
- } else {
544
+ guard isInit else {
579
545
  throw CapacitorSQLiteError.failed(message: initMessage)
580
546
  }
547
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
548
+ let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
549
+ guard let mDb: Database = dbDict[connName] else {
550
+ let msg = "Connection to \(mDbName) not available"
551
+ throw CapacitorSQLiteError.failed(message: msg)
552
+ }
553
+ let res: String = mDb.getUrl()
554
+ return res
581
555
  }
582
556
 
583
557
  // MARK: - GetVersion
584
558
 
585
559
  @objc public func getVersion(_ dbName: String, readonly: Bool)
586
560
  throws -> NSNumber {
587
- if isInit {
588
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
589
- let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
590
- guard let mDb: Database = dbDict[connName] else {
591
- let msg = "Connection to \(mDbName) not available"
592
- throw CapacitorSQLiteError.failed(message: msg)
593
- }
594
- do {
595
- let version: Int = try mDb.getVersion()
596
- return NSNumber(value: version)
597
-
598
- } catch DatabaseError.open(let message) {
599
- throw CapacitorSQLiteError.failed(message: message)
600
- }
601
- } else {
561
+ guard isInit else {
602
562
  throw CapacitorSQLiteError.failed(message: initMessage)
603
563
  }
564
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
565
+ let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
566
+ guard let mDb: Database = dbDict[connName] else {
567
+ let msg = "Connection to \(mDbName) not available"
568
+ throw CapacitorSQLiteError.failed(message: msg)
569
+ }
570
+ do {
571
+ let version: Int = try mDb.getVersion()
572
+ return NSNumber(value: version)
573
+
574
+ } catch DatabaseError.open(let message) {
575
+ throw CapacitorSQLiteError.failed(message: message)
576
+ }
604
577
  }
605
578
 
606
579
  // MARK: - GetFromHTTPRequest
607
580
 
608
581
  @objc public func getFromHTTPRequest(_ call: CAPPluginCall, url: String) throws {
609
- if isInit {
582
+ guard isInit else {
583
+ throw CapacitorSQLiteError.failed(message: initMessage)
584
+ }
585
+ UtilsDownloadFromHTTP.download(databaseLocation: databaseLocation,
586
+ url: url) { ( result) in
587
+ switch result {
588
+ case .success(_):
589
+ self.retHandler.rResult(call: call)
590
+ return
591
+ case .failure(let error):
610
592
 
611
- UtilsDownloadFromHTTP.download(databaseLocation: databaseLocation,
612
- url: url) { ( result) in
613
- switch result {
614
- case .success(_):
615
- self.retHandler.rResult(call: call)
593
+ if error == .downloadFromHTTPFailed {
594
+ let msg = "Download from HTTP failed"
595
+ self.retHandler.rResult(call: call, message: msg)
616
596
  return
617
- case .failure(let error):
618
-
619
- if error == .downloadFromHTTPFailed {
620
- let msg = "Download from HTTP failed"
621
- self.retHandler.rResult(call: call, message: msg)
622
- return
623
- }
624
-
625
597
  }
626
598
 
627
599
  }
628
- } else {
629
- throw CapacitorSQLiteError.failed(message: initMessage)
600
+
630
601
  }
631
602
  }
632
603
 
633
604
  // MARK: - Close Connection
634
605
 
635
606
  @objc public func closeConnection(_ dbName: String, readonly: Bool) throws {
636
- if isInit {
637
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
638
- let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
639
- guard let mDb: Database = dbDict[connName] else {
640
- return
641
- }
642
- if mDb.isDBOpen() {
643
- do {
644
- try mDb.close()
645
- } catch DatabaseError.close(let message) {
646
- throw CapacitorSQLiteError.failed(message: message)
647
- }
648
- }
649
- dbDict.removeValue(forKey: connName)
650
- return
651
- } else {
607
+ guard isInit else {
652
608
  throw CapacitorSQLiteError.failed(message: initMessage)
653
609
  }
610
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
611
+ let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
612
+ guard let mDb: Database = dbDict[connName] else {
613
+ return
614
+ }
615
+ if mDb.isDBOpen() {
616
+ do {
617
+ try mDb.close()
618
+ } catch DatabaseError.close(let message) {
619
+ throw CapacitorSQLiteError.failed(message: message)
620
+ }
621
+ }
622
+ dbDict.removeValue(forKey: connName)
623
+ return
654
624
  }
655
625
 
656
626
  // MARK: - CheckConnectionsConsistency
@@ -658,149 +628,144 @@ enum CapacitorSQLiteError: Error {
658
628
  @objc public func checkConnectionsConsistency(_ dbNames: [String],
659
629
  openModes: [String])
660
630
  throws -> NSNumber {
661
- if isInit {
662
- var keys: [String] = Array(self.dbDict.keys)
663
- var idx: Int = 0
664
- var conns: [String] = []
665
- for name in dbNames {
666
- conns.append("\(openModes[idx])_\(name)")
667
- idx += 1
631
+ guard isInit else {
632
+ throw CapacitorSQLiteError.failed(message: initMessage)
633
+ }
634
+ var keys: [String] = Array(self.dbDict.keys)
635
+ var idx: Int = 0
636
+ var conns: [String] = []
637
+ for name in dbNames {
638
+ conns.append("\(openModes[idx])_\(name)")
639
+ idx += 1
640
+ }
641
+ do {
642
+ if conns.count == 0 {
643
+ try closeAllConnections()
644
+ return 0
668
645
  }
669
- do {
670
- if conns.count == 0 {
671
- try closeAllConnections()
672
- return 0
673
- }
674
- if keys.count < conns.count {
675
- // not solvable inconsistency
676
- try closeAllConnections()
677
- return 0
678
- }
679
- if keys.count > conns.count {
680
- for key in keys {
681
- if !conns.contains(key) {
682
- self.dbDict.removeValue(forKey: key)
683
- }
646
+ if keys.count < conns.count {
647
+ // not solvable inconsistency
648
+ try closeAllConnections()
649
+ return 0
650
+ }
651
+ if keys.count > conns.count {
652
+ for key in keys {
653
+ if !conns.contains(key) {
654
+ self.dbDict.removeValue(forKey: key)
684
655
  }
685
656
  }
686
- keys = Array(self.dbDict.keys)
687
- if keys.count == conns.count {
688
- let set1 = Set(keys)
689
- let set2 = Set(conns)
690
- let arr = Array(set1.symmetricDifference(set2))
691
- if arr.count == 0 {
692
- return 1
693
- } else {
694
- // not solvable inconsistency
695
- try closeAllConnections()
696
- return 0
697
- }
657
+ }
658
+ keys = Array(self.dbDict.keys)
659
+ if keys.count == conns.count {
660
+ let set1 = Set(keys)
661
+ let set2 = Set(conns)
662
+ let arr = Array(set1.symmetricDifference(set2))
663
+ if arr.count == 0 {
664
+ return 1
698
665
  } else {
666
+ // not solvable inconsistency
699
667
  try closeAllConnections()
700
668
  return 0
701
669
  }
702
- } catch let error {
703
- throw CapacitorSQLiteError.failed(message: "\(error)")
670
+ } else {
671
+ try closeAllConnections()
672
+ return 0
704
673
  }
705
- } else {
706
- throw CapacitorSQLiteError.failed(message: initMessage)
674
+ } catch let error {
675
+ throw CapacitorSQLiteError.failed(message: "\(error)")
707
676
  }
708
677
  }
709
678
 
710
679
  // MARK: - IsDatabase
711
680
 
712
681
  @objc public func isDatabase(_ dbName: String) throws -> NSNumber {
713
- if isInit {
714
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
715
- let isFileExists: Bool = UtilsFile
716
- .isFileExist(databaseLocation: databaseLocation,
717
- fileName: mDbName + "SQLite.db")
718
- if isFileExists {
719
- return 1
720
- } else {
721
- return 0
722
- }
723
- } else {
682
+ guard isInit else {
724
683
  throw CapacitorSQLiteError.failed(message: initMessage)
725
684
  }
685
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
686
+ let isFileExists: Bool = UtilsFile
687
+ .isFileExist(databaseLocation: databaseLocation,
688
+ fileName: mDbName + "SQLite.db")
689
+ if isFileExists {
690
+ return 1
691
+ } else {
692
+ return 0
693
+ }
726
694
  }
727
695
 
728
696
  // MARK: - IsDatabaseEncrypted
729
697
 
730
698
  @objc public func isDatabaseEncrypted(_ dbName: String) throws -> NSNumber {
731
- if isInit {
732
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
733
- let isFileExists: Bool = UtilsFile
734
- .isFileExist(databaseLocation: databaseLocation,
735
- fileName: mDbName + "SQLite.db")
736
- if isFileExists {
737
- let state: State = UtilsSQLCipher
738
- .getDatabaseState(databaseLocation: databaseLocation,
739
- databaseName: mDbName + "SQLite.db",
740
- account: account)
741
- if state.rawValue == "ENCRYPTEDGLOBALSECRET" || state.rawValue == "ENCRYPTEDSECRET" {
742
- return 1
743
- }
744
- if state.rawValue == "UNENCRYPTED" {
745
- return 0
746
- }
747
- throw CapacitorSQLiteError.failed(message: "Database unknown")
748
- } else {
749
- throw CapacitorSQLiteError.failed(message: "Database does not exist")
699
+ guard isInit else {
700
+ throw CapacitorSQLiteError.failed(message: initMessage)
701
+ }
702
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
703
+ let isFileExists: Bool = UtilsFile
704
+ .isFileExist(databaseLocation: databaseLocation,
705
+ fileName: mDbName + "SQLite.db")
706
+ if isFileExists {
707
+ let state: State = UtilsSQLCipher
708
+ .getDatabaseState(databaseLocation: databaseLocation,
709
+ databaseName: mDbName + "SQLite.db",
710
+ account: account)
711
+ if state.rawValue == "ENCRYPTEDGLOBALSECRET" || state.rawValue == "ENCRYPTEDSECRET" {
712
+ return 1
750
713
  }
714
+ if state.rawValue == "UNENCRYPTED" {
715
+ return 0
716
+ }
717
+ throw CapacitorSQLiteError.failed(message: "Database unknown")
751
718
  } else {
752
- throw CapacitorSQLiteError.failed(message: initMessage)
719
+ throw CapacitorSQLiteError.failed(message: "Database does not exist")
753
720
  }
754
721
  }
755
722
 
756
723
  // MARK: - IsNCDatabase
757
724
 
758
725
  @objc public func isNCDatabase(_ databasePath: String) throws -> NSNumber {
759
- if isInit {
760
- let isFileExists: Bool = UtilsFile
761
- .isFileExist(filePath: databasePath)
762
- if isFileExists {
763
- return 1
764
- } else {
765
- return 0
766
- }
767
- } else {
726
+ guard isInit else {
768
727
  throw CapacitorSQLiteError.failed(message: initMessage)
769
728
  }
729
+ let isFileExists: Bool = UtilsFile
730
+ .isFileExist(filePath: databasePath)
731
+ if isFileExists {
732
+ return 1
733
+ } else {
734
+ return 0
735
+ }
770
736
  }
771
737
 
772
738
  // MARK: - IsTableExists
773
739
 
774
740
  @objc public func isTableExists(_ dbName: String, tableName: String,
775
741
  readonly: Bool) throws -> NSNumber {
776
- if isInit {
777
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
778
- let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
779
- guard let mDb: Database = dbDict[connName] else {
780
- let msg = "Connection to \(mDbName) not available"
781
- throw CapacitorSQLiteError.failed(message: msg)
782
- }
783
- do {
784
- let isExists: Bool = try
785
- UtilsJson.isTableExists(mDB: mDb,
786
- tableName: tableName)
787
- if isExists {
788
- return 1
789
- } else {
790
- return 0
791
- }
792
- } catch UtilsJsonError.tableNotExists(let message) {
793
- var msg: String = "IsTableExists:"
794
- msg.append(" \(message)")
795
- throw CapacitorSQLiteError.failed(message: msg)
796
- } catch let error {
797
- var msg: String = "IsTableExists:"
798
- msg.append(" \(error)")
799
- throw CapacitorSQLiteError.failed(message: msg)
800
- }
801
- } else {
742
+ guard isInit else {
802
743
  throw CapacitorSQLiteError.failed(message: initMessage)
803
744
  }
745
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
746
+ let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
747
+ guard let mDb: Database = dbDict[connName] else {
748
+ let msg = "Connection to \(mDbName) not available"
749
+ throw CapacitorSQLiteError.failed(message: msg)
750
+ }
751
+ do {
752
+ let isExists: Bool = try
753
+ UtilsJson.isTableExists(mDB: mDb,
754
+ tableName: tableName)
755
+ if isExists {
756
+ return 1
757
+ } else {
758
+ return 0
759
+ }
760
+ } catch UtilsJsonError.tableNotExists(let message) {
761
+ var msg: String = "IsTableExists:"
762
+ msg.append(" \(message)")
763
+ throw CapacitorSQLiteError.failed(message: msg)
764
+ } catch let error {
765
+ var msg: String = "IsTableExists:"
766
+ msg.append(" \(error)")
767
+ throw CapacitorSQLiteError.failed(message: msg)
768
+ }
804
769
  }
805
770
 
806
771
  // MARK: - Execute
@@ -808,46 +773,45 @@ enum CapacitorSQLiteError: Error {
808
773
  @objc public func execute(_ dbName: String, statements: String,
809
774
  transaction: Bool, readonly: Bool)
810
775
  throws -> [String: Any] {
811
- if isInit {
812
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
813
- let connName: String = "RW_\(mDbName)"
814
- guard let mDb: Database = dbDict[connName] else {
815
- let msg = "Connection to \(mDbName) not available"
816
- throw CapacitorSQLiteError.failed(message: msg)
817
- }
818
- if readonly {
819
- let msg = "not allowed in read-only mode"
820
- throw CapacitorSQLiteError.failed(message: msg)
821
- }
822
- if !mDb.isNCDB() && mDb.isDBOpen() {
823
- do {
824
- var stmts = statements
825
- // remove carriage returns
826
- let isReturn = stmts.indicesOf(string: "\n")
827
- if isReturn.count != 0 {
828
- let cmds = stmts.split(separator: "\n")
829
- var strcmds: [String] = []
830
- for cmd in cmds {
831
- strcmds.append(String(cmd
832
- .trimmingCharacters(in: .whitespacesAndNewlines)))
833
- }
834
- stmts = strcmds.joined(separator: "\n")
776
+ guard isInit else {
777
+ throw CapacitorSQLiteError.failed(message: initMessage)
778
+ }
779
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
780
+ let connName: String = "RW_\(mDbName)"
781
+ guard let mDb: Database = dbDict[connName] else {
782
+ let msg = "Connection to \(mDbName) not available"
783
+ throw CapacitorSQLiteError.failed(message: msg)
784
+ }
785
+ if readonly {
786
+ let msg = "not allowed in read-only mode"
787
+ throw CapacitorSQLiteError.failed(message: msg)
788
+ }
789
+ if !mDb.isNCDB() && mDb.isDBOpen() {
790
+ do {
791
+ var stmts = statements
792
+ // remove carriage returns
793
+ let isReturn = stmts.indicesOf(string: "\n")
794
+ if isReturn.count != 0 {
795
+ let cmds = stmts.split(separator: "\n")
796
+ var strcmds: [String] = []
797
+ for cmd in cmds {
798
+ strcmds.append(String(cmd
799
+ .trimmingCharacters(in: .whitespacesAndNewlines)))
835
800
  }
836
- let res = try mDb.executeSQL(sql: stmts,
837
- transaction: transaction)
838
- return ["changes": res]
839
- } catch DatabaseError.executeSQL(let message) {
840
- throw CapacitorSQLiteError.failed(message: message)
841
- } catch let error {
842
- let msg: String = "\(error)"
843
- throw CapacitorSQLiteError.failed(message: msg)
801
+ stmts = strcmds.joined(separator: "\n")
844
802
  }
845
- } else {
846
- let msg = "Database \(mDbName) not opened or in read-only"
803
+ let res = try mDb.executeSQL(sql: stmts,
804
+ transaction: transaction)
805
+ return ["changes": res]
806
+ } catch DatabaseError.executeSQL(let message) {
807
+ throw CapacitorSQLiteError.failed(message: message)
808
+ } catch let error {
809
+ let msg: String = "\(error)"
847
810
  throw CapacitorSQLiteError.failed(message: msg)
848
811
  }
849
812
  } else {
850
- throw CapacitorSQLiteError.failed(message: initMessage)
813
+ let msg = "Database \(mDbName) not opened or in read-only"
814
+ throw CapacitorSQLiteError.failed(message: msg)
851
815
  }
852
816
  }
853
817
 
@@ -856,34 +820,33 @@ enum CapacitorSQLiteError: Error {
856
820
  @objc func executeSet(_ dbName: String, set: [[String: Any]],
857
821
  transaction: Bool, readonly: Bool, returnMode: String)
858
822
  throws -> [String: Any] {
859
- if isInit {
860
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
861
- let connName: String = "RW_\(mDbName)"
862
- guard let mDb: Database = dbDict[connName] else {
863
- let msg = "Connection to \(mDbName) not available"
864
- throw CapacitorSQLiteError.failed(message: msg)
865
- }
866
- if readonly {
867
- let msg = "not allowed in read-only mode"
868
- throw CapacitorSQLiteError.failed(message: msg)
869
- }
870
- if !mDb.isNCDB() && mDb.isDBOpen() {
871
- do {
872
- let res = try mDb.execSet(set: set, transaction: transaction,
873
- returnMode: returnMode)
874
- return res
875
- } catch DatabaseError.execSet(let message) {
876
- throw CapacitorSQLiteError.failed(message: message)
877
- } catch let error {
878
- let msg: String = "\(error)"
879
- throw CapacitorSQLiteError.failed(message: msg)
880
- }
881
- } else {
882
- let msg = "Database \(mDbName) not opened or in read-only"
823
+ guard isInit else {
824
+ throw CapacitorSQLiteError.failed(message: initMessage)
825
+ }
826
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
827
+ let connName: String = "RW_\(mDbName)"
828
+ guard let mDb: Database = dbDict[connName] else {
829
+ let msg = "Connection to \(mDbName) not available"
830
+ throw CapacitorSQLiteError.failed(message: msg)
831
+ }
832
+ if readonly {
833
+ let msg = "not allowed in read-only mode"
834
+ throw CapacitorSQLiteError.failed(message: msg)
835
+ }
836
+ if !mDb.isNCDB() && mDb.isDBOpen() {
837
+ do {
838
+ let res = try mDb.execSet(set: set, transaction: transaction,
839
+ returnMode: returnMode)
840
+ return res
841
+ } catch DatabaseError.execSet(let message) {
842
+ throw CapacitorSQLiteError.failed(message: message)
843
+ } catch let error {
844
+ let msg: String = "\(error)"
883
845
  throw CapacitorSQLiteError.failed(message: msg)
884
846
  }
885
847
  } else {
886
- throw CapacitorSQLiteError.failed(message: initMessage)
848
+ let msg = "Database \(mDbName) not opened or in read-only"
849
+ throw CapacitorSQLiteError.failed(message: msg)
887
850
  }
888
851
  }
889
852
 
@@ -895,81 +858,80 @@ enum CapacitorSQLiteError: Error {
895
858
  @objc func run(_ dbName: String, statement: String, values: [Any],
896
859
  transaction: Bool, readonly: Bool, returnMode: String)
897
860
  throws -> [String: Any] {
898
- if isInit {
899
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
900
- let connName: String = "RW_\(mDbName)"
901
- guard let mDb: Database = dbDict[connName] else {
902
- let msg = "Connection to \(mDbName) not available"
903
- throw CapacitorSQLiteError.failed(message: msg)
904
- }
905
- if readonly {
906
- let msg = "not allowed in read-only mode"
907
- throw CapacitorSQLiteError.failed(message: msg)
908
- }
909
- if !mDb.isNCDB() && mDb.isDBOpen() {
910
- do {
911
- var val: [Any] = []
912
- if values.count > 0 {
913
- for value in values {
914
- if let obj = value as? String {
915
- let str: String =
916
- "\(String(describing: obj))"
917
- val.append(str)
918
- } else if let obj = value as? Int {
919
- val.append(obj)
920
- } else if let obj = value as? Float {
921
- val.append(obj)
922
- } else if let obj = value as? Double {
923
- val.append(obj)
924
- } else if value is NSNull {
925
- val.append(value)
926
- } else if let obj = value as? [String: Any] {
927
- if var keys = Array(obj.keys) as? [String] {
928
- if #available(iOS 15.0, *) {
929
- keys.sort(using: .localizedStandard)
930
- var valuesArr: [UInt8] = []
931
- for key in keys {
932
- if let mVal = obj[key] {
933
- if let iVal = mVal as? Int {
934
- valuesArr.append(UInt8(iVal))
935
- } else {
936
- let msg: String = "Error in reading buffer"
937
- throw CapacitorSQLiteError.failed(message: msg)
938
- }
861
+ guard isInit else {
862
+ throw CapacitorSQLiteError.failed(message: initMessage)
863
+ }
864
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
865
+ let connName: String = "RW_\(mDbName)"
866
+ guard let mDb: Database = dbDict[connName] else {
867
+ let msg = "Connection to \(mDbName) not available"
868
+ throw CapacitorSQLiteError.failed(message: msg)
869
+ }
870
+ if readonly {
871
+ let msg = "not allowed in read-only mode"
872
+ throw CapacitorSQLiteError.failed(message: msg)
873
+ }
874
+ if !mDb.isNCDB() && mDb.isDBOpen() {
875
+ do {
876
+ var val: [Any] = []
877
+ if values.count > 0 {
878
+ for value in values {
879
+ if let obj = value as? String {
880
+ let str: String =
881
+ "\(String(describing: obj))"
882
+ val.append(str)
883
+ } else if let obj = value as? Int {
884
+ val.append(obj)
885
+ } else if let obj = value as? Float {
886
+ val.append(obj)
887
+ } else if let obj = value as? Double {
888
+ val.append(obj)
889
+ } else if value is NSNull {
890
+ val.append(value)
891
+ } else if let obj = value as? [String: Any] {
892
+ if var keys = Array(obj.keys) as? [String] {
893
+ if #available(iOS 15.0, *) {
894
+ keys.sort(using: .localizedStandard)
895
+ var valuesArr: [UInt8] = []
896
+ for key in keys {
897
+ if let mVal = obj[key] {
898
+ if let iVal = mVal as? Int {
899
+ valuesArr.append(UInt8(iVal))
939
900
  } else {
940
901
  let msg: String = "Error in reading buffer"
941
902
  throw CapacitorSQLiteError.failed(message: msg)
942
903
  }
904
+ } else {
905
+ let msg: String = "Error in reading buffer"
906
+ throw CapacitorSQLiteError.failed(message: msg)
943
907
  }
944
- val.append(valuesArr)
945
- } else {
946
- let msg: String = "Error buffer sorted not implemented"
947
- throw CapacitorSQLiteError.failed(message: msg)
948
-
949
908
  }
909
+ val.append(valuesArr)
910
+ } else {
911
+ let msg: String = "Error buffer sorted not implemented"
912
+ throw CapacitorSQLiteError.failed(message: msg)
913
+
950
914
  }
951
- } else {
952
- let msg: String = "Not a SQL type"
953
- throw CapacitorSQLiteError.failed(message: msg)
954
915
  }
916
+ } else {
917
+ let msg: String = "Not a SQL type"
918
+ throw CapacitorSQLiteError.failed(message: msg)
955
919
  }
956
920
  }
957
- let res = try mDb.runSQL(sql: statement, values: val,
958
- transaction: transaction,
959
- returnMode: returnMode)
960
- return res
961
- } catch DatabaseError.runSQL(let message) {
962
- throw CapacitorSQLiteError.failed(message: message)
963
- } catch let error {
964
- let msg: String = "\(error)"
965
- throw CapacitorSQLiteError.failed(message: msg)
966
921
  }
967
- } else {
968
- let msg = "Database \(mDbName) not opened or in read-only"
922
+ let res = try mDb.runSQL(sql: statement, values: val,
923
+ transaction: transaction,
924
+ returnMode: returnMode)
925
+ return res
926
+ } catch DatabaseError.runSQL(let message) {
927
+ throw CapacitorSQLiteError.failed(message: message)
928
+ } catch let error {
929
+ let msg: String = "\(error)"
969
930
  throw CapacitorSQLiteError.failed(message: msg)
970
931
  }
971
932
  } else {
972
- throw CapacitorSQLiteError.failed(message: initMessage)
933
+ let msg = "Database \(mDbName) not opened or in read-only"
934
+ throw CapacitorSQLiteError.failed(message: msg)
973
935
  }
974
936
  }
975
937
  // swiftlint:enable function_parameter_count
@@ -980,75 +942,72 @@ enum CapacitorSQLiteError: Error {
980
942
 
981
943
  @objc func query(_ dbName: String, statement: String,
982
944
  values: [Any], readonly: Bool) throws -> [[String: Any]] {
983
- if isInit {
984
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
985
- let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
986
- guard let mDb: Database = dbDict[connName] else {
987
- let msg = "Connection to \(mDbName) not available"
988
- throw CapacitorSQLiteError.failed(message: msg)
989
- }
990
- if mDb.isDBOpen() {
991
- do {
992
- let res: [[String: Any]] = try mDb
993
- .selectSQL(sql: statement, values: values)
994
- return res
995
- } catch DatabaseError.selectSQL(let message) {
996
- throw CapacitorSQLiteError.failed(message: message)
997
- } catch let error {
998
- let msg: String = "\(error)"
999
- throw CapacitorSQLiteError.failed(message: msg)
1000
- }
1001
- } else {
1002
- let msg = "Database \(mDbName) not opened"
945
+ guard isInit else {
946
+ throw CapacitorSQLiteError.failed(message: initMessage)
947
+ }
948
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
949
+ let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
950
+ guard let mDb: Database = dbDict[connName] else {
951
+ let msg = "Connection to \(mDbName) not available"
952
+ throw CapacitorSQLiteError.failed(message: msg)
953
+ }
954
+ if mDb.isDBOpen() {
955
+ do {
956
+ let res: [[String: Any]] = try mDb
957
+ .selectSQL(sql: statement, values: values)
958
+ return res
959
+ } catch DatabaseError.selectSQL(let message) {
960
+ throw CapacitorSQLiteError.failed(message: message)
961
+ } catch let error {
962
+ let msg: String = "\(error)"
1003
963
  throw CapacitorSQLiteError.failed(message: msg)
1004
964
  }
1005
965
  } else {
1006
- throw CapacitorSQLiteError.failed(message: initMessage)
966
+ let msg = "Database \(mDbName) not opened"
967
+ throw CapacitorSQLiteError.failed(message: msg)
1007
968
  }
1008
969
  }
1009
970
 
1010
971
  // MARK: - isDBExists
1011
972
 
1012
973
  @objc func isDBExists(_ dbName: String, readonly: Bool) throws -> NSNumber {
1013
- if isInit {
1014
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
1015
- let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
1016
- guard let _: Database = dbDict[connName] else {
1017
- let msg = "Connection to \(mDbName) not available"
1018
- throw CapacitorSQLiteError.failed(message: msg)
1019
- }
1020
- let res: Bool = UtilsFile
1021
- .isFileExist(databaseLocation: databaseLocation,
1022
- fileName: "\(mDbName)SQLite.db")
1023
- if res {
1024
- return 1
1025
- } else {
1026
- return 0
1027
- }
1028
- } else {
974
+ guard isInit else {
1029
975
  throw CapacitorSQLiteError.failed(message: initMessage)
1030
976
  }
977
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
978
+ let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
979
+ guard let _: Database = dbDict[connName] else {
980
+ let msg = "Connection to \(mDbName) not available"
981
+ throw CapacitorSQLiteError.failed(message: msg)
982
+ }
983
+ let res: Bool = UtilsFile
984
+ .isFileExist(databaseLocation: databaseLocation,
985
+ fileName: "\(mDbName)SQLite.db")
986
+ if res {
987
+ return 1
988
+ } else {
989
+ return 0
990
+ }
1031
991
  }
1032
992
 
1033
993
  // MARK: - isDBOpen
1034
994
 
1035
995
  @objc func isDBOpen(_ dbName: String, readonly: Bool) throws -> NSNumber {
1036
- if isInit {
1037
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
1038
- let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
1039
- guard let mDb: Database = dbDict[connName] else {
1040
- let msg = "Connection to \(mDbName) not available"
1041
- throw CapacitorSQLiteError.failed(message: msg)
1042
- }
1043
- let isOpen: Bool = mDb.isDBOpen()
1044
- if isOpen {
1045
- return 1
1046
- } else {
1047
- return 0
1048
- }
1049
- } else {
996
+ guard isInit else {
1050
997
  throw CapacitorSQLiteError.failed(message: initMessage)
1051
998
  }
999
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
1000
+ let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
1001
+ guard let mDb: Database = dbDict[connName] else {
1002
+ let msg = "Connection to \(mDbName) not available"
1003
+ throw CapacitorSQLiteError.failed(message: msg)
1004
+ }
1005
+ let isOpen: Bool = mDb.isDBOpen()
1006
+ if isOpen {
1007
+ return 1
1008
+ } else {
1009
+ return 0
1010
+ }
1052
1011
  }
1053
1012
 
1054
1013
  // MARK: - deleteDatabase
@@ -1056,56 +1015,55 @@ enum CapacitorSQLiteError: Error {
1056
1015
  // swiftlint:disable function_body_length
1057
1016
  // swiftlint:disable cyclomatic_complexity
1058
1017
  @objc func deleteDatabase(_ dbName: String, readonly: Bool) throws {
1059
- if isInit {
1060
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
1061
- let connName: String = "RW_\(mDbName)"
1062
- guard let mDb: Database = dbDict[connName] else {
1063
- let msg = "Connection to \(mDbName) not available"
1064
- throw CapacitorSQLiteError.failed(message: msg)
1065
- }
1066
- if readonly {
1067
- let msg = "not allowed in read-only mode"
1068
- throw CapacitorSQLiteError.failed(message: msg)
1069
- }
1070
- do {
1071
- if !mDb.isDBOpen() {
1072
- // check the state of the DB
1073
- let state: State = UtilsSQLCipher.getDatabaseState(databaseLocation: databaseLocation, databaseName: "\(mDbName)SQLite.db", account: account)
1074
- if !isEncryption && (state.rawValue == "ENCRYPTEDGLOBALSECRET" ||
1075
- state.rawValue == "ENCRYPTEDSECRET") {
1076
- var msg = "Cannot delete an Encrypted database with "
1077
- msg += "No Encryption set in capacitor.config"
1078
- throw CapacitorSQLiteError.failed(message: msg)
1079
- } else if state.rawValue == "UNENCRYPTED" {
1080
- do {
1081
- try UtilsSQLCipher.deleteDB(databaseLocation: databaseLocation,
1082
- databaseName: "\(mDbName)SQLite.db")
1083
- return
1084
- } catch UtilsSQLCipherError.deleteDB(let message ) {
1085
- throw CapacitorSQLiteError.failed(message: message)
1086
- }
1087
-
1088
- } else {
1089
- try mDb.open()
1018
+ guard isInit else {
1019
+ throw CapacitorSQLiteError.failed(message: initMessage)
1020
+ }
1021
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
1022
+ let connName: String = "RW_\(mDbName)"
1023
+ guard let mDb: Database = dbDict[connName] else {
1024
+ let msg = "Connection to \(mDbName) not available"
1025
+ throw CapacitorSQLiteError.failed(message: msg)
1026
+ }
1027
+ if readonly {
1028
+ let msg = "not allowed in read-only mode"
1029
+ throw CapacitorSQLiteError.failed(message: msg)
1030
+ }
1031
+ do {
1032
+ if !mDb.isDBOpen() {
1033
+ // check the state of the DB
1034
+ let state: State = UtilsSQLCipher.getDatabaseState(databaseLocation: databaseLocation, databaseName: "\(mDbName)SQLite.db", account: account)
1035
+ if !isEncryption && (state.rawValue == "ENCRYPTEDGLOBALSECRET" ||
1036
+ state.rawValue == "ENCRYPTEDSECRET") {
1037
+ var msg = "Cannot delete an Encrypted database with "
1038
+ msg += "No Encryption set in capacitor.config"
1039
+ throw CapacitorSQLiteError.failed(message: msg)
1040
+ } else if state.rawValue == "UNENCRYPTED" {
1041
+ do {
1042
+ try UtilsSQLCipher.deleteDB(databaseLocation: databaseLocation,
1043
+ databaseName: "\(mDbName)SQLite.db")
1044
+ return
1045
+ } catch UtilsSQLCipherError.deleteDB(let message ) {
1046
+ throw CapacitorSQLiteError.failed(message: message)
1090
1047
  }
1091
- }
1092
- let res: Bool = try mDb.deleteDB(databaseName: "\(mDbName)SQLite.db")
1093
- if res {
1094
- return
1048
+
1095
1049
  } else {
1096
- let msg: String = "deleteDB return false"
1097
- throw CapacitorSQLiteError.failed(message: msg)
1050
+ try mDb.open()
1098
1051
  }
1099
- } catch DatabaseError.open(let message) {
1100
- throw CapacitorSQLiteError.failed(message: message)
1101
- } catch DatabaseError.deleteDB(let message) {
1102
- throw CapacitorSQLiteError.failed(message: message)
1103
- } catch let error {
1104
- let msg: String = "\(error)"
1052
+ }
1053
+ let res: Bool = try mDb.deleteDB(databaseName: "\(mDbName)SQLite.db")
1054
+ if res {
1055
+ return
1056
+ } else {
1057
+ let msg: String = "deleteDB return false"
1105
1058
  throw CapacitorSQLiteError.failed(message: msg)
1106
1059
  }
1107
- } else {
1108
- throw CapacitorSQLiteError.failed(message: initMessage)
1060
+ } catch DatabaseError.open(let message) {
1061
+ throw CapacitorSQLiteError.failed(message: message)
1062
+ } catch DatabaseError.deleteDB(let message) {
1063
+ throw CapacitorSQLiteError.failed(message: message)
1064
+ } catch let error {
1065
+ let msg: String = "\(error)"
1066
+ throw CapacitorSQLiteError.failed(message: msg)
1109
1067
  }
1110
1068
  }
1111
1069
  // swiftlint:enable cyclomatic_complexity
@@ -1114,22 +1072,21 @@ enum CapacitorSQLiteError: Error {
1114
1072
  // MARK: - isJsonValid
1115
1073
 
1116
1074
  @objc func isJsonValid(_ parsingData: String) throws {
1117
- if isInit {
1118
- if let data = ("["+parsingData+"]").data(using: .utf8) {
1119
- do {
1120
- _ = try JSONDecoder().decode([JsonSQLite].self,
1121
- from: data)
1122
- return
1123
- } catch let error {
1124
- let msg: String = "\(error)"
1125
- throw CapacitorSQLiteError.failed(message: msg)
1126
- }
1127
- } else {
1128
- let msg: String = "Stringify Json Object not Valid"
1075
+ guard isInit else {
1076
+ throw CapacitorSQLiteError.failed(message: initMessage)
1077
+ }
1078
+ if let data = ("["+parsingData+"]").data(using: .utf8) {
1079
+ do {
1080
+ _ = try JSONDecoder().decode([JsonSQLite].self,
1081
+ from: data)
1082
+ return
1083
+ } catch let error {
1084
+ let msg: String = "\(error)"
1129
1085
  throw CapacitorSQLiteError.failed(message: msg)
1130
1086
  }
1131
1087
  } else {
1132
- throw CapacitorSQLiteError.failed(message: initMessage)
1088
+ let msg: String = "Stringify Json Object not Valid"
1089
+ throw CapacitorSQLiteError.failed(message: msg)
1133
1090
  }
1134
1091
  }
1135
1092
 
@@ -1139,145 +1096,144 @@ enum CapacitorSQLiteError: Error {
1139
1096
  // swiftlint:disable cyclomatic_complexity
1140
1097
  @objc func importFromJson(_ parsingData: String)
1141
1098
  throws -> [String: Int] {
1142
- if isInit {
1143
- var mDb: Database
1144
- var importData: ImportData
1145
- // check if json object is encrypted
1146
- if parsingData.contains("expData") {
1147
- guard let data = ("["+parsingData+"]")
1148
- .data(using: .utf8) else {
1149
- let msg: String = "Stringify Encrypted Json Object " +
1150
- "not Valid"
1151
- throw CapacitorSQLiteError.failed(message: msg)
1152
- }
1153
- var encryptJson: [EncryptJson]
1154
- do {
1155
- encryptJson = try JSONDecoder()
1156
- .decode([EncryptJson].self, from: data)
1157
- let dict: [String: Any] = try
1158
- UtilsJson.decryptBase64ToDictionary(
1159
- encryptJson[0].expData,
1160
- forAccount: account)
1161
- importData = ImportData(jsonDict: dict)
1162
- } catch let error {
1163
- var msg: String = "Encrypted Json Object not Valid "
1164
- msg.append("\(error)")
1165
- throw CapacitorSQLiteError.failed(message: msg)
1166
- }
1167
-
1168
- } else {
1099
+ guard isInit else {
1100
+ throw CapacitorSQLiteError.failed(message: initMessage)
1101
+ }
1102
+ var mDb: Database
1103
+ var importData: ImportData
1104
+ // check if json object is encrypted
1105
+ if parsingData.contains("expData") {
1106
+ guard let data = ("["+parsingData+"]")
1107
+ .data(using: .utf8) else {
1108
+ let msg: String = "Stringify Encrypted Json Object " +
1109
+ "not Valid"
1110
+ throw CapacitorSQLiteError.failed(message: msg)
1111
+ }
1112
+ var encryptJson: [EncryptJson]
1113
+ do {
1114
+ encryptJson = try JSONDecoder()
1115
+ .decode([EncryptJson].self, from: data)
1116
+ let dict: [String: Any] = try
1117
+ UtilsJson.decryptBase64ToDictionary(
1118
+ encryptJson[0].expData,
1119
+ forAccount: account)
1120
+ importData = ImportData(jsonDict: dict)
1121
+ } catch let error {
1122
+ var msg: String = "Encrypted Json Object not Valid "
1123
+ msg.append("\(error)")
1124
+ throw CapacitorSQLiteError.failed(message: msg)
1125
+ }
1169
1126
 
1170
- guard let data = ("["+parsingData+"]")
1171
- .data(using: .utf8) else {
1172
- let msg: String = "Stringify Json Object not Valid"
1173
- throw CapacitorSQLiteError.failed(message: msg)
1174
- }
1175
- var jsonSQLite: [JsonSQLite]
1176
- do {
1177
- jsonSQLite = try JSONDecoder()
1178
- .decode([JsonSQLite].self, from: data)
1179
- importData = ImportData(jsonSQLite: jsonSQLite[0])
1180
- jsonSQLite = []
1181
- } catch let error {
1182
- var msg: String = "Stringify Json Object not Valid "
1183
- msg.append("\(error)")
1184
- throw CapacitorSQLiteError.failed(message: msg)
1185
- }
1127
+ } else {
1186
1128
 
1129
+ guard let data = ("["+parsingData+"]")
1130
+ .data(using: .utf8) else {
1131
+ let msg: String = "Stringify Json Object not Valid"
1132
+ throw CapacitorSQLiteError.failed(message: msg)
1187
1133
  }
1188
- let encrypted: Bool = importData.encrypted
1189
- let overwrite: Bool = importData.overwrite ?
1190
- importData.overwrite : false
1191
- let mode: String = importData.mode
1192
- let inMode: String = encrypted ? "secret"
1193
- : "no-encryption"
1194
- let version: Int = importData.version
1195
- var dbName: String = CapacitorSQLite.getDatabaseName(
1196
- dbName: importData.database
1197
- )
1198
- dbName.append("SQLite.db")
1199
- // open the database
1134
+ var jsonSQLite: [JsonSQLite]
1200
1135
  do {
1201
- mDb = try Database(
1202
- databaseLocation: databaseLocation, databaseName: dbName,
1203
- encrypted: encrypted, isEncryption: isEncryption,
1204
- account: account,
1205
- mode: inMode, version: version, readonly: false,
1206
- vUpgDict: [:])
1207
- if overwrite && mode == "full" {
1208
- let isExists = UtilsFile
1209
- .isFileExist(databaseLocation: databaseLocation,
1210
- fileName: dbName)
1211
- if isExists {
1212
- _ = try UtilsFile
1213
- .deleteFile(fileName: dbName,
1214
- databaseLocation: databaseLocation)
1215
- }
1216
- }
1217
- try mDb.open()
1218
- } catch UtilsFileError.deleteFileFailed {
1219
- let message = "Delete Database failed"
1220
- throw CapacitorSQLiteError.failed(message: message)
1221
- } catch DatabaseError.open(let message) {
1222
- throw CapacitorSQLiteError.failed(message: message)
1136
+ jsonSQLite = try JSONDecoder()
1137
+ .decode([JsonSQLite].self, from: data)
1138
+ importData = ImportData(jsonSQLite: jsonSQLite[0])
1139
+ jsonSQLite = []
1223
1140
  } catch let error {
1224
- let msg: String = "\(error)"
1141
+ var msg: String = "Stringify Json Object not Valid "
1142
+ msg.append("\(error)")
1225
1143
  throw CapacitorSQLiteError.failed(message: msg)
1226
1144
  }
1227
- // check if the database as some tables
1228
- do {
1229
- let tableList: [String] = try mDb.getTableNames()
1230
- if mode == "full" && tableList.count > 0 {
1231
- let curVersion = try mDb.getVersion()
1232
- if version < curVersion {
1233
- var msg: String = "ImportFromJson: Cannot import a "
1234
- msg += "version lower than \(curVersion)"
1235
- throw CapacitorSQLiteError.failed(message: msg)
1236
- }
1237
- if curVersion == version {
1238
- var res: [String: Int] = [:]
1239
- res["changes"] = 0
1240
- return res
1241
- }
1145
+
1146
+ }
1147
+ let encrypted: Bool = importData.encrypted
1148
+ let overwrite: Bool = importData.overwrite ?
1149
+ importData.overwrite : false
1150
+ let mode: String = importData.mode
1151
+ let inMode: String = encrypted ? "secret"
1152
+ : "no-encryption"
1153
+ let version: Int = importData.version
1154
+ var dbName: String = CapacitorSQLite.getDatabaseName(
1155
+ dbName: importData.database
1156
+ )
1157
+ dbName.append("SQLite.db")
1158
+ // open the database
1159
+ do {
1160
+ mDb = try Database(
1161
+ databaseLocation: databaseLocation, databaseName: dbName,
1162
+ encrypted: encrypted, isEncryption: isEncryption,
1163
+ account: account,
1164
+ mode: inMode, version: version, readonly: false,
1165
+ vUpgDict: [:])
1166
+ if overwrite && mode == "full" {
1167
+ let isExists = UtilsFile
1168
+ .isFileExist(databaseLocation: databaseLocation,
1169
+ fileName: dbName)
1170
+ if isExists {
1171
+ _ = try UtilsFile
1172
+ .deleteFile(fileName: dbName,
1173
+ databaseLocation: databaseLocation)
1242
1174
  }
1175
+ }
1176
+ try mDb.open()
1177
+ } catch UtilsFileError.deleteFileFailed {
1178
+ let message = "Delete Database failed"
1179
+ throw CapacitorSQLiteError.failed(message: message)
1180
+ } catch DatabaseError.open(let message) {
1181
+ throw CapacitorSQLiteError.failed(message: message)
1182
+ } catch let error {
1183
+ let msg: String = "\(error)"
1184
+ throw CapacitorSQLiteError.failed(message: msg)
1185
+ }
1186
+ // check if the database as some tables
1187
+ do {
1188
+ let tableList: [String] = try mDb.getTableNames()
1189
+ if mode == "full" && tableList.count > 0 {
1190
+ let curVersion = try mDb.getVersion()
1191
+ if version < curVersion {
1192
+ var msg: String = "ImportFromJson: Cannot import a "
1193
+ msg += "version lower than \(curVersion)"
1194
+ throw CapacitorSQLiteError.failed(message: msg)
1195
+ }
1196
+ if curVersion == version {
1197
+ var res: [String: Int] = [:]
1198
+ res["changes"] = 0
1199
+ return res
1200
+ }
1201
+ }
1243
1202
 
1244
- } catch DatabaseError.getTableNames(let message) {
1245
- throw CapacitorSQLiteError.failed(message: message)
1246
- } catch let error {
1247
- let msg: String = "\(error)"
1203
+ } catch DatabaseError.getTableNames(let message) {
1204
+ throw CapacitorSQLiteError.failed(message: message)
1205
+ } catch let error {
1206
+ let msg: String = "\(error)"
1207
+ throw CapacitorSQLiteError.failed(message: msg)
1208
+ }
1209
+ // import from Json Object
1210
+ do {
1211
+ let res: [String: Int] = try mDb
1212
+ .importFromJson(importData: importData)
1213
+ try mDb.close()
1214
+ if let result = res["changes"] {
1215
+ if result < 0 {
1216
+ let msg: String = "changes < 0"
1217
+ throw CapacitorSQLiteError
1218
+ .failed(message: msg)
1219
+ } else {
1220
+ return res
1221
+ }
1222
+ } else {
1223
+ let msg: String = "changes not found"
1248
1224
  throw CapacitorSQLiteError.failed(message: msg)
1249
1225
  }
1250
- // import from Json Object
1226
+ } catch DatabaseError.importFromJson(let message) {
1227
+ var msg = message
1251
1228
  do {
1252
- let res: [String: Int] = try mDb
1253
- .importFromJson(importData: importData)
1254
1229
  try mDb.close()
1255
- if let result = res["changes"] {
1256
- if result < 0 {
1257
- let msg: String = "changes < 0"
1258
- throw CapacitorSQLiteError
1259
- .failed(message: msg)
1260
- } else {
1261
- return res
1262
- }
1263
- } else {
1264
- let msg: String = "changes not found"
1265
- throw CapacitorSQLiteError.failed(message: msg)
1266
- }
1267
- } catch DatabaseError.importFromJson(let message) {
1268
- var msg = message
1269
- do {
1270
- try mDb.close()
1271
- throw CapacitorSQLiteError.failed(message: msg)
1272
- } catch DatabaseError.close(let message) {
1273
- msg.append(" \(message)")
1274
- throw CapacitorSQLiteError.failed(message: msg)
1275
- }
1230
+ throw CapacitorSQLiteError.failed(message: msg)
1276
1231
  } catch DatabaseError.close(let message) {
1277
- throw CapacitorSQLiteError.failed(message: message)
1232
+ msg.append(" \(message)")
1233
+ throw CapacitorSQLiteError.failed(message: msg)
1278
1234
  }
1279
- } else {
1280
- throw CapacitorSQLiteError.failed(message: initMessage)
1235
+ } catch DatabaseError.close(let message) {
1236
+ throw CapacitorSQLiteError.failed(message: message)
1281
1237
  }
1282
1238
  }
1283
1239
  // swiftlint:enable cyclomatic_complexity
@@ -1287,111 +1243,108 @@ enum CapacitorSQLiteError: Error {
1287
1243
 
1288
1244
  @objc func exportToJson(_ dbName: String, expMode: String, readonly: Bool, encrypted: Bool)
1289
1245
  throws -> [String: Any] {
1290
- if isInit {
1291
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
1292
- let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
1293
- guard let mDb: Database = dbDict[connName] else {
1294
- let msg = "Connection to \(mDbName) not available"
1295
- throw CapacitorSQLiteError.failed(message: msg)
1296
- }
1297
- if mDb.isDBOpen() {
1246
+ guard isInit else {
1247
+ throw CapacitorSQLiteError.failed(message: initMessage)
1248
+ }
1249
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
1250
+ let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
1251
+ guard let mDb: Database = dbDict[connName] else {
1252
+ let msg = "Connection to \(mDbName) not available"
1253
+ throw CapacitorSQLiteError.failed(message: msg)
1254
+ }
1255
+ if mDb.isDBOpen() {
1298
1256
 
1299
- do {
1300
- let res: [String: Any] = try
1301
- mDb.exportToJson(
1302
- expMode: expMode,
1303
- isEncrypted: encrypted)
1304
- if res.count == 0 {
1305
- var msg: String = "return Object is empty "
1306
- msg.append("No data to synchronize")
1307
- throw CapacitorSQLiteError.failed(message: msg)
1308
- } else if res.count == 1 && res.contains(where: { $0.key == "expData" }) {
1309
- return res
1310
- } else if res.count == 5 || res.count == 6 ||
1311
- res.count == 7 {
1312
- return res
1313
- } else {
1314
- var msg: String = "return Object is not a "
1315
- msg.append("JsonSQLite Object")
1316
- throw CapacitorSQLiteError.failed(message: msg)
1317
- }
1318
- } catch DatabaseError.exportToJson(let message) {
1319
- throw CapacitorSQLiteError.failed(message: message)
1320
- } catch let error {
1321
- let msg: String = "\(error)"
1257
+ do {
1258
+ let res: [String: Any] = try
1259
+ mDb.exportToJson(
1260
+ expMode: expMode,
1261
+ isEncrypted: encrypted)
1262
+ if res.count == 0 {
1263
+ var msg: String = "return Object is empty "
1264
+ msg.append("No data to synchronize")
1265
+ throw CapacitorSQLiteError.failed(message: msg)
1266
+ } else if res.count == 1 && res.contains(where: { $0.key == "expData" }) {
1267
+ return res
1268
+ } else if res.count == 5 || res.count == 6 ||
1269
+ res.count == 7 {
1270
+ return res
1271
+ } else {
1272
+ var msg: String = "return Object is not a "
1273
+ msg.append("JsonSQLite Object")
1322
1274
  throw CapacitorSQLiteError.failed(message: msg)
1323
1275
  }
1324
- } else {
1325
- let msg = "Database \(mDbName) not opened"
1276
+ } catch DatabaseError.exportToJson(let message) {
1277
+ throw CapacitorSQLiteError.failed(message: message)
1278
+ } catch let error {
1279
+ let msg: String = "\(error)"
1326
1280
  throw CapacitorSQLiteError.failed(message: msg)
1327
1281
  }
1328
1282
  } else {
1329
- throw CapacitorSQLiteError.failed(message: initMessage)
1283
+ let msg = "Database \(mDbName) not opened"
1284
+ throw CapacitorSQLiteError.failed(message: msg)
1330
1285
  }
1331
1286
  }
1332
1287
 
1333
1288
  // MARK: - deleteExportedRows
1334
1289
 
1335
1290
  @objc func deleteExportedRows(_ dbName: String, readonly: Bool) throws {
1336
- if isInit {
1337
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
1338
- let connName: String = "RW_\(mDbName)"
1339
- guard let mDb: Database = dbDict[connName] else {
1340
- let msg = "Connection to \(mDbName) not available"
1341
- throw CapacitorSQLiteError.failed(message: msg)
1342
- }
1343
- if readonly {
1344
- let msg = "not allowed in read-only mode"
1345
- throw CapacitorSQLiteError.failed(message: msg)
1346
- }
1347
- if mDb.isDBOpen() {
1348
- do {
1349
- try mDb.deleteExportedRows()
1350
- } catch DatabaseError.deleteExportedRows(let message) {
1351
- throw CapacitorSQLiteError.failed(message: message)
1352
- } catch let error {
1353
- let msg: String = "\(error)"
1354
- throw CapacitorSQLiteError.failed(message: msg)
1355
- }
1356
- } else {
1357
- let msg = "Database \(mDbName) not opened"
1291
+ guard isInit else {
1292
+ throw CapacitorSQLiteError.failed(message: initMessage)
1293
+ }
1294
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
1295
+ let connName: String = "RW_\(mDbName)"
1296
+ guard let mDb: Database = dbDict[connName] else {
1297
+ let msg = "Connection to \(mDbName) not available"
1298
+ throw CapacitorSQLiteError.failed(message: msg)
1299
+ }
1300
+ if readonly {
1301
+ let msg = "not allowed in read-only mode"
1302
+ throw CapacitorSQLiteError.failed(message: msg)
1303
+ }
1304
+ if mDb.isDBOpen() {
1305
+ do {
1306
+ try mDb.deleteExportedRows()
1307
+ } catch DatabaseError.deleteExportedRows(let message) {
1308
+ throw CapacitorSQLiteError.failed(message: message)
1309
+ } catch let error {
1310
+ let msg: String = "\(error)"
1358
1311
  throw CapacitorSQLiteError.failed(message: msg)
1359
1312
  }
1360
1313
  } else {
1361
- throw CapacitorSQLiteError.failed(message: initMessage)
1314
+ let msg = "Database \(mDbName) not opened"
1315
+ throw CapacitorSQLiteError.failed(message: msg)
1362
1316
  }
1363
1317
  }
1364
1318
 
1365
1319
  // MARK: - createSyncTable
1366
1320
 
1367
1321
  @objc func createSyncTable(_ dbName: String, readonly: Bool) throws -> NSNumber {
1368
- if isInit {
1369
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
1370
- let connName: String = "RW_\(mDbName)"
1371
- guard let mDb: Database = dbDict[connName] else {
1372
- let msg = "Connection to \(mDbName) not available"
1373
- throw CapacitorSQLiteError.failed(message: msg)
1374
- }
1375
- if readonly {
1376
- let msg = "not allowed in read-only mode"
1377
- throw CapacitorSQLiteError.failed(message: msg)
1378
- }
1379
- if mDb.isDBOpen() {
1380
- do {
1381
- let res: Int = try mDb.createSyncTable()
1382
- return res as NSNumber
1383
- } catch DatabaseError.createSyncTable(let message) {
1384
- throw CapacitorSQLiteError.failed(message: message)
1385
- } catch let error {
1386
- let msg: String = "\(error)"
1387
- throw CapacitorSQLiteError.failed(message: msg)
1388
- }
1389
- } else {
1390
- let msg = "Database \(mDbName) not opened"
1322
+ guard isInit else {
1323
+ throw CapacitorSQLiteError.failed(message: initMessage)
1324
+ }
1325
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
1326
+ let connName: String = "RW_\(mDbName)"
1327
+ guard let mDb: Database = dbDict[connName] else {
1328
+ let msg = "Connection to \(mDbName) not available"
1329
+ throw CapacitorSQLiteError.failed(message: msg)
1330
+ }
1331
+ if readonly {
1332
+ let msg = "not allowed in read-only mode"
1333
+ throw CapacitorSQLiteError.failed(message: msg)
1334
+ }
1335
+ if mDb.isDBOpen() {
1336
+ do {
1337
+ let res: Int = try mDb.createSyncTable()
1338
+ return res as NSNumber
1339
+ } catch DatabaseError.createSyncTable(let message) {
1340
+ throw CapacitorSQLiteError.failed(message: message)
1341
+ } catch let error {
1342
+ let msg: String = "\(error)"
1391
1343
  throw CapacitorSQLiteError.failed(message: msg)
1392
1344
  }
1393
1345
  } else {
1394
- throw CapacitorSQLiteError.failed(message: initMessage)
1346
+ let msg = "Database \(mDbName) not opened"
1347
+ throw CapacitorSQLiteError.failed(message: msg)
1395
1348
  }
1396
1349
  }
1397
1350
 
@@ -1399,74 +1352,72 @@ enum CapacitorSQLiteError: Error {
1399
1352
 
1400
1353
  @objc func setSyncDate(_ dbName: String, syncDate: String, readonly: Bool)
1401
1354
  throws {
1402
- if isInit {
1403
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
1404
- let connName: String = "RW_\(mDbName)"
1405
- guard let mDb: Database = dbDict[connName] else {
1406
- let msg = "Connection to \(mDbName) not available"
1407
- throw CapacitorSQLiteError.failed(message: msg)
1408
- }
1409
- if readonly {
1410
- let msg = "not allowed in read-only mode"
1411
- throw CapacitorSQLiteError.failed(message: msg)
1412
- }
1413
- if mDb.isDBOpen() {
1355
+ guard isInit else {
1356
+ throw CapacitorSQLiteError.failed(message: initMessage)
1357
+ }
1358
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
1359
+ let connName: String = "RW_\(mDbName)"
1360
+ guard let mDb: Database = dbDict[connName] else {
1361
+ let msg = "Connection to \(mDbName) not available"
1362
+ throw CapacitorSQLiteError.failed(message: msg)
1363
+ }
1364
+ if readonly {
1365
+ let msg = "not allowed in read-only mode"
1366
+ throw CapacitorSQLiteError.failed(message: msg)
1367
+ }
1368
+ if mDb.isDBOpen() {
1414
1369
 
1415
- do {
1416
- let res: Bool = try mDb
1417
- .setSyncDate(syncDate: syncDate)
1418
- if res {
1419
- return
1420
- } else {
1421
- let msg: String = "return false"
1422
- throw CapacitorSQLiteError.failed(message: msg)
1423
- }
1424
- } catch DatabaseError.createSyncDate(let message) {
1425
- throw CapacitorSQLiteError.failed(message: message)
1426
- } catch let error {
1427
- let msg: String = "\(error)"
1370
+ do {
1371
+ let res: Bool = try mDb
1372
+ .setSyncDate(syncDate: syncDate)
1373
+ if res {
1374
+ return
1375
+ } else {
1376
+ let msg: String = "return false"
1428
1377
  throw CapacitorSQLiteError.failed(message: msg)
1429
1378
  }
1430
- } else {
1431
- let msg = "Database \(mDbName) not opened"
1379
+ } catch DatabaseError.createSyncDate(let message) {
1380
+ throw CapacitorSQLiteError.failed(message: message)
1381
+ } catch let error {
1382
+ let msg: String = "\(error)"
1432
1383
  throw CapacitorSQLiteError.failed(message: msg)
1433
1384
  }
1434
1385
  } else {
1435
- throw CapacitorSQLiteError.failed(message: initMessage)
1386
+ let msg = "Database \(mDbName) not opened"
1387
+ throw CapacitorSQLiteError.failed(message: msg)
1436
1388
  }
1437
1389
  }
1438
1390
 
1439
1391
  // MARK: - getSyncDate
1440
1392
 
1441
1393
  @objc func getSyncDate(_ dbName: String, readonly: Bool) throws -> NSNumber {
1442
- if isInit {
1443
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
1444
- let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
1445
- guard let mDb: Database = dbDict[connName] else {
1446
- let msg = "Connection to \(mDbName) not available"
1447
- throw CapacitorSQLiteError.failed(message: msg)
1448
- }
1449
- if mDb.isDBOpen() {
1450
- do {
1451
- let res: Int64 = try mDb.getSyncDate()
1452
- if res > 0 {
1453
- return res as NSNumber
1454
- } else {
1455
- let msg: String = "return no sync date"
1456
- throw CapacitorSQLiteError.failed(message: msg)
1457
- }
1458
- } catch DatabaseError.getSyncDate(let message) {
1459
- throw CapacitorSQLiteError.failed(message: message)
1460
- } catch let error {
1461
- let msg: String = "\(error)"
1394
+ guard isInit else {
1395
+ throw CapacitorSQLiteError.failed(message: initMessage)
1396
+ }
1397
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
1398
+ let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
1399
+ guard let mDb: Database = dbDict[connName] else {
1400
+ let msg = "Connection to \(mDbName) not available"
1401
+ throw CapacitorSQLiteError.failed(message: msg)
1402
+ }
1403
+ if mDb.isDBOpen() {
1404
+ do {
1405
+ let res: Int64 = try mDb.getSyncDate()
1406
+ if res > 0 {
1407
+ return res as NSNumber
1408
+ } else {
1409
+ let msg: String = "return no sync date"
1462
1410
  throw CapacitorSQLiteError.failed(message: msg)
1463
1411
  }
1464
- } else {
1465
- let msg = "Database \(mDbName) not opened"
1412
+ } catch DatabaseError.getSyncDate(let message) {
1413
+ throw CapacitorSQLiteError.failed(message: message)
1414
+ } catch let error {
1415
+ let msg: String = "\(error)"
1466
1416
  throw CapacitorSQLiteError.failed(message: msg)
1467
1417
  }
1468
1418
  } else {
1469
- throw CapacitorSQLiteError.failed(message: initMessage)
1419
+ let msg = "Database \(mDbName) not opened"
1420
+ throw CapacitorSQLiteError.failed(message: msg)
1470
1421
  }
1471
1422
  }
1472
1423
 
@@ -1475,87 +1426,85 @@ enum CapacitorSQLiteError: Error {
1475
1426
  @objc func addUpgradeStatement(_ dbName: String,
1476
1427
  upgrade: [[String: Any]])
1477
1428
  throws -> [Int: [String: Any]] {
1478
- if isInit {
1479
- var upgDict: [String: Any] = [:]
1480
- var upgVersionDict: [Int: [String: Any]] = [:]
1481
- for dict in upgrade {
1482
- let keys = dict.keys
1483
- if !(keys.contains("toVersion")) || !(keys.contains("statements")) {
1484
- var msg: String = "upgrade must have keys in "
1485
- msg.append("{toVersion,statements}")
1486
- throw CapacitorSQLiteError.failed(message: msg)
1487
- }
1488
- for (key, value) in dict {
1489
- upgDict[key] = value
1490
- }
1491
- guard let toVersion = upgDict["toVersion"] as? Int else {
1492
- let msg: String = "toVersion key must be an Int"
1493
- throw CapacitorSQLiteError.failed(message: msg)
1494
- }
1495
- upgVersionDict[toVersion] = upgDict
1496
- }
1497
- return upgVersionDict
1498
- } else {
1429
+ guard isInit else {
1499
1430
  throw CapacitorSQLiteError.failed(message: initMessage)
1500
1431
  }
1432
+ var upgDict: [String: Any] = [:]
1433
+ var upgVersionDict: [Int: [String: Any]] = [:]
1434
+ for dict in upgrade {
1435
+ let keys = dict.keys
1436
+ if !(keys.contains("toVersion")) || !(keys.contains("statements")) {
1437
+ var msg: String = "upgrade must have keys in "
1438
+ msg.append("{toVersion,statements}")
1439
+ throw CapacitorSQLiteError.failed(message: msg)
1440
+ }
1441
+ for (key, value) in dict {
1442
+ upgDict[key] = value
1443
+ }
1444
+ guard let toVersion = upgDict["toVersion"] as? Int else {
1445
+ let msg: String = "toVersion key must be an Int"
1446
+ throw CapacitorSQLiteError.failed(message: msg)
1447
+ }
1448
+ upgVersionDict[toVersion] = upgDict
1449
+ }
1450
+ return upgVersionDict
1501
1451
  }
1502
1452
 
1503
1453
  // MARK: - copyFromAssets
1504
1454
 
1505
1455
  @objc func copyFromAssets(overwrite: Bool) throws {
1506
- if isInit {
1456
+ guard isInit else {
1457
+ throw CapacitorSQLiteError.failed(message: initMessage)
1458
+ }
1507
1459
 
1508
- // check if the assets/database folder exists
1509
- do {
1510
- let assetsDbURL: URL = try
1511
- UtilsFile.getAssetsDatabasesPath()
1512
- let aPath: String = assetsDbURL.path
1513
- let bRes: Bool = UtilsFile.isDirExist(dirPath: aPath)
1514
- if bRes {
1515
- // get the database files from assets
1516
- let dbList: [String] = try UtilsFile
1517
- .getFileList(path: aPath, ext: ".db")
1518
- // loop through the database files
1519
- for mDb in dbList {
1520
- // for each check if the suffix SQLite.db is there
1521
- // or add it
1522
- let toDb: String = UtilsFile
1523
- .setPathSuffix(sDb: mDb)
1524
- // for each copy the file to the Application
1525
- // database folder
1526
- _ = try UtilsFile
1527
- .copyFromAssetToDatabase(databaseLocation: databaseLocation,
1528
- fromDb: mDb,
1529
- toDb: toDb, overwrite: overwrite)
1530
- }
1531
- // get the zip files
1532
- let zipList: [String] = try UtilsFile
1533
- .getFileList(path: aPath, ext: ".zip")
1534
- // loop through the database files
1535
- for zip in zipList {
1536
- // for each zip uncompress the file to the Application
1537
- // database folder
1538
- _ = try UtilsFile.unzipToDatabase(
1539
- fromURL: assetsDbURL,
1540
- databaseLocation: databaseLocation,
1541
- zip: zip,
1542
- overwrite: overwrite)
1543
- }
1544
- return
1545
- } else {
1546
- let msg: String = "assets database path does not exist"
1547
- throw CapacitorSQLiteError.failed(message: msg)
1460
+ // check if the assets/database folder exists
1461
+ do {
1462
+ let assetsDbURL: URL = try
1463
+ UtilsFile.getAssetsDatabasesPath()
1464
+ let aPath: String = assetsDbURL.path
1465
+ let bRes: Bool = UtilsFile.isDirExist(dirPath: aPath)
1466
+ if bRes {
1467
+ // get the database files from assets
1468
+ let dbList: [String] = try UtilsFile
1469
+ .getFileList(path: aPath, ext: ".db")
1470
+ // loop through the database files
1471
+ for mDb in dbList {
1472
+ // for each check if the suffix SQLite.db is there
1473
+ // or add it
1474
+ let toDb: String = UtilsFile
1475
+ .setPathSuffix(sDb: mDb)
1476
+ // for each copy the file to the Application
1477
+ // database folder
1478
+ _ = try UtilsFile
1479
+ .copyFromAssetToDatabase(databaseLocation: databaseLocation,
1480
+ fromDb: mDb,
1481
+ toDb: toDb, overwrite: overwrite)
1548
1482
  }
1549
- } catch UtilsFileError.copyFromAssetToDatabaseFailed(let message) {
1550
- throw CapacitorSQLiteError.failed(message: message)
1551
- } catch UtilsFileError.unzipToDatabaseFailed(let message) {
1552
- throw CapacitorSQLiteError.failed(message: message)
1553
- } catch let error {
1554
- let msg: String = "\(error)"
1483
+ // get the zip files
1484
+ let zipList: [String] = try UtilsFile
1485
+ .getFileList(path: aPath, ext: ".zip")
1486
+ // loop through the database files
1487
+ for zip in zipList {
1488
+ // for each zip uncompress the file to the Application
1489
+ // database folder
1490
+ _ = try UtilsFile.unzipToDatabase(
1491
+ fromURL: assetsDbURL,
1492
+ databaseLocation: databaseLocation,
1493
+ zip: zip,
1494
+ overwrite: overwrite)
1495
+ }
1496
+ return
1497
+ } else {
1498
+ let msg: String = "assets database path does not exist"
1555
1499
  throw CapacitorSQLiteError.failed(message: msg)
1556
1500
  }
1557
- } else {
1558
- throw CapacitorSQLiteError.failed(message: initMessage)
1501
+ } catch UtilsFileError.copyFromAssetToDatabaseFailed(let message) {
1502
+ throw CapacitorSQLiteError.failed(message: message)
1503
+ } catch UtilsFileError.unzipToDatabaseFailed(let message) {
1504
+ throw CapacitorSQLiteError.failed(message: message)
1505
+ } catch let error {
1506
+ let msg: String = "\(error)"
1507
+ throw CapacitorSQLiteError.failed(message: msg)
1559
1508
  }
1560
1509
  }
1561
1510
 
@@ -1563,143 +1512,137 @@ enum CapacitorSQLiteError: Error {
1563
1512
 
1564
1513
  @objc func getTableList(_ dbName: String, readonly: Bool)
1565
1514
  throws -> [String] {
1566
- if isInit {
1567
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
1568
- let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
1569
- guard let mDb: Database = dbDict[connName] else {
1570
- let msg = "Connection to \(mDbName) not available"
1571
- throw CapacitorSQLiteError.failed(message: msg)
1572
- }
1573
- if mDb.isDBOpen() {
1574
- do {
1575
- let res: [String] = try mDb.getTableNames()
1576
- return res
1577
- } catch DatabaseError.getTableNames(let message) {
1578
- throw CapacitorSQLiteError.failed(message: message)
1579
- } catch let error {
1580
- let msg: String = "\(error)"
1581
- throw CapacitorSQLiteError.failed(message: msg)
1582
- }
1583
- } else {
1584
- let msg = "Database \(mDbName) not opened"
1515
+ guard isInit else {
1516
+ throw CapacitorSQLiteError.failed(message: initMessage)
1517
+ }
1518
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
1519
+ let connName: String = readonly ? "RO_\(mDbName)" : "RW_\(mDbName)"
1520
+ guard let mDb: Database = dbDict[connName] else {
1521
+ let msg = "Connection to \(mDbName) not available"
1522
+ throw CapacitorSQLiteError.failed(message: msg)
1523
+ }
1524
+ if mDb.isDBOpen() {
1525
+ do {
1526
+ let res: [String] = try mDb.getTableNames()
1527
+ return res
1528
+ } catch DatabaseError.getTableNames(let message) {
1529
+ throw CapacitorSQLiteError.failed(message: message)
1530
+ } catch let error {
1531
+ let msg: String = "\(error)"
1585
1532
  throw CapacitorSQLiteError.failed(message: msg)
1586
1533
  }
1587
1534
  } else {
1588
- throw CapacitorSQLiteError.failed(message: initMessage)
1535
+ let msg = "Database \(mDbName) not opened"
1536
+ throw CapacitorSQLiteError.failed(message: msg)
1589
1537
  }
1590
1538
  }
1591
1539
 
1592
1540
  // MARK: - getDatabaseList
1593
1541
 
1594
1542
  @objc func getDatabaseList() throws -> [String] {
1595
- if isInit {
1596
- do {
1597
- let aPath: String = try (UtilsFile.getFolderURL(folderPath: databaseLocation)).path
1598
- // get the database files
1599
- let dbList: [String] = try UtilsFile.getFileList(path: aPath, ext: ".db")
1600
- return dbList
1601
-
1602
- } catch let error {
1603
- let msg: String = "\(error)"
1604
- throw CapacitorSQLiteError.failed(message: msg)
1605
- }
1606
- } else {
1543
+ guard isInit else {
1607
1544
  throw CapacitorSQLiteError.failed(message: initMessage)
1608
1545
  }
1546
+ do {
1547
+ let aPath: String = try (UtilsFile.getFolderURL(folderPath: databaseLocation)).path
1548
+ // get the database files
1549
+ let dbList: [String] = try UtilsFile.getFileList(path: aPath, ext: ".db")
1550
+ return dbList
1551
+
1552
+ } catch let error {
1553
+ let msg: String = "\(error)"
1554
+ throw CapacitorSQLiteError.failed(message: msg)
1555
+ }
1609
1556
  }
1610
1557
 
1611
1558
  // MARK: - getMigratableDbList
1612
1559
 
1613
1560
  @objc func getMigratableDbList(_ folderPath: String) throws -> [String] {
1614
- if isInit {
1615
- do {
1616
- let dbList: [String] = try UtilsMigrate
1617
- .getMigratableList(folderPath: folderPath)
1618
- return dbList
1619
-
1620
- } catch UtilsMigrateError.getMigratableList(let message) {
1621
- var msg: String = "getMigratableList:"
1622
- msg.append(" \(message)")
1623
- throw CapacitorSQLiteError.failed(message: msg)
1624
- } catch let error {
1625
- let msg: String = "\(error)"
1626
- throw CapacitorSQLiteError.failed(message: msg)
1627
- }
1628
- } else {
1561
+ guard isInit else {
1629
1562
  throw CapacitorSQLiteError.failed(message: initMessage)
1630
1563
  }
1564
+ do {
1565
+ let dbList: [String] = try UtilsMigrate
1566
+ .getMigratableList(folderPath: folderPath)
1567
+ return dbList
1568
+
1569
+ } catch UtilsMigrateError.getMigratableList(let message) {
1570
+ var msg: String = "getMigratableList:"
1571
+ msg.append(" \(message)")
1572
+ throw CapacitorSQLiteError.failed(message: msg)
1573
+ } catch let error {
1574
+ let msg: String = "\(error)"
1575
+ throw CapacitorSQLiteError.failed(message: msg)
1576
+ }
1631
1577
  }
1632
1578
 
1633
1579
  // MARK: - addSQLiteSuffix
1634
1580
 
1635
1581
  @objc func addSQLiteSuffix(_ folderPath: String, dbList: [String]) throws {
1636
- if isInit {
1637
- do {
1638
- try UtilsMigrate.addSQLiteSuffix(databaseLocation: databaseLocation,
1639
- folderPath: folderPath,
1640
- dbList: dbList)
1641
- return
1642
- } catch UtilsMigrateError.addSQLiteSuffix(let message) {
1643
- var msg: String = "addSQLiteSuffix:"
1644
- msg.append(" \(message)")
1645
- throw CapacitorSQLiteError.failed(message: msg)
1646
-
1647
- } catch let error {
1648
- var msg: String = "addSQLiteSuffix:"
1649
- msg.append(" \(error)")
1650
- throw CapacitorSQLiteError.failed(message: msg)
1651
- }
1652
- } else {
1582
+ guard isInit else {
1653
1583
  throw CapacitorSQLiteError.failed(message: initMessage)
1654
1584
  }
1585
+ do {
1586
+ try UtilsMigrate.addSQLiteSuffix(databaseLocation: databaseLocation,
1587
+ folderPath: folderPath,
1588
+ dbList: dbList)
1589
+ return
1590
+ } catch UtilsMigrateError.addSQLiteSuffix(let message) {
1591
+ var msg: String = "addSQLiteSuffix:"
1592
+ msg.append(" \(message)")
1593
+ throw CapacitorSQLiteError.failed(message: msg)
1594
+
1595
+ } catch let error {
1596
+ var msg: String = "addSQLiteSuffix:"
1597
+ msg.append(" \(error)")
1598
+ throw CapacitorSQLiteError.failed(message: msg)
1599
+ }
1655
1600
  }
1656
1601
 
1657
1602
  // MARK: - deleteOldDatabases
1658
1603
 
1659
1604
  @objc func deleteOldDatabases(_ folderPath: String, dbList: [String]) throws {
1660
- if isInit {
1661
- do {
1662
- try UtilsMigrate
1663
- .deleteOldDatabases(folderPath: folderPath, dbList: dbList)
1664
- return
1665
- } catch UtilsMigrateError.deleteOldDatabases(let message) {
1666
- var msg: String = "deleteOldDatabases:"
1667
- msg.append(" \(message)")
1668
- throw CapacitorSQLiteError.failed(message: msg)
1669
-
1670
- } catch let error {
1671
- var msg: String = "deleteOldDatabases:"
1672
- msg.append(" \(error)")
1673
- throw CapacitorSQLiteError.failed(message: msg)
1674
- }
1675
- } else {
1605
+ guard isInit else {
1676
1606
  throw CapacitorSQLiteError.failed(message: initMessage)
1677
1607
  }
1608
+ do {
1609
+ try UtilsMigrate
1610
+ .deleteOldDatabases(folderPath: folderPath, dbList: dbList)
1611
+ return
1612
+ } catch UtilsMigrateError.deleteOldDatabases(let message) {
1613
+ var msg: String = "deleteOldDatabases:"
1614
+ msg.append(" \(message)")
1615
+ throw CapacitorSQLiteError.failed(message: msg)
1616
+
1617
+ } catch let error {
1618
+ var msg: String = "deleteOldDatabases:"
1619
+ msg.append(" \(error)")
1620
+ throw CapacitorSQLiteError.failed(message: msg)
1621
+ }
1678
1622
  }
1679
1623
 
1680
1624
  // MARK: - moveDatabasesAndAddSuffix
1681
1625
 
1682
1626
  @objc func moveDatabasesAndAddSuffix(_ folderPath: String, dbList: [String]) throws {
1683
- if isInit {
1684
- do {
1685
- try UtilsMigrate
1686
- .moveDatabasesAndAddSuffix(databaseLocation: databaseLocation,
1687
- folderPath: folderPath,
1688
- dbList: dbList)
1689
- return
1690
- } catch UtilsMigrateError.moveDatabasesAndAddSuffix(let message) {
1691
- var msg: String = "moveDatabasesAndAddSuffix:"
1692
- msg.append(" \(message)")
1693
- throw CapacitorSQLiteError.failed(message: msg)
1694
-
1695
- } catch let error {
1696
- var msg: String = "moveDatabasesAndAddSuffix:"
1697
- msg.append(" \(error)")
1698
- throw CapacitorSQLiteError.failed(message: msg)
1699
- }
1700
- } else {
1627
+ guard isInit else {
1701
1628
  throw CapacitorSQLiteError.failed(message: initMessage)
1702
1629
  }
1630
+ do {
1631
+ try UtilsMigrate
1632
+ .moveDatabasesAndAddSuffix(databaseLocation: databaseLocation,
1633
+ folderPath: folderPath,
1634
+ dbList: dbList)
1635
+ return
1636
+ } catch UtilsMigrateError.moveDatabasesAndAddSuffix(let message) {
1637
+ var msg: String = "moveDatabasesAndAddSuffix:"
1638
+ msg.append(" \(message)")
1639
+ throw CapacitorSQLiteError.failed(message: msg)
1640
+
1641
+ } catch let error {
1642
+ var msg: String = "moveDatabasesAndAddSuffix:"
1643
+ msg.append(" \(error)")
1644
+ throw CapacitorSQLiteError.failed(message: msg)
1645
+ }
1703
1646
  }
1704
1647
 
1705
1648
  class func getDatabaseName(dbName: String) -> String {