@capacitor-community/sqlite 3.3.3-4 → 3.3.3-5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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,204 +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)
191
- }
192
- do {
193
- try mDb.close()
194
- return
195
- } catch DatabaseError.close(let message) {
196
- throw CapacitorSQLiteError.failed(message: message)
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)
197
246
  }
198
247
  }
199
248
 
200
249
  // MARK: - getUrl
201
250
 
202
251
  @objc public func getUrl(_ dbName: String) throws -> String {
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 res: String = try mDb.getUrl()
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()
210
259
  return res
211
- } catch DatabaseError.close(let message) {
212
- throw CapacitorSQLiteError.failed(message: message)
260
+ } else {
261
+ throw CapacitorSQLiteError.failed(message: initMessage)
213
262
  }
214
263
  }
215
264
 
216
265
  // MARK: - GetVersion
217
266
 
218
267
  @objc public func getVersion(_ dbName: String) throws -> NSNumber {
219
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
220
- guard let mDb: Database = dbDict[mDbName] else {
221
- let msg = "Connection to \(mDbName) not available"
222
- throw CapacitorSQLiteError.failed(message: msg)
223
- }
224
- do {
225
- let version: Int = try mDb.getVersion()
226
- 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)
227
277
 
228
- } catch DatabaseError.open(let message) {
229
- 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)
230
283
  }
231
284
  }
232
285
 
233
286
  // MARK: - Close Connection
234
287
 
235
288
  @objc public func closeConnection(_ dbName: String) throws {
236
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
237
- guard let mDb: Database = dbDict[mDbName] else {
238
- let msg = "Connection to \(mDbName) not available"
239
- throw CapacitorSQLiteError.failed(message: msg)
240
- }
241
- if mDb.isDBOpen() {
242
- do {
243
- try mDb.close()
244
- } catch DatabaseError.close(let message) {
245
- 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)
246
294
  }
295
+ if mDb.isDBOpen() {
296
+ do {
297
+ try mDb.close()
298
+ } catch DatabaseError.close(let message) {
299
+ throw CapacitorSQLiteError.failed(message: message)
300
+ }
301
+ }
302
+ dbDict.removeValue(forKey: mDbName)
303
+ return
304
+ } else {
305
+ throw CapacitorSQLiteError.failed(message: initMessage)
247
306
  }
248
- dbDict.removeValue(forKey: mDbName)
249
- return
250
307
  }
251
308
 
252
309
  // MARK: - CheckConnectionsConsistency
253
310
 
254
311
  @objc public func checkConnectionsConsistency(_ dbNames: [String]) throws -> NSNumber {
255
- var keys: [String] = Array(self.dbDict.keys)
256
- do {
257
- if dbNames.count == 0 {
258
- try closeAllConnections()
259
- return 0
260
- }
261
- if keys.count < dbNames.count {
262
- // not solvable inconsistency
263
- try closeAllConnections()
264
- return 0
265
- }
266
- if keys.count > dbNames.count {
267
- for key in keys {
268
- if !dbNames.contains(key) {
269
- 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
+ }
270
329
  }
271
330
  }
272
- }
273
- keys = Array(self.dbDict.keys)
274
- if keys.count == dbNames.count {
275
- let set1 = Set(keys)
276
- let set2 = Set(dbNames)
277
- let arr = Array(set1.symmetricDifference(set2))
278
- if arr.count == 0 {
279
- 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
+ }
280
343
  } else {
281
- // not solvable inconsistency
282
344
  try closeAllConnections()
283
345
  return 0
284
346
  }
285
- } else {
286
- try closeAllConnections()
287
- return 0
347
+ } catch let error {
348
+ throw CapacitorSQLiteError.failed(message: "\(error)")
288
349
  }
289
- } catch let error {
290
- throw CapacitorSQLiteError.failed(message: "\(error)")
350
+ } else {
351
+ throw CapacitorSQLiteError.failed(message: initMessage)
291
352
  }
292
353
  }
293
354
 
294
355
  // MARK: - IsDatabase
295
356
 
296
357
  @objc public func isDatabase(_ dbName: String) throws -> NSNumber {
297
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
298
- let isFileExists: Bool = UtilsFile
299
- .isFileExist(databaseLocation: databaseLocation,
300
- fileName: mDbName + "SQLite.db")
301
- if isFileExists {
302
- 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
+ }
303
368
  } else {
304
- return 0
369
+ throw CapacitorSQLiteError.failed(message: initMessage)
305
370
  }
