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