@capacitor-community/sqlite 3.3.3-1 → 3.3.3-2
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/CHANGELOG.md +17 -0
- package/README.md +20 -2
- package/ios/Plugin/CapacitorSQLite.swift +37 -11
- package/ios/Plugin/CapacitorSQLitePlugin.swift +133 -72
- package/ios/Plugin/Database.swift +23 -11
- package/ios/Plugin/SqliteConfig.swift +10 -0
- package/ios/Plugin/Utils/UtilsEncryption.swift +10 -7
- package/ios/Plugin/Utils/UtilsFile.swift +195 -33
- package/ios/Plugin/Utils/UtilsMigrate.swift +11 -44
- package/ios/Plugin/Utils/UtilsNCDatabase.swift +2 -2
- package/ios/Plugin/Utils/UtilsSQLCipher.swift +29 -24
- package/ios/Plugin/Utils/UtilsSecret.swift +22 -8
- package/ios/Plugin/Utils/UtilsUpgrade.swift +6 -2
- package/package.json +1 -1
|
@@ -5,7 +5,7 @@ import Capacitor
|
|
|
5
5
|
// swiftlint:disable file_length
|
|
6
6
|
// swiftlint:disable type_body_length
|
|
7
7
|
public class CapacitorSQLitePlugin: CAPPlugin {
|
|
8
|
-
private
|
|
8
|
+
private var implementation: CapacitorSQLite?
|
|
9
9
|
private let modeList: [String] = ["no-encryption", "encryption", "secret", "newsecret", "wrongsecret"]
|
|
10
10
|
private let retHandler: ReturnHandler = ReturnHandler()
|
|
11
11
|
private var versionUpgrades: [String: [Int: [String: Any]]] = [:]
|
|
@@ -13,6 +13,7 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
13
13
|
var exportObserver: Any?
|
|
14
14
|
|
|
15
15
|
override public func load() {
|
|
16
|
+
self.implementation = CapacitorSQLite(config: sqliteConfig())
|
|
16
17
|
self.addObserversToNotificationCenter()
|
|
17
18
|
}
|
|
18
19
|
deinit {
|
|
@@ -24,16 +25,18 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
24
25
|
|
|
25
26
|
@objc func echo(_ call: CAPPluginCall) {
|
|
26
27
|
let value = call.getString("value") ?? ""
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
if let retValue: String = implementation?.echo(value) {
|
|
29
|
+
call.resolve([
|
|
30
|
+
"value": retValue
|
|
31
|
+
])
|
|
32
|
+
}
|
|
30
33
|
}
|
|
31
34
|
|
|
32
35
|
// MARK: - IsSecretStored
|
|
33
36
|
|
|
34
37
|
@objc func isSecretStored(_ call: CAPPluginCall) {
|
|
35
38
|
do {
|
|
36
|
-
let res = try implementation
|
|
39
|
+
let res = try implementation?.isSecretStored()
|
|
37
40
|
var bRes: Bool = false
|
|
38
41
|
if res == 1 {
|
|
39
42
|
bRes = true
|
|
@@ -64,7 +67,7 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
64
67
|
return
|
|
65
68
|
}
|
|
66
69
|
do {
|
|
67
|
-
try implementation
|
|
70
|
+
try implementation?.setEncryptionSecret(passphrase: passphrase)
|
|
68
71
|
retHandler.rResult(call: call)
|
|
69
72
|
return
|
|
70
73
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
@@ -96,7 +99,7 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
96
99
|
return
|
|
97
100
|
}
|
|
98
101
|
do {
|
|
99
|
-
try implementation
|
|
102
|
+
try implementation?.changeEncryptionSecret(passphrase: passphrase, oldPassphrase: oldPassphrase)
|
|
100
103
|
retHandler.rResult(call: call)
|
|
101
104
|
return
|
|
102
105
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
@@ -135,11 +138,11 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
135
138
|
upgDict = cUpgDict
|
|
136
139
|
}
|
|
137
140
|
do {
|
|
138
|
-
try implementation
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
141
|
+
try implementation?.createConnection(dbName,
|
|
142
|
+
encrypted: encrypted,
|
|
143
|
+
mode: inMode,
|
|
144
|
+
version: version,
|
|
145
|
+
vUpgDict: upgDict)
|
|
143
146
|
retHandler.rResult(call: call)
|
|
144
147
|
return
|
|
145
148
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
@@ -164,7 +167,7 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
164
167
|
return
|
|
165
168
|
}
|
|
166
169
|
do {
|
|
167
|
-
try implementation
|
|
170
|
+
try implementation?.open(dbName)
|
|
168
171
|
retHandler.rResult(call: call)
|
|
169
172
|
return
|
|
170
173
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
@@ -189,7 +192,7 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
189
192
|
return
|
|
190
193
|
}
|
|
191
194
|
do {
|
|
192
|
-
try implementation
|
|
195
|
+
try implementation?.close(dbName)
|
|
193
196
|
retHandler.rResult(call: call)
|
|
194
197
|
return
|
|
195
198
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
@@ -214,9 +217,15 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
214
217
|
return
|
|
215
218
|
}
|
|
216
219
|
do {
|
|
217
|
-
let version: NSNumber = try implementation
|
|
218
|
-
|
|
219
|
-
|
|
220
|
+
if let version: NSNumber = try implementation?
|
|
221
|
+
.getVersion(dbName) {
|
|
222
|
+
retHandler.rVersion(call: call, ret: version)
|
|
223
|
+
return
|
|
224
|
+
} else {
|
|
225
|
+
let msg = "GetVersion: Does not return a valid version"
|
|
226
|
+
retHandler.rVersion(call: call, message: msg)
|
|
227
|
+
return
|
|
228
|
+
}
|
|
220
229
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
221
230
|
let msg = "GetVersion: \(message)"
|
|
222
231
|
retHandler.rVersion(call: call, message: msg)
|
|
@@ -239,7 +248,7 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
239
248
|
return
|
|
240
249
|
}
|
|
241
250
|
do {
|
|
242
|
-
try implementation
|
|
251
|
+
try implementation?.closeConnection(dbName)
|
|
243
252
|
retHandler.rResult(call: call)
|
|
244
253
|
return
|
|
245
254
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
@@ -266,7 +275,7 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
266
275
|
return
|
|
267
276
|
}
|
|
268
277
|
do {
|
|
269
|
-
let res = try implementation
|
|
278
|
+
let res = try implementation?.checkConnectionsConsistency(dbNames)
|
|
270
279
|
var bRes: Bool = false
|
|
271
280
|
if res == 1 {
|
|
272
281
|
bRes = true
|
|
@@ -291,7 +300,7 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
291
300
|
return
|
|
292
301
|
}
|
|
293
302
|
do {
|
|
294
|
-
let res = try implementation
|
|
303
|
+
let res = try implementation?.isDatabase(dbName)
|
|
295
304
|
var bRes: Bool = false
|
|
296
305
|
if res == 1 {
|
|
297
306
|
bRes = true
|
|
@@ -326,7 +335,7 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
326
335
|
return
|
|
327
336
|
}
|
|
328
337
|
do {
|
|
329
|
-
let res = try implementation
|
|
338
|
+
let res = try implementation?.isTableExists(dbName, tableName: tableName)
|
|
330
339
|
var bRes: Bool = false
|
|
331
340
|
if res == 1 {
|
|
332
341
|
bRes = true
|
|
@@ -349,7 +358,7 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
349
358
|
@objc func getDatabaseList(_ call: CAPPluginCall) {
|
|
350
359
|
|
|
351
360
|
do {
|
|
352
|
-
let res = try implementation
|
|
361
|
+
let res = try implementation?.getDatabaseList() ?? []
|
|
353
362
|
retHandler.rValues(call: call, ret: res)
|
|
354
363
|
return
|
|
355
364
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
@@ -376,7 +385,8 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
376
385
|
}
|
|
377
386
|
|
|
378
387
|
do {
|
|
379
|
-
let res = try implementation
|
|
388
|
+
let res = try implementation?
|
|
389
|
+
.getMigratableDbList(dbFolder) ?? []
|
|
380
390
|
retHandler.rValues(call: call, ret: res)
|
|
381
391
|
return
|
|
382
392
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
@@ -406,7 +416,7 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
406
416
|
}
|
|
407
417
|
}
|
|
408
418
|
do {
|
|
409
|
-
try implementation
|
|
419
|
+
try implementation?.addSQLiteSuffix(folderPath, dbList: dbList)
|
|
410
420
|
retHandler.rResult(call: call)
|
|
411
421
|
return
|
|
412
422
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
@@ -435,7 +445,7 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
435
445
|
}
|
|
436
446
|
}
|
|
437
447
|
do {
|
|
438
|
-
try implementation
|
|
448
|
+
try implementation?.deleteOldDatabases(folderPath, dbList: dbList)
|
|
439
449
|
retHandler.rResult(call: call)
|
|
440
450
|
return
|
|
441
451
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
@@ -470,10 +480,17 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
470
480
|
return
|
|
471
481
|
}
|
|
472
482
|
do {
|
|
473
|
-
let res = try implementation
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
483
|
+
if let res = try implementation?
|
|
484
|
+
.execute(dbName, statements: statements,
|
|
485
|
+
transaction: transaction) {
|
|
486
|
+
retHandler.rChanges(call: call, ret: res)
|
|
487
|
+
return
|
|
488
|
+
} else {
|
|
489
|
+
retHandler.rChanges(
|
|
490
|
+
call: call, ret: ["changes": -1],
|
|
491
|
+
message: "Execute: Does not return a valid execute")
|
|
492
|
+
return
|
|
493
|
+
}
|
|
477
494
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
478
495
|
retHandler.rChanges(
|
|
479
496
|
call: call, ret: ["changes": -1],
|
|
@@ -526,10 +543,17 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
526
543
|
}
|
|
527
544
|
let transaction: Bool = call.getBool("transaction") ?? true
|
|
528
545
|
do {
|
|
529
|
-
let res = try implementation
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
546
|
+
if let res = try implementation?.executeSet(dbName, set: set,
|
|
547
|
+
transaction: transaction) {
|
|
548
|
+
retHandler.rChanges(call: call, ret: res)
|
|
549
|
+
return
|
|
550
|
+
} else {
|
|
551
|
+
retHandler.rChanges(
|
|
552
|
+
call: call, ret: ["changes": -1],
|
|
553
|
+
message: "ExecuteSet: Does not return a valid executeSet")
|
|
554
|
+
return
|
|
555
|
+
|
|
556
|
+
}
|
|
533
557
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
534
558
|
retHandler.rChanges(
|
|
535
559
|
call: call, ret: ["changes": -1],
|
|
@@ -547,6 +571,7 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
547
571
|
|
|
548
572
|
// MARK: - Run
|
|
549
573
|
|
|
574
|
+
// swiftlint:disable function_body_length
|
|
550
575
|
@objc func run(_ call: CAPPluginCall) {
|
|
551
576
|
guard let dbName = call.options["database"] as? String else {
|
|
552
577
|
retHandler.rChanges(
|
|
@@ -572,13 +597,19 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
572
597
|
}
|
|
573
598
|
let transaction: Bool = call.getBool("transaction") ?? true
|
|
574
599
|
do {
|
|
575
|
-
let res = try
|
|
576
|
-
implementation
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
600
|
+
if let res = try
|
|
601
|
+
implementation?.run(dbName,
|
|
602
|
+
statement: statement,
|
|
603
|
+
values: values,
|
|
604
|
+
transaction: transaction) {
|
|
605
|
+
retHandler.rChanges(call: call, ret: res)
|
|
606
|
+
return
|
|
607
|
+
} else {
|
|
608
|
+
retHandler.rChanges(
|
|
609
|
+
call: call, ret: ["changes": -1],
|
|
610
|
+
message: "Run: Does not return a valid run")
|
|
611
|
+
return
|
|
612
|
+
}
|
|
582
613
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
583
614
|
retHandler.rChanges(
|
|
584
615
|
call: call, ret: ["changes": -1],
|
|
@@ -592,9 +623,11 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
592
623
|
}
|
|
593
624
|
|
|
594
625
|
}
|
|
626
|
+
// swiftlint:enable function_body_length
|
|
595
627
|
|
|
596
628
|
// MARK: - Query
|
|
597
629
|
|
|
630
|
+
// swiftlint:disable function_body_length
|
|
598
631
|
@objc func query(_ call: CAPPluginCall) {
|
|
599
632
|
guard let dbName = call.options["database"]
|
|
600
633
|
as? String else {
|
|
@@ -622,12 +655,19 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
622
655
|
|
|
623
656
|
}
|
|
624
657
|
do {
|
|
625
|
-
let res = try
|
|
626
|
-
implementation
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
658
|
+
if let res = try
|
|
659
|
+
implementation?.query(dbName,
|
|
660
|
+
statement: statement,
|
|
661
|
+
values: values) {
|
|
662
|
+
retHandler.rValues(call: call, ret: res)
|
|
663
|
+
return
|
|
664
|
+
} else {
|
|
665
|
+
retHandler.rValues(
|
|
666
|
+
call: call, ret: [],
|
|
667
|
+
message: "Query: Does not return a valid query")
|
|
668
|
+
return
|
|
669
|
+
|
|
670
|
+
}
|
|
631
671
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
632
672
|
retHandler.rValues(
|
|
633
673
|
call: call, ret: [],
|
|
@@ -641,6 +681,7 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
641
681
|
}
|
|
642
682
|
|
|
643
683
|
}
|
|
684
|
+
// swiftlint:enable function_body_length
|
|
644
685
|
|
|
645
686
|
// MARK: - isDBExists
|
|
646
687
|
|
|
@@ -653,7 +694,7 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
653
694
|
return
|
|
654
695
|
}
|
|
655
696
|
do {
|
|
656
|
-
let res = try implementation
|
|
697
|
+
let res = try implementation?.isDBExists(dbName)
|
|
657
698
|
var bRes: Bool = false
|
|
658
699
|
if res == 1 {
|
|
659
700
|
bRes = true
|
|
@@ -683,7 +724,7 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
683
724
|
return
|
|
684
725
|
}
|
|
685
726
|
do {
|
|
686
|
-
let res = try implementation
|
|
727
|
+
let res = try implementation?.isDBOpen(dbName)
|
|
687
728
|
var bRes: Bool = false
|
|
688
729
|
if res == 1 {
|
|
689
730
|
bRes = true
|
|
@@ -712,7 +753,7 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
712
753
|
return
|
|
713
754
|
}
|
|
714
755
|
do {
|
|
715
|
-
try implementation
|
|
756
|
+
try implementation?.deleteDatabase(dbName)
|
|
716
757
|
retHandler.rResult(call: call)
|
|
717
758
|
return
|
|
718
759
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
@@ -737,7 +778,7 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
737
778
|
return
|
|
738
779
|
}
|
|
739
780
|
do {
|
|
740
|
-
try implementation
|
|
781
|
+
try implementation?.isJsonValid(parsingData)
|
|
741
782
|
retHandler.rResult(call: call, ret: true)
|
|
742
783
|
return
|
|
743
784
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
@@ -762,7 +803,8 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
762
803
|
return
|
|
763
804
|
}
|
|
764
805
|
do {
|
|
765
|
-
let res: [String: Int] = try implementation
|
|
806
|
+
let res: [String: Int] = try implementation?
|
|
807
|
+
.importFromJson(parsingData) ?? ["changes": -1]
|
|
766
808
|
retHandler.rChanges(call: call, ret: res)
|
|
767
809
|
return
|
|
768
810
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
@@ -808,8 +850,8 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
808
850
|
}
|
|
809
851
|
|
|
810
852
|
do {
|
|
811
|
-
let res: [String: Any] = try implementation
|
|
812
|
-
.exportToJson(dbName, expMode: expMode)
|
|
853
|
+
let res: [String: Any] = try implementation?
|
|
854
|
+
.exportToJson(dbName, expMode: expMode) ?? [:]
|
|
813
855
|
retHandler.rJsonSQLite(call: call, ret: res)
|
|
814
856
|
return
|
|
815
857
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
@@ -837,8 +879,8 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
837
879
|
return
|
|
838
880
|
}
|
|
839
881
|
do {
|
|
840
|
-
let res: NSNumber = try implementation
|
|
841
|
-
.createSyncTable(dbName)
|
|
882
|
+
let res: NSNumber = try implementation?
|
|
883
|
+
.createSyncTable(dbName) ?? -1
|
|
842
884
|
retHandler.rChanges(call: call,
|
|
843
885
|
ret: ["changes": Int(truncating: res)])
|
|
844
886
|
return
|
|
@@ -872,7 +914,7 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
872
914
|
return
|
|
873
915
|
}
|
|
874
916
|
do {
|
|
875
|
-
try implementation
|
|
917
|
+
try implementation?.setSyncDate( dbName, syncDate: syncDate)
|
|
876
918
|
retHandler.rResult(call: call)
|
|
877
919
|
return
|
|
878
920
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
@@ -896,7 +938,7 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
896
938
|
return
|
|
897
939
|
}
|
|
898
940
|
do {
|
|
899
|
-
let res: NSNumber = try implementation
|
|
941
|
+
let res: NSNumber = try implementation?.getSyncDate( dbName) ?? 0
|
|
900
942
|
retHandler.rSyncDate(call: call,
|
|
901
943
|
ret: Int64(truncating: res))
|
|
902
944
|
return
|
|
@@ -928,12 +970,17 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
928
970
|
return
|
|
929
971
|
}
|
|
930
972
|
do {
|
|
931
|
-
let upgVersionDict: [Int: [String: Any]] = try
|
|
932
|
-
implementation
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
973
|
+
if let upgVersionDict: [Int: [String: Any]] = try
|
|
974
|
+
implementation?.addUpgradeStatement(dbName,
|
|
975
|
+
upgrade: upgrade) {
|
|
976
|
+
versionUpgrades = ["\(dbName)": upgVersionDict]
|
|
977
|
+
retHandler.rResult(call: call)
|
|
978
|
+
return
|
|
979
|
+
} else {
|
|
980
|
+
let msg = "addUpgradeStatement: Error in returned upgVersionDict"
|
|
981
|
+
retHandler.rResult(call: call, message: msg)
|
|
982
|
+
return
|
|
983
|
+
}
|
|
937
984
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
938
985
|
let msg = "addUpgradeStatement: \(message)"
|
|
939
986
|
retHandler.rResult(call: call, message: msg)
|
|
@@ -952,7 +999,7 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
952
999
|
let overwrite: Bool = call.getBool("overwrite") ?? true
|
|
953
1000
|
|
|
954
1001
|
do {
|
|
955
|
-
try implementation
|
|
1002
|
+
try implementation?.copyFromAssets(overwrite: overwrite)
|
|
956
1003
|
retHandler.rResult(call: call)
|
|
957
1004
|
return
|
|
958
1005
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
@@ -981,10 +1028,16 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
981
1028
|
}
|
|
982
1029
|
do {
|
|
983
1030
|
|
|
984
|
-
let path: String = try implementation
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
1031
|
+
if let path: String = try implementation?
|
|
1032
|
+
.getNCDatabasePath(folderPath, dbName: dbName) {
|
|
1033
|
+
retHandler.rPath(call: call, ret: path)
|
|
1034
|
+
return
|
|
1035
|
+
} else {
|
|
1036
|
+
let msg = "getNCDatabasePath: Does not return a NC path"
|
|
1037
|
+
retHandler.rPath(call: call, ret: "", message: msg)
|
|
1038
|
+
return
|
|
1039
|
+
|
|
1040
|
+
}
|
|
988
1041
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
989
1042
|
let msg = "getNCDatabasePath: \(message)"
|
|
990
1043
|
retHandler.rPath(call: call, ret: "", message: msg)
|
|
@@ -1007,8 +1060,8 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
1007
1060
|
}
|
|
1008
1061
|
let version: Int = call.getInt("version") ?? 1
|
|
1009
1062
|
do {
|
|
1010
|
-
try implementation
|
|
1011
|
-
|
|
1063
|
+
try implementation?.createNCConnection(dbPath,
|
|
1064
|
+
version: version)
|
|
1012
1065
|
retHandler.rResult(call: call)
|
|
1013
1066
|
return
|
|
1014
1067
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
@@ -1033,7 +1086,7 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
1033
1086
|
return
|
|
1034
1087
|
}
|
|
1035
1088
|
do {
|
|
1036
|
-
try implementation
|
|
1089
|
+
try implementation?.closeNCConnection(dbPath)
|
|
1037
1090
|
retHandler.rResult(call: call)
|
|
1038
1091
|
return
|
|
1039
1092
|
} catch CapacitorSQLiteError.failed(let message) {
|
|
@@ -1058,7 +1111,7 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
1058
1111
|
return
|
|
1059
1112
|
}
|
|
1060
1113
|
do {
|
|
1061
|
-
let res = try implementation
|
|
1114
|
+
let res = try implementation?.isNCDatabase(dbPath)
|
|
1062
1115
|
var bRes: Bool = false
|
|
1063
1116
|
if res == 1 {
|
|
1064
1117
|
bRes = true
|
|
@@ -1103,6 +1156,14 @@ public class CapacitorSQLitePlugin: CAPPlugin {
|
|
|
1103
1156
|
return
|
|
1104
1157
|
}
|
|
1105
1158
|
}
|
|
1159
|
+
private func sqliteConfig() -> SqliteConfig {
|
|
1160
|
+
var config = SqliteConfig()
|
|
1161
|
+
|
|
1162
|
+
if let iosDatabaseLocation = getConfigValue("iosDatabaseLocation") as? String {
|
|
1163
|
+
config.iosDatabaseLocation = iosDatabaseLocation
|
|
1164
|
+
}
|
|
1165
|
+
return config
|
|
1166
|
+
}
|
|
1106
1167
|
|
|
1107
1168
|
}
|
|
1108
1169
|
// swiftlint:enable type_body_length
|
|
@@ -33,6 +33,7 @@ class Database {
|
|
|
33
33
|
var encrypted: Bool
|
|
34
34
|
var mode: String
|
|
35
35
|
var vUpgDict: [Int: [String: Any]]
|
|
36
|
+
var databaseLocation: String
|
|
36
37
|
var path: String = ""
|
|
37
38
|
var mDb: OpaquePointer?
|
|
38
39
|
let globalData: GlobalSQLite = GlobalSQLite()
|
|
@@ -41,15 +42,15 @@ class Database {
|
|
|
41
42
|
var ncDB: Bool = false
|
|
42
43
|
|
|
43
44
|
// MARK: - Init
|
|
44
|
-
init(databaseName: String, encrypted: Bool,
|
|
45
|
+
init(databaseLocation: String, databaseName: String, encrypted: Bool,
|
|
45
46
|
mode: String, version: Int,
|
|
46
47
|
vUpgDict: [Int: [String: Any]] = [:]) throws {
|
|
47
|
-
print("databaseName: \(databaseName) ")
|
|
48
48
|
self.dbVersion = version
|
|
49
49
|
self.encrypted = encrypted
|
|
50
50
|
self.dbName = databaseName
|
|
51
51
|
self.mode = mode
|
|
52
52
|
self.vUpgDict = vUpgDict
|
|
53
|
+
self.databaseLocation = databaseLocation
|
|
53
54
|
if databaseName.contains("/") &&
|
|
54
55
|
databaseName.suffix(9) != "SQLite.db" {
|
|
55
56
|
self.readOnly = true
|
|
@@ -58,6 +59,7 @@ class Database {
|
|
|
58
59
|
} else {
|
|
59
60
|
do {
|
|
60
61
|
self.path = try UtilsFile.getFilePath(
|
|
62
|
+
databaseLocation: databaseLocation,
|
|
61
63
|
fileName: databaseName)
|
|
62
64
|
} catch UtilsFileError.getFilePathFailed {
|
|
63
65
|
throw DatabaseError.filePath(
|
|
@@ -91,7 +93,8 @@ class Database {
|
|
|
91
93
|
if mode == "encryption" {
|
|
92
94
|
do {
|
|
93
95
|
let ret: Bool = try UtilsEncryption
|
|
94
|
-
.encryptDatabase(
|
|
96
|
+
.encryptDatabase(databaseLocation: databaseLocation,
|
|
97
|
+
filePath: path, password: password)
|
|
95
98
|
if !ret {
|
|
96
99
|
let msg: String = "Failed in encryption"
|
|
97
100
|
throw DatabaseError.open(message: msg)
|
|
@@ -122,11 +125,15 @@ class Database {
|
|
|
122
125
|
}
|
|
123
126
|
if dbVersion > curVersion {
|
|
124
127
|
if vUpgDict.count > 0 {
|
|
125
|
-
_ = try uUpg
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
128
|
+
_ = try uUpg
|
|
129
|
+
.onUpgrade(mDB: self, upgDict: vUpgDict,
|
|
130
|
+
dbName: dbName,
|
|
131
|
+
currentVersion: curVersion,
|
|
132
|
+
targetVersion: dbVersion,
|
|
133
|
+
databaseLocation: databaseLocation)
|
|
134
|
+
try UtilsSQLCipher
|
|
135
|
+
.deleteBackupDB(databaseLocation: databaseLocation,
|
|
136
|
+
databaseName: dbName)
|
|
130
137
|
} else {
|
|
131
138
|
try UtilsSQLCipher.setVersion(mDB: self, version: dbVersion)
|
|
132
139
|
}
|
|
@@ -157,7 +164,9 @@ class Database {
|
|
|
157
164
|
} catch UtilsUpgradeError.onUpgradeFailed(let message) {
|
|
158
165
|
//restore the database
|
|
159
166
|
do {
|
|
160
|
-
try UtilsSQLCipher
|
|
167
|
+
try UtilsSQLCipher
|
|
168
|
+
.restoreDB(databaseLocation: databaseLocation,
|
|
169
|
+
databaseName: dbName)
|
|
161
170
|
let msg: String = "Failed OnUpgrade \(message)"
|
|
162
171
|
try close()
|
|
163
172
|
throw DatabaseError.open(message: msg)
|
|
@@ -227,6 +236,7 @@ class Database {
|
|
|
227
236
|
do {
|
|
228
237
|
try UtilsSQLCipher.execute(mDB: self, sql: sql)
|
|
229
238
|
changes = UtilsSQLCipher.dbChanges(mDB: mDb) - initChanges
|
|
239
|
+
|
|
230
240
|
} catch UtilsSQLCipherError.execute(let message) {
|
|
231
241
|
if transaction {
|
|
232
242
|
do {
|
|
@@ -378,7 +388,8 @@ class Database {
|
|
|
378
388
|
|
|
379
389
|
func deleteDB(databaseName: String) throws -> Bool {
|
|
380
390
|
let isFileExists: Bool = UtilsFile
|
|
381
|
-
.isFileExist(
|
|
391
|
+
.isFileExist(databaseLocation: databaseLocation,
|
|
392
|
+
fileName: databaseName)
|
|
382
393
|
if isFileExists && !isOpen {
|
|
383
394
|
// open the database
|
|
384
395
|
do {
|
|
@@ -396,7 +407,8 @@ class Database {
|
|
|
396
407
|
// delete the database
|
|
397
408
|
if isFileExists {
|
|
398
409
|
do {
|
|
399
|
-
try UtilsSQLCipher.deleteDB(
|
|
410
|
+
try UtilsSQLCipher.deleteDB(databaseLocation: databaseLocation,
|
|
411
|
+
databaseName: databaseName)
|
|
400
412
|
} catch UtilsSQLCipherError.deleteDB(let message ) {
|
|
401
413
|
throw DatabaseError.deleteDB(message: message)
|
|
402
414
|
}
|
|
@@ -17,17 +17,19 @@ class UtilsEncryption {
|
|
|
17
17
|
// MARK: - EncryptDatabase
|
|
18
18
|
|
|
19
19
|
// swiftlint:disable function_body_length
|
|
20
|
-
class func encryptDatabase(
|
|
21
|
-
|
|
20
|
+
class func encryptDatabase(databaseLocation: String, filePath: String,
|
|
21
|
+
password: String) throws -> Bool {
|
|
22
22
|
var ret: Bool = false
|
|
23
23
|
var oDB: OpaquePointer?
|
|
24
24
|
do {
|
|
25
25
|
if UtilsFile.isFileExist(filePath: filePath) {
|
|
26
26
|
do {
|
|
27
|
-
let tempPath: String = try UtilsFile
|
|
28
|
-
|
|
27
|
+
let tempPath: String = try UtilsFile
|
|
28
|
+
.getFilePath(databaseLocation: databaseLocation,
|
|
29
|
+
fileName: "temp.db")
|
|
29
30
|
try UtilsFile.renameFile(filePath: filePath,
|
|
30
|
-
toFilePath: tempPath
|
|
31
|
+
toFilePath: tempPath,
|
|
32
|
+
databaseLocation: databaseLocation)
|
|
31
33
|
oDB = try UtilsSQLCipher
|
|
32
34
|
.openOrCreateDatabase(filename: tempPath,
|
|
33
35
|
password: "",
|
|
@@ -43,8 +45,9 @@ class UtilsEncryption {
|
|
|
43
45
|
stmt.append("DETACH DATABASE encrypted;")
|
|
44
46
|
if sqlite3_exec(oDB, stmt, nil, nil, nil) ==
|
|
45
47
|
SQLITE_OK {
|
|
46
|
-
try _ = UtilsFile
|
|
47
|
-
fileName: "temp.db"
|
|
48
|
+
try _ = UtilsFile
|
|
49
|
+
.deleteFile(fileName: "temp.db",
|
|
50
|
+
databaseLocation: databaseLocation)
|
|
48
51
|
ret = true
|
|
49
52
|
}
|
|
50
53
|
// close the db
|