@capacitor-community/sqlite 5.0.7-1 → 5.0.7

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 (33) hide show
  1. package/README.md +6 -1
  2. package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLite.java +123 -1
  3. package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLitePlugin.java +112 -0
  4. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java +140 -78
  5. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ImportFromJson.java +9 -9
  6. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsDelete.java +484 -0
  7. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsDrop.java +3 -3
  8. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsSQLStatement.java +169 -0
  9. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsUpgrade.java +4 -4
  10. package/dist/esm/definitions.d.ts +96 -11
  11. package/dist/esm/definitions.js +99 -50
  12. package/dist/esm/definitions.js.map +1 -1
  13. package/dist/esm/web.d.ts +4 -0
  14. package/dist/esm/web.js +44 -0
  15. package/dist/esm/web.js.map +1 -1
  16. package/dist/plugin.cjs.js +143 -50
  17. package/dist/plugin.cjs.js.map +1 -1
  18. package/dist/plugin.js +143 -50
  19. package/dist/plugin.js.map +1 -1
  20. package/electron/dist/plugin.js +1102 -260
  21. package/electron/dist/plugin.js.map +1 -1
  22. package/ios/Plugin/CapacitorSQLite.swift +119 -0
  23. package/ios/Plugin/CapacitorSQLitePlugin.m +4 -0
  24. package/ios/Plugin/CapacitorSQLitePlugin.swift +128 -0
  25. package/ios/Plugin/Database.swift +76 -0
  26. package/ios/Plugin/ImportExportJson/ImportFromJson.swift +13 -2
  27. package/ios/Plugin/Utils/UtilsDelete.swift +116 -114
  28. package/ios/Plugin/Utils/UtilsSQLCipher.swift +10 -3
  29. package/ios/Plugin/Utils/UtilsSQLStatement.swift +84 -84
  30. package/ios/Plugin/Utils/UtilsUpgrade.swift +3 -0
  31. package/package.json +2 -2
  32. package/src/definitions.ts +187 -53
  33. package/src/web.ts +48 -0
@@ -20,6 +20,7 @@ enum UtilsDeleteError: Error {
20
20
  case upDateWhereForCascade(message: String)
21
21
  }
22
22
 
23
+ // swiftlint:disable file_length
23
24
  // swiftlint:disable type_body_length
