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

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