@capgo/capacitor-data-storage-sqlite 6.0.0

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.
Files changed (59) hide show
  1. package/CapacitorDataStorageSqlite.podspec +18 -0
  2. package/LICENSE +21 -0
  3. package/android/build.gradle +63 -0
  4. package/android/src/main/AndroidManifest.xml +3 -0
  5. package/android/src/main/java/com/jeep/plugin/capacitor/capacitordatastoragesqlite/CapacitorDataStorageSqlite.java +387 -0
  6. package/android/src/main/java/com/jeep/plugin/capacitor/capacitordatastoragesqlite/CapacitorDataStorageSqlitePlugin.java +447 -0
  7. package/android/src/main/java/com/jeep/plugin/capacitor/capacitordatastoragesqlite/RetHandler.java +117 -0
  8. package/android/src/main/java/com/jeep/plugin/capacitor/capacitordatastoragesqlite/cdssUtils/Data.java +8 -0
  9. package/android/src/main/java/com/jeep/plugin/capacitor/capacitordatastoragesqlite/cdssUtils/Global.java +7 -0
  10. package/android/src/main/java/com/jeep/plugin/capacitor/capacitordatastoragesqlite/cdssUtils/ImportExportJson/JsonStore.java +131 -0
  11. package/android/src/main/java/com/jeep/plugin/capacitor/capacitordatastoragesqlite/cdssUtils/ImportExportJson/JsonTable.java +110 -0
  12. package/android/src/main/java/com/jeep/plugin/capacitor/capacitordatastoragesqlite/cdssUtils/ImportExportJson/JsonValue.java +89 -0
  13. package/android/src/main/java/com/jeep/plugin/capacitor/capacitordatastoragesqlite/cdssUtils/StorageDatabaseHelper.java +691 -0
  14. package/android/src/main/java/com/jeep/plugin/capacitor/capacitordatastoragesqlite/cdssUtils/UtilsSQLCipher.java +162 -0
  15. package/android/src/main/res/.gitkeep +0 -0
  16. package/dist/docs.json +995 -0
  17. package/dist/esm/definitions.d.ts +296 -0
  18. package/dist/esm/definitions.js +2 -0
  19. package/dist/esm/definitions.js.map +1 -0
  20. package/dist/esm/index.d.ts +4 -0
  21. package/dist/esm/index.js +9 -0
  22. package/dist/esm/index.js.map +1 -0
  23. package/dist/esm/web-utils/Data.d.ts +5 -0
  24. package/dist/esm/web-utils/Data.js +3 -0
  25. package/dist/esm/web-utils/Data.js.map +1 -0
  26. package/dist/esm/web-utils/StorageDatabaseHelper.d.ts +23 -0
  27. package/dist/esm/web-utils/StorageDatabaseHelper.js +247 -0
  28. package/dist/esm/web-utils/StorageDatabaseHelper.js.map +1 -0
  29. package/dist/esm/web-utils/json-utils.d.ts +15 -0
  30. package/dist/esm/web-utils/json-utils.js +76 -0
  31. package/dist/esm/web-utils/json-utils.js.map +1 -0
  32. package/dist/esm/web.d.ts +27 -0
  33. package/dist/esm/web.js +295 -0
  34. package/dist/esm/web.js.map +1 -0
  35. package/dist/plugin.cjs.js +633 -0
  36. package/dist/plugin.cjs.js.map +1 -0
  37. package/dist/plugin.js +635 -0
  38. package/dist/plugin.js.map +1 -0
  39. package/electron/dist/plugin.js +1044 -0
  40. package/electron/dist/plugin.js.map +1 -0
  41. package/electron/rollup.config.mjs +17 -0
  42. package/electron/tsconfig.json +19 -0
  43. package/ios/Plugin/CapacitorDataStorageSqlite.swift +550 -0
  44. package/ios/Plugin/CapacitorDataStorageSqlitePlugin.h +10 -0
  45. package/ios/Plugin/CapacitorDataStorageSqlitePlugin.m +29 -0
  46. package/ios/Plugin/CapacitorDataStorageSqlitePlugin.swift +550 -0
  47. package/ios/Plugin/Data.swift +16 -0
  48. package/ios/Plugin/Global.swift +13 -0
  49. package/ios/Plugin/ImportExportJson/JsonStore.swift +47 -0
  50. package/ios/Plugin/Info.plist +24 -0
  51. package/ios/Plugin/ReturnHandler.swift +85 -0
  52. package/ios/Plugin/StorageDatabaseHelper.swift +603 -0
  53. package/ios/Plugin/Utils/Blob.swift +41 -0
  54. package/ios/Plugin/Utils/UtilsBinding.swift +73 -0
  55. package/ios/Plugin/Utils/UtilsEncryption.swift +79 -0
  56. package/ios/Plugin/Utils/UtilsFile.swift +244 -0
  57. package/ios/Plugin/Utils/UtilsSQLCipher.swift +605 -0
  58. package/package.json +96 -0
  59. package/readme.md +203 -0