24
25
  class UtilsDelete {
25
26
 
@@ -54,7 +55,7 @@ class UtilsDelete {
54
55
  guard let refTable = foreignKeyInfo["tableName"]
55
56
  as? String else {
56
57
  let msg = "findReferencesAndUpdate: no foreignKeyInfo " +
57
- "tableName"
58
+ "tableName"
58
59
  throw UtilsDeleteError
59
60
  .findReferencesAndUpdate(message: msg)
60
61
  }
@@ -63,23 +64,23 @@ class UtilsDelete {
63
64
  }
64
65
  // get the with ref columnName
65
66
  guard let withRefsNames = foreignKeyInfo["forKeys"]
66
- as? [String] else {
67
+ as? [String] else {
67
68
 
68
69
  let msg = "findReferencesAndUpdate: no foreignKeyInfo " +
69
- "forKeys"
70
+ "forKeys"
70
71
  throw UtilsDeleteError
71
72
  .findReferencesAndUpdate(message: msg)
72
73
  }
73
74
  guard let colNames = foreignKeyInfo["refKeys"]
74
75
  as? [String] else {
75
76
  let msg = "findReferencesAndUpdate: no foreignKeyInfo " +
76
- "refKeys"
77
+ "refKeys"
77
78
  throw UtilsDeleteError
78
79
  .findReferencesAndUpdate(message: msg)
79
80
  }
80
81
  if colNames.count != withRefsNames.count {
81
82
  let msg = "findReferencesAndUpdate: no foreignKeyInfo " +
82
- "colNames"
83
+ "colNames"
83
84
  throw UtilsDeleteError
84
85
  .findReferencesAndUpdate(message: msg)
85
86
 
@@ -102,58 +103,58 @@ class UtilsDelete {
102
103
  if !checkValuesMatch(withRefsNames,
103
104
  against: initColNames) {
104
105
  // Search for related items in tableName
105
- let result: (String,[[String: Any]]) = try UtilsDelete
106
+ let result: (String, [[String: Any]]) = try UtilsDelete
106
107
  .searchForRelatedItems(mDB: mDB,
107
108
  updTableName: updTableName,
108
109
  tableName: tableName,
109
110
  whStmt: whereStmt,
110
111
  withRefsNames: withRefsNames,
111
112
  colNames: colNames,
112
- values: values);
113
+ values: values)
113
114
  let key: String = result.0
114
115
  let relatedItems: [Any] = result.1
115
116
  if relatedItems.count == 0 && key.count <= 0 {
116
- continue
117
+ continue
117
118
  }
118
119
 
119
120
  // case no match
120
121
  if updTableName != tableName {
121
122
  switch action {
122
- case "CASCADE":
123
- // updTableName
124
- // update all related element
125
- // set sql_deleted = 1 and last_modified
126
- // tableName
127
- //update all by sending return true
128
- results = try upDateWhereForCascade(
129
- results: result)
130
-
131
- case "RESTRICT":
132
- // find for elements related in updTableName
133
- // if some elements
134
- // send a message
135
- // do not update tableName
136
- // return false
137
- // If no related elements in updTableName
138
- // return true to update tableName
139
- results = try upDateWhereForRestrict(
140
- results: result)
141
-
142
- default:
143
- // updTableName
144
- // update the result_id result_slug to Null
145
- // update the last_modified
146
- // keep sql_deleted to 0
147
- // return true to update tableName
148
- results = try upDateWhereForDefault(
149
- withRefsNames: withRefsNames,
150
- results: result)
123
+ case "CASCADE":
124
+ // updTableName
125
+ // update all related element
126
+ // set sql_deleted = 1 and last_modified
127
+ // tableName
128
+ //update all by sending return true
129
+ results = try upDateWhereForCascade(
130
+ results: result)
131
+
132
+ case "RESTRICT":
133
+ // find for elements related in updTableName
134
+ // if some elements
135
+ // send a message
136
+ // do not update tableName
137
+ // return false
138
+ // If no related elements in updTableName
139
+ // return true to update tableName
140
+ results = try upDateWhereForRestrict(
141
+ results: result)
142
+
143
+ default:
144
+ // updTableName
145
+ // update the result_id result_slug to Null
146
+ // update the last_modified
147
+ // keep sql_deleted to 0
148
+ // return true to update tableName
149
+ results = try upDateWhereForDefault(
150
+ withRefsNames: withRefsNames,
151
+ results: result)
151
152
  }
152
153
  }
153
154
 
154
155
  } else {
155
156
  let msg = "Not implemented. Please transfer your " +
156
- "example to the maintener"
157
+ "example to the maintener"
157
158
  throw UtilsDeleteError
158
159
  .findReferencesAndUpdate(message: msg)
159
160
  }
@@ -194,7 +195,7 @@ class UtilsDelete {
194
195
  // MARK: - upDateWhereForDefault
195
196
 
196
197
  class func upDateWhereForDefault(withRefsNames: [String],
197
- results: (String,[[String:Any]]))
198
+ results: (String, [[String: Any]]))
198
199
  throws -> ((setStmt: String, uWhereStmt: String)) {
199
200
 
200
201
  var setStmt = ""
@@ -225,31 +226,31 @@ class UtilsDelete {
225
226
 
226
227
  uWhereStmt += ");"
227
228
 
228
- return(setStmt, uWhereStmt)
229
+ return(setStmt, uWhereStmt)
229
230
  }
230
231
 
231
232
  // MARK: - upDateWhereForRestrict
232
233
 
233
- class func upDateWhereForRestrict(results: (String,[[String: Any]]))
234
- throws -> ((setStmt: String, uWhereStmt: String)) {
234
+ class func upDateWhereForRestrict(results: (String, [[String: Any]]))
235
+ throws -> ((setStmt: String, uWhereStmt: String)) {
235
236
 
236
- // Search for related items in tableName
237
+ // Search for related items in tableName
237
238
  let setStmt = ""
238
239
  let uWhereStmt = ""
239
240
  let relatedItems = results.1
240
241
 
241
242
  if !relatedItems.isEmpty {
242
243
  let msg = "Restrict mode related items exist" +
243
- " please delete them first"
244
+ " please delete them first"
244
245
  throw UtilsDeleteError
245
- .upDateWhereForRestrict(message: msg)
246
+ .upDateWhereForRestrict(message: msg)
246
247
  }
247
248
  return(setStmt, uWhereStmt)
248
249
  }
249
250
 
250
251
  // MARK: - upDateWhereForCascade
251
252
 
252
- class func upDateWhereForCascade(results: (String,[[String:Any]]))
253
+ class func upDateWhereForCascade(results: (String, [[String: Any]]))
253
254
  throws -> ((setStmt: String, uWhereStmt: String)) {
254
255
 
255
256
  // Search for related items in tableName
@@ -285,48 +286,48 @@ class UtilsDelete {
285
286
  tableName: String, whStmt: String,
286
287
  withRefsNames: [String],
287
288
  colNames: [String], values: [Any])
288
- throws -> (String,[[String: Any]]) {
289
+ throws -> (String, [[String: Any]]) {
289
290
  var relatedItems: [[String: Any]] = []
290
291
  var key: String = ""
291
292
  let t1Names = withRefsNames.map { "t1.\($0)" }
292
293
  let t2Names = colNames.map { "t2.\($0)" }
293
294
 
294
- do {
295
- var whereClause = try UtilsSQLStatement
296
- .addPrefixToWhereClause(whStmt, from: colNames,
297
- to: withRefsNames,
298
- prefix: "t2.")
299
- if whereClause.hasSuffix(";") {
300
- whereClause = String(whereClause.dropLast())
301
- }
302
- let resultString = zip(t1Names, t2Names)
303
- .map {"\($0) = \($1)" }
304
- .joined(separator: " AND ")
305
-
306
- let sql = "SELECT t1.rowid FROM \(updTableName) t1 " +
307
- "JOIN \(tableName) t2 ON \(resultString) " +
308
- "WHERE \(whereClause) AND t1.sql_deleted = 0;"
309
- var vals = try UtilsSQLCipher.querySQL(mDB: mDB, sql: sql,
310
- values: values)
311
- if vals.count > 1 {
312
- if let mVals = vals[0]["ios_columns"] as? [String] {
313
- key = mVals[0]
314
- let keyToRemove = "ios_columns"
315
- vals.removeAll { dict in
316
- return dict.keys.contains(keyToRemove)
317
- }
318
- for val in vals {
319
- relatedItems.append(val)
320
- }
321
- }
322
- }
323
- return (key, relatedItems)
324
- } catch UtilsSQLStatementError.addPrefixToWhereClause(let message) {
325
- throw UtilsDeleteError
326
- .searchForRelatedItems(message: message)
295
+ do {
296
+ var whereClause = try UtilsSQLStatement
297
+ .addPrefixToWhereClause(whStmt, from: colNames,
298
+ to: withRefsNames,
299
+ prefix: "t2.")
300
+ if whereClause.hasSuffix(";") {
301
+ whereClause = String(whereClause.dropLast())
302
+ }
303
+ let resultString = zip(t1Names, t2Names)
304
+ .map {"\($0) = \($1)" }
305
+ .joined(separator: " AND ")
306
+
307
+ let sql = "SELECT t1.rowid FROM \(updTableName) t1 " +
308
+ "JOIN \(tableName) t2 ON \(resultString) " +
309
+ "WHERE \(whereClause) AND t1.sql_deleted = 0;"
310
+ var vals = try UtilsSQLCipher.querySQL(mDB: mDB, sql: sql,
311
+ values: values)
312
+ if vals.count > 1 {
313
+ if let mVals = vals[0]["ios_columns"] as? [String] {
314
+ key = mVals[0]
315
+ let keyToRemove = "ios_columns"
316
+ vals.removeAll { dict in
317
+ return dict.keys.contains(keyToRemove)
318
+ }
319
+ for val in vals {
320
+ relatedItems.append(val)
321
+ }
322
+ }
323
+ }
324
+ return (key, relatedItems)
325
+ } catch UtilsSQLStatementError.addPrefixToWhereClause(let message) {
326
+ throw UtilsDeleteError
327
+ .searchForRelatedItems(message: message)
327
328
  } catch UtilsSQLCipherError.querySQL(let message) {
328
- throw UtilsDeleteError
329
- .searchForRelatedItems(message: message)
329
+ throw UtilsDeleteError
330
+ .searchForRelatedItems(message: message)
330
331
  }
331
332
  }
332
333
  // swiftlint:enable function_parameter_count
@@ -337,14 +338,14 @@ class UtilsDelete {
337
338
  class func executeUpdateForDelete(mDB: Database, tableName: String,
338
339
  whereStmt: String, setStmt: String,
339
340
  colNames: [String], values: [Any])
340
- throws {
341
+ throws {
341
342
  var lastId: Int64 = -1
342
- //update sql_deleted for this references
343
+ //update sql_deleted for this references
343
344
  let stmt = "UPDATE \(tableName) SET \(setStmt) \(whereStmt)"
344
345
  var selValues: [Any] = []
345
346
  if !values.isEmpty {
346
347
  var arrVal: [String] = whereStmt
347
- .components(separatedBy: "?")
348
+ .components(separatedBy: "?")
348
349
  if arrVal[arrVal.count - 1] == ";" {
349
350
  arrVal.removeLast()
350
351
  }
@@ -359,8 +360,8 @@ class UtilsDelete {
359
360
  }
360
361
 
361
362
  let resp = try UtilsSQLCipher.prepareSQL(mDB: mDB, sql: stmt,
362
- values: selValues,
363
- fromJson: false, returnMode: "no")
363
+ values: selValues,
364
+ fromJson: false, returnMode: "no")
364
365
  lastId = resp.0
365
366
  if lastId == -1 {
366
367
  let msg = "UPDATE sql_deleted failed for " +
@@ -378,16 +379,16 @@ class UtilsDelete {
378
379
  return Int(currentTime)
379
380
  }
380
381
 
381
- // MARK: - checkValuesMatch
382
+ // MARK: - checkValuesMatch
382
383
 
383
- class func checkValuesMatch(_ array1: [String],
384
- against array2: [String]) -> Bool {
385
- for value in array1 {
386
- if !array2.contains(value) {
387
- return false
388
- }
389
- }
390
- return true
384
+ class func checkValuesMatch(_ array1: [String],
385
+ against array2: [String]) -> Bool {
386
+ for value in array1 {
387
+ if !array2.contains(value) {
388
+ return false
389
+ }
390
+ }
391
+ return true
391
392
  }
392
393
 
393
394
  // MARK: - getReferences
@@ -402,7 +403,7 @@ class UtilsDelete {
402
403
  sqlStmt += "sql LIKE('%ON DELETE%');"
403
404
  do {
404
405
  var references: [[String: Any]] = try UtilsSQLCipher
405
- .querySQL(mDB: mDB, sql: sqlStmt,values: [])
406
+ .querySQL(mDB: mDB, sql: sqlStmt, values: [])
406
407
  var retRefs = [String]()
407
408
  var tableWithRefs: String = ""
408
409
  if references.count > 1 {
@@ -426,7 +427,7 @@ class UtilsDelete {
426
427
  // MARK: - getRefs
427
428
 
428
429
  class func getRefs(sqlStatement: String)
429
- throws -> (tableName: String, foreignKeys: [String]) {
430
+ throws -> (tableName: String, foreignKeys: [String]) {
430
431
  var tableName = ""
431
432
  var foreignKeys = [String]()
432
433
  let statement = UtilsSQLStatement
@@ -440,33 +441,33 @@ class UtilsDelete {
440
441
  if let tableNameMatch = tableNameRegex
441
442
  .firstMatch(in: statement, options: [],
442
443
  range: NSRange(location: 0,
443
- length: statement.utf16.count)) {
444
+ length: statement.utf16.count)) {
444
445
  let tableNameRange = Range(tableNameMatch.range(
445
- at: 1), in: statement)!
446
+ at: 1), in: statement)!
446
447
  tableName = String(statement[tableNameRange])
447
448
  }
448
449
 
449
450
  // Regular expression pattern to match the FOREIGN KEY
450
451
  // constraints
451
452
  // swiftlint:disable line_length
452
- let foreignKeyPattern = #"FOREIGN\s+KEY\s+\([^)]+\)\s+REFERENCES\s+(\w+)\s*\([^)]+\)\s+ON\s+DELETE\s+(CASCADE|RESTRICT|SET\s+DEFAULT|SET\s+NULL|NO\s+ACTION)"#
453
+ let foreignKeyPattern = #"FOREIGN\s+KEY\s+\([^)]+\)\s+REFERENCES\s+(\w+)\s*\([^)]+\)\s+ON\s+DELETE\s+(CASCADE|RESTRICT|SET\s+DEFAULT|SET\s+NULL|NO\s+ACTION)"#
453
454
  // swiftlint:enable line_length
454
455
 
455
456
  let foreignKeyRegex = try NSRegularExpression(
456
- pattern: foreignKeyPattern, options: [])
457
+ pattern: foreignKeyPattern, options: [])
457
458
  let foreignKeyMatches = foreignKeyRegex
458
- .matches(in: statement, options: [],
459
- range: NSRange(location: 0,
460
- length: statement.utf16.count))
459
+ .matches(in: statement, options: [],
460
+ range: NSRange(location: 0,
461
+ length: statement.utf16.count))
461
462
  for foreignKeyMatch in foreignKeyMatches {
462
- let foreignKeyRange = Range(
463
+ let foreignKeyRange = Range(
463
464
  foreignKeyMatch.range(at: 0), in: statement)!
464
- let foreignKey = String(statement[foreignKeyRange])
465
- foreignKeys.append(foreignKey)
465
+ let foreignKey = String(statement[foreignKeyRange])
466
+ foreignKeys.append(foreignKey)
466
467
  }
467
468
  } catch {
468
469
  let msg = "getRefs: Error creating regular expression: " +
469
- "\(error)"
470
+ "\(error)"
470
471
  throw UtilsDeleteError.getRefs(message: msg)
471
472
  }
472
473
  return (tableName, foreignKeys)
@@ -477,12 +478,12 @@ class UtilsDelete {
477
478
  class func getUpdDelReturnedValues(mDB: Database,
478
479
  sqlStmt: String,
479
480
  names: String )
480
- throws -> [[String: Any]] {
481
+ throws -> [[String: Any]] {
481
482
  var result: [[String: Any]] = []
482
483
  let tableName = UtilsSQLStatement
483
- .extractTableName(from: sqlStmt)
484
+ .extractTableName(from: sqlStmt)
484
485
  let whereClause = UtilsSQLStatement
485
- .extractWhereClause(from: sqlStmt)
486
+ .extractWhereClause(from: sqlStmt)
486
487
  if let tblName = tableName {
487
488
  if var wClause = whereClause {
488
489
  if wClause.suffix(1) == ";" {
@@ -490,13 +491,13 @@ class UtilsDelete {
490
491
  }
491
492
  do {
492
493
  var query: String = "SELECT \(names) FROM " +
493
- "\(tblName) WHERE "
494
+ "\(tblName) WHERE "
494
495
  query += "\(wClause);"
495
496
  result = try UtilsSQLCipher
496
497
  .querySQL(mDB: mDB, sql: query, values: [])
497
498
  } catch UtilsSQLCipherError.querySQL(let message) {
498
499
  throw UtilsDeleteError
499
- .getUpdDelReturnedValues(message: message)
500
+ .getUpdDelReturnedValues(message: message)
500
501
  }
501
502
  }
502
503
  }
@@ -504,3 +505,4 @@ class UtilsDelete {
504
505
  }
505
506
  }
506
507
  // swiftlint:enable type_body_length
508
+ // swiftlint:enable file_length
@@ -633,6 +633,7 @@ class UtilsSQLCipher {
633
633
 
634
634
  // MARK: - deleteSQL
635
635
 
636
+ // swiftlint:disable function_body_length
636
637
  class func deleteSQL(mDB: Database, sql: String, values: [Any])
637
638
  throws -> String {
638
639
  var sqlStmt = sql
@@ -657,9 +658,9 @@ class UtilsSQLCipher {
657
658
  "WHERE clause"
658
659
  throw UtilsSQLCipherError.deleteSQL(message: msg)
659
660
  }
660
- guard let colNames = try? UtilsSQLStatement
661
- .extractColumnNames(from: whereClause)
662
- else {
661
+ let colNames = UtilsSQLStatement
662
+ .extractColumnNames(from: whereClause)
663
+ if colNames.count == 0 {
663
664
  let msg = "Did not find column names in the" +
664
665
  "WHERE Statement"
665
666
  throw UtilsSQLCipherError.deleteSQL(message: msg)
@@ -695,6 +696,7 @@ class UtilsSQLCipher {
695
696
  throw UtilsSQLCipherError.deleteSQL(message: message)
696
697
  }
697
698
  }
699
+ // swiftlint:enable function_body_length
698
700
 
699
701
  // MARK: - querySQL
700
702
 
@@ -835,6 +837,7 @@ class UtilsSQLCipher {
835
837
 
836
838
  // MARK: - Execute
837
839
 
840
+ // swiftlint:disable function_body_length
838
841
  class func execute(mDB: Database, sql: String) throws {
839
842
  var msg: String = "Error execute: "
840
843
  if !mDB.isDBOpen() {
@@ -886,6 +889,7 @@ class UtilsSQLCipher {
886
889
  }
887
890
  return
888
891
  }
892
+ // swiftlint:enable function_body_length
889
893
 
890
894
  // MARK: - DeleteDB
891
895
 
@@ -917,6 +921,7 @@ class UtilsSQLCipher {
917
921
  // MARK: - ExecuteSet
918
922
 
919
923
  // swiftlint:disable function_body_length
924
+ // swiftlint:disable cyclomatic_complexity
920
925
  class func executeSet(mDB: Database, set: [[String: Any]],
921
926
  returnMode: String)
922
927
  throws -> (Int64, [[String: Any]]) {
@@ -986,6 +991,8 @@ class UtilsSQLCipher {
986
991
  message: message)
987
992
  }
988
993
  }
994
+ // swiftlint:enable cyclomatic_complexity
995
+ // swiftlint:enable function_body_length
989
996
 
990
997
  class func addToResponse(response: [[String: Any]],
991
998
  respSet: [[String: Any]]) -> [[String: Any]] {