306
371
  }
307
372
 
308
373
  // MARK: - IsNCDatabase
309
374
 
310
375
  @objc public func isNCDatabase(_ databasePath: String) throws -> NSNumber {
311
- let isFileExists: Bool = UtilsFile
312
- .isFileExist(filePath: databasePath)
313
- if isFileExists {
314
- 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
+ }
315
384
  } else {
316
- return 0
385
+ throw CapacitorSQLiteError.failed(message: initMessage)
317
386
  }
318
387
  }
319
388
 
320
389
  // MARK: - IsTableExists
321
390
 
322
391
  @objc public func isTableExists(_ dbName: String, tableName: String) throws -> NSNumber {
323
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
324
- guard let mDb: Database = dbDict[mDbName] else {
325
- let msg = "Connection to \(mDbName) not available"
326
- throw CapacitorSQLiteError.failed(message: msg)
327
- }
328
- do {
329
- let isExists: Bool = try
330
- UtilsJson.isTableExists(mDB: mDb,
331
- tableName: tableName)
332
- if isExists {
333
- return 1
334
- } else {
335
- 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)
336
397
  }
337
- } catch UtilsJsonError.tableNotExists(let message) {
338
- var msg: String = "IsTableExists:"
339
- msg.append(" \(message)")
340
- throw CapacitorSQLiteError.failed(message: msg)
341
- } catch let error {
342
- var msg: String = "IsTableExists:"
343
- msg.append(" \(error)")
344
- 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)
345
418
  }
346
419
  }
347
420
 
