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

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.
@@ -8,13 +8,27 @@ enum CapacitorSQLiteError: Error {
8
8
  private var config: SqliteConfig
9
9
  private var dbDict: [String: Database] = [:]
10
10
  private var databaseLocation: String
11
+ private var initMessage: String = ""
12
+ private var isInit: Bool = false
11
13
 
12
14
  init(config: SqliteConfig) {
13
15
  self.config = config
14
16
  if let isLocation = config.iosDatabaseLocation {
15
17
  self.databaseLocation = isLocation
18
+ // create the databaseLocation directory
19
+ do {
20
+ try UtilsFile.createDatabaseLocation(location: isLocation)
21
+ isInit = true
22
+ } catch UtilsFileError.createDatabaseLocationFailed(let message) {
23
+ initMessage = message
24
+ } catch let error {
25
+ initMessage = "Init Plugin failed :"
26
+ initMessage.append(" \(error.localizedDescription)")
27
+ }
28
+
16
29
  } else {
17
30
  self.databaseLocation = "Documents"
31
+ isInit = true
18
32
  }
19
33
  super.init()
20
34
  }
@@ -28,29 +42,37 @@ enum CapacitorSQLiteError: Error {
28
42
  // MARK: - IsSecretStored
29
43
 
30
44
  @objc public func isSecretStored() throws -> NSNumber {
31
- let isSecretExists: Bool = UtilsSecret.isPassphrase()
32
- if isSecretExists {
33
- return 1
45
+ if isInit {
46
+ let isSecretExists: Bool = UtilsSecret.isPassphrase()
47
+ if isSecretExists {
48
+ return 1
49
+ } else {
50
+ return 0
51
+ }
34
52
  } else {
35
- return 0
53
+ throw CapacitorSQLiteError.failed(message: initMessage)
36
54
  }
37
55
  }
38
56
 
39
57
  // MARK: - SetEncryptionSecret
40
58
 
41
59
  @objc public func setEncryptionSecret(passphrase: String) throws {
42
- do {
43
- // close all connections
44
- try closeAllConnections()
45
- // set encryption secret
46
- try UtilsSecret
47
- .setEncryptionSecret(passphrase: passphrase,
48
- databaseLocation: databaseLocation)
49
- return
50
- } catch UtilsSecretError.setEncryptionSecret(let message) {
51
- throw CapacitorSQLiteError.failed(message: message)
52
- } catch let error {
53
- throw CapacitorSQLiteError.failed(message: "\(error)")
60
+ if isInit {
61
+ do {
62
+ // close all connections
63
+ try closeAllConnections()
64
+ // set encryption secret
65
+ try UtilsSecret
66
+ .setEncryptionSecret(passphrase: passphrase,
67
+ databaseLocation: databaseLocation)
68
+ return
69
+ } catch UtilsSecretError.setEncryptionSecret(let message) {
70
+ throw CapacitorSQLiteError.failed(message: message)
71
+ } catch let error {
72
+ throw CapacitorSQLiteError.failed(message: "\(error)")
73
+ }
74
+ } else {
75
+ throw CapacitorSQLiteError.failed(message: initMessage)
54
76
  }
55
77
  }
56
78
 
@@ -58,83 +80,98 @@ enum CapacitorSQLiteError: Error {
58
80
 
59
81
  @objc public func changeEncryptionSecret(passphrase: String,
60
82
  oldPassphrase: String) throws {
61
- do {
62
- // close all connections
63
- try closeAllConnections()
64
- // set encryption secret
65
- try UtilsSecret
66
- .changeEncryptionSecret(passphrase: passphrase,
67
- oldPassphrase: oldPassphrase,
68
- databaseLocation: databaseLocation)
69
- return
70
- } catch UtilsSecretError.changeEncryptionSecret(let message) {
71
- throw CapacitorSQLiteError.failed(message: message)
72
- } catch let error {
73
- throw CapacitorSQLiteError.failed(message: "\(error)")
83
+ if isInit {
84
+ do {
85
+ // close all connections
86
+ try closeAllConnections()
87
+ // set encryption secret
88
+ try UtilsSecret
89
+ .changeEncryptionSecret(passphrase: passphrase,
90
+ oldPassphrase: oldPassphrase,
91
+ databaseLocation: databaseLocation)
92
+ return
93
+ } catch UtilsSecretError.changeEncryptionSecret(let message) {
94
+ throw CapacitorSQLiteError.failed(message: message)
95
+ } catch let error {
96
+ throw CapacitorSQLiteError.failed(message: "\(error)")
97
+ }
98
+ } else {
99
+ throw CapacitorSQLiteError.failed(message: initMessage)
74
100
  }
75
101
 
76
102
  }
77
103
  // MARK: - getNCDatabasePath
78
104
 
79
105
  @objc public func getNCDatabasePath(_ folderPath: String, dbName: String ) throws -> String {
80
- do {
81
- let databasePath: String = try UtilsNCDatabase
82
- .getNCDatabasePath(folderPath: folderPath,
83
- database: dbName )
84
- return databasePath
85
- } catch let error {
86
- throw CapacitorSQLiteError.failed(message: "\(error)")
106
+ if isInit {
107
+ do {
108
+ let databasePath: String = try UtilsNCDatabase
109
+ .getNCDatabasePath(folderPath: folderPath,
110
+ database: dbName )
111
+ return databasePath
112
+ } catch let error {
113
+ throw CapacitorSQLiteError.failed(message: "\(error)")
114
+ }
115
+ } else {
116
+ throw CapacitorSQLiteError.failed(message: initMessage)
87
117
  }
88
-
89
118
  }
90
119
 
91
120
  // MARK: - CreateNCConnection
92
121
 
93
122
  @objc public func createNCConnection(_ databasePath: String,
94
123
  version: Int) throws {
124
+ if isInit {
95
125
 
96
- // check if the connection already exists
97
- let conn = dbDict[databasePath]
98
- if conn != nil {
99
- let msg = "Connection \(databasePath) already exists"
100
- throw CapacitorSQLiteError.failed(message: msg)
101
- }
126
+ // check if the connection already exists
127
+ let conn = dbDict[databasePath]
128
+ if conn != nil {
129
+ let msg = "Connection \(databasePath) already exists"
130
+ throw CapacitorSQLiteError.failed(message: msg)
131
+ }
102
132
 
103
- do {
104
- let isFileExists: Bool = UtilsFile
105
- .isFileExist(filePath: databasePath)
133
+ do {
134
+ let isFileExists: Bool = UtilsFile
135
+ .isFileExist(filePath: databasePath)
106
136
 
107
- if !isFileExists {
108
- throw CapacitorSQLiteError.failed(message: "database \(databasePath) does not exist")
137
+ if !isFileExists {
138
+ throw CapacitorSQLiteError.failed(message: "database \(databasePath) does not exist")
139
+ }
140
+ let mDb: Database = try Database(
141
+ databaseLocation: databaseLocation,
142
+ databaseName: databasePath,
143
+ encrypted: false, mode: "no-encryption", version: version,
144
+ vUpgDict: [:])
145
+ dbDict[databasePath] = mDb
146
+ return
147
+ } catch let error {
148
+ throw CapacitorSQLiteError.failed(message: "\(error)")
109
149
  }
110
- let mDb: Database = try Database(
111
- databaseLocation: databaseLocation,
112
- databaseName: databasePath,
113
- encrypted: false, mode: "no-encryption", version: version,
114
- vUpgDict: [:])
115
- dbDict[databasePath] = mDb
116
- return
117
- } catch let error {
118
- throw CapacitorSQLiteError.failed(message: "\(error)")
150
+ } else {
151
+ throw CapacitorSQLiteError.failed(message: initMessage)
119
152
  }
120
153
  }
121
154
 
122
155
  // MARK: - CloseNCConnection
123
156
 
124
157
  @objc public func closeNCConnection(_ dbName: String) throws {
125
- guard let mDb: Database = dbDict[dbName] else {
126
- let msg = "Connection to \(dbName) not available"
127
- throw CapacitorSQLiteError.failed(message: msg)
128
- }
129
- if mDb.isDBOpen() {
130
- do {
131
- try mDb.close()
132
- } catch DatabaseError.close(let message) {
133
- throw CapacitorSQLiteError.failed(message: message)
158
+ if isInit {
159
+ guard let mDb: Database = dbDict[dbName] else {
160
+ let msg = "Connection to \(dbName) not available"
161
+ throw CapacitorSQLiteError.failed(message: msg)
134
162
  }
163
+ if mDb.isDBOpen() {
164
+ do {
165
+ try mDb.close()
166
+ } catch DatabaseError.close(let message) {
167
+ throw CapacitorSQLiteError.failed(message: message)
168
+ }
169
+ }
170
+ dbDict.removeValue(forKey: dbName)
171
+ return
172
+ } else {
173
+ throw CapacitorSQLiteError.failed(message: initMessage)
135
174
  }
136
- dbDict.removeValue(forKey: dbName)
137
- return
138
175
  }
139
176
 
140
177
  // MARK: - CreateConnection
@@ -144,188 +181,240 @@ enum CapacitorSQLiteError: Error {
144
181
  mode: String,
145
182
  version: Int,
146
183
  vUpgDict: [Int: [String: Any]]) throws {
147
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
148
- // check if the connection already exists
149
- let conn = dbDict[mDbName]
150
- if conn != nil {
151
- let msg = "Connection \(mDbName) already exists"
152
- throw CapacitorSQLiteError.failed(message: msg)
153
- }
154
-
155
- do {
156
- let mDb: Database = try Database(
157
- databaseLocation: databaseLocation,
158
- databaseName: "\(mDbName)SQLite.db",
159
- encrypted: encrypted, mode: mode, version: version,
160
- vUpgDict: vUpgDict)
161
- dbDict[mDbName] = mDb
162
- return
163
- } catch let error {
164
- throw CapacitorSQLiteError.failed(message: "\(error)")
184
+ if isInit {
185
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
186
+ // check if the connection already exists
187
+ let conn = dbDict[mDbName]
188
+ if conn != nil {
189
+ let msg = "Connection \(mDbName) already exists"
190
+ throw CapacitorSQLiteError.failed(message: msg)
191
+ }
192
+
193
+ do {
194
+ let mDb: Database = try Database(
195
+ databaseLocation: databaseLocation,
196
+ databaseName: "\(mDbName)SQLite.db",
197
+ encrypted: encrypted, mode: mode, version: version,
198
+ vUpgDict: vUpgDict)
199
+ dbDict[mDbName] = mDb
200
+ return
201
+ } catch let error {
202
+ throw CapacitorSQLiteError.failed(message: "\(error)")
203
+ }
204
+ } else {
205
+ throw CapacitorSQLiteError.failed(message: initMessage)
165
206
  }
166
207
  }
167
208
 
168
209
  // MARK: - Open
169
210
 
170
211
  @objc public func open(_ dbName: String) throws {
171
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
172
- guard let mDb: Database = dbDict[mDbName] else {
173
- let msg = "Connection to \(mDbName) not available"
174
- throw CapacitorSQLiteError.failed(message: msg)
175
- }
176
- do {
177
- try mDb.open()
178
- return
179
- } catch DatabaseError.open(let message) {
180
- throw CapacitorSQLiteError.failed(message: message)
212
+ if isInit {
213
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
214
+ guard let mDb: Database = dbDict[mDbName] else {
215
+ let msg = "Connection to \(mDbName) not available"
216
+ throw CapacitorSQLiteError.failed(message: msg)
217
+ }
218
+ do {
219
+ try mDb.open()
220
+ return
221
+ } catch DatabaseError.open(let message) {
222
+ throw CapacitorSQLiteError.failed(message: message)
223
+ }
224
+ } else {
225
+ throw CapacitorSQLiteError.failed(message: initMessage)
181
226
  }
182
227
  }
183
228
 
184
229
  // MARK: - Close
185
230
 
186
231
  @objc public func close(_ dbName: String) throws {
187
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
188
- guard let mDb: Database = dbDict[mDbName] else {
189
- let msg = "Connection to \(mDbName) not available"
190
- throw CapacitorSQLiteError.failed(message: msg)
232
+ if isInit {
233
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
234
+ guard let mDb: Database = dbDict[mDbName] else {
235
+ let msg = "Connection to \(mDbName) not available"
236
+ throw CapacitorSQLiteError.failed(message: msg)
237
+ }
238
+ do {
239
+ try mDb.close()
240
+ return
241
+ } catch DatabaseError.close(let message) {
242
+ throw CapacitorSQLiteError.failed(message: message)
243
+ }
244
+ } else {
245
+ throw CapacitorSQLiteError.failed(message: initMessage)
191
246
  }
192
- do {
193
- try mDb.close()
194
- return
195
- } catch DatabaseError.close(let message) {
196
- throw CapacitorSQLiteError.failed(message: message)
247
+ }
248
+
249
+ // MARK: - getUrl
250
+
251
+ @objc public func getUrl(_ dbName: String) throws -> String {
252
+ if isInit {
253
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
254
+ guard let mDb: Database = dbDict[mDbName] else {
255
+ let msg = "Connection to \(mDbName) not available"
256
+ throw CapacitorSQLiteError.failed(message: msg)
257
+ }
258
+ let res: String = mDb.getUrl()
259
+ return res
260
+ } else {
261
+ throw CapacitorSQLiteError.failed(message: initMessage)
197
262
  }
198
263
  }
199
264
 
200
265
  // MARK: - GetVersion
201
266
 
202
267
  @objc public func getVersion(_ dbName: String) throws -> NSNumber {
203
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
204
- guard let mDb: Database = dbDict[mDbName] else {
205
- let msg = "Connection to \(mDbName) not available"
206
- throw CapacitorSQLiteError.failed(message: msg)
207
- }
208
- do {
209
- let version: Int = try mDb.getVersion()
210
- return NSNumber(value: version)
268
+ if isInit {
269
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
270
+ guard let mDb: Database = dbDict[mDbName] else {
271
+ let msg = "Connection to \(mDbName) not available"
272
+ throw CapacitorSQLiteError.failed(message: msg)
273
+ }
274
+ do {
275
+ let version: Int = try mDb.getVersion()
276
+ return NSNumber(value: version)
211
277
 
212
- } catch DatabaseError.open(let message) {
213
- throw CapacitorSQLiteError.failed(message: message)
278
+ } catch DatabaseError.open(let message) {
279
+ throw CapacitorSQLiteError.failed(message: message)
280
+ }
281
+ } else {
282
+ throw CapacitorSQLiteError.failed(message: initMessage)
214
283
  }
215
284
  }
216
285
 
217
286
  // MARK: - Close Connection
218
287
 
219
288
  @objc public func closeConnection(_ dbName: String) throws {
220
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
221
- guard let mDb: Database = dbDict[mDbName] else {
222
- let msg = "Connection to \(mDbName) not available"
223
- throw CapacitorSQLiteError.failed(message: msg)
224
- }
225
- if mDb.isDBOpen() {
226
- do {
227
- try mDb.close()
228
- } catch DatabaseError.close(let message) {
229
- throw CapacitorSQLiteError.failed(message: message)
289
+ if isInit {
290
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
291
+ guard let mDb: Database = dbDict[mDbName] else {
292
+ let msg = "Connection to \(mDbName) not available"
293
+ throw CapacitorSQLiteError.failed(message: msg)
294
+ }
295
+ if mDb.isDBOpen() {
296
+ do {
297
+ try mDb.close()
298
+ } catch DatabaseError.close(let message) {
299
+ throw CapacitorSQLiteError.failed(message: message)
300
+ }
230
301
  }
302
+ dbDict.removeValue(forKey: mDbName)
303
+ return
304
+ } else {
305
+ throw CapacitorSQLiteError.failed(message: initMessage)
231
306
  }
232
- dbDict.removeValue(forKey: mDbName)
233
- return
234
307
  }
235
308
 
236
309
  // MARK: - CheckConnectionsConsistency
237
310
 
238
311
  @objc public func checkConnectionsConsistency(_ dbNames: [String]) throws -> NSNumber {
239
- var keys: [String] = Array(self.dbDict.keys)
240
- do {
241
- if dbNames.count == 0 {
242
- try closeAllConnections()
243
- return 0
244
- }
245
- if keys.count < dbNames.count {
246
- // not solvable inconsistency
247
- try closeAllConnections()
248
- return 0
249
- }
250
- if keys.count > dbNames.count {
251
- for key in keys {
252
- if !dbNames.contains(key) {
253
- self.dbDict.removeValue(forKey: key)
312
+ if isInit {
313
+ var keys: [String] = Array(self.dbDict.keys)
314
+ do {
315
+ if dbNames.count == 0 {
316
+ try closeAllConnections()
317
+ return 0
318
+ }
319
+ if keys.count < dbNames.count {
320
+ // not solvable inconsistency
321
+ try closeAllConnections()
322
+ return 0
323
+ }
324
+ if keys.count > dbNames.count {
325
+ for key in keys {
326
+ if !dbNames.contains(key) {
327
+ self.dbDict.removeValue(forKey: key)
328
+ }
254
329
  }
255
330
  }
256
- }
257
- keys = Array(self.dbDict.keys)
258
- if keys.count == dbNames.count {
259
- let set1 = Set(keys)
260
- let set2 = Set(dbNames)
261
- let arr = Array(set1.symmetricDifference(set2))
262
- if arr.count == 0 {
263
- return 1
331
+ keys = Array(self.dbDict.keys)
332
+ if keys.count == dbNames.count {
333
+ let set1 = Set(keys)
334
+ let set2 = Set(dbNames)
335
+ let arr = Array(set1.symmetricDifference(set2))
336
+ if arr.count == 0 {
337
+ return 1
338
+ } else {
339
+ // not solvable inconsistency
340
+ try closeAllConnections()
341
+ return 0
342
+ }
264
343
  } else {
265
- // not solvable inconsistency
266
344
  try closeAllConnections()
267
345
  return 0
268
346
  }
269
- } else {
270
- try closeAllConnections()
271
- return 0
347
+ } catch let error {
348
+ throw CapacitorSQLiteError.failed(message: "\(error)")
272
349
  }
273
- } catch let error {
274
- throw CapacitorSQLiteError.failed(message: "\(error)")
350
+ } else {
351
+ throw CapacitorSQLiteError.failed(message: initMessage)
275
352
  }
276
353
  }
277
354
 
278
355
  // MARK: - IsDatabase
279
356
 
280
357
  @objc public func isDatabase(_ dbName: String) throws -> NSNumber {
281
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
282
- let isFileExists: Bool = UtilsFile
283
- .isFileExist(databaseLocation: databaseLocation,
284
- fileName: mDbName + "SQLite.db")
285
- if isFileExists {
286
- return 1
358
+ if isInit {
359
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
360
+ let isFileExists: Bool = UtilsFile
361
+ .isFileExist(databaseLocation: databaseLocation,
362
+ fileName: mDbName + "SQLite.db")
363
+ if isFileExists {
364
+ return 1
365
+ } else {
366
+ return 0
367
+ }
287
368
  } else {
288
- return 0
369
+ throw CapacitorSQLiteError.failed(message: initMessage)
289
370
  }
290
371
  }
291
372
 
292
373
  // MARK: - IsNCDatabase
293
374
 
294
375
  @objc public func isNCDatabase(_ databasePath: String) throws -> NSNumber {
295
- let isFileExists: Bool = UtilsFile
296
- .isFileExist(filePath: databasePath)
297
- if isFileExists {
298
- return 1
376
+ if isInit {
377
+ let isFileExists: Bool = UtilsFile
378
+ .isFileExist(filePath: databasePath)
379
+ if isFileExists {
380
+ return 1
381
+ } else {
382
+ return 0
383
+ }
299
384
  } else {
300
- return 0
385
+ throw CapacitorSQLiteError.failed(message: initMessage)
301
386
  }
302
387
  }
303
388
 
304
389
  // MARK: - IsTableExists
305
390
 
306
391
  @objc public func isTableExists(_ dbName: String, tableName: String) throws -> NSNumber {
307
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
308
- guard let mDb: Database = dbDict[mDbName] else {
309
- let msg = "Connection to \(mDbName) not available"
310
- throw CapacitorSQLiteError.failed(message: msg)
311
- }
312
- do {
313
- let isExists: Bool = try
314
- UtilsJson.isTableExists(mDB: mDb,
315
- tableName: tableName)
316
- if isExists {
317
- return 1
318
- } else {
319
- return 0
392
+ if isInit {
393
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
394
+ guard let mDb: Database = dbDict[mDbName] else {
395
+ let msg = "Connection to \(mDbName) not available"
396
+ throw CapacitorSQLiteError.failed(message: msg)
320
397
  }
321
- } catch UtilsJsonError.tableNotExists(let message) {
322
- var msg: String = "IsTableExists:"
323
- msg.append(" \(message)")
324
- throw CapacitorSQLiteError.failed(message: msg)
325
- } catch let error {
326
- var msg: String = "IsTableExists:"
327
- msg.append(" \(error)")
328
- throw CapacitorSQLiteError.failed(message: msg)
398
+ do {
399
+ let isExists: Bool = try
400
+ UtilsJson.isTableExists(mDB: mDb,
401
+ tableName: tableName)
402
+ if isExists {
403
+ return 1
404
+ } else {
405
+ return 0
406
+ }
407
+ } catch UtilsJsonError.tableNotExists(let message) {
408
+ var msg: String = "IsTableExists:"
409
+ msg.append(" \(message)")
410
+ throw CapacitorSQLiteError.failed(message: msg)
411
+ } catch let error {
412
+ var msg: String = "IsTableExists:"
413
+ msg.append(" \(error)")
414
+ throw CapacitorSQLiteError.failed(message: msg)
415
+ }
416
+ } else {
417
+ throw CapacitorSQLiteError.failed(message: initMessage)
329
418
  }
330
419
  }
331
420
 
@@ -334,37 +423,41 @@ enum CapacitorSQLiteError: Error {
334
423
  @objc public func execute(_ dbName: String, statements: String,
335
424
  transaction: Bool)
336
425
  throws -> [String: Any] {
337
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
338
- guard let mDb: Database = dbDict[mDbName] else {
339
- let msg = "Connection to \(mDbName) not available"
340
- throw CapacitorSQLiteError.failed(message: msg)
341
- }
342
- if !mDb.isNCDB() && mDb.isDBOpen() {
343
- do {
344
- var stmts = statements
345
- // remove carriage returns
346
- let isReturn = stmts.indicesOf(string: "\n")
347
- if isReturn.count != 0 {
348
- let cmds = stmts.split(separator: "\n")
349
- var strcmds: [String] = []
350
- for cmd in cmds {
351
- strcmds.append(String(cmd
352
- .trimmingCharacters(in: .whitespacesAndNewlines)))
426
+ if isInit {
427
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
428
+ guard let mDb: Database = dbDict[mDbName] else {
429
+ let msg = "Connection to \(mDbName) not available"
430
+ throw CapacitorSQLiteError.failed(message: msg)
431
+ }
432
+ if !mDb.isNCDB() && mDb.isDBOpen() {
433
+ do {
434
+ var stmts = statements
435
+ // remove carriage returns
436
+ let isReturn = stmts.indicesOf(string: "\n")
437
+ if isReturn.count != 0 {
438
+ let cmds = stmts.split(separator: "\n")
439
+ var strcmds: [String] = []
440
+ for cmd in cmds {
441
+ strcmds.append(String(cmd
442
+ .trimmingCharacters(in: .whitespacesAndNewlines)))
443
+ }
444
+ stmts = strcmds.joined(separator: "\n")
353
445
  }
354
- stmts = strcmds.joined(separator: "\n")
446
+ let res = try mDb.executeSQL(sql: stmts,
447
+ transaction: transaction)
448
+ return ["changes": res]
449
+ } catch DatabaseError.executeSQL(let message) {
450
+ throw CapacitorSQLiteError.failed(message: message)
451
+ } catch let error {
452
+ let msg: String = "\(error)"
453
+ throw CapacitorSQLiteError.failed(message: msg)
355
454
  }
356
- let res = try mDb.executeSQL(sql: stmts,
357
- transaction: transaction)
358
- return ["changes": res]
359
- } catch DatabaseError.executeSQL(let message) {
360
- throw CapacitorSQLiteError.failed(message: message)
361
- } catch let error {
362
- let msg: String = "\(error)"
455
+ } else {
456
+ let msg = "Database \(mDbName) not opened or in read-only"
363
457
  throw CapacitorSQLiteError.failed(message: msg)
364
458
  }
365
459
  } else {
366
- let msg = "Database \(mDbName) not opened or in read-only"
367
- throw CapacitorSQLiteError.failed(message: msg)
460
+ throw CapacitorSQLiteError.failed(message: initMessage)
368
461
  }
369
462
  }
370
463
 
@@ -373,24 +466,28 @@ enum CapacitorSQLiteError: Error {
373
466
  @objc func executeSet(_ dbName: String, set: [[String: Any]],
374
467
  transaction: Bool)
375
468
  throws -> [String: Any] {
376
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
377
- guard let mDb: Database = dbDict[mDbName] else {
378
- let msg = "Connection to \(mDbName) not available"
379
- throw CapacitorSQLiteError.failed(message: msg)
380
- }
381
- if !mDb.isNCDB() && mDb.isDBOpen() {
382
- do {
383
- let res = try mDb.execSet(set: set, transaction: transaction)
384
- return res
385
- } catch DatabaseError.execSet(let message) {
386
- throw CapacitorSQLiteError.failed(message: message)
387
- } catch let error {
388
- let msg: String = "\(error)"
469
+ if isInit {
470
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
471
+ guard let mDb: Database = dbDict[mDbName] else {
472
+ let msg = "Connection to \(mDbName) not available"
473
+ throw CapacitorSQLiteError.failed(message: msg)
474
+ }
475
+ if !mDb.isNCDB() && mDb.isDBOpen() {
476
+ do {
477
+ let res = try mDb.execSet(set: set, transaction: transaction)
478
+ return res
479
+ } catch DatabaseError.execSet(let message) {
480
+ throw CapacitorSQLiteError.failed(message: message)
481
+ } catch let error {
482
+ let msg: String = "\(error)"
483
+ throw CapacitorSQLiteError.failed(message: msg)
484
+ }
485
+ } else {
486
+ let msg = "Database \(mDbName) not opened or in read-only"
389
487
  throw CapacitorSQLiteError.failed(message: msg)
390
488
  }
391
489
  } else {
392
- let msg = "Database \(mDbName) not opened or in read-only"
393
- throw CapacitorSQLiteError.failed(message: msg)
490
+ throw CapacitorSQLiteError.failed(message: initMessage)
394
491
  }
395
492
  }
396
493
 
@@ -400,46 +497,50 @@ enum CapacitorSQLiteError: Error {
400
497
  @objc func run(_ dbName: String, statement: String, values: [Any],
401
498
  transaction: Bool)
402
499
  throws -> [String: Any] {
403
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
404
- guard let mDb: Database = dbDict[mDbName] else {
405
- let msg = "Connection to \(mDbName) not available"
406
- throw CapacitorSQLiteError.failed(message: msg)
407
- }
408
- if !mDb.isNCDB() && mDb.isDBOpen() {
409
- do {
410
- var val: [Any] = []
411
- if values.count > 0 {
412
- for value in values {
413
- if let obj = value as? String {
414
- let str: String =
415
- "\(String(describing: obj))"
416
- val.append(str)
417
- } else if let obj = value as? Int {
418
- val.append(obj)
419
- } else if let obj = value as? Float {
420
- val.append(obj)
421
- } else if let obj = value as? Double {
422
- val.append(obj)
423
- } else if value is NSNull {
424
- val.append(value)
425
- } else {
426
- let msg: String = "Not a SQL type"
427
- throw CapacitorSQLiteError.failed(message: msg)
500
+ if isInit {
501
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
502
+ guard let mDb: Database = dbDict[mDbName] else {
503
+ let msg = "Connection to \(mDbName) not available"
504
+ throw CapacitorSQLiteError.failed(message: msg)
505
+ }
506
+ if !mDb.isNCDB() && mDb.isDBOpen() {
507
+ do {
508
+ var val: [Any] = []
509
+ if values.count > 0 {
510
+ for value in values {
511
+ if let obj = value as? String {
512
+ let str: String =
513
+ "\(String(describing: obj))"
514
+ val.append(str)
515
+ } else if let obj = value as? Int {
516
+ val.append(obj)
517
+ } else if let obj = value as? Float {
518
+ val.append(obj)
519
+ } else if let obj = value as? Double {
520
+ val.append(obj)
521
+ } else if value is NSNull {
522
+ val.append(value)
523
+ } else {
524
+ let msg: String = "Not a SQL type"
525
+ throw CapacitorSQLiteError.failed(message: msg)
526
+ }
428
527
  }
429
528
  }
529
+ let res = try mDb.runSQL(sql: statement, values: val,
530
+ transaction: transaction)
531
+ return res
532
+ } catch DatabaseError.runSQL(let message) {
533
+ throw CapacitorSQLiteError.failed(message: message)
534
+ } catch let error {
535
+ let msg: String = "\(error)"
536
+ throw CapacitorSQLiteError.failed(message: msg)
430
537
  }
431
- let res = try mDb.runSQL(sql: statement, values: val,
432
- transaction: transaction)
433
- return res
434
- } catch DatabaseError.runSQL(let message) {
435
- throw CapacitorSQLiteError.failed(message: message)
436
- } catch let error {
437
- let msg: String = "\(error)"
538
+ } else {
539
+ let msg = "Database \(mDbName) not opened or in read-only"
438
540
  throw CapacitorSQLiteError.failed(message: msg)
439
541
  }
440
542
  } else {
441
- let msg = "Database \(mDbName) not opened or in read-only"
442
- throw CapacitorSQLiteError.failed(message: msg)
543
+ throw CapacitorSQLiteError.failed(message: initMessage)
443
544
  }
444
545
  }
445
546
  // swiftlint:enable cyclomatic_complexity
@@ -448,108 +549,127 @@ enum CapacitorSQLiteError: Error {
448
549
 
449
550
  @objc func query(_ dbName: String, statement: String,
450
551
  values: [Any]) throws -> [[String: Any]] {
451
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
452
- guard let mDb: Database = dbDict[mDbName] else {
453
- let msg = "Connection to \(mDbName) not available"
454
- throw CapacitorSQLiteError.failed(message: msg)
455
- }
456
- if mDb.isDBOpen() {
457
- do {
458
- let res: [[String: Any]] = try mDb
459
- .selectSQL(sql: statement, values: values)
460
- return res
461
- } catch DatabaseError.selectSQL(let message) {
462
- throw CapacitorSQLiteError.failed(message: message)
463
- } catch let error {
464
- let msg: String = "\(error)"
552
+ if isInit {
553
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
554
+ guard let mDb: Database = dbDict[mDbName] else {
555
+ let msg = "Connection to \(mDbName) not available"
556
+ throw CapacitorSQLiteError.failed(message: msg)
557
+ }
558
+ if mDb.isDBOpen() {
559
+ do {
560
+ let res: [[String: Any]] = try mDb
561
+ .selectSQL(sql: statement, values: values)
562
+ return res
563
+ } catch DatabaseError.selectSQL(let message) {
564
+ throw CapacitorSQLiteError.failed(message: message)
565
+ } catch let error {
566
+ let msg: String = "\(error)"
567
+ throw CapacitorSQLiteError.failed(message: msg)
568
+ }
569
+ } else {
570
+ let msg = "Database \(mDbName) not opened"
465
571
  throw CapacitorSQLiteError.failed(message: msg)
466
572
  }
467
573
  } else {
468
- let msg = "Database \(mDbName) not opened"
469
- throw CapacitorSQLiteError.failed(message: msg)
574
+ throw CapacitorSQLiteError.failed(message: initMessage)
470
575
  }
471
576
  }
472
577
 
473
578
  // MARK: - isDBExists
474
579
 
475
580
  @objc func isDBExists(_ dbName: String) throws -> NSNumber {
476
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
477
- guard let _: Database = dbDict[mDbName] else {
478
- let msg = "Connection to \(mDbName) not available"
479
- throw CapacitorSQLiteError.failed(message: msg)
480
- }
481
- let res: Bool = UtilsFile
482
- .isFileExist(databaseLocation: databaseLocation,
483
- fileName: "\(mDbName)SQLite.db")
484
- if res {
485
- return 1
581
+ if isInit {
582
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
583
+ guard let _: Database = dbDict[mDbName] else {
584
+ let msg = "Connection to \(mDbName) not available"
585
+ throw CapacitorSQLiteError.failed(message: msg)
586
+ }
587
+ let res: Bool = UtilsFile
588
+ .isFileExist(databaseLocation: databaseLocation,
589
+ fileName: "\(mDbName)SQLite.db")
590
+ if res {
591
+ return 1
592
+ } else {
593
+ return 0
594
+ }
486
595
  } else {
487
- return 0
596
+ throw CapacitorSQLiteError.failed(message: initMessage)
488
597
  }
489
598
  }
490
599
 
491
600
  // MARK: - isDBOpen
492
601
 
493
602
  @objc func isDBOpen(_ dbName: String) throws -> NSNumber {
494
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
495
- guard let mDb: Database = dbDict[mDbName] else {
496
- let msg = "Connection to \(mDbName) not available"
497
- throw CapacitorSQLiteError.failed(message: msg)
498
- }
499
- let isOpen: Bool = mDb.isDBOpen()
500
- if isOpen {
501
- return 1
603
+ if isInit {
604
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
605
+ guard let mDb: Database = dbDict[mDbName] else {
606
+ let msg = "Connection to \(mDbName) not available"
607
+ throw CapacitorSQLiteError.failed(message: msg)
608
+ }
609
+ let isOpen: Bool = mDb.isDBOpen()
610
+ if isOpen {
611
+ return 1
612
+ } else {
613
+ return 0
614
+ }
502
615
  } else {
503
- return 0
616
+ throw CapacitorSQLiteError.failed(message: initMessage)
504
617
  }
505
618
  }
506
619
 
507
620
  // MARK: - deleteDatabase
508
621
 
509
622
  @objc func deleteDatabase(_ dbName: String) throws {
510
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
511
- guard let mDb: Database = dbDict[mDbName] else {
512
- let msg = "Connection to \(mDbName) not available"
513
- throw CapacitorSQLiteError.failed(message: msg)
514
- }
515
-
516
- do {
517
- if !mDb.isDBOpen() {
518
- try mDb.open()
623
+ if isInit {
624
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
625
+ guard let mDb: Database = dbDict[mDbName] else {
626
+ let msg = "Connection to \(mDbName) not available"
627
+ throw CapacitorSQLiteError.failed(message: msg)
519
628
  }
520
- let res: Bool = try mDb.deleteDB(databaseName: "\(mDbName)SQLite.db")
521
- if res {
522
- return
523
- } else {
524
- let msg: String = "deleteDB return false"
629
+
630
+ do {
631
+ if !mDb.isDBOpen() {
632
+ try mDb.open()
633
+ }
634
+ let res: Bool = try mDb.deleteDB(databaseName: "\(mDbName)SQLite.db")
635
+ if res {
636
+ return
637
+ } else {
638
+ let msg: String = "deleteDB return false"
639
+ throw CapacitorSQLiteError.failed(message: msg)
640
+ }
641
+ } catch DatabaseError.open(let message) {
642
+ throw CapacitorSQLiteError.failed(message: message)
643
+ } catch DatabaseError.deleteDB(let message) {
644
+ throw CapacitorSQLiteError.failed(message: message)
645
+ } catch let error {
646
+ let msg: String = "\(error)"
525
647
  throw CapacitorSQLiteError.failed(message: msg)
526
648
  }
527
- } catch DatabaseError.open(let message) {
528
- throw CapacitorSQLiteError.failed(message: message)
529
- } catch DatabaseError.deleteDB(let message) {
530
- throw CapacitorSQLiteError.failed(message: message)
531
- } catch let error {
532
- let msg: String = "\(error)"
533
- throw CapacitorSQLiteError.failed(message: msg)
649
+ } else {
650
+ throw CapacitorSQLiteError.failed(message: initMessage)
534
651
  }
535
652
  }
536
653
 
537
654
  // MARK: - isJsonValid
538
655
 
539
656
  @objc func isJsonValid(_ parsingData: String) throws {
540
-
541
- if let data = ("["+parsingData+"]").data(using: .utf8) {
542
- do {
543
- _ = try JSONDecoder().decode([JsonSQLite].self,
544
- from: data)
545
- return
546
- } catch let error {
547
- let msg: String = "\(error)"
657
+ if isInit {
658
+ if let data = ("["+parsingData+"]").data(using: .utf8) {
659
+ do {
660
+ _ = try JSONDecoder().decode([JsonSQLite].self,
661
+ from: data)
662
+ return
663
+ } catch let error {
664
+ let msg: String = "\(error)"
665
+ throw CapacitorSQLiteError.failed(message: msg)
666
+ }
667
+ } else {
668
+ let msg: String = "Stringify Json Object not Valid"
548
669
  throw CapacitorSQLiteError.failed(message: msg)
549
670
  }
550
671
  } else {
551
- let msg: String = "Stringify Json Object not Valid"
552
- throw CapacitorSQLiteError.failed(message: msg)
672
+ throw CapacitorSQLiteError.failed(message: initMessage)
553
673
  }
554
674
  }
555
675
 
@@ -558,68 +678,72 @@ enum CapacitorSQLiteError: Error {
558
678
  // swiftlint:disable function_body_length
559
679
  @objc func importFromJson(_ parsingData: String)
560
680
  throws -> [String: Int] {
561
- var mDb: Database
562
- if let data = ("["+parsingData+"]").data(using: .utf8) {
563
- var jsonSQLite: [JsonSQLite]
564
- do {
565
- jsonSQLite = try JSONDecoder()
566
- .decode([JsonSQLite].self, from: data)
567
- } catch let error {
568
- var msg: String = "Stringify Json Object not Valid "
569
- msg.append("\(error)")
570
- throw CapacitorSQLiteError.failed(message: msg)
571
- }
572
- let encrypted: Bool = jsonSQLite[0].encrypted
573
- let inMode: String = encrypted ? "secret"
574
- : "no-encryption"
575
- let version: Int = jsonSQLite[0].version
576
- var dbName: String = CapacitorSQLite.getDatabaseName(dbName: jsonSQLite[0].database)
577
- dbName.append("SQLite.db")
578
- // open the database
579
- do {
580
- mDb = try Database(
581
- databaseLocation: databaseLocation,
582
- databaseName: dbName, encrypted: encrypted,
583
- mode: inMode, version: version, vUpgDict: [:])
584
- try mDb.open()
585
- } catch DatabaseError.open(let message) {
586
- throw CapacitorSQLiteError.failed(message: message)
587
- } catch let error {
588
- let msg: String = "\(error)"
589
- throw CapacitorSQLiteError.failed(message: msg)
590
- }
591
- // import from Json Object
592
- do {
593
- let res: [String: Int] = try mDb
594
- .importFromJson(jsonSQLite: jsonSQLite[0])
595
- try mDb.close()
596
- if let result = res["changes"] {
597
- if result < 0 {
598
- let msg: String = "changes < 0"
599
- throw CapacitorSQLiteError
600
- .failed(message: msg)
601
- } else {
602
- return res
603
- }
604
- } else {
605
- let msg: String = "changes not found"
681
+ if isInit {
682
+ var mDb: Database
683
+ if let data = ("["+parsingData+"]").data(using: .utf8) {
684
+ var jsonSQLite: [JsonSQLite]
685
+ do {
686
+ jsonSQLite = try JSONDecoder()
687
+ .decode([JsonSQLite].self, from: data)
688
+ } catch let error {
689
+ var msg: String = "Stringify Json Object not Valid "
690
+ msg.append("\(error)")
606
691
  throw CapacitorSQLiteError.failed(message: msg)
607
692
  }
608
- } catch DatabaseError.importFromJson(let message) {
609
- var msg = message
693
+ let encrypted: Bool = jsonSQLite[0].encrypted
694
+ let inMode: String = encrypted ? "secret"
695
+ : "no-encryption"
696
+ let version: Int = jsonSQLite[0].version
697
+ var dbName: String = CapacitorSQLite.getDatabaseName(dbName: jsonSQLite[0].database)
698
+ dbName.append("SQLite.db")
699
+ // open the database
610
700
  do {
611
- try mDb.close()
701
+ mDb = try Database(
702
+ databaseLocation: databaseLocation,
703
+ databaseName: dbName, encrypted: encrypted,
704
+ mode: inMode, version: version, vUpgDict: [:])
705
+ try mDb.open()
706
+ } catch DatabaseError.open(let message) {
707
+ throw CapacitorSQLiteError.failed(message: message)
708
+ } catch let error {
709
+ let msg: String = "\(error)"
612
710
  throw CapacitorSQLiteError.failed(message: msg)
711
+ }
712
+ // import from Json Object
713
+ do {
714
+ let res: [String: Int] = try mDb
715
+ .importFromJson(jsonSQLite: jsonSQLite[0])
716
+ try mDb.close()
717
+ if let result = res["changes"] {
718
+ if result < 0 {
719
+ let msg: String = "changes < 0"
720
+ throw CapacitorSQLiteError
721
+ .failed(message: msg)
722
+ } else {
723
+ return res
724
+ }
725
+ } else {
726
+ let msg: String = "changes not found"
727
+ throw CapacitorSQLiteError.failed(message: msg)
728
+ }
729
+ } catch DatabaseError.importFromJson(let message) {
730
+ var msg = message
731
+ do {
732
+ try mDb.close()
733
+ throw CapacitorSQLiteError.failed(message: msg)
734
+ } catch DatabaseError.close(let message) {
735
+ msg.append(" \(message)")
736
+ throw CapacitorSQLiteError.failed(message: msg)
737
+ }
613
738
  } catch DatabaseError.close(let message) {
614
- msg.append(" \(message)")
615
- throw CapacitorSQLiteError.failed(message: msg)
739
+ throw CapacitorSQLiteError.failed(message: message)
616
740
  }
617
- } catch DatabaseError.close(let message) {
618
- throw CapacitorSQLiteError.failed(message: message)
741
+ } else {
742
+ let msg: String = "Stringify Json Object not Valid"
743
+ throw CapacitorSQLiteError.failed(message: msg)
619
744
  }
620
745
  } else {
621
- let msg: String = "Stringify Json Object not Valid"
622
- throw CapacitorSQLiteError.failed(message: msg)
746
+ throw CapacitorSQLiteError.failed(message: initMessage)
623
747
  }
624
748
  }
625
749
  // swiftlint:enable function_body_length
@@ -628,56 +752,64 @@ enum CapacitorSQLiteError: Error {
628
752
 
629
753
  @objc func exportToJson(_ dbName: String, expMode: String)
630
754
  throws -> [String: Any] {
631
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
632
- guard let mDb: Database = dbDict[mDbName] else {
633
- let msg = "Connection to \(mDbName) not available"
634
- throw CapacitorSQLiteError.failed(message: msg)
635
- }
636
- if mDb.isDBOpen() {
755
+ if isInit {
756
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
757
+ guard let mDb: Database = dbDict[mDbName] else {
758
+ let msg = "Connection to \(mDbName) not available"
759
+ throw CapacitorSQLiteError.failed(message: msg)
760
+ }
761
+ if mDb.isDBOpen() {
637
762
 
638
- do {
639
- let res: [String: Any] = try
640
- mDb.exportToJson(expMode: expMode)
641
- if res.count == 5 || res.count == 6 {
642
- return res
643
- } else {
644
- var msg: String = "return Object is not a "
645
- msg.append("JsonSQLite Object")
763
+ do {
764
+ let res: [String: Any] = try
765
+ mDb.exportToJson(expMode: expMode)
766
+ if res.count == 5 || res.count == 6 {
767
+ return res
768
+ } else {
769
+ var msg: String = "return Object is not a "
770
+ msg.append("JsonSQLite Object")
771
+ throw CapacitorSQLiteError.failed(message: msg)
772
+ }
773
+ } catch DatabaseError.exportToJson(let message) {
774
+ throw CapacitorSQLiteError.failed(message: message)
775
+ } catch let error {
776
+ let msg: String = "\(error)"
646
777
  throw CapacitorSQLiteError.failed(message: msg)
647
778
  }
648
- } catch DatabaseError.exportToJson(let message) {
649
- throw CapacitorSQLiteError.failed(message: message)
650
- } catch let error {
651
- let msg: String = "\(error)"
779
+ } else {
780
+ let msg = "Database \(mDbName) not opened"
652
781
  throw CapacitorSQLiteError.failed(message: msg)
653
782
  }
654
783
  } else {
655
- let msg = "Database \(mDbName) not opened"
656
- throw CapacitorSQLiteError.failed(message: msg)
784
+ throw CapacitorSQLiteError.failed(message: initMessage)
657
785
  }
658
786
  }
659
787
 
660
788
  // MARK: - createSyncTable
661
789
 
662
790
  @objc func createSyncTable(_ dbName: String) throws -> NSNumber {
663
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
664
- guard let mDb: Database = dbDict[mDbName] else {
665
- let msg = "Connection to \(mDbName) not available"
666
- throw CapacitorSQLiteError.failed(message: msg)
667
- }
668
- if mDb.isDBOpen() {
669
- do {
670
- let res: Int = try mDb.createSyncTable()
671
- return res as NSNumber
672
- } catch DatabaseError.createSyncTable(let message) {
673
- throw CapacitorSQLiteError.failed(message: message)
674
- } catch let error {
675
- let msg: String = "\(error)"
791
+ if isInit {
792
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
793
+ guard let mDb: Database = dbDict[mDbName] else {
794
+ let msg = "Connection to \(mDbName) not available"
795
+ throw CapacitorSQLiteError.failed(message: msg)
796
+ }
797
+ if mDb.isDBOpen() {
798
+ do {
799
+ let res: Int = try mDb.createSyncTable()
800
+ return res as NSNumber
801
+ } catch DatabaseError.createSyncTable(let message) {
802
+ throw CapacitorSQLiteError.failed(message: message)
803
+ } catch let error {
804
+ let msg: String = "\(error)"
805
+ throw CapacitorSQLiteError.failed(message: msg)
806
+ }
807
+ } else {
808
+ let msg = "Database \(mDbName) not opened"
676
809
  throw CapacitorSQLiteError.failed(message: msg)
677
810
  }
678
811
  } else {
679
- let msg = "Database \(mDbName) not opened"
680
- throw CapacitorSQLiteError.failed(message: msg)
812
+ throw CapacitorSQLiteError.failed(message: initMessage)
681
813
  }
682
814
  }
683
815
 
@@ -685,60 +817,68 @@ enum CapacitorSQLiteError: Error {
685
817
 
686
818
  @objc func setSyncDate(_ dbName: String, syncDate: String)
687
819
  throws {
688
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
689
- guard let mDb: Database = dbDict[mDbName] else {
690
- let msg = "Connection to \(mDbName) not available"
691
- throw CapacitorSQLiteError.failed(message: msg)
692
- }
693
- if mDb.isDBOpen() {
820
+ if isInit {
821
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
822
+ guard let mDb: Database = dbDict[mDbName] else {
823
+ let msg = "Connection to \(mDbName) not available"
824
+ throw CapacitorSQLiteError.failed(message: msg)
825
+ }
826
+ if mDb.isDBOpen() {
694
827
 
695
- do {
696
- let res: Bool = try mDb
697
- .setSyncDate(syncDate: syncDate)
698
- if res {
699
- return
700
- } else {
701
- let msg: String = "return false"
828
+ do {
829
+ let res: Bool = try mDb
830
+ .setSyncDate(syncDate: syncDate)
831
+ if res {
832
+ return
833
+ } else {
834
+ let msg: String = "return false"
835
+ throw CapacitorSQLiteError.failed(message: msg)
836
+ }
837
+ } catch DatabaseError.createSyncDate(let message) {
838
+ throw CapacitorSQLiteError.failed(message: message)
839
+ } catch let error {
840
+ let msg: String = "\(error)"
702
841
  throw CapacitorSQLiteError.failed(message: msg)
703
842
  }
704
- } catch DatabaseError.createSyncDate(let message) {
705
- throw CapacitorSQLiteError.failed(message: message)
706
- } catch let error {
707
- let msg: String = "\(error)"
843
+ } else {
844
+ let msg = "Database \(mDbName) not opened"
708
845
  throw CapacitorSQLiteError.failed(message: msg)
709
846
  }
710
847
  } else {
711
- let msg = "Database \(mDbName) not opened"
712
- throw CapacitorSQLiteError.failed(message: msg)
848
+ throw CapacitorSQLiteError.failed(message: initMessage)
713
849
  }
714
850
  }
715
851
 
716
852
  // MARK: - getSyncDate
717
853
 
718
854
  @objc func getSyncDate(_ dbName: String) throws -> NSNumber {
719
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
720
- guard let mDb: Database = dbDict[mDbName] else {
721
- let msg = "Connection to \(mDbName) not available"
722
- throw CapacitorSQLiteError.failed(message: msg)
723
- }
724
- if mDb.isDBOpen() {
725
- do {
726
- let res: Int64 = try mDb.getSyncDate()
727
- if res > 0 {
728
- return res as NSNumber
729
- } else {
730
- let msg: String = "return no sync date"
855
+ if isInit {
856
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
857
+ guard let mDb: Database = dbDict[mDbName] else {
858
+ let msg = "Connection to \(mDbName) not available"
859
+ throw CapacitorSQLiteError.failed(message: msg)
860
+ }
861
+ if mDb.isDBOpen() {
862
+ do {
863
+ let res: Int64 = try mDb.getSyncDate()
864
+ if res > 0 {
865
+ return res as NSNumber
866
+ } else {
867
+ let msg: String = "return no sync date"
868
+ throw CapacitorSQLiteError.failed(message: msg)
869
+ }
870
+ } catch DatabaseError.getSyncDate(let message) {
871
+ throw CapacitorSQLiteError.failed(message: message)
872
+ } catch let error {
873
+ let msg: String = "\(error)"
731
874
  throw CapacitorSQLiteError.failed(message: msg)
732
875
  }
733
- } catch DatabaseError.getSyncDate(let message) {
734
- throw CapacitorSQLiteError.failed(message: message)
735
- } catch let error {
736
- let msg: String = "\(error)"
876
+ } else {
877
+ let msg = "Database \(mDbName) not opened"
737
878
  throw CapacitorSQLiteError.failed(message: msg)
738
879
  }
739
880
  } else {
740
- let msg = "Database \(mDbName) not opened"
741
- throw CapacitorSQLiteError.failed(message: msg)
881
+ throw CapacitorSQLiteError.failed(message: initMessage)
742
882
  }
743
883
  }
744
884
 
@@ -747,156 +887,176 @@ enum CapacitorSQLiteError: Error {
747
887
  @objc func addUpgradeStatement(_ dbName: String,
748
888
  upgrade: [[String: Any]])
749
889
  throws -> [Int: [String: Any]] {
750
- var upgDict: [String: Any] = [:]
751
- for dict in upgrade {
752
- let keys = dict.keys
753
- if !(keys.contains("fromVersion")) ||
754
- !(keys.contains("toVersion")) ||
755
- !(keys.contains("statement")) {
756
- var msg: String = "upgrade must have keys in "
757
- msg.append("{fromVersion,toVersion,statement}")
758
- throw CapacitorSQLiteError.failed(message: msg)
890
+ if isInit {
891
+ var upgDict: [String: Any] = [:]
892
+ for dict in upgrade {
893
+ let keys = dict.keys
894
+ if !(keys.contains("fromVersion")) ||
895
+ !(keys.contains("toVersion")) ||
896
+ !(keys.contains("statement")) {
897
+ var msg: String = "upgrade must have keys in "
898
+ msg.append("{fromVersion,toVersion,statement}")
899
+ throw CapacitorSQLiteError.failed(message: msg)
900
+ }
901
+ for (key, value) in dict {
902
+ upgDict[key] = value
903
+ }
759
904
  }
760
- for (key, value) in dict {
761
- upgDict[key] = value
905
+ guard let fromVersion = upgDict["fromVersion"] as? Int else {
906
+ let msg: String = "fromVersion key must be an Int"
907
+ throw CapacitorSQLiteError.failed(message: msg)
762
908
  }
909
+ let upgVersionDict: [Int: [String: Any]] =
910
+ [fromVersion: upgDict]
911
+ return upgVersionDict
912
+ } else {
913
+ throw CapacitorSQLiteError.failed(message: initMessage)
763
914
  }
764
- guard let fromVersion = upgDict["fromVersion"] as? Int else {
765
- let msg: String = "fromVersion key must be an Int"
766
- throw CapacitorSQLiteError.failed(message: msg)
767
- }
768
- let upgVersionDict: [Int: [String: Any]] =
769
- [fromVersion: upgDict]
770
- return upgVersionDict
771
915
  }
772
916
 
773
917
  // MARK: - copyFromAssets
774
918
 
775
919
  @objc func copyFromAssets(overwrite: Bool) throws {
920
+ if isInit {
776
921
 
777
- // check if the assets/database folder exists
778
- do {
779
- let assetsDbPath: URL = try
780
- UtilsFile.getAssetsDatabasesPath()
781
- let aPath: String = assetsDbPath.path
782
- let bRes: Bool = UtilsFile.isDirExist(dirPath: aPath)
783
- if bRes {
784
- // get the database files from assets
785
- let dbList: [String] = try UtilsFile
786
- .getFileList(path: aPath, ext: ".db")
787
- // loop through the database files
788
- for mDb in dbList {
789
- // for each check if the suffix SQLite.db is there
790
- // or add it
791
- let toDb: String = UtilsFile
792
- .setPathSuffix(sDb: mDb)
793
- // for each copy the file to the Application
794
- // database folder
795
- _ = try UtilsFile
796
- .copyFromAssetToDatabase(databaseLocation: databaseLocation,
797
- fromDb: mDb,
798
- toDb: toDb, overwrite: overwrite)
799
- }
800
- // get the zip files
801
- let zipList: [String] = try UtilsFile
802
- .getFileList(path: aPath, ext: ".zip")
803
- // loop through the database files
804
- for zip in zipList {
805
- // for each zip uncompress the file to the Application
806
- // database folder
807
- _ = try UtilsFile
808
- .unzipFromAssetToDatabase(databaseLocation: databaseLocation,
809
- zip: zip, overwrite: overwrite)
922
+ // check if the assets/database folder exists
923
+ do {
924
+ let assetsDbPath: URL = try
925
+ UtilsFile.getAssetsDatabasesPath()
926
+ let aPath: String = assetsDbPath.path
927
+ let bRes: Bool = UtilsFile.isDirExist(dirPath: aPath)
928
+ if bRes {
929
+ // get the database files from assets
930
+ let dbList: [String] = try UtilsFile
931
+ .getFileList(path: aPath, ext: ".db")
932
+ // loop through the database files
933
+ for mDb in dbList {
934
+ // for each check if the suffix SQLite.db is there
935
+ // or add it
936
+ let toDb: String = UtilsFile
937
+ .setPathSuffix(sDb: mDb)
938
+ // for each copy the file to the Application
939
+ // database folder
940
+ _ = try UtilsFile
941
+ .copyFromAssetToDatabase(databaseLocation: databaseLocation,
942
+ fromDb: mDb,
943
+ toDb: toDb, overwrite: overwrite)
944
+ }
945
+ // get the zip files
946
+ let zipList: [String] = try UtilsFile
947
+ .getFileList(path: aPath, ext: ".zip")
948
+ // loop through the database files
949
+ for zip in zipList {
950
+ // for each zip uncompress the file to the Application
951
+ // database folder
952
+ _ = try UtilsFile
953
+ .unzipFromAssetToDatabase(databaseLocation: databaseLocation,
954
+ zip: zip, overwrite: overwrite)
955
+ }
956
+ return
957
+ } else {
958
+ let msg: String = "assets database path does not exist"
959
+ throw CapacitorSQLiteError.failed(message: msg)
810
960
  }
811
- return
812
- } else {
813
- let msg: String = "assets database path does not exist"
961
+ } catch UtilsFileError.copyFromAssetToDatabaseFailed(let message) {
962
+ throw CapacitorSQLiteError.failed(message: message)
963
+ } catch UtilsFileError.unzipFromAssetToDatabaseFailed(let message) {
964
+ throw CapacitorSQLiteError.failed(message: message)
965
+ } catch let error {
966
+ let msg: String = "\(error)"
814
967
  throw CapacitorSQLiteError.failed(message: msg)
815
968
  }
816
- } catch UtilsFileError.copyFromAssetToDatabaseFailed(let message) {
817
- throw CapacitorSQLiteError.failed(message: message)
818
- } catch UtilsFileError.unzipFromAssetToDatabaseFailed(let message) {
819
- throw CapacitorSQLiteError.failed(message: message)
820
- } catch let error {
821
- let msg: String = "\(error)"
822
- throw CapacitorSQLiteError.failed(message: msg)
969
+ } else {
970
+ throw CapacitorSQLiteError.failed(message: initMessage)
823
971
  }
824
972
  }
825
973
 
826
974
  // MARK: - getDatabaseList
827
975
 
828
976
  @objc func getDatabaseList() throws -> [String] {
829
- do {
830
- let aPath: String = try UtilsFile.getDatabasesPath()
831
- // get the database files
832
- let dbList: [String] = try UtilsFile.getFileList(path: aPath, ext: ".db")
833
- return dbList
977
+ if isInit {
978
+ do {
979
+ let aPath: String = try (UtilsFile.getFolderURL(folderPath: databaseLocation)).path
980
+ // get the database files
981
+ let dbList: [String] = try UtilsFile.getFileList(path: aPath, ext: ".db")
982
+ return dbList
834
983
 
835
- } catch let error {
836
- let msg: String = "\(error)"
837
- throw CapacitorSQLiteError.failed(message: msg)
984
+ } catch let error {
985
+ let msg: String = "\(error)"
986
+ throw CapacitorSQLiteError.failed(message: msg)
987
+ }
988
+ } else {
989
+ throw CapacitorSQLiteError.failed(message: initMessage)
838
990
  }
839
-
840
991
  }
841
992
 
842
993
  // MARK: - getMigratableDbList
843
994
 
844
995
  @objc func getMigratableDbList(_ folderPath: String) throws -> [String] {
845
- do {
846
- let dbList: [String] = try UtilsMigrate
847
- .getMigratableList(folderPath: folderPath)
848
- return dbList
996
+ if isInit {
997
+ do {
998
+ let dbList: [String] = try UtilsMigrate
999
+ .getMigratableList(folderPath: folderPath)
1000
+ return dbList
849
1001
 
850
- } catch UtilsMigrateError.getMigratableList(let message) {
851
- var msg: String = "getMigratableList:"
852
- msg.append(" \(message)")
853
- throw CapacitorSQLiteError.failed(message: msg)
854
- } catch let error {
855
- let msg: String = "\(error)"
856
- throw CapacitorSQLiteError.failed(message: msg)
1002
+ } catch UtilsMigrateError.getMigratableList(let message) {
1003
+ var msg: String = "getMigratableList:"
1004
+ msg.append(" \(message)")
1005
+ throw CapacitorSQLiteError.failed(message: msg)
1006
+ } catch let error {
1007
+ let msg: String = "\(error)"
1008
+ throw CapacitorSQLiteError.failed(message: msg)
1009
+ }
1010
+ } else {
1011
+ throw CapacitorSQLiteError.failed(message: initMessage)
857
1012
  }
858
-
859
1013
  }
860
1014
 
861
1015
  // MARK: - addSQLiteSuffix
862
1016
 
863
1017
  @objc func addSQLiteSuffix(_ folderPath: String, dbList: [String]) throws {
1018
+ if isInit {
1019
+ do {
1020
+ try UtilsMigrate.addSQLiteSuffix(databaseLocation: databaseLocation,
1021
+ folderPath: folderPath,
1022
+ dbList: dbList)
1023
+ return
1024
+ } catch UtilsMigrateError.addSQLiteSuffix(let message) {
1025
+ var msg: String = "addSQLiteSuffix:"
1026
+ msg.append(" \(message)")
1027
+ throw CapacitorSQLiteError.failed(message: msg)
864
1028
 
865
- do {
866
- try UtilsMigrate.addSQLiteSuffix(databaseLocation: databaseLocation,
867
- folderPath: folderPath,
868
- dbList: dbList)
869
- return
870
- } catch UtilsMigrateError.addSQLiteSuffix(let message) {
871
- var msg: String = "addSQLiteSuffix:"
872
- msg.append(" \(message)")
873
- throw CapacitorSQLiteError.failed(message: msg)
874
-
875
- } catch let error {
876
- var msg: String = "addSQLiteSuffix:"
877
- msg.append(" \(error)")
878
- throw CapacitorSQLiteError.failed(message: msg)
1029
+ } catch let error {
1030
+ var msg: String = "addSQLiteSuffix:"
1031
+ msg.append(" \(error)")
1032
+ throw CapacitorSQLiteError.failed(message: msg)
1033
+ }
1034
+ } else {
1035
+ throw CapacitorSQLiteError.failed(message: initMessage)
879
1036
  }
880
1037
  }
881
1038
 
882
1039
  // MARK: - deleteOldDatabases
883
1040
 
884
1041
  @objc func deleteOldDatabases(_ folderPath: String, dbList: [String]) throws {
885
- do {
886
- try UtilsMigrate
887
- .deleteOldDatabases(folderPath: folderPath, dbList: dbList)
888
- return
889
- } catch UtilsMigrateError.deleteOldDatabases(let message) {
890
- var msg: String = "deleteOldDatabases:"
891
- msg.append(" \(message)")
892
- throw CapacitorSQLiteError.failed(message: msg)
1042
+ if isInit {
1043
+ do {
1044
+ try UtilsMigrate
1045
+ .deleteOldDatabases(folderPath: folderPath, dbList: dbList)
1046
+ return
1047
+ } catch UtilsMigrateError.deleteOldDatabases(let message) {
1048
+ var msg: String = "deleteOldDatabases:"
1049
+ msg.append(" \(message)")
1050
+ throw CapacitorSQLiteError.failed(message: msg)
893
1051
 
894
- } catch let error {
895
- var msg: String = "deleteOldDatabases:"
896
- msg.append(" \(error)")
897
- throw CapacitorSQLiteError.failed(message: msg)
1052
+ } catch let error {
1053
+ var msg: String = "deleteOldDatabases:"
1054
+ msg.append(" \(error)")
1055
+ throw CapacitorSQLiteError.failed(message: msg)
1056
+ }
1057
+ } else {
1058
+ throw CapacitorSQLiteError.failed(message: initMessage)
898
1059
  }
899
-
900
1060
  }
901
1061
  class func getDatabaseName(dbName: String) -> String {
902
1062
  var retName: String = dbName