@capacitor-community/sqlite 5.0.7-2 → 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 +600 -177
  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
@@ -7,13 +7,15 @@
7
7
 
8
8
  import Foundation
9
9
  enum UtilsSQLStatementError: Error {
10
- case extractColumnNames(message:String)
11
- case extractForeignKeyInfo(message:String)
12
- case addPrefixToWhereClause(message:String)
10
+ case extractColumnNames(message: String)
11
+ case extractForeignKeyInfo(message: String)
12
+ case addPrefixToWhereClause(message: String)
13
13
  }
14
14
 
15
+ // swiftlint:disable file_length
16
+ // swiftlint:disable type_body_length
15
17
  class UtilsSQLStatement {
16
-
18
+
17
19
  // MARK: - extractTableName
18
20
 
19
21
  class func extractTableName(from statement: String) -> String? {
@@ -38,7 +40,7 @@ class UtilsSQLStatement {
38
40
  }
39
41
 
40
42
  // MARK: - extractWhereClause
41
-
43
+
42
44
  class func extractWhereClause(from statement: String) -> String? {
43
45
  let pattern = "WHERE(.+?)(?:ORDER\\s+BY|LIMIT|$)"
44
46
  guard let regex = try? NSRegularExpression(
@@ -63,55 +65,55 @@ class UtilsSQLStatement {
63
65
 
64
66
  return nil
65
67
  }
66
-
68
+
67
69
  // MARK: - addPrefixToWhereClause
68
-
69
- class func addPrefixToWhereClause(_ whereClause: String, from: [String],
70
- to: [String],prefix: String)
71
- throws -> String {
70
+
71
+ class func addPrefixToWhereClause(_ whereClause: String,
72
+ from: [String],
73
+ to: [String], prefix: String)
74
+ throws -> String {
72
75
  var columnValuePairs: [String]
73
76
  if whereClause.contains("AND") {
74
77
  if #available(iOS 16.0, *) {
75
- let subSequenceArray = whereClause
76
- .split(separator: "AND")
78
+ let subSequenceArray = whereClause.split(separator: "AND")
77
79
  columnValuePairs = subSequenceArray.map { String($0) }
78
80
  } else {
79
81
  columnValuePairs = whereClause
80
- .components(separatedBy: "AND")
82
+ .components(separatedBy: "AND")
81
83
  }
82
84
  } else {
83
85
  columnValuePairs = [whereClause]
84
86
  }
85
87
  let modifiedPairs = try columnValuePairs.map { pair -> String in
86
88
  let pattern = #"(\w+)\s*(=|IN|BETWEEN|LIKE)\s*(.+)"#
87
-
89
+
88
90
  if let range = pair.range(of: pattern, options: .regularExpression) {
89
91
  let match = String(pair[range])
90
92
  let regex = try NSRegularExpression(pattern: pattern)
91
93
  let matchRange = NSRange(match.startIndex..., in: match)
92
-
94
+
93
95
  if let matchResult = regex.firstMatch(in: match, range: matchRange) {
94
96
  let columnRange = Range(matchResult.range(at: 1), in: match)!
95
97
  let operatorRange = Range(matchResult.range(at: 2), in: match)!
96
98
  let valueRange = Range(matchResult.range(at: 3), in: match)!
97
-
99
+
98
100
  let column = String(match[columnRange]).trimmingCharacters(in: .whitespacesAndNewlines)
99
101
  let mOperator = String(match[operatorRange]).trimmingCharacters(in: .whitespacesAndNewlines)
100
102
  let value = String(match[valueRange]).trimmingCharacters(in: .whitespacesAndNewlines)
101
-
103
+
102
104
  var newColumn = column
103
105
  if let index = UtilsSQLStatement
104
106
  .findIndexOfStringInArray(column, to), index != -1 {
105
107
  guard let mNewColumn = UtilsSQLStatement
106
- .getStringAtIndex(from, index) else {
108
+ .getStringAtIndex(from, index) else {
107
109
  let msg = "addPrefixToWhereClause: index " +
108
- "mistmatch "
110
+ "mistmatch "
109
111
  throw UtilsSQLStatementError
110
- .addPrefixToWhereClause(message: msg)
112
+ .addPrefixToWhereClause(message: msg)
111
113
  }
112
114
  newColumn = mNewColumn
113
115
  }
114
-
116
+
115
117
  let modifiedColumn = "\(prefix)\(newColumn)"
116
118
  return "\(modifiedColumn) \(mOperator) \(value)"
117
119
  }
@@ -119,17 +121,17 @@ class UtilsSQLStatement {
119
121
  return pair
120
122
  }
121
123
  return modifiedPairs.joined(separator: " AND ")
122
-
124
+
123
125
  }
124
-
126
+
125
127
  // MARK: - findIndexOfStringInArray
126
-
128
+
127
129
  class func findIndexOfStringInArray(_ target: String, _ array: [String]) -> Int? {
128
130
  return array.firstIndex(of: target)
129
131
  }
130
-
132
+
131
133
  // MARK: - getStringAtIndex
132
-
134
+
133
135
  class func getStringAtIndex(_ array: [String], _ index: Int) -> String? {
134
136
  if index >= 0 && index < array.count {
135
137
  return array[index]
@@ -138,10 +140,11 @@ class UtilsSQLStatement {
138
140
  }
139
141
  }
140
142
  // MARK: - extractForeignKeyInfo
141
-
143
+
144
+ // swiftlint:enable type_body_length
142
145
  class func extractForeignKeyInfo(from sqlStatement: String)
143
- throws ->
144
- [String: Any] {
146
+ throws ->
147
+ [String: Any] {
145
148
  var foreignKeyInfo: [String: Any] = [:]
146
149
  // Define the regular expression patterns for extracting the
147
150
  // FOREIGN KEY clause and composite primary keys
@@ -151,9 +154,9 @@ class UtilsSQLStatement {
151
154
  guard let regex = try? NSRegularExpression(
152
155
  pattern: foreignKeyPattern, options: []) else {
153
156
  let msg = "extractForeignKeyInfo: creating regular " +
154
- "expression pattern"
157
+ "expression pattern"
155
158
  throw UtilsSQLStatementError
156
- .extractForeignKeyInfo(message: msg)
159
+ .extractForeignKeyInfo(message: msg)
157
160
 
158
161
  }
159
162
 
@@ -168,8 +171,8 @@ class UtilsSQLStatement {
168
171
  if let foreignKeyColumnsRangeInString =
169
172
  Range(foreignKeyColumnsRange, in: sqlStatement) {
170
173
  let foreignKeyColumns =
171
- String(sqlStatement[foreignKeyColumnsRangeInString])
172
- .components(separatedBy: ", ")
174
+ String(sqlStatement[foreignKeyColumnsRangeInString])
175
+ .components(separatedBy: ", ")
173
176
 
174
177
  // Extract the referenced table and columns
175
178
  let referencedTableRange = match.range(at: 2)
@@ -207,13 +210,13 @@ class UtilsSQLStatement {
207
210
  } else {
208
211
  let msg = "extractForeignKeyInfo: No match found"
209
212
  throw UtilsSQLStatementError
210
- .extractForeignKeyInfo(message: msg)
213
+ .extractForeignKeyInfo(message: msg)
211
214
  }
212
215
  }
213
216
  } else {
214
217
  let msg = "extractForeignKeyInfo: No FOREIGN KEY found"
215
218
  throw UtilsSQLStatementError
216
- .extractForeignKeyInfo(message: msg)
219
+ .extractForeignKeyInfo(message: msg)
217
220
  }
218
221
  foreignKeyInfo["forKeys"] = []
219
222
  foreignKeyInfo["tableName"] = ""
@@ -221,41 +224,36 @@ class UtilsSQLStatement {
221
224
  foreignKeyInfo["action"] = "NO_ACTION"
222
225
  return foreignKeyInfo
223
226
  }
224
-
227
+ // swiftlint:enable type_body_length
225
228
 
226
229
  // MARK: - extractColumnNames
227
-
228
- class func extractColumnNames(from whereClause: String)
229
- throws -> [String] {
230
- let regexPattern = #"\b(\w+)\s*(?=(?:[=<>]|IN\s*\(VALUES\s*|\bBETWEEN\b|\bLIKE\s*'))|(?<=\()\s*(\w+),\s*(\w+)\s*(?=\))"#
231
-
232
- do {
233
-
234
- let regex = try NSRegularExpression(pattern: regexPattern)
235
- let matches = regex.matches(in: whereClause, range: NSRange(whereClause.startIndex..., in: whereClause))
236
- var columnNames: [String] = []
237
-
238
- for match in matches {
239
- for rangeIdx in 1..<match.numberOfRanges {
240
- let range = match.range(at: rangeIdx)
241
- if range.location != NSNotFound, let columnNameRange = Range(range, in: whereClause) {
242
- let columnName = String(whereClause[columnNameRange])
243
- columnNames.append(columnName)
244
- }
245
- }
246
- }
247
230
 
248
- return columnNames
231
+ class func extractColumnNames(from whereClause: String) -> [String] {
232
+ let keywords: Set<String> = ["AND", "OR", "IN", "VALUES", "LIKE", "BETWEEN", "NOT"]
233
+ let tokens = whereClause.components(separatedBy: CharacterSet(charactersIn: " ,()"))
234
+
235
+ var columns = [String]()
236
+ var inClause = false
237
+ var inValues = false
249
238
 
250
- } catch {
251
- let msg = "Error creating regular expression: " +
252
- "\(error.localizedDescription)"
253
- throw UtilsSQLStatementError.extractColumnNames(message: msg)
239
+ for token in tokens {
240
+ if token == "IN" {
241
+ inClause = true
242
+ } else if inClause && token == "(" {
243
+ inValues = true
244
+ } else if inValues && token == ")" {
245
+ inValues = false
246
+ } else if token.range(of: "\\b[a-zA-Z]\\w*\\b", options: .regularExpression) != nil
247
+ && !inValues && !keywords.contains(token.uppercased()) {
248
+ columns.append(token)
249
+ }
254
250
  }
251
+
252
+ return Array(Set(columns))
255
253
  }
256
-
254
+
257
255
  // MARK: - flattenMultilineString
258
-
256
+
259
257
  class func flattenMultilineString(_ input: String) -> String {
260
258
  let lines = input
261
259
  .components(separatedBy: CharacterSet.newlines)
@@ -265,7 +263,7 @@ class UtilsSQLStatement {
265
263
  // MARK: - getStmtAndRetColNames
266
264
 
267
265
  class func getStmtAndRetColNames(sqlStmt: String, retMode: String)
268
- -> [String: String] {
266
+ -> [String: String] {
269
267
  let retWord = "RETURNING"
270
268
 
271
269
  var retStmtNames: [String: String] = [:]
@@ -291,12 +289,12 @@ class UtilsSQLStatement {
291
289
  // MARK: - extractCombinedPrimaryKey
292
290
 
293
291
  class func extractCombinedPrimaryKey(from whereClause: String)
294
- -> [[String]]? {
292
+ -> [[String]]? {
295
293
  // Regular expression pattern to match the combined primary
296
294
  // key comparison with IN operator or without it
297
295
  // meaning = operator
298
296
  let pattern = #"WHERE\s*\((.+?)\)\s*(?:=|IN)\s*\((.+?)\)"#
299
-
297
+
300
298
  guard let regex = try? NSRegularExpression(pattern: pattern,
301
299
  options: []) else {
302
300
  print("Invalid regular expression pattern.")
@@ -309,7 +307,7 @@ class UtilsSQLStatement {
309
307
  var primaryKeySets: [[String]] = []
310
308
 
311
309
  for match in matches {
312
-
310
+
313
311
  let keysRange = Range(match.range(at: 1), in: whereClause)!
314
312
  let keysString = String(whereClause[keysRange])
315
313
  let keys = keysString.split(separator: ",").map {
@@ -318,14 +316,14 @@ class UtilsSQLStatement {
318
316
  }
319
317
  return primaryKeySets.isEmpty ? nil : primaryKeySets
320
318
  }
321
-
319
+
322
320
  // MARK: - getWhereStatementForCombinedPK
323
321
 
324
322
  class func getWhereStmtForCombinedPK(whStmt: String,
325
323
  withRefs: [String],
326
324
  colNames: [String],
327
325
  keys: [[String]])
328
- -> String {
326
+ -> String {
329
327
  var retWhere: String = whStmt
330
328
  var repKeys: [String] = []
331
329
  for grpKeys in keys {
@@ -342,18 +340,18 @@ class UtilsSQLStatement {
342
340
  }
343
341
  return retWhere
344
342
  }
345
-
343
+
346
344
  // MARK: - replaceAllString
347
345
 
348
346
  class func replaceAllString(originalStr: String, searchStr: String,
349
347
  replaceStr: String) -> String {
350
348
  let modifiedStr = originalStr
351
- .replacingOccurrences(of: searchStr, with: replaceStr)
349
+ .replacingOccurrences(of: searchStr, with: replaceStr)
352
350
  return modifiedStr
353
351
  }
354
-
352
+
355
353
  // MARK: - replaceString
356
-
354
+
357
355
  class func replaceString(originalStr: String, searchStr: String,
358
356
  replaceStr: String) -> String {
359
357
  var modifiedStr = originalStr
@@ -362,13 +360,13 @@ class UtilsSQLStatement {
362
360
  }
363
361
  return modifiedStr
364
362
  }
365
-
363
+
366
364
  // MARK: - getWhereStmtForNonCombinedPK
367
365
 
368
366
  class func getWhereStmtForNonCombinedPK(whStmt: String,
369
367
  withRefs: [String],
370
368
  colNames: [String])
371
- -> String {
369
+ -> String {
372
370
  var whereStmt = ""
373
371
  var stmt: String = String(whStmt.stringRange(
374
372
  fromIdx: 6,
@@ -385,22 +383,22 @@ class UtilsSQLStatement {
385
383
  let indicesEqual: [Int] = stmt
386
384
  .indicesOf(string: "=",
387
385
  fromIdx: idxs[0])
388
-
386
+
389
387
  if indicesEqual.count > 0 {
390
388
  let indicesAnd: [Int] = stmt
391
389
  .indicesOf(string: "AND",
392
390
  fromIdx: indicesEqual[0])
393
391
  if indicesAnd.count > 0 {
394
392
  valStr = String(stmt.stringRange(
395
- fromIdx: indicesEqual[0] + 1,
396
- toIdx: indicesAnd[0] - 1))
393
+ fromIdx: indicesEqual[0] + 1,
394
+ toIdx: indicesAnd[0] - 1))
397
395
  stmt = String(stmt.stringRange(
398
- fromIdx: indicesAnd[0] + 3,
399
- toIdx: stmt.count))
396
+ fromIdx: indicesAnd[0] + 3,
397
+ toIdx: stmt.count))
400
398
  } else {
401
399
  valStr = String(stmt.stringRange(
402
- fromIdx: indicesEqual[0] + 1,
403
- toIdx: stmt.count))
400
+ fromIdx: indicesEqual[0] + 1,
401
+ toIdx: stmt.count))
404
402
  }
405
403
  if idx > 0 {
406
404
  whereStmt += " AND "
@@ -416,9 +414,9 @@ class UtilsSQLStatement {
416
414
  whereStmt = "WHERE " + whereStmt
417
415
  return whereStmt
418
416
  }
419
-
417
+
420
418
  // MARK: - updateWhere
421
-
419
+
422
420
  class func updateWhere(whStmt: String, withRefs: [String],
423
421
  colNames: [String]) -> String {
424
422
  var whereStmt = ""
@@ -448,3 +446,5 @@ class UtilsSQLStatement {
448
446
  return whereStmt
449
447
  }
450
448
  }
449
+ // swiftlint:enable type_body_length
450
+ // swiftlint:enable file_length
@@ -52,6 +52,7 @@ class UtilsUpgrade {
52
52
  do {
53
53
  do {
54
54
  try UtilsSQLCipher.beginTransaction(mDB: mDB)
55
+ mDB.setIsTransActive(newValue: true)
55
56
  } catch UtilsSQLCipherError.beginTransaction(let message) {
56
57
  throw DatabaseError.executeSQL(message: "Error: onUpgrade: \(message)")
57
58
  }
@@ -63,6 +64,7 @@ class UtilsUpgrade {
63
64
 
64
65
  do {
65
66
  try UtilsSQLCipher.commitTransaction(mDB: mDB)
67
+ mDB.setIsTransActive(newValue: false)
66
68
  } catch UtilsSQLCipherError.commitTransaction(let message) {
67
69
  throw DatabaseError.executeSQL(message: "Error: onUpgrade: \(message)")
68
70
  }
@@ -70,6 +72,7 @@ class UtilsUpgrade {
70
72
  } catch DatabaseError.executeSQL(let message) {
71
73
  do {
72
74
  try UtilsSQLCipher.rollbackTransaction(mDB: mDB)
75
+ mDB.setIsTransActive(newValue: false)
73
76
  } catch UtilsSQLCipherError.rollbackTransaction(let message2) {
74
77
  throw DatabaseError.executeSQL(message: "Error: onUpgrade: \(message) \(message2)")
75
78
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capacitor-community/sqlite",
3
- "version": "5.0.7-2",
3
+ "version": "5.0.7",
4
4
  "description": "Community plugin for native & electron SQLite databases",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
@@ -95,6 +95,6 @@
95
95
  }
96
96
  },
97
97
  "dependencies": {
98
- "jeep-sqlite": "^2.4.1"
98
+ "jeep-sqlite": "^2.5.1"
99
99
  }
100
100
  }