@@ -350,37 +423,41 @@ enum CapacitorSQLiteError: Error {
350
423
  @objc public func execute(_ dbName: String, statements: String,
351
424
  transaction: Bool)
352
425
  throws -> [String: Any] {
353
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
354
- guard let mDb: Database = dbDict[mDbName] else {
355
- let msg = "Connection to \(mDbName) not available"
356
- throw CapacitorSQLiteError.failed(message: msg)
357
- }
358
- if !mDb.isNCDB() && mDb.isDBOpen() {
359
- do {
360
- var stmts = statements
361
- // remove carriage returns
362
- let isReturn = stmts.indicesOf(string: "\n")
363
- if isReturn.count != 0 {
364
- let cmds = stmts.split(separator: "\n")
365
- var strcmds: [String] = []
366
- for cmd in cmds {
367
- strcmds.append(String(cmd
368
- .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")
369
445
  }
370
- 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)
371
454
  }
372
- let res = try mDb.executeSQL(sql: stmts,
373
- transaction: transaction)
374
- return ["changes": res]
375
- } catch DatabaseError.executeSQL(let message) {
376
- throw CapacitorSQLiteError.failed(message: message)
377
- } catch let error {
378
- let msg: String = "\(error)"
455
+ } else {
456
+ let msg = "Database \(mDbName) not opened or in read-only"
379
457
  throw CapacitorSQLiteError.failed(message: msg)
380
458
  }
381
459
  } else {
382
- let msg = "Database \(mDbName) not opened or in read-only"
383
- throw CapacitorSQLiteError.failed(message: msg)
460
+ throw CapacitorSQLiteError.failed(message: initMessage)
384
461
  }
385
462
  }
386
463
 
@@ -389,24 +466,28 @@ enum CapacitorSQLiteError: Error {
389
466
  @objc func executeSet(_ dbName: String, set: [[String: Any]],
390
467
  transaction: Bool)
391
468
  throws -> [String: Any] {
392
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
393
- guard let mDb: Database = dbDict[mDbName] else {
394
- let msg = "Connection to \(mDbName) not available"
395
- throw CapacitorSQLiteError.failed(message: msg)
396
- }
397
- if !mDb.isNCDB() && mDb.isDBOpen() {
398
- do {
399
- let res = try mDb.execSet(set: set, transaction: transaction)
400
- return res
401
- } catch DatabaseError.execSet(let message) {
402
- throw CapacitorSQLiteError.failed(message: message)
403
- } catch let error {
404
- 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"
405
487
  throw CapacitorSQLiteError.failed(message: msg)
406
488
  }
407
489
  } else {
408
- let msg = "Database \(mDbName) not opened or in read-only"
409
- throw CapacitorSQLiteError.failed(message: msg)
490
+ throw CapacitorSQLiteError.failed(message: initMessage)
410
491
  }
411
492
  }
412
493
 
@@ -416,46 +497,50 @@ enum CapacitorSQLiteError: Error {
416
497
  @objc func run(_ dbName: String, statement: String, values: [Any],
417
498
  transaction: Bool)
418
499
  throws -> [String: Any] {
419
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
420
- guard let mDb: Database = dbDict[mDbName] else {
421
- let msg = "Connection to \(mDbName) not available"
422
- throw CapacitorSQLiteError.failed(message: msg)
423
- }
424
- if !mDb.isNCDB() && mDb.isDBOpen() {
425
- do {
426
- var val: [Any] = []
427
- if values.count > 0 {
428
- for value in values {
429
- if let obj = value as? String {
430
- let str: String =
431
- "\(String(describing: obj))"
432
- val.append(str)
433
- } else if let obj = value as? Int {
434
- val.append(obj)
435
- } else if let obj = value as? Float {
436
- val.append(obj)
437
- } else if let obj = value as? Double {
438
- val.append(obj)
439
- } else if value is NSNull {
440
- val.append(value)
441
- } else {
442
- let msg: String = "Not a SQL type"
443
- 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
+ }
444
527
  }
445
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)
446
537
  }
447
- let res = try mDb.runSQL(sql: statement, values: val,
448
- transaction: transaction)
449
- return res
450
- } catch DatabaseError.runSQL(let message) {
451
- throw CapacitorSQLiteError.failed(message: message)
452
- } catch let error {
453
- let msg: String = "\(error)"
538
+ } else {
539
+ let msg = "Database \(mDbName) not opened or in read-only"
454
540
  throw CapacitorSQLiteError.failed(message: msg)
455
541
  }
456
542
  } else {
457
- let msg = "Database \(mDbName) not opened or in read-only"
458
- throw CapacitorSQLiteError.failed(message: msg)
543
+ throw CapacitorSQLiteError.failed(message: initMessage)
459
544
  }
460
545
  }
461
546
  // swiftlint:enable cyclomatic_complexity
@@ -464,108 +549,127 @@ enum CapacitorSQLiteError: Error {
464
549
 
465
550
  @objc func query(_ dbName: String, statement: String,
466
551
  values: [Any]) throws -> [[String: Any]] {
467
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
468
- guard let mDb: Database = dbDict[mDbName] else {
469
- let msg = "Connection to \(mDbName) not available"
470
- throw CapacitorSQLiteError.failed(message: msg)
471
- }
472
- if mDb.isDBOpen() {
473
- do {
474
- let res: [[String: Any]] = try mDb
475
- .selectSQL(sql: statement, values: values)
476
- return res
477
- } catch DatabaseError.selectSQL(let message) {
478
- throw CapacitorSQLiteError.failed(message: message)
479
- } catch let error {
480
- 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"
481
571
  throw CapacitorSQLiteError.failed(message: msg)
482
572
  }
483
573
  } else {
484
- let msg = "Database \(mDbName) not opened"
485
- throw CapacitorSQLiteError.failed(message: msg)
574
+ throw CapacitorSQLiteError.failed(message: initMessage)
486
575
  }
487
576
  }
488
577
 
489
578
  // MARK: - isDBExists
490
579
 
491
580
  @objc func isDBExists(_ dbName: String) throws -> NSNumber {
492
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
493
- guard let _: Database = dbDict[mDbName] else {
494
- let msg = "Connection to \(mDbName) not available"
495
- throw CapacitorSQLiteError.failed(message: msg)
496
- }
497
- let res: Bool = UtilsFile
498
- .isFileExist(databaseLocation: databaseLocation,
499
- fileName: "\(mDbName)SQLite.db")
500
- if res {
501
- 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
+ }
502
595
  } else {
503
- return 0
596
+ throw CapacitorSQLiteError.failed(message: initMessage)
504
597
  }
505
598
  }
506
599
 
507
600
  // MARK: - isDBOpen
508
601
 
509
602
  @objc func isDBOpen(_ dbName: String) throws -> NSNumber {
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
- let isOpen: Bool = mDb.isDBOpen()
516
- if isOpen {
517
- 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
+ }
518
615
  } else {
519
- return 0
616
+ throw CapacitorSQLiteError.failed(message: initMessage)
520
617
  }
521
618
  }
522
619
 
523
620
  // MARK: - deleteDatabase
524
621
 
525
622
  @objc func deleteDatabase(_ dbName: String) throws {
526
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
527
- guard let mDb: Database = dbDict[mDbName] else {
528
- let msg = "Connection to \(mDbName) not available"
529
- throw CapacitorSQLiteError.failed(message: msg)
530
- }
531
-
532
- do {
533
- if !mDb.isDBOpen() {
534
- 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)
535
628
  }
536
- let res: Bool = try mDb.deleteDB(databaseName: "\(mDbName)SQLite.db")
537
- if res {
538
- return
539
- } else {
540
- 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)"
541
647
  throw CapacitorSQLiteError.failed(message: msg)
542
648
  }
543
- } catch DatabaseError.open(let message) {
544
- throw CapacitorSQLiteError.failed(message: message)
545
- } catch DatabaseError.deleteDB(let message) {
546
- throw CapacitorSQLiteError.failed(message: message)
547
- } catch let error {
548
- let msg: String = "\(error)"
549
- throw CapacitorSQLiteError.failed(message: msg)
649
+ } else {
650
+ throw CapacitorSQLiteError.failed(message: initMessage)
550
651
  }
551
652
  }
