@capacitor-community/sqlite 3.2.0 → 3.2.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.
Files changed (32) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/README.md +15 -2
  3. package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLite.java +1 -1
  4. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java +14 -6
  5. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ExportToJson.java +25 -5
  6. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ImportFromJson.java +43 -0
  7. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/JsonSQLite.java +42 -1
  8. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/JsonView.java +85 -0
  9. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/UtilsJson.java +45 -0
  10. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsDrop.java +54 -1
  11. package/dist/esm/definitions.d.ts +14 -0
  12. package/dist/esm/definitions.js +3 -1
  13. package/dist/esm/definitions.js.map +1 -1
  14. package/dist/esm/web.js +11 -9
  15. package/dist/esm/web.js.map +1 -1
  16. package/dist/plugin.cjs.js +14 -10
  17. package/dist/plugin.cjs.js.map +1 -1
  18. package/dist/plugin.js +14 -10
  19. package/dist/plugin.js.map +1 -1
  20. package/electron/dist/plugin.js +227 -16
  21. package/electron/dist/plugin.js.map +1 -1
  22. package/ios/Plugin/CapacitorSQLite.swift +22 -22
  23. package/ios/Plugin/CapacitorSQLitePlugin.swift +29 -29
  24. package/ios/Plugin/Database.swift +19 -8
  25. package/ios/Plugin/ImportExportJson/ExportToJson.swift +51 -6
  26. package/ios/Plugin/ImportExportJson/ImportFromJson.swift +75 -0
  27. package/ios/Plugin/ImportExportJson/JsonSQLite.swift +16 -0
  28. package/ios/Plugin/ReturnHandler.swift +1 -1
  29. package/ios/Plugin/Utils/UtilsDrop.swift +53 -3
  30. package/ios/Plugin/Utils/UtilsJson.swift +53 -0
  31. package/ios/Plugin/Utils/UtilsSQLCipher.swift +1 -1
  32. package/package.json +5 -5
@@ -26,6 +26,7 @@ enum ExportToJsonError: Error {
26
26
  case getSyncDate(message: String)
27
27
  case createRowValues(message: String)
28
28
  case modEmbeddedParentheses(message: String)
29
+ case getViews(message: String)
29
30
  }
30
31
 
