@capacitor-community/sqlite 5.0.5 → 5.0.7-1
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/README.md +3 -0
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLite.java +9 -0
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java +9 -0
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/UtilsEncryption.java +111 -0
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsSecret.java +3 -3
- package/dist/esm/definitions.d.ts +66 -0
- package/dist/esm/definitions.js +37 -0
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/web.d.ts +3 -1
- package/dist/esm/web.js +8 -0
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +45 -0
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +45 -0
- package/dist/plugin.js.map +1 -1
- package/electron/dist/plugin.js +138 -52
- package/electron/dist/plugin.js.map +1 -1
- package/electron/rollup.config.js +2 -0
- package/ios/Plugin/CapacitorSQLite.swift +117 -88
- package/ios/Plugin/Database.swift +17 -5
- package/ios/Plugin/ImportExportJson/ImportData.swift +434 -0
- package/ios/Plugin/ImportExportJson/ImportFromJson.swift +43 -57
- package/ios/Plugin/ImportExportJson/JsonSQLite.swift +7 -0
- package/ios/Plugin/Utils/UtilsDelete.swift +506 -0
- package/ios/Plugin/Utils/UtilsJson.swift +123 -1
- package/ios/Plugin/Utils/UtilsSQLCipher.swift +173 -477
- package/ios/Plugin/Utils/UtilsSQLStatement.swift +450 -0
- package/package.json +2 -2
- package/src/definitions.ts +104 -0
- package/src/web.ts +10 -0
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
//
|
|
2
|
+
// ImportData.swift
|
|
3
|
+
// CapacitorCommunitySqlite
|
|
4
|
+
//
|
|
5
|
+
// Created by Quéau Jean Pierre on 23/07/2023.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
import Foundation
|
|
9
|
+
|
|
10
|
+
// swiftlint:disable file_length
|
|
11
|
+
public class ImportData {
|
|
12
|
+
var jsonSQLite: [JsonSQLite]
|
|
13
|
+
var jsonDict: [String: Any]
|
|
14
|
+
|
|
15
|
+
init(jsonSQLite: JsonSQLite) {
|
|
16
|
+
self.jsonSQLite = [jsonSQLite]
|
|
17
|
+
self.jsonDict = [:]
|
|
18
|
+
}
|
|
19
|
+
init(jsonDict: [String: Any]) {
|
|
20
|
+
self.jsonSQLite = []
|
|
21
|
+
self.jsonDict = jsonDict
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
var database: String {
|
|
25
|
+
var mDatabase = ""
|
|
26
|
+
mDatabase = jsonSQLite.count > 0 ? jsonSQLite[0].database : ""
|
|
27
|
+
if !jsonDict.isEmpty {
|
|
28
|
+
if let mDBName = jsonDict["database"] as? String {
|
|
29
|
+
mDatabase = mDBName
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return mDatabase
|
|
33
|
+
}
|
|
34
|
+
var mode: String {
|
|
35
|
+
var mMode = ""
|
|
36
|
+
mMode = jsonSQLite.count > 0 ? jsonSQLite[0].mode : ""
|
|
37
|
+
if !jsonDict.isEmpty {
|
|
38
|
+
if let nMode = jsonDict["mode"] as? String {
|
|
39
|
+
mMode = nMode
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return mMode
|
|
43
|
+
}
|
|
44
|
+
var encrypted: Bool {
|
|
45
|
+
var mEnc = false
|
|
46
|
+
mEnc = jsonSQLite.count > 0 ? jsonSQLite[0].encrypted : false
|
|
47
|
+
if !jsonDict.isEmpty {
|
|
48
|
+
if let nEnc = jsonDict["encrypted"] as? Bool {
|
|
49
|
+
mEnc = nEnc
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return mEnc
|
|
53
|
+
}
|
|
54
|
+
var overwrite: Bool {
|
|
55
|
+
var mOve = false
|
|
56
|
+
if jsonSQLite.count > 0 {
|
|
57
|
+
if let nOve = jsonSQLite[0].overwrite {
|
|
58
|
+
mOve = nOve
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if !jsonDict.isEmpty {
|
|
63
|
+
if let nOve = jsonDict["overwrite"] as? Bool {
|
|
64
|
+
mOve = nOve
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return mOve
|
|
68
|
+
}
|
|
69
|
+
var version: Int {
|
|
70
|
+
var mVer = 1
|
|
71
|
+
mVer = jsonSQLite.count > 0 ? jsonSQLite[0].version : 1
|
|
72
|
+
if !jsonDict.isEmpty {
|
|
73
|
+
if let nVer = jsonDict["version"] as? Int {
|
|
74
|
+
mVer = nVer
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return mVer
|
|
78
|
+
}
|
|
79
|
+
var tables: [ImportTable] {
|
|
80
|
+
var fTab: [ImportTable] = []
|
|
81
|
+
if jsonSQLite.count > 0 {
|
|
82
|
+
let mTables = jsonSQLite[0].tables
|
|
83
|
+
for table in mTables {
|
|
84
|
+
let mTab: ImportTable = ImportTable(jsonTable: [table])
|
|
85
|
+
fTab.append(mTab)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if !jsonDict.isEmpty {
|
|
89
|
+
if let mTables = jsonDict["tables"] as? [[String: Any]] {
|
|
90
|
+
for table in mTables {
|
|
91
|
+
let mTab: ImportTable = ImportTable(tableDict: table)
|
|
92
|
+
fTab.append(mTab)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return fTab
|
|
97
|
+
}
|
|
98
|
+
var views: [ImportView]? {
|
|
99
|
+
var fView: [ImportView] = []
|
|
100
|
+
if jsonSQLite.count > 0 {
|
|
101
|
+
if let mViews = jsonSQLite[0].views {
|
|
102
|
+
for view in mViews {
|
|
103
|
+
let mVw: ImportView = ImportView(jsonView: [view])
|
|
104
|
+
fView.append(mVw)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if !jsonDict.isEmpty {
|
|
109
|
+
if let mViews = jsonDict["views"] as? [[String: Any]] {
|
|
110
|
+
for view in mViews {
|
|
111
|
+
let mVw: ImportView = ImportView(viewDict: view)
|
|
112
|
+
fView.append(mVw)
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return fView
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
public class ImportTable {
|
|
120
|
+
var jsonTable: [JsonTable]
|
|
121
|
+
var tableDict: [String: Any]
|
|
122
|
+
|
|
123
|
+
init(jsonTable: [JsonTable]) {
|
|
124
|
+
self.jsonTable = jsonTable
|
|
125
|
+
self.tableDict = [:]
|
|
126
|
+
}
|
|
127
|
+
init(tableDict: [String: Any]) {
|
|
128
|
+
self.jsonTable = []
|
|
129
|
+
self.tableDict = tableDict
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
var name: String {
|
|
133
|
+
var mName = ""
|
|
134
|
+
mName = jsonTable.count > 0 ? jsonTable[0].name : ""
|
|
135
|
+
if !tableDict.isEmpty {
|
|
136
|
+
if let nName = tableDict["name"] as? String {
|
|
137
|
+
mName = nName
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return mName
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
var schema: [ImportColumn]? {
|
|
144
|
+
var mSchema: [ImportColumn] = []
|
|
145
|
+
if jsonTable.count > 0 {
|
|
146
|
+
if let nSchema = jsonTable[0].schema {
|
|
147
|
+
for schm in nSchema {
|
|
148
|
+
let nSchm: ImportColumn = ImportColumn(jsonColumn: [schm])
|
|
149
|
+
mSchema.append(nSchm)
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
}
|
|
154
|
+
if !tableDict.isEmpty {
|
|
155
|
+
if let nSchema = tableDict["schema"] as? [[String: Any]] {
|
|
156
|
+
for schm in nSchema {
|
|
157
|
+
let nSchm: ImportColumn =
|
|
158
|
+
ImportColumn(columnDict: schm)
|
|
159
|
+
mSchema.append(nSchm)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return mSchema
|
|
164
|
+
}
|
|
165
|
+
var indexes: [ImportIndex]? {
|
|
166
|
+
var mIndexes: [ImportIndex] = []
|
|
167
|
+
if jsonTable.count > 0 {
|
|
168
|
+
if let nIndexes = jsonTable[0].indexes {
|
|
169
|
+
for nIdx in nIndexes {
|
|
170
|
+
let nIndex: ImportIndex = ImportIndex(jsonIndex: [nIdx])
|
|
171
|
+
mIndexes.append(nIndex)
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
}
|
|
176
|
+
if !tableDict.isEmpty {
|
|
177
|
+
if let nIndexes = tableDict["indexes"] as? [[String: Any]] {
|
|
178
|
+
for nIdx in nIndexes {
|
|
179
|
+
let nIndex: ImportIndex = ImportIndex(indexDict: nIdx)
|
|
180
|
+
mIndexes.append(nIndex)
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return mIndexes
|
|
185
|
+
}
|
|
186
|
+
var triggers: [ImportTrigger]? {
|
|
187
|
+
var mTrigs: [ImportTrigger] = []
|
|
188
|
+
if jsonTable.count > 0 {
|
|
189
|
+
if let nTrigs = jsonTable[0].triggers {
|
|
190
|
+
for mTrig in nTrigs {
|
|
191
|
+
let nTrig: ImportTrigger = ImportTrigger(jsonTrigger: [mTrig])
|
|
192
|
+
mTrigs.append(nTrig)
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
}
|
|
197
|
+
if !tableDict.isEmpty {
|
|
198
|
+
if let nTrigs = tableDict["triggers"] as? [[String: Any]] {
|
|
199
|
+
for mTrig in nTrigs {
|
|
200
|
+
let nTrig: ImportTrigger = ImportTrigger(triggerDict: mTrig)
|
|
201
|
+
mTrigs.append(nTrig)
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return mTrigs
|
|
206
|
+
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
var values: [[Any]]? {
|
|
210
|
+
var mValues: [[Any]] = []
|
|
211
|
+
if jsonTable.count > 0 {
|
|
212
|
+
if let nValues = jsonTable[0].values {
|
|
213
|
+
for row in nValues {
|
|
214
|
+
let nrow = UtilsJson.getValuesFromRow(rowValues: row)
|
|
215
|
+
mValues.append(nrow)
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if !tableDict.isEmpty {
|
|
220
|
+
if let nValues = tableDict["values"] as? [[Any]] {
|
|
221
|
+
mValues = nValues
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return mValues
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
}
|
|
228
|
+
public class ImportColumn {
|
|
229
|
+
var jsonColumn: [JsonColumn]
|
|
230
|
+
var columnDict: [String: Any]
|
|
231
|
+
|
|
232
|
+
init(jsonColumn: [JsonColumn]) {
|
|
233
|
+
self.jsonColumn = jsonColumn
|
|
234
|
+
self.columnDict = [:]
|
|
235
|
+
}
|
|
236
|
+
init(columnDict: [String: Any]) {
|
|
237
|
+
self.jsonColumn = []
|
|
238
|
+
self.columnDict = columnDict
|
|
239
|
+
}
|
|
240
|
+
var column: String? {
|
|
241
|
+
var mCol = ""
|
|
242
|
+
if jsonColumn.count > 0 {
|
|
243
|
+
if let nCol = jsonColumn[0].column {
|
|
244
|
+
mCol = nCol
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if !columnDict.isEmpty {
|
|
248
|
+
if let nCol = columnDict["column"] as? String {
|
|
249
|
+
mCol = nCol
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return mCol
|
|
253
|
+
}
|
|
254
|
+
var value: String {
|
|
255
|
+
var mVal = ""
|
|
256
|
+
mVal = jsonColumn.count > 0 ? jsonColumn[0].value : ""
|
|
257
|
+
if !columnDict.isEmpty {
|
|
258
|
+
if let nVal = columnDict["value"] as? String {
|
|
259
|
+
mVal = nVal
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return mVal
|
|
263
|
+
}
|
|
264
|
+
var foreignkey: String? {
|
|
265
|
+
var mFK = ""
|
|
266
|
+
if jsonColumn.count > 0 {
|
|
267
|
+
if let nFK = jsonColumn[0].foreignkey {
|
|
268
|
+
mFK = nFK
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
if !columnDict.isEmpty {
|
|
272
|
+
if let nFK = columnDict["foreignkey"] as? String {
|
|
273
|
+
mFK = nFK
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
return mFK
|
|
277
|
+
}
|
|
278
|
+
var constraint: String? {
|
|
279
|
+
var mCon = ""
|
|
280
|
+
if jsonColumn.count > 0 {
|
|
281
|
+
if let nCon = jsonColumn[0].constraint {
|
|
282
|
+
mCon = nCon
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
if !columnDict.isEmpty {
|
|
286
|
+
if let nCon = columnDict["constraint"] as? String {
|
|
287
|
+
mCon = nCon
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return mCon
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
public class ImportIndex {
|
|
294
|
+
var jsonIndex: [JsonIndex]
|
|
295
|
+
var indexDict: [String: Any]
|
|
296
|
+
|
|
297
|
+
init(jsonIndex: [JsonIndex]) {
|
|
298
|
+
self.jsonIndex = jsonIndex
|
|
299
|
+
self.indexDict = [:]
|
|
300
|
+
}
|
|
301
|
+
init(indexDict: [String: Any]) {
|
|
302
|
+
self.jsonIndex = []
|
|
303
|
+
self.indexDict = indexDict
|
|
304
|
+
}
|
|
305
|
+
var mode: String? {
|
|
306
|
+
var mMode = ""
|
|
307
|
+
if jsonIndex.count > 0 {
|
|
308
|
+
if let nMode = jsonIndex[0].mode {
|
|
309
|
+
mMode = nMode
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
if !indexDict.isEmpty {
|
|
313
|
+
if let nMode = indexDict["mode"] as? String {
|
|
314
|
+
mMode = nMode
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return mMode
|
|
318
|
+
}
|
|
319
|
+
var value: String {
|
|
320
|
+
var mVal = ""
|
|
321
|
+
mVal = jsonIndex.count > 0 ? jsonIndex[0].value : ""
|
|
322
|
+
if !indexDict.isEmpty {
|
|
323
|
+
if let nVal = indexDict["value"] as? String {
|
|
324
|
+
mVal = nVal
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return mVal
|
|
328
|
+
}
|
|
329
|
+
var name: String {
|
|
330
|
+
var mName = ""
|
|
331
|
+
mName = jsonIndex.count > 0 ? jsonIndex[0].name : ""
|
|
332
|
+
if !indexDict.isEmpty {
|
|
333
|
+
if let nName = indexDict["name"] as? String {
|
|
334
|
+
mName = nName
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
return mName
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
public class ImportTrigger {
|
|
341
|
+
var jsonTrigger: [JsonTrigger]
|
|
342
|
+
var triggerDict: [String: Any]
|
|
343
|
+
|
|
344
|
+
init(jsonTrigger: [JsonTrigger]) {
|
|
345
|
+
self.jsonTrigger = jsonTrigger
|
|
346
|
+
self.triggerDict = [:]
|
|
347
|
+
}
|
|
348
|
+
init(triggerDict: [String: Any]) {
|
|
349
|
+
self.jsonTrigger = []
|
|
350
|
+
self.triggerDict = triggerDict
|
|
351
|
+
}
|
|
352
|
+
var name: String {
|
|
353
|
+
var mName = ""
|
|
354
|
+
mName = jsonTrigger.count > 0 ? jsonTrigger[0].name : ""
|
|
355
|
+
if !triggerDict.isEmpty {
|
|
356
|
+
if let nName = triggerDict["name"] as? String {
|
|
357
|
+
mName = nName
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return mName
|
|
361
|
+
}
|
|
362
|
+
var timeevent: String {
|
|
363
|
+
var mTime = ""
|
|
364
|
+
mTime = jsonTrigger.count > 0 ? jsonTrigger[0].timeevent : ""
|
|
365
|
+
if !triggerDict.isEmpty {
|
|
366
|
+
if let nTime = triggerDict["timeevent"] as? String {
|
|
367
|
+
mTime = nTime
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
return mTime
|
|
371
|
+
}
|
|
372
|
+
var logic: String {
|
|
373
|
+
var mLog = ""
|
|
374
|
+
mLog = jsonTrigger.count > 0 ? jsonTrigger[0].logic : ""
|
|
375
|
+
if !triggerDict.isEmpty {
|
|
376
|
+
if let nLog = triggerDict["logic"] as? String {
|
|
377
|
+
mLog = nLog
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return mLog
|
|
381
|
+
}
|
|
382
|
+
var condition: String? {
|
|
383
|
+
var mCon = ""
|
|
384
|
+
if jsonTrigger.count > 0 {
|
|
385
|
+
if let nCon = jsonTrigger[0].condition {
|
|
386
|
+
mCon = nCon
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
if !triggerDict.isEmpty {
|
|
390
|
+
if let nCon = triggerDict["condition"] as? String {
|
|
391
|
+
mCon = nCon
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
return mCon
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
public class ImportView {
|
|
400
|
+
var jsonView: [JsonView]
|
|
401
|
+
var viewDict: [String: Any]
|
|
402
|
+
|
|
403
|
+
init(jsonView: [JsonView]) {
|
|
404
|
+
self.jsonView = jsonView
|
|
405
|
+
self.viewDict = [:]
|
|
406
|
+
}
|
|
407
|
+
init(viewDict: [String: Any]) {
|
|
408
|
+
self.jsonView = []
|
|
409
|
+
self.viewDict = viewDict
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
var name: String {
|
|
413
|
+
var mName = ""
|
|
414
|
+
mName = jsonView.count > 0 ? jsonView[0].name : ""
|
|
415
|
+
if !viewDict.isEmpty {
|
|
416
|
+
if let nName = viewDict["name"] as? String {
|
|
417
|
+
mName = nName
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
return mName
|
|
421
|
+
}
|
|
422
|
+
var value: String {
|
|
423
|
+
var mValue = ""
|
|
424
|
+
mValue = jsonView.count > 0 ? jsonView[0].value : ""
|
|
425
|
+
if !viewDict.isEmpty {
|
|
426
|
+
if let nView = viewDict["value"] as? String {
|
|
427
|
+
mValue = nView
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return mValue
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
}
|
|
434
|
+
// swiftlint:enable file_length
|
|
@@ -34,25 +34,24 @@ class ImportFromJson {
|
|
|
34
34
|
|
|
35
35
|
// MARK: - ImportFromJson - CreateDatabaseSchema
|
|
36
36
|
|
|
37
|
-
class func createDatabaseSchema(mDB: Database,
|
|
38
|
-
|
|
37
|
+
class func createDatabaseSchema(mDB: Database, tables: [ImportTable],
|
|
38
|
+
mode: String, version: Int)
|
|
39
39
|
throws -> Int {
|
|
40
40
|
let msg = "importFromJson: "
|
|
41
41
|
var changes: Int = -1
|
|
42
|
-
let version: Int = jsonSQLite.version
|
|
43
42
|
|
|
44
43
|
do {
|
|
45
44
|
// Set PRAGMAS
|
|
46
45
|
try UtilsSQLCipher.setVersion(mDB: mDB,
|
|
47
46
|
version: version)
|
|
48
|
-
if
|
|
47
|
+
if mode == "full" {
|
|
49
48
|
// Drop All Tables, Indexes and Triggers
|
|
50
49
|
try _ = UtilsDrop.dropAll(mDB: mDB)
|
|
51
50
|
}
|
|
52
51
|
// create database schema
|
|
53
52
|
changes = try ImportFromJson
|
|
54
53
|
.createSchema(mDB: mDB,
|
|
55
|
-
|
|
54
|
+
tables: tables, mode: mode)
|
|
56
55
|
let msg = "Schema creation completed changes: \(changes)"
|
|
57
56
|
notifyImportProgressEvent(msg: msg)
|
|
58
57
|
return changes
|
|
@@ -76,8 +75,8 @@ class ImportFromJson {
|
|
|
76
75
|
// MARK: - ImportFromJson - createSchema
|
|
77
76
|
|
|
78
77
|
// swiftlint:disable function_body_length
|
|
79
|
-
class func createSchema(mDB: Database,
|
|
80
|
-
|
|
78
|
+
class func createSchema(mDB: Database, tables: [ImportTable],
|
|
79
|
+
mode: String) throws -> Int {
|
|
81
80
|
var changes: Int = 0
|
|
82
81
|
var initChanges: Int = 0
|
|
83
82
|
do {
|
|
@@ -88,7 +87,7 @@ class ImportFromJson {
|
|
|
88
87
|
}
|
|
89
88
|
// Create a Schema Statements
|
|
90
89
|
let statements = ImportFromJson
|
|
91
|
-
.createSchemaStatement(
|
|
90
|
+
.createSchemaStatement(tables: tables, mode: mode )
|
|
92
91
|
if statements.count > 0 {
|
|
93
92
|
let joined = statements.joined(separator: "\n")
|
|
94
93
|
let mStmt: String = joined.replacingOccurrences(of: "\'", with: "'")
|
|
@@ -132,7 +131,7 @@ class ImportFromJson {
|
|
|
132
131
|
.createSchema(message: message)
|
|
133
132
|
}
|
|
134
133
|
} else {
|
|
135
|
-
if
|
|
134
|
+
if mode == "partial" {
|
|
136
135
|
changes = 0
|
|
137
136
|
// Commit the transaction
|
|
138
137
|
try UtilsSQLCipher.commitTransaction(mDB: mDB)
|
|
@@ -144,16 +143,15 @@ class ImportFromJson {
|
|
|
144
143
|
|
|
145
144
|
// MARK: - ImportFromJson - createSchemaStatement
|
|
146
145
|
|
|
147
|
-
class func createSchemaStatement(
|
|
148
|
-
|
|
146
|
+
class func createSchemaStatement(tables: [ImportTable],
|
|
147
|
+
mode: String) -> [String] {
|
|
149
148
|
// Create the Database Schema
|
|
150
149
|
var statements: [String] = []
|
|
151
150
|
// Loop through Tables
|
|
152
|
-
for ipos in 0..<
|
|
153
|
-
let mode: String =
|
|
154
|
-
let tableName: String =
|
|
155
|
-
if let mSchema: [
|
|
156
|
-
jsonSQLite.tables[ipos].schema {
|
|
151
|
+
for ipos in 0..<tables.count {
|
|
152
|
+
let mode: String = mode
|
|
153
|
+
let tableName: String = tables[ipos].name
|
|
154
|
+
if let mSchema: [ImportColumn] = tables[ipos].schema {
|
|
157
155
|
if mSchema.count > 0 {
|
|
158
156
|
let stmt: [String] =
|
|
159
157
|
ImportFromJson.createTableSchema(
|
|
@@ -162,8 +160,7 @@ class ImportFromJson {
|
|
|
162
160
|
statements.append(contentsOf: stmt)
|
|
163
161
|
}
|
|
164
162
|
}
|
|
165
|
-
if let mIndexes: [
|
|
166
|
-
jsonSQLite.tables[ipos].indexes {
|
|
163
|
+
if let mIndexes: [ImportIndex] = tables[ipos].indexes {
|
|
167
164
|
if mIndexes.count > 0 {
|
|
168
165
|
let stmt: [String] =
|
|
169
166
|
ImportFromJson.createTableIndexes(
|
|
@@ -171,8 +168,7 @@ class ImportFromJson {
|
|
|
171
168
|
statements.append(contentsOf: stmt)
|
|
172
169
|
}
|
|
173
170
|
}
|
|
174
|
-
if let mTriggers: [
|
|
175
|
-
jsonSQLite.tables[ipos].triggers {
|
|
171
|
+
if let mTriggers: [ImportTrigger] = tables[ipos].triggers {
|
|
176
172
|
if mTriggers.count > 0 {
|
|
177
173
|
let stmt: [String] =
|
|
178
174
|
ImportFromJson.createTableTriggers(
|
|
@@ -188,7 +184,7 @@ class ImportFromJson {
|
|
|
188
184
|
|
|
189
185
|
// swiftlint:disable function_body_length
|
|
190
186
|
// swiftlint:disable cyclomatic_complexity
|
|
191
|
-
class func createTableSchema(mSchema: [
|
|
187
|
+
class func createTableSchema(mSchema: [ImportColumn],
|
|
192
188
|
tableName: String, mode: String)
|
|
193
189
|
-> [String] {
|
|
194
190
|
var statements: [String] = []
|
|
@@ -251,7 +247,7 @@ class ImportFromJson {
|
|
|
251
247
|
|
|
252
248
|
// MARK: - ImportFromJson - CreateTableIndexes
|
|
253
249
|
|
|
254
|
-
class func createTableIndexes(mIndexes: [
|
|
250
|
+
class func createTableIndexes(mIndexes: [ImportIndex],
|
|
255
251
|
tableName: String) -> [String] {
|
|
256
252
|
var statements: [String] = []
|
|
257
253
|
for jpos in 0..<mIndexes.count {
|
|
@@ -279,7 +275,7 @@ class ImportFromJson {
|
|
|
279
275
|
|
|
280
276
|
// MARK: - ImportFromJson - CreateTableTriggers
|
|
281
277
|
|
|
282
|
-
class func createTableTriggers(mTriggers: [
|
|
278
|
+
class func createTableTriggers(mTriggers: [ImportTrigger],
|
|
283
279
|
tableName: String) -> [String] {
|
|
284
280
|
var statements: [String] = []
|
|
285
281
|
|
|
@@ -308,8 +304,8 @@ class ImportFromJson {
|
|
|
308
304
|
|
|
309
305
|
// swiftlint:disable function_body_length
|
|
310
306
|
class func createDatabaseData(mDB: Database,
|
|
311
|
-
|
|
312
|
-
|
|
307
|
+
tables: [ImportTable],
|
|
308
|
+
mode: String) throws -> Int {
|
|
313
309
|
var changes: Int = -1
|
|
314
310
|
var initChanges: Int = -1
|
|
315
311
|
var isValue: Bool = false
|
|
@@ -322,19 +318,19 @@ class ImportFromJson {
|
|
|
322
318
|
throw ImportFromJsonError.createDatabaseData(message: message)
|
|
323
319
|
}
|
|
324
320
|
// Loop on tables to create Data
|
|
325
|
-
for ipos in 0..<
|
|
326
|
-
if let mValues =
|
|
321
|
+
for ipos in 0..<tables.count {
|
|
322
|
+
if let mValues = tables[ipos].values {
|
|
327
323
|
if mValues.count > 0 {
|
|
328
324
|
isValue = true
|
|
329
325
|
do {
|
|
330
|
-
let tableName =
|
|
326
|
+
let tableName = tables[ipos].name
|
|
331
327
|
try ImportFromJson.createTableData(
|
|
332
328
|
mDB: mDB,
|
|
333
|
-
mode:
|
|
329
|
+
mode: mode,
|
|
334
330
|
mValues: mValues,
|
|
335
331
|
tableName: tableName)
|
|
336
332
|
let msg = "Table \(tableName) data creation completed " +
|
|
337
|
-
"\(ipos + 1)/\(
|
|
333
|
+
"\(ipos + 1)/\(tables.count) ..."
|
|
338
334
|
notifyImportProgressEvent(msg: msg)
|
|
339
335
|
|
|
340
336
|
} catch ImportFromJsonError
|
|
@@ -382,7 +378,7 @@ class ImportFromJson {
|
|
|
382
378
|
// swiftlint:disable function_body_length
|
|
383
379
|
class func createTableData(
|
|
384
380
|
mDB: Database, mode: String,
|
|
385
|
-
mValues: [[
|
|
381
|
+
mValues: [[Any]],
|
|
386
382
|
tableName: String) throws {
|
|
387
383
|
var lastId: Int64 = -1
|
|
388
384
|
// Check if table exists
|
|
@@ -410,8 +406,7 @@ class ImportFromJson {
|
|
|
410
406
|
}
|
|
411
407
|
for jpos in 0..<mValues.count {
|
|
412
408
|
// Check row validity
|
|
413
|
-
|
|
414
|
-
mValues[jpos]
|
|
409
|
+
var rowValues = mValues[jpos]
|
|
415
410
|
var isRun: Bool = true
|
|
416
411
|
|
|
417
412
|
/* Remove types checking for allowing RDBMS Types
|
|
@@ -430,10 +425,9 @@ class ImportFromJson {
|
|
|
430
425
|
"tableName": tableName]
|
|
431
426
|
let stmt: String = try ImportFromJson
|
|
432
427
|
.createRowStatement(mDB: mDB, data: data,
|
|
433
|
-
row:
|
|
428
|
+
row: rowValues,
|
|
434
429
|
jsonNamesTypes: jsonNamesTypes)
|
|
435
|
-
|
|
436
|
-
rowValues: row)
|
|
430
|
+
|
|
437
431
|
isRun = try UtilsJson.checkUpdate(mDB: mDB, stmt: stmt,
|
|
438
432
|
values: rowValues,
|
|
439
433
|
tableName: tableName,
|
|
@@ -471,7 +465,7 @@ class ImportFromJson {
|
|
|
471
465
|
class func createRowStatement(
|
|
472
466
|
mDB: Database,
|
|
473
467
|
data: [String: Any],
|
|
474
|
-
row: [
|
|
468
|
+
row: [Any],
|
|
475
469
|
jsonNamesTypes: JsonNamesTypes) throws -> String {
|
|
476
470
|
var stmt: String = ""
|
|
477
471
|
var retisIdExists: Bool = false
|
|
@@ -489,19 +483,11 @@ class ImportFromJson {
|
|
|
489
483
|
message: message + " tableName")
|
|
490
484
|
}
|
|
491
485
|
do {
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
} else {
|
|
498
|
-
var message: String = "createRowStatement: Table "
|
|
499
|
-
message.append("\(tableName) values row[0] does not ")
|
|
500
|
-
message.append("exist")
|
|
501
|
-
throw ImportFromJsonError.createRowStatement(
|
|
502
|
-
message: message)
|
|
503
|
-
}
|
|
504
|
-
|
|
486
|
+
let rwValue = row[0]
|
|
487
|
+
retisIdExists = try UtilsJson.isIdExist(
|
|
488
|
+
mDB: mDB, tableName: tableName,
|
|
489
|
+
firstColumnName: jsonNamesTypes.names[0],
|
|
490
|
+
key: rwValue)
|
|
505
491
|
} catch UtilsJsonError.isIdExists(let message) {
|
|
506
492
|
throw ImportFromJsonError.createRowStatement(
|
|
507
493
|
message: message)
|
|
@@ -520,13 +506,13 @@ class ImportFromJson {
|
|
|
520
506
|
let idxDelete: Int = jsonNamesTypes
|
|
521
507
|
.names.firstIndex(where: {$0 == "sql_deleted"}) ?? -1
|
|
522
508
|
if idxDelete >= 0 {
|
|
523
|
-
if let delValue = row[idxDelete]
|
|
509
|
+
if let delValue = row[idxDelete] as? Int {
|
|
524
510
|
if delValue == 1 {
|
|
525
511
|
isUpdate = false
|
|
526
512
|
stmt = "DELETE FROM \(tableName) WHERE "
|
|
527
|
-
if let rwValue = row[0]
|
|
513
|
+
if let rwValue = row[0] as? String {
|
|
528
514
|
stmt += "\(jsonNamesTypes.names[0]) = '\(rwValue)';"
|
|
529
|
-
} else if let rwValue = row[0]
|
|
515
|
+
} else if let rwValue = row[0] as? Int {
|
|
530
516
|
stmt += "\(jsonNamesTypes.names[0]) = \(rwValue);"
|
|
531
517
|
} else {
|
|
532
518
|
var msg: String = "importFromJson: Table "
|
|
@@ -550,9 +536,9 @@ class ImportFromJson {
|
|
|
550
536
|
}
|
|
551
537
|
|
|
552
538
|
stmt = "UPDATE \(tableName) SET \(setString) WHERE "
|
|
553
|
-
if let rwValue = row[0]
|
|
539
|
+
if let rwValue = row[0] as? String {
|
|
554
540
|
stmt += "\(jsonNamesTypes.names[0]) = '\(rwValue)';"
|
|
555
|
-
} else if let rwValue = row[0]
|
|
541
|
+
} else if let rwValue = row[0] as? Int {
|
|
556
542
|
stmt += "\(jsonNamesTypes.names[0]) = \(rwValue);"
|
|
557
543
|
} else {
|
|
558
544
|
var msg: String = "importFromJson: Table "
|
|
@@ -570,7 +556,7 @@ class ImportFromJson {
|
|
|
570
556
|
// MARK: - ImportFromJson - createViews
|
|
571
557
|
|
|
572
558
|
// swiftlint:disable function_body_length
|
|
573
|
-
class func createViews(mDB: Database, views: [
|
|
559
|
+
class func createViews(mDB: Database, views: [ImportView]) throws -> Int {
|
|
574
560
|
var changes: Int = 0
|
|
575
561
|
var initChanges: Int = -1
|
|
576
562
|
var isView: Bool = false
|
|
@@ -633,7 +619,7 @@ class ImportFromJson {
|
|
|
633
619
|
return changes
|
|
634
620
|
|
|
635
621
|
}
|
|
636
|
-
class func createView(mDB: Database, view:
|
|
622
|
+
class func createView(mDB: Database, view: ImportView) throws {
|
|
637
623
|
let stmt = "CREATE VIEW IF NOT EXISTS \(view.name) AS \(view.value);"
|
|
638
624
|
do {
|
|
639
625
|
try UtilsSQLCipher.execute(mDB: mDB, sql: stmt)
|