552
653
 
553
654
  // MARK: - isJsonValid
554
655
 
555
656
  @objc func isJsonValid(_ parsingData: String) throws {
556
-
557
- if let data = ("["+parsingData+"]").data(using: .utf8) {
558
- do {
559
- _ = try JSONDecoder().decode([JsonSQLite].self,
560
- from: data)
561
- return
562
- } catch let error {
563
- 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"
564
669
  throw CapacitorSQLiteError.failed(message: msg)
565
670
  }
566
671
  } else {
567
- let msg: String = "Stringify Json Object not Valid"
568
- throw CapacitorSQLiteError.failed(message: msg)
672
+ throw CapacitorSQLiteError.failed(message: initMessage)
569
673
  }
570
674
  }
571
675
 
@@ -574,68 +678,72 @@ enum CapacitorSQLiteError: Error {
574
678
  // swiftlint:disable function_body_length
575
679
  @objc func importFromJson(_ parsingData: String)
576
680
  throws -> [String: Int] {
577
- var mDb: Database
578
- if let data = ("["+parsingData+"]").data(using: .utf8) {
579
- var jsonSQLite: [JsonSQLite]
580
- do {
581
- jsonSQLite = try JSONDecoder()
582
- .decode([JsonSQLite].self, from: data)
583
- } catch let error {
584
- var msg: String = "Stringify Json Object not Valid "
585
- msg.append("\(error)")
586
- throw CapacitorSQLiteError.failed(message: msg)
587
- }
588
- let encrypted: Bool = jsonSQLite[0].encrypted
589
- let inMode: String = encrypted ? "secret"
590
- : "no-encryption"
591
- let version: Int = jsonSQLite[0].version
592
- var dbName: String = CapacitorSQLite.getDatabaseName(dbName: jsonSQLite[0].database)
593
- dbName.append("SQLite.db")
594
- // open the database
595
- do {
596
- mDb = try Database(
597
- databaseLocation: databaseLocation,
598
- databaseName: dbName, encrypted: encrypted,
599
- mode: inMode, version: version, vUpgDict: [:])
600
- try mDb.open()
601
- } catch DatabaseError.open(let message) {
602
- throw CapacitorSQLiteError.failed(message: message)
603
- } catch let error {
604
- let msg: String = "\(error)"
605
- throw CapacitorSQLiteError.failed(message: msg)
606
- }
607
- // import from Json Object
608
- do {
609
- let res: [String: Int] = try mDb
610
- .importFromJson(jsonSQLite: jsonSQLite[0])
611
- try mDb.close()
612
- if let result = res["changes"] {
613
- if result < 0 {
614
- let msg: String = "changes < 0"
615
- throw CapacitorSQLiteError
616
- .failed(message: msg)
617
- } else {
618
- return res
619
- }
620
- } else {
621
- 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)")
622
691
  throw CapacitorSQLiteError.failed(message: msg)
623
692
  }
624
- } catch DatabaseError.importFromJson(let message) {
625
- 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
626
700
  do {
627
- 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)"
628
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
+ }
629
738
  } catch DatabaseError.close(let message) {
630
- msg.append(" \(message)")
631
- throw CapacitorSQLiteError.failed(message: msg)
739
+ throw CapacitorSQLiteError.failed(message: message)
632
740
  }
633
- } catch DatabaseError.close(let message) {
634
- throw CapacitorSQLiteError.failed(message: message)
741
+ } else {
742
+ let msg: String = "Stringify Json Object not Valid"
743
+ throw CapacitorSQLiteError.failed(message: msg)
635
744
  }
