@capacitor-community/sqlite 3.3.1 → 3.3.3-3
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 +49 -0
- package/README.md +27 -2
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLite.java +83 -7
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLitePlugin.java +101 -3
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/RetHandler.java +24 -0
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java +67 -47
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/JsonIndex.java +5 -4
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsFile.java +9 -0
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsMigrate.java +34 -15
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsNCDatabase.java +26 -0
- package/dist/esm/definitions.d.ts +111 -2
- package/dist/esm/definitions.js +63 -0
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/web.d.ts +5 -1
- package/dist/esm/web.js +16 -0
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +79 -0
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +79 -0
- package/dist/plugin.js.map +1 -1
- package/electron/dist/plugin.js +17 -1
- package/electron/dist/plugin.js.map +1 -1
- package/ios/Plugin/CapacitorSQLite.swift +122 -19
- package/ios/Plugin/CapacitorSQLitePlugin.m +4 -0
- package/ios/Plugin/CapacitorSQLitePlugin.swift +236 -64
- package/ios/Plugin/Database.swift +57 -28
- package/ios/Plugin/ImportExportJson/ImportFromJson.swift +2 -1
- package/ios/Plugin/ImportExportJson/JsonSQLite.swift +1 -1
- package/ios/Plugin/ReturnHandler.swift +13 -0
- package/ios/Plugin/SqliteConfig.swift +10 -0
- package/ios/Plugin/Utils/UtilsEncryption.swift +10 -7
- package/ios/Plugin/Utils/UtilsFile.swift +207 -38
- package/ios/Plugin/Utils/UtilsMigrate.swift +70 -57
- package/ios/Plugin/Utils/UtilsNCDatabase.swift +31 -0
- 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 +5 -5
|
@@ -5,7 +5,19 @@ enum CapacitorSQLiteError: Error {
|
|
|
5
5
|
// swiftlint:disable file_length
|
|
6
6
|
// swiftlint:disable type_body_length
|
|
7
7
|
@objc public class CapacitorSQLite: NSObject {
|
|
8
|
+
private var config: SqliteConfig
|
|
8
9
|
private var dbDict: [String: Database] = [:]
|
|
10
|
+
private var databaseLocation: String
|
|
11
|
+
|
|
12
|
+
init(config: SqliteConfig) {
|
|
13
|
+
self.config = config
|
|
14
|
+
if let isLocation = config.iosDatabaseLocation {
|
|
15
|
+
self.databaseLocation = isLocation
|
|
16
|
+
} else {
|
|
17
|
+
self.databaseLocation = "Documents"
|
|
18
|
+
}
|
|
19
|
+
super.init()
|
|
20
|
+
}
|
|
9
21
|
|
|
10
22
|
// MARK: - Echo
|
|
11
23
|
|
|
@@ -31,7 +43,9 @@ enum CapacitorSQLiteError: Error {
|
|
|
31
43
|
// close all connections
|
|
32
44
|
try closeAllConnections()
|
|
33
45
|
// set encryption secret
|
|
34
|
-
try UtilsSecret
|
|
46
|
+
try UtilsSecret
|
|
47
|
+
.setEncryptionSecret(passphrase: passphrase,
|
|
48
|
+
databaseLocation: databaseLocation)
|
|
35
49
|
return
|
|
36
50
|
} catch UtilsSecretError.setEncryptionSecret(let message) {
|
|
37
51
|
throw CapacitorSQLiteError.failed(message: message)
|
|
@@ -48,7 +62,10 @@ enum CapacitorSQLiteError: Error {
|
|
|
48
62
|
// close all connections
|
|
49
63
|
try closeAllConnections()
|
|
50
64
|
// set encryption secret
|
|
51
|
-
try UtilsSecret
|
|
65
|
+
try UtilsSecret
|
|
66
|
+
.changeEncryptionSecret(passphrase: passphrase,
|
|
67
|
+
oldPassphrase: oldPassphrase,
|
|
68
|
+
databaseLocation: databaseLocation)
|
|
52
69
|
return
|
|
53
70
|
} catch UtilsSecretError.changeEncryptionSecret(let message) {
|
|
54
71
|
throw CapacitorSQLiteError.failed(message: message)
|
|
@@ -57,6 +74,68 @@ enum CapacitorSQLiteError: Error {
|
|
|
57
74
|
}
|
|
58
75
|
|
|
59
76
|
}
|
|
77
|
+
// MARK: - getNCDatabasePath
|
|
78
|
+
|
|
79
|
+
@objc public func getNCDatabasePath(_ folderPath: String, dbName: String ) throws -> String {
|
|
80
|
+
do {
|
|
81
|
+
let databasePath: String = try UtilsNCDatabase
|
|
82
|
+
.getNCDatabasePath(folderPath: folderPath,
|
|
83
|
+
database: dbName )
|
|
84
|
+
return databasePath
|
|
85
|
+
} catch let error {
|
|
86
|
+
throw CapacitorSQLiteError.failed(message: "\(error)")
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// MARK: - CreateNCConnection
|
|
92
|
+
|
|
93
|
+
@objc public func createNCConnection(_ databasePath: String,
|
|
94
|
+
version: Int) throws {
|
|
95
|
+
|
|
96
|
+
// check if the connection already exists
|
|
97
|
+
let conn = dbDict[databasePath]
|
|
98
|
+
if conn != nil {
|
|
99
|
+
let msg = "Connection \(databasePath) already exists"
|
|
100
|
+
throw CapacitorSQLiteError.failed(message: msg)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
do {
|
|
104
|
+
let isFileExists: Bool = UtilsFile
|
|
105
|
+
.isFileExist(filePath: databasePath)
|
|
106
|
+
|
|
107
|
+
if !isFileExists {
|
|
108
|
+
throw CapacitorSQLiteError.failed(message: "database \(databasePath) does not exist")
|
|
109
|
+
}
|
|
110
|
+
let mDb: Database = try Database(
|
|
111
|
+
databaseLocation: databaseLocation,
|
|
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)")
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// MARK: - CloseNCConnection
|
|
123
|
+
|
|
124
|
+
@objc public func closeNCConnection(_ dbName: String) throws {
|
|
125
|
+
guard let mDb: Database = dbDict[dbName] else {
|
|
126
|
+
let msg = "Connection to \(dbName) not available"
|
|
127
|
+
throw CapacitorSQLiteError.failed(message: msg)
|
|
128
|
+
}
|
|
129
|
+
if mDb.isDBOpen() {
|
|
130
|
+
do {
|
|
131
|
+
try mDb.close()
|
|
132
|
+
} catch DatabaseError.close(let message) {
|
|
133
|
+
throw CapacitorSQLiteError.failed(message: message)
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
dbDict.removeValue(forKey: dbName)
|
|
137
|
+
return
|
|
138
|
+
}
|
|
60
139
|
|
|
61
140
|
// MARK: - CreateConnection
|
|
62
141
|
|
|
@@ -75,6 +154,7 @@ enum CapacitorSQLiteError: Error {
|
|
|
75
154
|
|
|
76
155
|
do {
|
|
77
156
|
let mDb: Database = try Database(
|
|
157
|
+
databaseLocation: databaseLocation,
|
|
78
158
|
databaseName: "\(mDbName)SQLite.db",
|
|
79
159
|
encrypted: encrypted, mode: mode, version: version,
|
|
80
160
|
vUpgDict: vUpgDict)
|
|
@@ -133,6 +213,7 @@ enum CapacitorSQLiteError: Error {
|
|
|
133
213
|
throw CapacitorSQLiteError.failed(message: message)
|
|
134
214
|
}
|
|
135
215
|
}
|
|
216
|
+
|
|
136
217
|
// MARK: - Close Connection
|
|
137
218
|
|
|
138
219
|
@objc public func closeConnection(_ dbName: String) throws {
|
|
@@ -193,12 +274,26 @@ enum CapacitorSQLiteError: Error {
|
|
|
193
274
|
throw CapacitorSQLiteError.failed(message: "\(error)")
|
|
194
275
|
}
|
|
195
276
|
}
|
|
277
|
+
|
|
196
278
|
// MARK: - IsDatabase
|
|
197
279
|
|
|
198
280
|
@objc public func isDatabase(_ dbName: String) throws -> NSNumber {
|
|
199
281
|
let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
|
|
200
282
|
let isFileExists: Bool = UtilsFile
|
|
201
|
-
.isFileExist(
|
|
283
|
+
.isFileExist(databaseLocation: databaseLocation,
|
|
284
|
+
fileName: mDbName + "SQLite.db")
|
|
285
|
+
if isFileExists {
|
|
286
|
+
return 1
|
|
287
|
+
} else {
|
|
288
|
+
return 0
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// MARK: - IsNCDatabase
|
|
293
|
+
|
|
294
|
+
@objc public func isNCDatabase(_ databasePath: String) throws -> NSNumber {
|
|
295
|
+
let isFileExists: Bool = UtilsFile
|
|
296
|
+
.isFileExist(filePath: databasePath)
|
|
202
297
|
if isFileExists {
|
|
203
298
|
return 1
|
|
204
299
|
} else {
|
|
@@ -244,7 +339,7 @@ enum CapacitorSQLiteError: Error {
|
|
|
244
339
|
let msg = "Connection to \(mDbName) not available"
|
|
245
340
|
throw CapacitorSQLiteError.failed(message: msg)
|
|
246
341
|
}
|
|
247
|
-
if mDb.isDBOpen() {
|
|
342
|
+
if !mDb.isNCDB() && mDb.isDBOpen() {
|
|
248
343
|
do {
|
|
249
344
|
var stmts = statements
|
|
250
345
|
// remove carriage returns
|
|
@@ -268,7 +363,7 @@ enum CapacitorSQLiteError: Error {
|
|
|
268
363
|
throw CapacitorSQLiteError.failed(message: msg)
|
|
269
364
|
}
|
|
270
365
|
} else {
|
|
271
|
-
let msg = "Database \(mDbName) not opened"
|
|
366
|
+
let msg = "Database \(mDbName) not opened or in read-only"
|
|
272
367
|
throw CapacitorSQLiteError.failed(message: msg)
|
|
273
368
|
}
|
|
274
369
|
}
|
|
@@ -283,7 +378,7 @@ enum CapacitorSQLiteError: Error {
|
|
|
283
378
|
let msg = "Connection to \(mDbName) not available"
|
|
284
379
|
throw CapacitorSQLiteError.failed(message: msg)
|
|
285
380
|
}
|
|
286
|
-
if mDb.isDBOpen() {
|
|
381
|
+
if !mDb.isNCDB() && mDb.isDBOpen() {
|
|
287
382
|
do {
|
|
288
383
|
let res = try mDb.execSet(set: set, transaction: transaction)
|
|
289
384
|
return res
|
|
@@ -294,7 +389,7 @@ enum CapacitorSQLiteError: Error {
|
|
|
294
389
|
throw CapacitorSQLiteError.failed(message: msg)
|
|
295
390
|
}
|
|
296
391
|
} else {
|
|
297
|
-
let msg = "Database \(mDbName) not opened"
|
|
392
|
+
let msg = "Database \(mDbName) not opened or in read-only"
|
|
298
393
|
throw CapacitorSQLiteError.failed(message: msg)
|
|
299
394
|
}
|
|
300
395
|
}
|
|
@@ -310,7 +405,7 @@ enum CapacitorSQLiteError: Error {
|
|
|
310
405
|
let msg = "Connection to \(mDbName) not available"
|
|
311
406
|
throw CapacitorSQLiteError.failed(message: msg)
|
|
312
407
|
}
|
|
313
|
-
if mDb.isDBOpen() {
|
|
408
|
+
if !mDb.isNCDB() && mDb.isDBOpen() {
|
|
314
409
|
do {
|
|
315
410
|
var val: [Any] = []
|
|
316
411
|
if values.count > 0 {
|
|
@@ -343,7 +438,7 @@ enum CapacitorSQLiteError: Error {
|
|
|
343
438
|
throw CapacitorSQLiteError.failed(message: msg)
|
|
344
439
|
}
|
|
345
440
|
} else {
|
|
346
|
-
let msg = "Database \(mDbName) not opened"
|
|
441
|
+
let msg = "Database \(mDbName) not opened or in read-only"
|
|
347
442
|
throw CapacitorSQLiteError.failed(message: msg)
|
|
348
443
|
}
|
|
349
444
|
}
|
|
@@ -384,7 +479,8 @@ enum CapacitorSQLiteError: Error {
|
|
|
384
479
|
throw CapacitorSQLiteError.failed(message: msg)
|
|
385
480
|
}
|
|
386
481
|
let res: Bool = UtilsFile
|
|
387
|
-
.isFileExist(
|
|
482
|
+
.isFileExist(databaseLocation: databaseLocation,
|
|
483
|
+
fileName: "\(mDbName)SQLite.db")
|
|
388
484
|
if res {
|
|
389
485
|
return 1
|
|
390
486
|
} else {
|
|
@@ -421,8 +517,7 @@ enum CapacitorSQLiteError: Error {
|
|
|
421
517
|
if !mDb.isDBOpen() {
|
|
422
518
|
try mDb.open()
|
|
423
519
|
}
|
|
424
|
-
let res: Bool = try mDb.deleteDB(
|
|
425
|
-
databaseName: "\(mDbName)SQLite.db")
|
|
520
|
+
let res: Bool = try mDb.deleteDB(databaseName: "\(mDbName)SQLite.db")
|
|
426
521
|
if res {
|
|
427
522
|
return
|
|
428
523
|
} else {
|
|
@@ -483,6 +578,7 @@ enum CapacitorSQLiteError: Error {
|
|
|
483
578
|
// open the database
|
|
484
579
|
do {
|
|
485
580
|
mDb = try Database(
|
|
581
|
+
databaseLocation: databaseLocation,
|
|
486
582
|
databaseName: dbName, encrypted: encrypted,
|
|
487
583
|
mode: inMode, version: version, vUpgDict: [:])
|
|
488
584
|
try mDb.open()
|
|
@@ -685,7 +781,7 @@ enum CapacitorSQLiteError: Error {
|
|
|
685
781
|
let aPath: String = assetsDbPath.path
|
|
686
782
|
let bRes: Bool = UtilsFile.isDirExist(dirPath: aPath)
|
|
687
783
|
if bRes {
|
|
688
|
-
// get the database files
|
|
784
|
+
// get the database files from assets
|
|
689
785
|
let dbList: [String] = try UtilsFile
|
|
690
786
|
.getFileList(path: aPath, ext: ".db")
|
|
691
787
|
// loop through the database files
|
|
@@ -697,7 +793,8 @@ enum CapacitorSQLiteError: Error {
|
|
|
697
793
|
// for each copy the file to the Application
|
|
698
794
|
// database folder
|
|
699
795
|
_ = try UtilsFile
|
|
700
|
-
.copyFromAssetToDatabase(
|
|
796
|
+
.copyFromAssetToDatabase(databaseLocation: databaseLocation,
|
|
797
|
+
fromDb: mDb,
|
|
701
798
|
toDb: toDb, overwrite: overwrite)
|
|
702
799
|
}
|
|
703
800
|
// get the zip files
|
|
@@ -708,7 +805,8 @@ enum CapacitorSQLiteError: Error {
|
|
|
708
805
|
// for each zip uncompress the file to the Application
|
|
709
806
|
// database folder
|
|
710
807
|
_ = try UtilsFile
|
|
711
|
-
.unzipFromAssetToDatabase(
|
|
808
|
+
.unzipFromAssetToDatabase(databaseLocation: databaseLocation,
|
|
809
|
+
zip: zip, overwrite: overwrite)
|
|
712
810
|
}
|
|
713
811
|
return
|
|
714
812
|
} else {
|
|
@@ -765,7 +863,9 @@ enum CapacitorSQLiteError: Error {
|
|
|
765
863
|
@objc func addSQLiteSuffix(_ folderPath: String, dbList: [String]) throws {
|
|
766
864
|
|
|
767
865
|
do {
|
|
768
|
-
try UtilsMigrate.addSQLiteSuffix(
|
|
866
|
+
try UtilsMigrate.addSQLiteSuffix(databaseLocation: databaseLocation,
|
|
867
|
+
folderPath: folderPath,
|
|
868
|
+
dbList: dbList)
|
|
769
869
|
return
|
|
770
870
|
} catch UtilsMigrateError.addSQLiteSuffix(let message) {
|
|
771
871
|
var msg: String = "addSQLiteSuffix:"
|
|
@@ -783,7 +883,8 @@ enum CapacitorSQLiteError: Error {
|
|
|
783
883
|
|
|
784
884
|
@objc func deleteOldDatabases(_ folderPath: String, dbList: [String]) throws {
|
|
785
885
|
do {
|
|
786
|
-
try UtilsMigrate
|
|
886
|
+
try UtilsMigrate
|
|
887
|
+
.deleteOldDatabases(folderPath: folderPath, dbList: dbList)
|
|
787
888
|
return
|
|
788
889
|
} catch UtilsMigrateError.deleteOldDatabases(let message) {
|
|
789
890
|
var msg: String = "deleteOldDatabases:"
|
|
@@ -799,8 +900,10 @@ enum CapacitorSQLiteError: Error {
|
|
|
799
900
|
}
|
|
800
901
|
class func getDatabaseName(dbName: String) -> String {
|
|
801
902
|
var retName: String = dbName
|
|
802
|
-
if retName.
|
|
803
|
-
|
|
903
|
+
if !retName.contains("/") {
|
|
904
|
+
if retName.suffix(3) == ".db" {
|
|
905
|
+
retName = String(retName.dropLast(3))
|
|
906
|
+
}
|
|
804
907
|
}
|
|
805
908
|
return retName
|
|
806
909
|
}
|
|
@@ -7,6 +7,9 @@ CAP_PLUGIN(CapacitorSQLitePlugin, "CapacitorSQLite",
|
|
|
7
7
|
CAP_PLUGIN_METHOD(echo, CAPPluginReturnPromise);
|
|
8
8
|
CAP_PLUGIN_METHOD(createConnection, CAPPluginReturnPromise);
|
|
9
9
|
CAP_PLUGIN_METHOD(closeConnection, CAPPluginReturnPromise);
|
|
10
|
+
CAP_PLUGIN_METHOD(createNCConnection, CAPPluginReturnPromise);
|
|
11
|
+
CAP_PLUGIN_METHOD(closeNCConnection, CAPPluginReturnPromise);
|
|
12
|
+
CAP_PLUGIN_METHOD(getNCDatabasePath, CAPPluginReturnPromise);
|
|
10
13
|
CAP_PLUGIN_METHOD(open, CAPPluginReturnPromise);
|
|
11
14
|
CAP_PLUGIN_METHOD(close, CAPPluginReturnPromise);
|
|
12
15
|
CAP_PLUGIN_METHOD(getVersion, CAPPluginReturnPromise);
|
|
@@ -26,6 +29,7 @@ CAP_PLUGIN(CapacitorSQLitePlugin, "CapacitorSQLite",
|
|
|
26
29
|
CAP_PLUGIN_METHOD(addUpgradeStatement, CAPPluginReturnPromise);
|
|
27
30
|
CAP_PLUGIN_METHOD(copyFromAssets, CAPPluginReturnPromise);
|
|
28
31
|
CAP_PLUGIN_METHOD(isDatabase, CAPPluginReturnPromise);
|
|
32
|
+
CAP_PLUGIN_METHOD(isNCDatabase, CAPPluginReturnPromise);
|
|
29
33
|
CAP_PLUGIN_METHOD(isTableExists, CAPPluginReturnPromise);
|
|
30
34
|
CAP_PLUGIN_METHOD(getDatabaseList, CAPPluginReturnPromise);
|
|
31
35
|
CAP_PLUGIN_METHOD(getMigratableDbList, CAPPluginReturnPromise);
|