31
32
  class ExportToJson {
@@ -64,14 +65,26 @@ class ExportToJson {
64
65
  throw ExportToJsonError.createExportObject(
65
66
  message: message + "version")
66
67
  }
67
-
68
+ var views: [[String: String]] = []
68
69
  var tables: [[String: Any]] = []
69
-
70
- // get the table's name
71
- var query: String = "SELECT name,sql FROM sqlite_master WHERE "
72
- query.append("type = 'table' AND name NOT LIKE 'sqlite_%' ")
73
- query.append("AND name NOT LIKE 'sync_table';")
74
70
  do {
71
+ // Get the views
72
+ // get the view's name
73
+ var stmtV: String = "SELECT name,sql FROM sqlite_master WHERE "
74
+ stmtV.append("type = 'view' AND name NOT LIKE 'sqlite_%';")
75
+ let resViews = try UtilsSQLCipher.querySQL(
76
+ mDB: mDB, sql: stmtV, values: [])
77
+ if resViews.count > 0 {
78
+ views = try ExportToJson
79
+ .getViews(mDB: mDB,
80
+ resViews: resViews)
81
+ }
82
+ // Get the tables
83
+
84
+ // get the table's name
85
+ var query: String = "SELECT name,sql FROM sqlite_master WHERE "
86
+ query.append("type = 'table' AND name NOT LIKE 'sqlite_%' ")
87
+ query.append("AND name NOT LIKE 'sync_table';")
75
88
  let resTables = try UtilsSQLCipher.querySQL(
76
89
  mDB: mDB, sql: query, values: [])
77
90
  if resTables.count > 0 {
@@ -92,6 +105,9 @@ class ExportToJson {
92
105
  } catch UtilsSQLCipherError.querySQL(let message) {
93
106
  throw ExportToJsonError.createExportObject(
94
107
  message: "Error get table's names failed : \(message)")
108
+ } catch ExportToJsonError.getViews(let message) {
109
+ throw ExportToJsonError.createExportObject(
110
+ message: "Error get views failed : \(message)")
95
111
  } catch ExportToJsonError.getTablesFull(let message) {
96
112
  throw ExportToJsonError.createExportObject(
97
113
  message: "Error get tables 'Full' failed : \(message)")
@@ -106,6 +122,9 @@ class ExportToJson {
106
122
  retObj["encrypted"] = encrypted
107
123
  retObj["mode"] = expMode
108
124
  retObj["tables"] = tables
125
+ if views.count > 0 {
126
+ retObj["views"] = views
127
+ }
109
128
  }
110
129
 
111
130
  return retObj
@@ -217,6 +236,32 @@ class ExportToJson {
217
236
  }
218
237
  }
219
238
 
239
+ // MARK: - ExportToJson - GetViews
240
+
241
+ class func getViews(mDB: Database,
242
+ resViews: [[String: Any]])
243
+ throws -> [[String: String]] {
244
+ var views: [[String: String]] = []
245
+ var iView: Int = 0
246
+ for rView in resViews {
247
+ iView += 1
248
+ guard let viewName: String = rView["name"] as? String
249
+ else {
250
+ throw ExportToJsonError.getViews(
251
+ message: "Error did not find view name")
252
+ }
253
+ guard let sqlStmt: String = rView["sql"] as? String else {
254
+ throw ExportToJsonError.getViews(
255
+ message: "Error did not find sql statement")
256
+ }
257
+ var view: [String: String] = [:]
258
+ view["name"] = viewName
259
+ view["value"] = sqlStmt.components(separatedBy: "AS ")[1]
260
+ views.append(view)
261
+ }
262
+ return views
263
+ }
264
+
220
265
  // MARK: - ExportToJson - GetTablesFull
221
266
 
222
267
  // swiftlint:disable cyclomatic_complexity
@@ -18,6 +18,8 @@ enum ImportFromJsonError: Error {
18
18
  case createSchemaStatement(message: String)
19
19
  case createTableData(message: String)
20
20
  case createRowStatement(message: String)
21
+ case createView(message: String)
22
+ case createViews(message: String)
21
23
  }
22
24
  class ImportFromJson {
23
25
 
@@ -512,6 +514,79 @@ class ImportFromJson {
512
514
  }
513
515
  return stmt
514
516
  }
517
+ // swiftlint:disable function_body_length
518
+ class func createViews(mDB: Database, views: [JsonView]) throws -> Int {
519
+ var changes: Int = 0
520
+ var initChanges: Int = -1
521
+ var isView: Bool = false
522
+ var msg: String = ""
523
+
524
+ do {
525
+ initChanges = UtilsSQLCipher.dbChanges(mDB: mDB.mDb)
526
+ // Start a transaction
527
+ try UtilsSQLCipher.beginTransaction(mDB: mDB)
528
+ } catch UtilsSQLCipherError.beginTransaction(let message) {
529
+ throw ImportFromJsonError.createDatabaseData(message: message)
530
+ }
531
+ for view in views {
532
+ if view.name.count > 0 && view.value.count > 0 {
533
+ do {
534
+ try ImportFromJson.createView(
535
+ mDB: mDB,
536
+ view: view)
537
+ isView = true
538
+ } catch ImportFromJsonError
539
+ .createView(let message) {
540
+ msg = message
541
+ }
542
+ } else {
543
+ msg = "no name and value"
544
+ break
545
+ }
546
+ }
547
+ if isView {
548
+ // commit
549
+ do {
550
+ // Commit the transaction
551
+ try UtilsSQLCipher.commitTransaction(mDB: mDB)
552
+ changes = UtilsSQLCipher
553
+ .dbChanges(mDB: mDB.mDb) - initChanges
554
+ } catch UtilsSQLCipherError.commitTransaction(
555
+ let message) {
556
+ throw ImportFromJsonError.createDatabaseData(
557
+ message: message)
558
+ }
559
+ } else {
560
+ if msg.count > 0 {
561
+ // rollback
562
+ do {
563
+ // Rollback the transaction
564
+ try UtilsSQLCipher
565
+ .rollbackTransaction(mDB: mDB)
566
+ throw ImportFromJsonError
567
+ .createViews(message: msg)
568
+ } catch UtilsSQLCipherError
569
+ .rollbackTransaction(let message) {
570
+ msg.append(" rollback: \(message)")
571
+ throw ImportFromJsonError
572
+ .createViews(message: msg)
573
+ }
574
+ } else {
575
+ changes = 0
576
+ }
577
+ }
578
+ return changes
579
+
580
+ }
581
+ class func createView(mDB: Database, view: JsonView) throws {
582
+ let stmt = "CREATE VIEW IF NOT EXISTS \(view.name) AS \(view.value);"
583
+ do {
584
+ try UtilsSQLCipher.execute(mDB: mDB, sql: stmt)
585
+ } catch UtilsSQLCipherError.execute(let message) {
586
+ throw ImportFromJsonError
587
+ .createView(message: message)
588
+ }
589
+ }
515
590
  }
516
591
  // swiftlint:enable type_body_length
517
592
  // swiftlint:enable file_length
@@ -13,6 +13,7 @@ public struct JsonSQLite: Codable {
13
13
  let encrypted: Bool
14
14
  let mode: String
15
15
  let tables: [JsonTable]
16
+ var views: [JsonView]?
16
17
 
17
18
  public func show() {
18
19
  print("databaseName: \(database) ")
@@ -23,6 +24,12 @@ public struct JsonSQLite: Codable {
23
24
  for table in tables {
24
25
  table.show()
25
26
  }
27
+ if let mViews = views {
28
+ print("Number of Views: \(mViews.count) ")
29
+ for view in mViews {
30
+ view.show()
31
+ }
32
+ }
26
33
  }
27
34
  }
28
35
 
@@ -137,6 +144,15 @@ public struct UncertainValue<T: Codable, U: Codable, V: Codable>: Codable {
137
144
  }
138
145
  }
139
146
  }
147
+ public struct JsonView: Codable {
148
+ let name: String
149
+ let value: String
150
+
151
+ public func show() {
152
+ print("name: \(name) ")
153
+ print("value: \(value) ")
154
+ }
155
+ }
140
156
 
141
157
  public struct JsonNamesTypes {
142
158
  var names: [String]
@@ -63,7 +63,7 @@ class ReturnHandler {
63
63
  call.reject(intMessage)
64
64
  return
65
65
  } else {
66
- call.resolve(["version": ret])
66
+ call.resolve(["version": ret as Any])
67
67
  return
68
68
  }
69
69
  }
@@ -11,9 +11,11 @@ import SQLCipher
11
11
 
12
12
  enum UtilsDropError: Error {
13
13
  case getTablesNamesFailed(message: String)
14
+ case getViewsNamesFailed(message: String)
14
15
  case getIndexesNamesFailed(message: String)
15
16
  case getTriggersNamesFailed(message: String)
16
17
  case dropTablesFailed(message: String)
18
+ case dropViewsFailed(message: String)
17
19
  case dropIndexesFailed(message: String)
18
20
  case dropTriggersFailed(message: String)
19
21
  case dropAllFailed(message: String)
@@ -71,6 +73,54 @@ class UtilsDrop {
71
73
  }
72
74
  }
73
75
 
76
+ // MARK: - getViewsNames
77
+
78
+ class func getViewsNames(mDB: Database) throws -> [String] {
79
+ var names: [String] = []
80
+ var query: String = "SELECT name FROM sqlite_master WHERE "
81
+ query.append("type='view' AND name NOT LIKE 'sqlite_%' ")
82
+ query.append("ORDER BY rootpage DESC;")
83
+ do {
84
+ let resQuery = try mDB.selectSQL(sql: query, values: [])
85
+ if resQuery.count > 0 {
86
+ for ipos in 0..<resQuery.count {
87
+ if let mName = resQuery[ipos]["name"] as? String {
88
+ names.append("\(mName)")
89
+ }
90
+ }
91
+ }
92
+ return names
93
+ } catch DatabaseError.selectSQL(let message) {
94
+ throw UtilsDropError.getViewsNamesFailed(message: message)
95
+ }
96
+ }
97
+
98
+ // MARK: - dropViews
99
+
100
+ class func dropViews(mDB: Database)
101
+ throws -> Int {
102
+ var changes: Int = 0
103
+ do {
104
+ let views: [String] = try getViewsNames(mDB: mDB)
105
+ var statements: [String] = []
106
+ for view in views {
107
+ var stmt: String = "DROP VIEW IF EXISTS "
108
+ stmt.append(view)
109
+ stmt.append(";")
110
+ statements.append(stmt)
111
+ }
112
+ if statements.count > 0 {
113
+ let joined = statements.joined(separator: "\n")
114
+ changes = try mDB.executeSQL(sql: joined)
115
+ }
116
+ return changes
117
+ } catch UtilsDropError.getViewsNamesFailed(let message) {
118
+ throw UtilsDropError.dropViewsFailed(message: message)
119
+ } catch DatabaseError.executeSQL(let message) {
120
+ throw UtilsDropError.dropViewsFailed(message: message)
121
+ }
122
+ }
123
+
74
124
  // MARK: - getIndexesNames
75
125
 
76
126
  class func getIndexesNames(mDB: Database) throws -> [String] {
@@ -172,13 +222,11 @@ class UtilsDrop {
172
222
 
173
223
  do {
174
224
  var retChanges: Int = try self.dropTables(mDB: mDB)
175
- print("after dropTables retChanges: \(retChanges)")
176
225
  retChanges = try self.dropIndexes(mDB: mDB)
177
226
  changes += retChanges
178
- print("after dropIndexes retChanges: \(retChanges)")
179
227
  retChanges = try self.dropTriggers(mDB: mDB)
180
- print("after dropTriggers retChanges: \(retChanges)")
181
228
  changes += retChanges
229
+ retChanges = try self.dropViews(mDB: mDB)
182
230
  if changes >= 0 {
183
231
  _ = try UtilsSQLCipher.prepareSQL(mDB: mDB,
184
232
  sql: "VACUUM;", values: [])
@@ -192,6 +240,8 @@ class UtilsDrop {
192
240
  throw UtilsDropError.dropAllFailed(message: message)
193
241
  } catch UtilsDropError.dropTriggersFailed(let message) {
194
242
  throw UtilsDropError.dropAllFailed(message: message)
243
+ } catch UtilsDropError.dropViewsFailed(let message) {
244
+ throw UtilsDropError.dropAllFailed(message: message)
195
245
  } catch UtilsSQLCipherError.prepareSQL(let message) {
196
246
  throw UtilsDropError.dropAllFailed(message: message)
197
247
  }
@@ -9,14 +9,17 @@ import Foundation
9
9
 
10
10
  enum UtilsJsonError: Error {
11
11
  case tableNotExists(message: String)
12
+ case viewNotExists(message: String)
12
13
  case getTableColumnNamesTypes(message: String)
13
14
  case isIdExists(message: String)
14
15
  case checkRowValidity(message: String)
15
16
  case validateSchema(message: String)
16
17
  case validateIndexes(message: String)
17
18
  case validateTriggers(message: String)
19
+ case validateViews(message: String)
18
20
  }
19
21
 
22
+ // swiftlint:disable type_body_length
20
23
  class UtilsJson {
21
24
 
22
25
  // MARK: - ImportFromJson - IsTableExists
@@ -43,6 +46,30 @@ class UtilsJson {
43
46
  return ret
44
47
  }
45
48
 
49
+ // MARK: - ImportFromJson - IsViewExists
50
+
51
+ class func isViewExists(mDB: Database, viewName: String)
52
+ throws -> Bool {
53
+ var msg: String = "Error isViewExists: "
54
+ if !mDB.isDBOpen() {
55
+ msg.append("Database not opened")
56
+ throw UtilsJsonError.viewNotExists(message: msg)
57
+ }
58
+ var ret: Bool = false
59
+ var query = "SELECT name FROM sqlite_master WHERE type='view'"
60
+ query.append(" AND name='")
61
+ query.append(viewName)
62
+ query.append("';")
63
+ do {
64
+ let resQuery: [Any] = try UtilsSQLCipher
65
+ .querySQL(mDB: mDB, sql: query, values: [])
66
+ if resQuery.count > 0 {ret = true}
67
+ } catch UtilsSQLCipherError.querySQL(let message) {
68
+ throw UtilsJsonError.viewNotExists(message: message)
69
+ }
70
+ return ret
71
+ }
72
+
46
73
  // MARK: - ImportFromJson - GetTableColumnNamesTypes
47
74
 
48
75
  class func getTableColumnNamesTypes(mDB: Database,
@@ -278,4 +305,30 @@ class UtilsJson {
278
305
  return isTriggers
279
306
  }
280
307
 
308
+ // MARK: - ExportToJson - validateIndexes
309
+
310
+ class func validateViews(views: [[String: String]])
311
+ throws -> Bool {
312
+
313
+ var isViews = false
314
+ do {
315
+ let eViews = try JSONEncoder().encode(views)
316
+ guard let eViewsString: String =
317
+ String(data: eViews, encoding: .utf8) else {
318
+ var message: String = "Error in converting "
319
+ message.append("eViews to String")
320
+ throw UtilsJsonError.validateViews(
321
+ message: message)
322
+ }
323
+ if eViewsString.count > 0 {
324
+ isViews = true
325
+ }
326
+ } catch {
327
+ throw UtilsJsonError.validateViews(
328
+ message: "Error in encoding views")
329
+ }
330
+ return isViews
331
+ }
332
+
281
333
  }
334
+ // swiftlint:enable type_body_length
@@ -629,7 +629,7 @@ class UtilsSQLCipher {
629
629
  throw UtilsSQLCipherError.executeSet(
630
630
  message: "No values given")
631
631
  }
632
- let isArray = UtilsSQLCipher.parse(mVar: values[0])
632
+ let isArray = values.count > 0 ? UtilsSQLCipher.parse(mVar: values[0]) : false
633
633
  if isArray {
634
634
  if let arrValues = values as? [[Any]] {
635
635
  for vals in arrValues {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capacitor-community/sqlite",
3
- "version": "3.2.0",
3
+ "version": "3.2.2",
4
4
  "description": "Community plugin for native & electron SQLite databases",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
@@ -54,10 +54,10 @@
54
54
  "prepublishOnly": "npm run build && npm run build-electron && npm run docgen"
55
55
  },
56
56
  "devDependencies": {
57
- "@capacitor/android": "^3.2.0",
58
- "@capacitor/core": "3.2.0",
57
+ "@capacitor/android": "^3.2.2",
58
+ "@capacitor/core": "3.2.2",
59
59
  "@capacitor/docgen": "^0.0.17",
60
- "@capacitor/ios": "^3.2.0",
60
+ "@capacitor/ios": "^3.2.2",
61
61
  "@ionic/eslint-config": "^0.3.0",
62
62
  "@ionic/prettier-config": "^1.0.1",
63
63
  "@ionic/swiftlint-config": "^1.1.2",
@@ -73,7 +73,7 @@
73
73
  "typescript": "~4.0.5"
74
74
  },
75
75
  "peerDependencies": {
76
- "@capacitor/core": "^3.2.0"
76
+ "@capacitor/core": "^3.2.2"
77
77
  },
78
78
  "prettier": "@ionic/prettier-config",
79
79
  "swiftlint": "@ionic/swiftlint-config",