636
745
  } else {
637
- let msg: String = "Stringify Json Object not Valid"
638
- throw CapacitorSQLiteError.failed(message: msg)
746
+ throw CapacitorSQLiteError.failed(message: initMessage)
639
747
  }
640
748
  }
641
749
  // swiftlint:enable function_body_length
@@ -644,56 +752,64 @@ enum CapacitorSQLiteError: Error {
644
752
 
645
753
  @objc func exportToJson(_ dbName: String, expMode: String)
646
754
  throws -> [String: Any] {
647
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
648
- guard let mDb: Database = dbDict[mDbName] else {
649
- let msg = "Connection to \(mDbName) not available"
650
- throw CapacitorSQLiteError.failed(message: msg)
651
- }
652
- 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() {
653
762
 
654
- do {
655
- let res: [String: Any] = try
656
- mDb.exportToJson(expMode: expMode)
657
- if res.count == 5 || res.count == 6 {
658
- return res
659
- } else {
660
- var msg: String = "return Object is not a "
661
- 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)"
662
777
  throw CapacitorSQLiteError.failed(message: msg)
663
778
  }
664
- } catch DatabaseError.exportToJson(let message) {
665
- throw CapacitorSQLiteError.failed(message: message)
666
- } catch let error {
667
- let msg: String = "\(error)"
779
+ } else {
780
+ let msg = "Database \(mDbName) not opened"
668
781
  throw CapacitorSQLiteError.failed(message: msg)
669
782
  }
670
783
  } else {
671
- let msg = "Database \(mDbName) not opened"
672
- throw CapacitorSQLiteError.failed(message: msg)
784
+ throw CapacitorSQLiteError.failed(message: initMessage)
673
785
  }
674
786
  }
675
787
 
676
788
  // MARK: - createSyncTable
677
789
 
678
790
  @objc func createSyncTable(_ dbName: String) throws -> NSNumber {
679
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
680
- guard let mDb: Database = dbDict[mDbName] else {
681
- let msg = "Connection to \(mDbName) not available"
682
- throw CapacitorSQLiteError.failed(message: msg)
683
- }
684
- if mDb.isDBOpen() {
685
- do {
686
- let res: Int = try mDb.createSyncTable()
687
- return res as NSNumber
688
- } catch DatabaseError.createSyncTable(let message) {
689
- throw CapacitorSQLiteError.failed(message: message)
690
- } catch let error {
691
- 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"
692
809
  throw CapacitorSQLiteError.failed(message: msg)
693
810
  }
694
811
  } else {
695
- let msg = "Database \(mDbName) not opened"
696
- throw CapacitorSQLiteError.failed(message: msg)
812
+ throw CapacitorSQLiteError.failed(message: initMessage)
697
813
  }
698
814
  }
699
815
 
@@ -701,60 +817,68 @@ enum CapacitorSQLiteError: Error {
701
817
 
702
818
  @objc func setSyncDate(_ dbName: String, syncDate: String)
703
819
  throws {
704
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
705
- guard let mDb: Database = dbDict[mDbName] else {
706
- let msg = "Connection to \(mDbName) not available"
707
- throw CapacitorSQLiteError.failed(message: msg)
708
- }
709
- 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() {
710
827
 
711
- do {
712
- let res: Bool = try mDb
713
- .setSyncDate(syncDate: syncDate)
714
- if res {
715
- return
716
- } else {
717
- 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)"
718
841
  throw CapacitorSQLiteError.failed(message: msg)
719
842
  }
720
- } catch DatabaseError.createSyncDate(let message) {
721
- throw CapacitorSQLiteError.failed(message: message)
722
- } catch let error {
723
- let msg: String = "\(error)"
843
+ } else {
844
+ let msg = "Database \(mDbName) not opened"
724
845
  throw CapacitorSQLiteError.failed(message: msg)
725
846
  }
726
847
  } else {
727
- let msg = "Database \(mDbName) not opened"
728
- throw CapacitorSQLiteError.failed(message: msg)
848
+ throw CapacitorSQLiteError.failed(message: initMessage)
729
849
  }
730
850
  }
