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