@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
package/CHANGELOG.md CHANGED
@@ -1,3 +1,33 @@
1
+ ## 3.2.2 (2021-09-17)
2
+
3
+ ### Bug Fixes
4
+
5
+ - Update API.md databases location for Web platform
6
+
7
+ ## 3.2.2-3 (2021-09-15)
8
+
9
+ ### Bug Fixes
10
+
11
+ - Fix Web Platform, add componentOnReady on constructor
12
+
13
+ ## 3.2.2-2 (2021-09-15)
14
+
15
+ ### Bug Fixes
16
+
17
+ - Fix All Platforms, executeSet breaks with empty array issue#170
18
+
19
+ ## 3.2.2-1 (2021-09-11)
20
+
21
+ ### Chore
22
+
23
+ - Update to @capacitor/core 3.2.2
24
+ - Update to @capacitor/ios 3.2.2
25
+ - Update to @capacitor/android 3.2.2
26
+
27
+ ### Added Features
28
+
29
+ - Add Views in JsonSQLite (issue#167)
30
+
1
31
  ## 3.2.0 (2021-08-31)
2
32
 
3
33
  ### Added Features
package/README.md CHANGED
@@ -28,6 +28,18 @@
28
28
 
29
29
  ## CAPACITOR 3 (Master)
30
30
 
31
+ 🚨 Since release 3.2.2-2 ->> 🚨
32
+
33
+ The executeSet method accepts now no values, see below
34
+
35
+ ```
36
+ const setIssue170: Array<capSQLiteSet> = [
37
+ { statement: "DROP TABLE IF EXISTS issue170", values: [] },
38
+ { statement: "CREATE TABLE issue170 (src VARCHAR(255))", values: [] },
39
+ { statement: "INSERT INTO issue170 (src) values (?)", values: ["google.com"] },
40
+ ]
41
+ ```
42
+
31
43
  🚨 Since release 3.2.0-5 ->> 🚨
32
44
 
33
45
  The Web plugin is now implemented based on the stencil companion `jeep-sqlite@0.0.7` which is using `sql.js@1.5.0` for database queries and `localeforage@1.9.0`for database persistency.
@@ -339,8 +351,9 @@ No configuration required for this plugin
339
351
 
340
352
  ## Dependencies
341
353
 
342
- The IOS and Android codes are using `SQLCipher` allowing for database encryption
343
- The Electron code is using `sqlite3`
354
+ The IOS and Android codes are using `SQLCipher` allowing for database encryption.
355
+ The Electron code is using `sqlite3`.
356
+ The Web code is using the Stencil component `jeep-sqlite` based on `sql.js` and `localforage`.
344
357
 
345
358
  ## Contributors ✨
346
359
 
@@ -687,7 +687,7 @@ public class CapacitorSQLite {
687
687
  try {
688
688
  JSObject ret = db.exportToJson(expMode);
689
689
 
690
- if (ret.length() == 5) {
690
+ if (ret.length() == 5 || ret.length() == 6) {
691
691
  return ret;
692
692
  } else {
693
693
  String msg = "ExportToJson: return Obj is not a JsonSQLite Obj";
@@ -329,7 +329,7 @@ public class Database {
329
329
  for (int j = 0; j < valuesJson.length(); j++) {
330
330
  values.add(valuesJson.get(j));
331
331
  }
332
- Boolean isArray = _uSqlite.parse(values.get(0));
332
+ Boolean isArray = values.size() > 0 ? _uSqlite.parse(values.get(0)) : false;
333
333
  if (isArray) {
334
334
  for (int j = 0; j < values.size(); j++) {
335
335
  JSONArray valsJson = (JSONArray) values.get(j);
@@ -603,12 +603,17 @@ public class Database {
603
603
  public JSObject importFromJson(JsonSQLite jsonSQL) throws Exception {
604
604
  Log.d(TAG, "importFromJson: ");
605
605
  JSObject retObj = new JSObject();
606
- int changes = Integer.valueOf(-1);
606
+ int changes = Integer.valueOf(0);
607
607
  try {
608
- // create the database schema
609
- changes = fromJson.createDatabaseSchema(this, jsonSQL);
610
- if (changes != -1) {
611
- changes = fromJson.createDatabaseData(this, jsonSQL);
608
+ if (jsonSQL.getTables().size() > 0) {
609
+ // create the database schema
610
+ changes = fromJson.createDatabaseSchema(this, jsonSQL);
611
+ if (changes != -1) {
612
+ changes += fromJson.createDatabaseData(this, jsonSQL);
613
+ }
614
+ }
615
+ if (jsonSQL.getViews().size() > 0) {
616
+ changes += fromJson.createViews(this, jsonSQL.getViews());
612
617
  }
613
618
  retObj.put("changes", changes);
614
619
  return retObj;
@@ -640,6 +645,9 @@ public class Database {
640
645
  retObj.put("encrypted", retJson.getEncrypted());
641
646
  retObj.put("mode", retJson.getMode());
642
647
  retObj.put("tables", retJson.getTablesAsJSObject());
648
+ if (keys.contains("views") && retJson.getViews().size() > 0) {
649
+ retObj.put("views", retJson.getViewsAsJSObject());
650
+ }
643
651
  }
644
652
  }
645
653
  } catch (Exception e) {
@@ -40,13 +40,30 @@ public class ExportToJson {
40
40
  */
41
41
  public JsonSQLite createExportObject(Database db, JsonSQLite sqlObj) throws Exception {
42
42
  JsonSQLite retObj = new JsonSQLite();
43
-
44
- String stmt = "SELECT name,sql FROM sqlite_master WHERE ";
45
- stmt += "type = 'table' AND name NOT LIKE 'sqlite_%' AND ";
46
- stmt += "name NOT LIKE 'android_%' AND ";
47
- stmt += "name NOT LIKE 'sync_table';";
43
+ ArrayList<JsonView> views = new ArrayList<>();
48
44
  ArrayList<JsonTable> tables = new ArrayList<>();
49
45
  try {
46
+ // Get Views
47
+ String stmtV = "SELECT name,sql FROM sqlite_master WHERE ";
48
+ stmtV += "type = 'view' AND name NOT LIKE 'sqlite_%';";
49
+ JSArray resViews = db.selectSQL(stmtV, new ArrayList<Object>());
50
+ if (resViews.length() > 0) {
51
+ for (int i = 0; i < resViews.length(); i++) {
52
+ JSONObject oView = resViews.getJSONObject(i);
53
+ JsonView v = new JsonView();
54
+ String val = (String) oView.get("sql");
55
+ val = val.substring(val.indexOf("AS ") + 3);
56
+ v.setName((String) oView.get("name"));
57
+ v.setValue(val);
58
+ views.add(v);
59
+ }
60
+ }
61
+ // Get Tables
62
+ String stmt = "SELECT name,sql FROM sqlite_master WHERE ";
63
+ stmt += "type = 'table' AND name NOT LIKE 'sqlite_%' AND ";
64
+ stmt += "name NOT LIKE 'android_%' AND ";
65
+ stmt += "name NOT LIKE 'sync_table';";
66
+
50
67
  JSArray resTables = db.selectSQL(stmt, new ArrayList<Object>());
51
68
  if (resTables.length() == 0) {
52
69
  throw new Exception("CreateExportObject: table's names failed");
@@ -71,6 +88,9 @@ public class ExportToJson {
71
88
  retObj.setEncrypted(sqlObj.getEncrypted());
72
89
  retObj.setMode(sqlObj.getMode());
73
90
  retObj.setTables(tables);
91
+ if (views.size() > 0) {
92
+ retObj.setViews(views);
93
+ }
74
94
  }
75
95
  return retObj;
76
96
  }
@@ -465,4 +465,47 @@ public class ImportFromJson {
465
465
  }
466
466
  return stmt;
467
467
  }
468
+
469
+ /**
470
+ * Create from the Json Object the database views
471
+ * @param mDb
472
+ * @param views
473
+ * @return
474
+ * @throws Exception
475
+ */
476
+ public Integer createViews(Database mDb, ArrayList<JsonView> views) throws Exception {
477
+ int changes = Integer.valueOf(0);
478
+ SupportSQLiteDatabase db = mDb.getDb();
479
+ try {
480
+ if (mDb != null && mDb.isOpen() && views.size() > 0) {
481
+ db.beginTransaction();
482
+ // Create Views
483
+ Integer initChanges = _uSqlite.dbChanges(db);
484
+ for (JsonView view : views) {
485
+ if (view.getName().length() > 0 && view.getValue().length() > 0) {
486
+ StringBuilder sBuilder = new StringBuilder("CREATE VIEW IF NOT EXISTS ")
487
+ .append(view.getName())
488
+ .append(" AS ")
489
+ .append(view.getValue())
490
+ .append(" ;");
491
+ String stmt = sBuilder.toString();
492
+ db.execSQL(stmt);
493
+ } else {
494
+ throw new Exception("CreateViews: no name and value");
495
+ }
496
+ }
497
+ changes = _uSqlite.dbChanges(db) - initChanges;
498
+ if (changes >= 0) {
499
+ db.setTransactionSuccessful();
500
+ }
501
+ } else {
502
+ throw new Exception("CreateViews: Database not opened");
503
+ }
504
+ } catch (Exception e) {
505
+ throw new Exception("CreateViews: " + e.getMessage());
506
+ } finally {
507
+ if (db != null && db.inTransaction()) db.endTransaction();
508
+ }
509
+ return changes;
510
+ }
468
511
  }
@@ -19,9 +19,10 @@ public class JsonSQLite {
19
19
  private Boolean encrypted = null;
20
20
  private String mode = "";
21
21
  private ArrayList<JsonTable> tables = new ArrayList<JsonTable>();
22
+ private ArrayList<JsonView> views = new ArrayList<JsonView>();
22
23
 
23
24
  private static final List<String> keyFirstLevel = new ArrayList<String>(
24
- Arrays.asList("database", "version", "encrypted", "mode", "tables")
25
+ Arrays.asList("database", "version", "encrypted", "mode", "tables", "views")
25
26
  );
26
27
 
27
28
  // Getter
@@ -45,6 +46,10 @@ public class JsonSQLite {
45
46
  return tables;
46
47
  }
47
48
 
49
+ public ArrayList<JsonView> getViews() {
50
+ return views;
51
+ }
52
+
48
53
  // Setter
49
54
  public void setDatabase(String newDatabase) {
50
55
  this.database = newDatabase;
@@ -66,6 +71,10 @@ public class JsonSQLite {
66
71
  this.tables = newTables;
67
72
  }
68
73
 
74
+ public void setViews(ArrayList<JsonView> newViews) {
75
+ this.views = newViews;
76
+ }
77
+
69
78
  public ArrayList<String> getKeys() {
70
79
  ArrayList<String> retArray = new ArrayList<String>();
71
80
  if (getDatabase().length() > 0) retArray.add("database");
@@ -73,6 +82,7 @@ public class JsonSQLite {
73
82
  if (getEncrypted() != null) retArray.add("encrypted");
74
83
  if (getMode().length() > 0) retArray.add("mode");
75
84
  if (getTables().size() > 0) retArray.add("tables");
85
+ if (getViews().size() > 0) retArray.add("views");
76
86
  return retArray;
77
87
  }
78
88
 
@@ -138,6 +148,23 @@ public class JsonSQLite {
138
148
  }
139
149
  }
140
150
  }
151
+ if (key.equals("views")) {
152
+ if (!(value instanceof JSONArray)) {
153
+ String msg = "value: not instance of JSONArray";
154
+ Log.d(TAG, msg);
155
+ return false;
156
+ } else {
157
+ JSONArray arrJS = jsObj.getJSONArray(key);
158
+ views = new ArrayList<>();
159
+
160
+ for (int i = 0; i < arrJS.length(); i++) {
161
+ JsonView view = new JsonView();
162
+ boolean retView = view.isView(arrJS.getJSONObject(i));
163
+ if (!retView) return false;
164
+ views.add(view);
165
+ }
166
+ }
167
+ }
141
168
  } catch (JSONException e) {
142
169
  e.printStackTrace();
143
170
  return false;
@@ -155,6 +182,12 @@ public class JsonSQLite {
155
182
  for (JsonTable table : this.getTables()) {
156
183
  table.print();
157
184
  }
185
+ if (this.getViews().size() > 0) {
186
+ Log.d(TAG, "number of Views: " + this.getViews().size());
187
+ for (JsonView view : this.getViews()) {
188
+ view.print();
189
+ }
190
+ }
158
191
  }
159
192
 
160
193
  public JSArray getTablesAsJSObject() {
@@ -164,4 +197,12 @@ public class JsonSQLite {
164
197
  }
165
198
  return JSTables;
166
199
  }
200
+
201
+ public JSArray getViewsAsJSObject() {
202
+ JSArray JSViews = new JSArray();
203
+ for (JsonView view : this.views) {
204
+ JSViews.put(view.getViewAsJSObject());
205
+ }
206
+ return JSViews;
207
+ }
167
208
  }
@@ -0,0 +1,85 @@
1
+ package com.getcapacitor.community.database.sqlite.SQLite.ImportExportJson;
2
+
3
+ import android.util.Log;
4
+ import com.getcapacitor.JSObject;
5
+ import java.util.ArrayList;
6
+ import java.util.Arrays;
7
+ import java.util.Iterator;
8
+ import java.util.List;
9
+ import org.json.JSONException;
10
+ import org.json.JSONObject;
11
+
12
+ public class JsonView {
13
+
14
+ private static final String TAG = "JsonView";
15
+ private static final List<String> keyViewsLevel = new ArrayList<String>(Arrays.asList("name", "value"));
16
+ private String name = "";
17
+ private String value = "";
18
+
19
+ // Getter
20
+ public String getName() {
21
+ return name;
22
+ }
23
+
24
+ public String getValue() {
25
+ return value;
26
+ }
27
+
28
+ // Setter
29
+ public void setName(String newName) {
30
+ this.name = newName;
31
+ }
32
+
33
+ public void setValue(String newValue) {
34
+ this.value = newValue;
35
+ }
36
+
37
+ public ArrayList<String> getKeys() {
38
+ ArrayList<String> retArray = new ArrayList<String>();
39
+ if (getName().length() > 0) retArray.add("name");
40
+ if (getValue().length() > 0) retArray.add("value");
41
+ return retArray;
42
+ }
43
+
44
+ public boolean isView(JSONObject jsObj) {
45
+ if (jsObj == null || jsObj.length() == 0) return false;
46
+ Iterator<String> keys = jsObj.keys();
47
+ while (keys.hasNext()) {
48
+ String key = keys.next();
49
+ if (!keyViewsLevel.contains(key)) return false;
50
+ try {
51
+ Object objValue = jsObj.get(key);
52
+ if (key.equals("name")) {
53
+ if (!(objValue instanceof String)) {
54
+ return false;
55
+ } else {
56
+ name = (String) objValue;
57
+ }
58
+ }
59
+ if (key.equals("value")) {
60
+ if (!(objValue instanceof String)) {
61
+ return false;
62
+ } else {
63
+ value = (String) objValue;
64
+ }
65
+ }
66
+ } catch (JSONException e) {
67
+ e.printStackTrace();
68
+ return false;
69
+ }
70
+ }
71
+ return true;
72
+ }
73
+
74
+ public void print() {
75
+ String toPrint = "name: " + this.getName() + " value: " + this.getValue();
76
+ Log.d(TAG, toPrint);
77
+ }
78
+
79
+ public JSObject getViewAsJSObject() {
80
+ JSObject retObj = new JSObject();
81
+ retObj.put("name", this.name);
82
+ retObj.put("value", this.value);
83
+ return retObj;
84
+ }
85
+ }
@@ -15,6 +15,7 @@ public class UtilsJson {
15
15
  private JsonColumn uJCol = new JsonColumn();
16
16
  private JsonIndex uJIdx = new JsonIndex();
17
17
  private JsonTrigger uJTrg = new JsonTrigger();
18
+ private JsonView uJView = new JsonView();
18
19
 
19
20
  /**
20
21
  * Check if a table exists
@@ -37,6 +38,27 @@ public class UtilsJson {
37
38
  }
38
39
  }
39
40
 
41
+ /**
42
+ * Check if a view exists
43
+ * @param db
44
+ * @param viewName
45
+ * @return
46
+ */
47
+ public boolean isViewExists(Database db, String viewName) throws Exception {
48
+ boolean ret = false;
49
+ String query = new StringBuilder("SELECT name FROM " + "sqlite_master WHERE type='view' AND name='")
50
+ .append(viewName)
51
+ .append("';")
52
+ .toString();
53
+ try {
54
+ JSArray resQuery = db.selectSQL(query, new ArrayList<Object>());
55
+ if (resQuery.length() > 0) ret = true;
56
+ return ret;
57
+ } catch (Exception e) {
58
+ throw new Exception("isViewExists: " + e.getMessage());
59
+ }
60
+ }
61
+
40
62
  /**
41
63
  * Check if the Id already exsists
42
64
  * @param mDb
@@ -338,4 +360,27 @@ public class UtilsJson {
338
360
  }
339
361
  return;
340
362
  }
363
+
364
+ /**
365
+ * Check Views Validity
366
+ * @param views
367
+ * @throws Exception
368
+ */
369
+ public void checkViewsValidity(ArrayList<JsonView> views) throws Exception {
370
+ for (int i = 0; i < views.size(); i++) {
371
+ JSONObject jsView = new JSONObject();
372
+ ArrayList<String> keys = views.get(i).getKeys();
373
+ if (keys.contains("value")) {
374
+ jsView.put("value", views.get(i).getValue());
375
+ }
376
+ if (keys.contains("name")) {
377
+ jsView.put("name", views.get(i).getName());
378
+ }
379
+ boolean isValid = uJView.isView(jsView);
380
+ if (!isValid) {
381
+ throw new Exception("checkViewsValidity: views[" + i + "] not valid");
382
+ }
383
+ }
384
+ return;
385
+ }
341
386
  }