731
851
 
732
852
  // MARK: - getSyncDate
733
853
 
734
854
  @objc func getSyncDate(_ dbName: String) throws -> NSNumber {
735
- let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
736
- guard let mDb: Database = dbDict[mDbName] else {
737
- let msg = "Connection to \(mDbName) not available"
738
- throw CapacitorSQLiteError.failed(message: msg)
739
- }
740
- if mDb.isDBOpen() {
741
- do {
742
- let res: Int64 = try mDb.getSyncDate()
743
- if res > 0 {
744
- return res as NSNumber
745
- } else {
746
- 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)"
747
874
  throw CapacitorSQLiteError.failed(message: msg)
748
875
  }
749
- } catch DatabaseError.getSyncDate(let message) {
750
- throw CapacitorSQLiteError.failed(message: message)
751
- } catch let error {
752
- let msg: String = "\(error)"
876
+ } else {
877
+ let msg = "Database \(mDbName) not opened"
753
878
  throw CapacitorSQLiteError.failed(message: msg)
754
879
  }
755
880
  } else {
756
- let msg = "Database \(mDbName) not opened"
757
- throw CapacitorSQLiteError.failed(message: msg)
881
+ throw CapacitorSQLiteError.failed(message: initMessage)
758
882
  }
759
883
  }
760
884
 
@@ -763,156 +887,176 @@ enum CapacitorSQLiteError: Error {
763
887
  @objc func addUpgradeStatement(_ dbName: String,
764
888
  upgrade: [[String: Any]])
765
889
  throws -> [Int: [String: Any]] {
766
- var upgDict: [String: Any] = [:]
767
- for dict in upgrade {
768
- let keys = dict.keys
769
- if !(keys.contains("fromVersion")) ||
770
- !(keys.contains("toVersion")) ||
771
- !(keys.contains("statement")) {
772
- var msg: String = "upgrade must have keys in "
773
- msg.append("{fromVersion,toVersion,statement}")
774
- 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
+ }
775
904
  }
776
- for (key, value) in dict {
777
- 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)
778
908
  }
909
+ let upgVersionDict: [Int: [String: Any]] =
910
+ [fromVersion: upgDict]
911
+ return upgVersionDict
912
+ } else {
913
+ throw CapacitorSQLiteError.failed(message: initMessage)
779
914
  }
780
- guard let fromVersion = upgDict["fromVersion"] as? Int else {
781
- let msg: String = "fromVersion key must be an Int"
782
- throw CapacitorSQLiteError.failed(message: msg)
783
- }
784
- let upgVersionDict: [Int: [String: Any]] =
785
- [fromVersion: upgDict]
786
- return upgVersionDict
787
915
  }
788
916
 
789
917
  // MARK: - copyFromAssets
790
918
 