@@ -0,0 +1,41 @@
1
+ //
2
+ // Blob.swift
3
+ // Plugin
4
+ //
5
+ // Created by Quéau Jean Pierre on 08/04/2021.
6
+ // Copyright © 2021 Max Lynch. All rights reserved.
7
+ //
8
+
9
+ import Foundation
10
+
11
+ public struct Blob {
12
+
13
+ public let bytes: [UInt8]
14
+
15
+ public init(bytes: [UInt8]) {
16
+ self.bytes = bytes
17
+ }
18
+
19
+ public init(bytes: UnsafeRawPointer, length: Int) {
20
+ let i8bufptr = UnsafeBufferPointer(start: bytes.assumingMemoryBound(to: UInt8.self), count: length)
21
+ self.init(bytes: [UInt8](i8bufptr))
22
+ }
23
+
24
+ public func toHex() -> String {
25
+ return bytes.map {($0 < 16 ? "0" : "") + String($0, radix: 16, uppercase: false)
26
+ }.joined(separator: "")
27
+ }
28
+
29
+ }
30
+
31
+ extension Blob: CustomStringConvertible {
32
+
33
+ public var description: String {
34
+ return "x'\(toHex())'"
35
+ }
36
+
37
+ }
38
+
39
+ public func == (lhs: Blob, rhs: Blob) -> Bool {
40
+ return lhs.bytes == rhs.bytes
41
+ }
@@ -0,0 +1,73 @@
1
+ //
2
+ // UtilsBinding.swift
3
+ // Plugin
4
+ //
5
+ // Created by Quéau Jean Pierre on 12/04/2021.
6
+ // Copyright © 2021 Max Lynch. All rights reserved.
7
+ //
8
+
9
+ import Foundation
10
+ import SQLCipher
11
+
12
+ let SQLITETRANSIENT = unsafeBitCast(-1, to:
13
+ sqlite3_destructor_type.self)
14
+
15
+ class UtilsBinding {
16
+ class func bindValues( handle: OpaquePointer?, values: [String])
17
+ -> String {
18
+ var message: String = ""
19
+ var idx: Int = 1
20
+ for value in values {
21
+ do {
22
+ if let selStmt = handle {
23
+ try UtilsBinding.bind(handle: selStmt,
24
+ value: value, idx: idx)
25
+ idx += 1
26
+ } else {
27
+ message = "Error: querySQL bind failed "
28
+ }
29
+ } catch let error as NSError {
30
+ message = "Error: querySQL bind failed "
31
+ message.append("\(error.localizedDescription)")
32
+ }
33
+ if message.count > 0 { break }
34
+ }
35
+ return message
36
+ }
37
+ class func bind( handle: OpaquePointer?, value: Any?, idx: Int)
38
+ throws {
39
+ if value == nil {
40
+ sqlite3_bind_null(handle, Int32(idx))
41
+ } else if (value as? NSNull) != nil {
42
+ sqlite3_bind_null(handle, Int32(idx))
43
+ } else if let value = value as? Double {
44
+ sqlite3_bind_double(handle, Int32(idx), value)
45
+ } else if let value = value as? Int64 {
46
+ sqlite3_bind_int64(handle, Int32(idx), value)
47
+ } else if let value = value as? String {
48
+ if value.contains("base64") {
49
+ // case Base64 string as Blob
50
+ // sqlite3_bind_blob(handle, Int32(idx), [value],
51
+ // Int32(value.count), SQLITETRANSIENT)
52
+ sqlite3_bind_text(handle, Int32(idx), value, -1,
53
+ SQLITETRANSIENT)
54
+
55
+ } else if value.uppercased() == "NULL" {
56
+ // case NULL
57
+ sqlite3_bind_null(handle, Int32(idx))
58
+ } else {
59
+ sqlite3_bind_text(handle, Int32(idx), value, -1,
60
+ SQLITETRANSIENT)
61
+ }
62
+ } else if let value = value as? Int {
63
+ sqlite3_bind_int64(handle, Int32(idx), Int64(value))
64
+ } else if let value = value as? Bool {
65
+ var bInt: Int32 = Int32(0)
66
+ if value {bInt = Int32(1)}
67
+ sqlite3_bind_int(handle, Int32(idx), Int32(bInt))
68
+ } else {
69
+ throw UtilsSQLCipherError.bindFailed
70
+ }
71
+
72
+ }
73
+ }
@@ -0,0 +1,79 @@
1
+ //
2
+ // UtilsEncryption.swift
3
+ // Plugin
4
+ //
5
+ // Created by Quéau Jean Pierre on 12/04/2021.
6
+ // Copyright © 2021 Max Lynch. All rights reserved.
7
+ //
8
+
9
+ import Foundation
10
+ import SQLCipher
11
+
12
+ enum UtilsEncryptionError: Error {
13
+ case encryptionFailed(message: String)
14
+ }
15
+ class UtilsEncryption {
16
+
17
+ // MARK: - EncryptDatabase
18
+ // swiftlint:disable function_body_length
19
+ class func encryptDatabase(filePath: String, password: String)
20
+ throws -> Bool {
21
+ var ret: Bool = false
22
+ var oDB: OpaquePointer?
23
+ do {
24
+ if UtilsFile.isFileExist(filePath: filePath) {
25
+ do {
26
+ let tempPath: String = try UtilsFile.getFilePath(
27
+ fileName: "temp.db")
28
+ try UtilsFile.renameFile(filePath: filePath,
29
+ toFilePath: tempPath)
30
+ oDB = try UtilsSQLCipher
31
+ .openOrCreateDatabase(filename: tempPath,
32
+ password: "",
33
+ readonly: false)
34
+ try _ = UtilsSQLCipher
35
+ .openOrCreateDatabase(filename: filePath,
36
+ password: password,
37
+ readonly: false)
38
+
39
+ var stmt: String = "ATTACH DATABASE '\(filePath)' "
40
+ stmt.append("AS encrypted KEY '\(password)';")
41
+ stmt.append("SELECT sqlcipher_export('encrypted');")
42
+ stmt.append("DETACH DATABASE encrypted;")
43
+ if sqlite3_exec(oDB, stmt, nil, nil, nil) ==
44
+ SQLITE_OK {
45
+ try UtilsFile.deleteFile(
46
+ fileName: "temp.db")
47
+ ret = true
48
+ }
49
+ // close the db
50
+ try UtilsSQLCipher.close(oDB: oDB)
51
+
52
+ } catch UtilsFileError.getFilePathFailed {
53
+ throw UtilsEncryptionError
54
+ .encryptionFailed(message: "file path failed")
55
+ } catch UtilsFileError.renameFileFailed {
56
+ throw UtilsEncryptionError
57
+ .encryptionFailed(message: "file rename failed")
58
+ } catch UtilsSQLCipherError.openOrCreateDatabase(_) {
59
+ throw UtilsEncryptionError
60
+ .encryptionFailed(message: "open failed")
61
+ } catch UtilsSQLCipherError.close(_) {
62
+ throw UtilsEncryptionError
63
+ .encryptionFailed(message: "close failed")
64
+ } catch let error {
65
+ print("Error: \(error)")
66
+ throw UtilsEncryptionError
67
+ .encryptionFailed(message: "Error: \(error)")
68
+ }
69
+ }
70
+ } catch let error {
71
+ print("Error: \(error)")
72
+ throw UtilsEncryptionError
73
+ .encryptionFailed(message: "Error: \(error)")
74
+ }
75
+ return ret
76
+ }
77
+
78
+ }
79
+ // swiftlint:enable function_body_length
@@ -0,0 +1,244 @@
1
+ //
2
+ // UtilsFile.swift
3
+ // Plugin
4
+ //
5
+ // Created by Quéau Jean Pierre on 12/04/2021.
6
+ // Copyright © 2021 Max Lynch. All rights reserved.
7
+ //
8
+
9
+ import Foundation
10
+ enum UtilsFileError: Error {
11
+ case getFilePathFailed
12
+ case copyFileFailed
13
+ case renameFileFailed
14
+ case deleteFileFailed
15
+ case getAssetsDatabasesPathFailed
16
+ case getDatabasesPathFailed
17
+ case getDatabasesURLFailed
18
+ case getApplicationPathFailed
19
+ case getApplicationURLFailed
20
+ case getLibraryPathFailed
21
+ case getLibraryURLFailed
22
+ case getFileListFailed
23
+ case copyFromAssetToDatabaseFailed
24
+ case copyFromNamesFailed
25
+ }
26
+
27
+ class UtilsFile {
28
+
29
+ // MARK: - IsFileExist
30
+
31
+ class func isDirExist(dirPath: String) -> Bool {
32
+ var isDir: ObjCBool = true
33
+ let fileManager = FileManager.default
34
+ let exists = fileManager.fileExists(atPath: dirPath, isDirectory: &isDir)
35
+ return exists && isDir.boolValue
36
+ }
37
+
38
+ // MARK: - IsFileExist
39
+
40
+ class func isFileExist(filePath: String) -> Bool {
41
+ var ret: Bool = false
42
+ let fileManager = FileManager.default
43
+ if fileManager.fileExists(atPath: filePath) {
44
+ ret = true
45
+ }
46
+ return ret
47
+ }
48
+ class func isFileExist(fileName: String) -> Bool {
49
+ var ret: Bool = false
50
+ do {
51
+ let filePath: String =
52
+ try UtilsFile.getFilePath(
53
+ fileName: fileName)
54
+ ret = UtilsFile.isFileExist(filePath: filePath)
55
+ return ret
56
+ } catch UtilsFileError.getFilePathFailed {
57
+ return false
58
+ } catch _ {
59
+ return false
60
+ }
61
+ }
62
+
63
+ // MARK: - GetFilePath
64
+
65
+ class func getFilePath(fileName: String) throws -> String {
66
+ do {
67
+ let url = try getDatabasesUrl()
68
+ return url.appendingPathComponent("\(fileName)").path
69
+ } catch UtilsFileError.getDatabasesURLFailed {
70
+ print("Error: getDatabasesUrl Failed")
71
+ throw UtilsFileError.getFilePathFailed
72
+ }
73
+ }
74
+
75
+ // MARK: - getDatabasesUrl
76
+
77
+ class func getDatabasesUrl() throws -> URL {
78
+ if let path: String = NSSearchPathForDirectoriesInDomains(
79
+ .documentDirectory, .userDomainMask, true
80
+ ).first {
81
+ return NSURL(fileURLWithPath: path) as URL
82
+ } else {
83
+ print("Error: getDatabasesURL did not find the document folder")
84
+ throw UtilsFileError.getDatabasesURLFailed
85
+ }
86
+ }
87
+
88
+ // MARK: - getDatabasesPath
89
+ class func getDatabasesPath() throws -> String {
90
+ if let path: String = NSSearchPathForDirectoriesInDomains(
91
+ .documentDirectory, .userDomainMask, true
92
+ ).first {
93
+ return path
94
+ } else {
95
+ print("Error: getDatabasesPath did not find the document folder")
96
+ throw UtilsFileError.getDatabasesPathFailed
97
+ }
98
+ }
99
+
100
+ // MARK: - GetFileList
101
+
102
+ class func getFileList(path: String) throws -> [String] {
103
+ do {
104
+ var dbs: [String] = []
105
+ let filenames = try FileManager.default
106
+ .contentsOfDirectory(atPath: path)
107
+ let ext: String = ".db"
108
+ for file in filenames {
109
+ if file.hasSuffix(ext) {
110
+ dbs.append(file)
111
+ }
112
+ }
113
+ return dbs
114
+ } catch let error {
115
+ print("Error: \(error)")
116
+ throw UtilsFileError.getFileListFailed
117
+ }
118
+ }
119
+
120
+ // MARK: - CopyFromNames
121
+
122
+ class func copyFromNames(dbPathURL: URL, fromFile: String, databaseURL: URL,
123
+ toFile: String) throws {
124
+ do {
125
+ let uFrom: URL = dbPathURL.appendingPathComponent(fromFile)
126
+ let uTo: URL = databaseURL.appendingPathComponent(toFile)
127
+ let pFrom: String = uFrom.path
128
+ let pTo: String = uTo.path
129
+ let bRet: Bool = try copyFile(pathName: pFrom, toPathName: pTo)
130
+ if bRet {
131
+ return
132
+ } else {
133
+ print("Error: FromNames return false")
134
+ throw UtilsFileError.copyFromNamesFailed
135
+ }
136
+ } catch UtilsFileError.copyFileFailed {
137
+ print("Error: copyFile Failed")
138
+ throw UtilsFileError.copyFromNamesFailed
139
+ }
140
+
141
+ }
142
+
143
+ // MARK: - CopyFile
144
+
145
+ class func copyFile(pathName: String, toPathName: String) throws -> Bool {
146
+ if pathName.count > 0 && toPathName.count > 0 {
147
+ let isPath = isFileExist(filePath: pathName)
148
+ if isPath {
149
+ do {
150
+ if isFileExist(filePath: toPathName) {
151
+ _ = try deleteFile(filePath: toPathName)
152
+ }
153
+ let fileManager = FileManager.default
154
+ try fileManager.copyItem(atPath: pathName,
155
+ toPath: toPathName)
156
+ return true
157
+ } catch let error {
158
+ print("Error: \(error)")
159
+ throw UtilsFileError.copyFileFailed
160
+ }
161
+ } else {
162
+ print("Error: CopyFilePath Failed pathName does not exist")
163
+ throw UtilsFileError.copyFileFailed
164
+ }
165
+ } else {
166
+ print("Error: CopyFilePath Failed paths count = 0")
167
+ throw UtilsFileError.copyFileFailed
168
+ }
169
+ }
170
+
171
+ // MARK: - CopyFile
172
+
173
+ class func copyFile(fileName: String, toFileName: String)
174
+ throws -> Bool {
175
+ var ret: Bool = false
176
+ do {
177
+ let fromPath: String = try getFilePath(fileName: fileName)
178
+ let toPath: String = try getFilePath(fileName: toFileName)
179
+ ret = try copyFile(pathName: fromPath, toPathName: toPath)
180
+ return ret
181
+ } catch UtilsFileError.getFilePathFailed {
182
+ print("Error: getFilePath Failed")
183
+ throw UtilsFileError.copyFileFailed
184
+ } catch let error {
185
+ print("Error: \(error)")
186
+ throw UtilsFileError.copyFileFailed
187
+ }
188
+ }
189
+
190
+ // MARK: - DeleteFile
191
+
192
+ class func deleteFile(filePath: String) throws {
193
+ do {
194
+ if isFileExist(filePath: filePath) {
195
+ let fileManager = FileManager.default
196
+ do {
197
+ try fileManager.removeItem(atPath: filePath)
198
+ } catch let error {
199
+ print("Error: \(error)")
200
+ throw UtilsFileError.deleteFileFailed
201
+ }
202
+ }
203
+ return
204
+ } catch let error {
205
+ print("Error: \(error)")
206
+ throw UtilsFileError.deleteFileFailed
207
+ }
208
+ }
209
+
210
+ // MARK: - DeleteFile
211
+
212
+ class func deleteFile(fileName: String) throws {
213
+ do {
214
+ let filePath: String = try getFilePath(fileName: fileName)
215
+ try deleteFile(filePath: filePath)
216
+ return
217
+ } catch let error {
218
+ print("Error: \(error)")
219
+ throw UtilsFileError.deleteFileFailed
220
+ }
221
+ }
222
+
223
+ // MARK: - RenameFile
224
+
225
+ class func renameFile (filePath: String, toFilePath: String)
226
+ throws {
227
+ let fileManager = FileManager.default
228
+ do {
229
+ if isFileExist(filePath: toFilePath) {
230
+ let fileName = URL(
231
+ fileURLWithPath: toFilePath).lastPathComponent
232
+ try deleteFile(fileName: fileName)
233
+ }
234
+ try fileManager.moveItem(atPath: filePath,
235
+ toPath: toFilePath)
236
+ } catch UtilsFileError.deleteFileFailed {
237
+ print("Error: Failed in delete file")
238
+ throw UtilsFileError.renameFileFailed
239
+ } catch let error {
240
+ print("Error: \(error)")
241
+ throw UtilsFileError.renameFileFailed
242
+ }
243
+ }
244
+ }