791
919
  @objc func copyFromAssets(overwrite: Bool) throws {
920
+ if isInit {
792
921
 
793
- // check if the assets/database folder exists
794
- do {
795
- let assetsDbPath: URL = try
796
- UtilsFile.getAssetsDatabasesPath()
797
- let aPath: String = assetsDbPath.path
798
- let bRes: Bool = UtilsFile.isDirExist(dirPath: aPath)
799
- if bRes {
800
- // get the database files from assets
801
- let dbList: [String] = try UtilsFile
802
- .getFileList(path: aPath, ext: ".db")
803
- // loop through the database files
804
- for mDb in dbList {
805
- // for each check if the suffix SQLite.db is there
806
- // or add it
807
- let toDb: String = UtilsFile
808
- .setPathSuffix(sDb: mDb)
809
- // for each copy the file to the Application
810
- // database folder
811
- _ = try UtilsFile
812
- .copyFromAssetToDatabase(databaseLocation: databaseLocation,
813
- fromDb: mDb,
814
- toDb: toDb, overwrite: overwrite)
815
- }
816
- // get the zip files
817
- let zipList: [String] = try UtilsFile
818
- .getFileList(path: aPath, ext: ".zip")
819
- // loop through the database files
820
- for zip in zipList {
821
- // for each zip uncompress the file to the Application
822
- // database folder
823
- _ = try UtilsFile
824
- .unzipFromAssetToDatabase(databaseLocation: databaseLocation,
825
- 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)
826
960
  }
827
- return
828
- } else {
829
- 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)"
830
967
  throw CapacitorSQLiteError.failed(message: msg)
831
968
  }
832
- } catch UtilsFileError.copyFromAssetToDatabaseFailed(let message) {
833
- throw CapacitorSQLiteError.failed(message: message)
834
- } catch UtilsFileError.unzipFromAssetToDatabaseFailed(let message) {
835
- throw CapacitorSQLiteError.failed(message: message)
836
- } catch let error {
837
- let msg: String = "\(error)"
838
- throw CapacitorSQLiteError.failed(message: msg)
969
+ } else {
970
+ throw CapacitorSQLiteError.failed(message: initMessage)
839
971
  }
840
972
  }
841
973
 
842
974
  // MARK: - getDatabaseList
843
975
 
844
976
  @objc func getDatabaseList() throws -> [String] {
845
- do {
846
- let aPath: String = try (UtilsFile.getFolderURL(folderPath: databaseLocation)).path
847
- // get the database files
848
- let dbList: [String] = try UtilsFile.getFileList(path: aPath, ext: ".db")
849
- 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
850
983
 
851
- } catch let error {
852
- let msg: String = "\(error)"
853
- 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)
854
990
  }
855
-
856
991
  }
857
992
 
858
993
  // MARK: - getMigratableDbList
859
994
 
860
995
  @objc func getMigratableDbList(_ folderPath: String) throws -> [String] {
861
- do {
862
- let dbList: [String] = try UtilsMigrate
863
- .getMigratableList(folderPath: folderPath)
864
- return dbList
996
+ if isInit {
997
+ do {
998
+ let dbList: [String] = try UtilsMigrate
999
+ .getMigratableList(folderPath: folderPath)
1000
+ return dbList
865
1001
 
866
- } catch UtilsMigrateError.getMigratableList(let message) {
867
- var msg: String = "getMigratableList:"
868
- msg.append(" \(message)")
869
- throw CapacitorSQLiteError.failed(message: msg)
870
- } catch let error {
871
- let msg: String = "\(error)"
872
- 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)
873
1012
  }
874
-
875
1013
  }
876
1014
 
877
1015
  // MARK: - addSQLiteSuffix
878
1016
 
879
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)
880
1028
 
881
- do {
882
- try UtilsMigrate.addSQLiteSuffix(databaseLocation: databaseLocation,
883
- folderPath: folderPath,
884
- dbList: dbList)
885
- return
886
- } catch UtilsMigrateError.addSQLiteSuffix(let message) {
887
- var msg: String = "addSQLiteSuffix:"
888
- msg.append(" \(message)")
889
- throw CapacitorSQLiteError.failed(message: msg)
890
-
891
- } catch let error {
892
- var msg: String = "addSQLiteSuffix:"
893
- msg.append(" \(error)")
894
- 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)
895
1036
  }
896
1037
  }
897
1038
 
898
1039
  // MARK: - deleteOldDatabases
899
1040
 
900
1041
  @objc func deleteOldDatabases(_ folderPath: String, dbList: [String]) throws {
901
- do {
902
- try UtilsMigrate
903
- .deleteOldDatabases(folderPath: folderPath, dbList: dbList)
904
- return
905
- } catch UtilsMigrateError.deleteOldDatabases(let message) {
906
- var msg: String = "deleteOldDatabases:"
907
- msg.append(" \(message)")
908
- 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)
909
1051
 
910
- } catch let error {
911
- var msg: String = "deleteOldDatabases:"
912
- msg.append(" \(error)")
913
- 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)
914
1059
  }
915
-
916
1060
  }
917
1061
  class func getDatabaseName(dbName: String) -> String {
918
1062
  var retName: String = dbName