@capacitor-community/sqlite 5.4.0 → 5.4.2-1
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.
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java +92 -249
- package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ImportFromJson.java +99 -189
- package/electron/dist/plugin.js +0 -1
- package/electron/dist/plugin.js.map +1 -1
- package/package.json +5 -5
package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java
CHANGED
|
@@ -31,6 +31,9 @@ import java.util.Date;
|
|
|
31
31
|
import java.util.Dictionary;
|
|
32
32
|
import java.util.List;
|
|
33
33
|
import java.util.Objects;
|
|
34
|
+
import java.util.regex.Matcher;
|
|
35
|
+
import java.util.regex.Pattern;
|
|
36
|
+
|
|
34
37
|
import net.sqlcipher.Cursor;
|
|
35
38
|
import net.sqlcipher.database.SQLiteDatabase;
|
|
36
39
|
import net.sqlcipher.database.SQLiteException;
|
|
@@ -185,6 +188,8 @@ public class Database {
|
|
|
185
188
|
String msg = "Failed in commitTransaction" + e.getMessage();
|
|
186
189
|
Log.v(TAG, msg);
|
|
187
190
|
throw new Exception(msg);
|
|
191
|
+
} finally {
|
|
192
|
+
_db.endTransaction();
|
|
188
193
|
}
|
|
189
194
|
} else {
|
|
190
195
|
throw new Exception("Database not opened");
|
|
@@ -453,42 +458,30 @@ public class Database {
|
|
|
453
458
|
if (transaction) beginTransaction();
|
|
454
459
|
for (int i = 0; i < set.length(); i++) {
|
|
455
460
|
JSONObject row = set.getJSONObject(i);
|
|
461
|
+
JSObject respSet = new JSObject();
|
|
456
462
|
String statement = row.getString("statement");
|
|
457
463
|
JSONArray valuesJson = row.getJSONArray("values");
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
values.add(valuesJson.get(j));
|
|
461
|
-
}
|
|
462
|
-
Boolean isArray = values.size() > 0 ? _uSqlite.parse(values.get(0)) : false;
|
|
464
|
+
// optimize executeSet
|
|
465
|
+
Boolean isArray = valuesJson.length() > 0 ? _uSqlite.parse(valuesJson.get(0)) : false;
|
|
463
466
|
if (isArray) {
|
|
464
|
-
|
|
465
|
-
JSONArray valsJson = (JSONArray) values.get(j);
|
|
466
|
-
ArrayList<Object> vals = new ArrayList<>();
|
|
467
|
-
for (int k = 0; k < valsJson.length(); k++) {
|
|
468
|
-
vals.add(valsJson.get(k));
|
|
469
|
-
}
|
|
470
|
-
JSObject respSet = prepareSQL(statement, vals, false, returnMode);
|
|
471
|
-
lastId = respSet.getLong("lastId");
|
|
472
|
-
if (lastId == -1) break;
|
|
473
|
-
response = addToResponse(response, respSet);
|
|
474
|
-
}
|
|
467
|
+
respSet = multipleRowsStatement( statement, valuesJson, returnMode);
|
|
475
468
|
} else {
|
|
476
|
-
|
|
477
|
-
lastId = respSet.getLong("lastId");
|
|
478
|
-
if (lastId == -1) break;
|
|
479
|
-
response = addToResponse(response, respSet);
|
|
469
|
+
respSet = oneRowStatement( statement, valuesJson, returnMode);
|
|
480
470
|
}
|
|
471
|
+
lastId = respSet.getLong("lastId");
|
|
472
|
+
if (lastId.equals(-1L)) break;
|
|
473
|
+
response = addToResponse(response, respSet);
|
|
474
|
+
|
|
481
475
|
}
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
if (transaction) commitTransaction();
|
|
485
|
-
changes = _uSqlite.dbChanges(_db) - initChanges;
|
|
486
|
-
retObj.put("changes", changes);
|
|
487
|
-
retObj.put("lastId", lastId);
|
|
488
|
-
retObj.put("values", response.getJSONArray("values"));
|
|
489
|
-
return retObj;
|
|
476
|
+
if (lastId.equals(-1L)) {
|
|
477
|
+
throw new Exception("lastId equals -1");
|
|
490
478
|
} else {
|
|
491
|
-
|
|
479
|
+
if (transaction) commitTransaction();
|
|
480
|
+
changes = _uSqlite.dbChanges(_db) - initChanges;
|
|
481
|
+
retObj.put("changes", changes);
|
|
482
|
+
retObj.put("lastId", lastId);
|
|
483
|
+
retObj.put("values", response.getJSONArray("values"));
|
|
484
|
+
return retObj;
|
|
492
485
|
}
|
|
493
486
|
} else {
|
|
494
487
|
throw new Exception("Database not opened");
|
|
@@ -499,7 +492,77 @@ public class Database {
|
|
|
499
492
|
if (_db != null && transaction && _db.inTransaction()) rollbackTransaction();
|
|
500
493
|
}
|
|
501
494
|
}
|
|
495
|
+
public JSObject multipleRowsStatement( String statement, JSONArray valuesJson,
|
|
496
|
+
String returnMode) throws Exception {
|
|
497
|
+
StringBuilder sqlBuilder = new StringBuilder();
|
|
498
|
+
try {
|
|
499
|
+
for (int j = 0; j < valuesJson.length(); j++) {
|
|
500
|
+
JSONArray innerArray = valuesJson.getJSONArray(j);
|
|
501
|
+
StringBuilder innerSqlBuilder = new StringBuilder();
|
|
502
|
+
for (int k = 0; k < innerArray.length(); k++) {
|
|
503
|
+
Object innerElement = innerArray.get(k);
|
|
504
|
+
String elementValue = "";
|
|
505
|
+
|
|
506
|
+
if (innerElement instanceof String) {
|
|
507
|
+
elementValue = "'" + innerElement + "'";
|
|
508
|
+
} else {
|
|
509
|
+
elementValue = String.valueOf(innerElement);
|
|
510
|
+
}
|
|
511
|
+
innerSqlBuilder.append(elementValue);
|
|
512
|
+
|
|
513
|
+
if (k < innerArray.length() - 1) {
|
|
514
|
+
innerSqlBuilder.append(",");
|
|
515
|
+
}
|
|
516
|
+
}
|
|
502
517
|
|
|
518
|
+
sqlBuilder.append("(")
|
|
519
|
+
.append(innerSqlBuilder.toString())
|
|
520
|
+
.append(")");
|
|
521
|
+
|
|
522
|
+
if (j < valuesJson.length() - 1) {
|
|
523
|
+
sqlBuilder.append(",");
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
sqlBuilder.append(";");
|
|
527
|
+
String questionMark = extractQuestionMarkValues(statement);
|
|
528
|
+
String finalSql = "";
|
|
529
|
+
if(questionMark != null) {
|
|
530
|
+
finalSql = statement.replace(questionMark, sqlBuilder.toString());
|
|
531
|
+
} else {
|
|
532
|
+
finalSql = statement;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
JSObject respSet = prepareSQL(finalSql, new ArrayList<>(),
|
|
536
|
+
false, returnMode);
|
|
537
|
+
return respSet;
|
|
538
|
+
} catch (Exception e) {
|
|
539
|
+
throw new Exception(e.getMessage());
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
public String extractQuestionMarkValues(String input) {
|
|
543
|
+
Pattern pattern = Pattern.compile("VALUES \\((\\?(?:,\\s*\\?\\s*)*)\\)");
|
|
544
|
+
Matcher matcher = pattern.matcher(input);
|
|
545
|
+
|
|
546
|
+
if (matcher.find()) {
|
|
547
|
+
String extractedSubstring = matcher.group(1);
|
|
548
|
+
return "(" + extractedSubstring.replaceAll("\\s*,\\s*", ",") + ")";
|
|
549
|
+
} else {
|
|
550
|
+
return null;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
public JSObject oneRowStatement( String statement, JSONArray valuesJson,
|
|
554
|
+
String returnMode) throws Exception {
|
|
555
|
+
ArrayList<Object> values = new ArrayList<>();
|
|
556
|
+
for (int j = 0; j < valuesJson.length(); j++) {
|
|
557
|
+
values.add(valuesJson.get(j));
|
|
558
|
+
}
|
|
559
|
+
try {
|
|
560
|
+
JSObject respSet = prepareSQL(statement, values, false, returnMode);
|
|
561
|
+
return respSet;
|
|
562
|
+
} catch (Exception e) {
|
|
563
|
+
throw new Exception(e.getMessage());
|
|
564
|
+
}
|
|
565
|
+
}
|
|
503
566
|
public JSObject addToResponse(JSObject response, JSObject respSet) throws JSONException {
|
|
504
567
|
JSObject retResp = response;
|
|
505
568
|
long lastId = respSet.getLong("lastId");
|
|
@@ -696,33 +759,6 @@ public class Database {
|
|
|
696
759
|
return retVals;
|
|
697
760
|
}
|
|
698
761
|
|
|
699
|
-
/*
|
|
700
|
-
private static String extractTableName(String statement) {
|
|
701
|
-
Pattern pattern = Pattern.compile("(?i)(?:INSERT\\s+INTO|UPDATE|DELETE\\s+FROM)\\s+(\\w+)");
|
|
702
|
-
Matcher matcher = pattern.matcher(statement);
|
|
703
|
-
if (matcher.find()) {
|
|
704
|
-
return matcher.group(1);
|
|
705
|
-
}
|
|
706
|
-
return null;
|
|
707
|
-
}
|
|
708
|
-
|
|
709
|
-
private static String extractWhereClause(String sqlStatement) {
|
|
710
|
-
// Regular expression pattern to match the WHERE clause and removing the
|
|
711
|
-
// ORDER BY and LIMIT if any
|
|
712
|
-
Pattern pattern = Pattern.compile("(?i)\\bWHERE\\b\\s*(.*?)(?:\\s*\\b(?:ORDER\\s+BY|LIMIT)\\b|$)");
|
|
713
|
-
Matcher matcher = pattern.matcher(sqlStatement);
|
|
714
|
-
|
|
715
|
-
if (matcher.find()) {
|
|
716
|
-
String whereClause = matcher.group(1);
|
|
717
|
-
if (whereClause != null && whereClause.endsWith(";")) {
|
|
718
|
-
whereClause = whereClause.substring(0, whereClause.length() - 1);
|
|
719
|
-
}
|
|
720
|
-
return whereClause.trim();
|
|
721
|
-
}
|
|
722
|
-
|
|
723
|
-
return null;
|
|
724
|
-
}
|
|
725
|
-
*/
|
|
726
762
|
/**
|
|
727
763
|
* DeleteSQL method
|
|
728
764
|
*
|
|
@@ -782,199 +818,6 @@ public class Database {
|
|
|
782
818
|
}
|
|
783
819
|
}
|
|
784
820
|
|
|
785
|
-
/*
|
|
786
|
-
public void findReferencesAndUpdate(Database mDB, String tableName, String whereStmt, ArrayList<Object> values) throws Exception {
|
|
787
|
-
try {
|
|
788
|
-
ArrayList<String> references = getReferences(mDB, tableName);
|
|
789
|
-
if (references.size() == 0) {
|
|
790
|
-
return;
|
|
791
|
-
}
|
|
792
|
-
String tableNameWithRefs = references.get(references.size() - 1);
|
|
793
|
-
references.remove(references.size() - 1);
|
|
794
|
-
for (String refe : references) {
|
|
795
|
-
// get the tableName of the reference
|
|
796
|
-
String refTable = getReferenceTableName(refe);
|
|
797
|
-
if (refTable.length() <= 0) {
|
|
798
|
-
continue;
|
|
799
|
-
}
|
|
800
|
-
// get the withRefsNames
|
|
801
|
-
String[] withRefsNames = getWithRefsColumnName(refe);
|
|
802
|
-
if (withRefsNames.length <= 0) {
|
|
803
|
-
continue;
|
|
804
|
-
}
|
|
805
|
-
// get the columnNames
|
|
806
|
-
String[] colNames = getReferencedColumnName(refe);
|
|
807
|
-
if (colNames.length <= 0) {
|
|
808
|
-
continue;
|
|
809
|
-
}
|
|
810
|
-
// update the where clause
|
|
811
|
-
String uWhereStmt = updateWhere(whereStmt, withRefsNames, colNames);
|
|
812
|
-
|
|
813
|
-
if (uWhereStmt.length() <= 0) {
|
|
814
|
-
continue;
|
|
815
|
-
}
|
|
816
|
-
String updTableName = tableNameWithRefs;
|
|
817
|
-
String[] updColNames = colNames;
|
|
818
|
-
if (tableNameWithRefs.equals(tableName)) {
|
|
819
|
-
updTableName = refTable;
|
|
820
|
-
updColNames = withRefsNames;
|
|
821
|
-
}
|
|
822
|
-
//update sql_deleted for this reference
|
|
823
|
-
String stmt = "UPDATE " + updTableName + " SET sql_deleted = 1 " + uWhereStmt;
|
|
824
|
-
ArrayList<Object> selValues = new ArrayList<Object>();
|
|
825
|
-
if (values != null && values.size() > 0) {
|
|
826
|
-
String[] arrVal = whereStmt.split("\\?");
|
|
827
|
-
String[] modArr = arrVal;
|
|
828
|
-
if (arrVal[arrVal.length - 1].equals(";")) {
|
|
829
|
-
modArr = Arrays.copyOf(arrVal, arrVal.length - 1);
|
|
830
|
-
}
|
|
831
|
-
for (int j = 0; j < modArr.length; j++) {
|
|
832
|
-
for (String updVal : updColNames) {
|
|
833
|
-
int idxVal = arrVal[j].indexOf(updVal);
|
|
834
|
-
if (idxVal > -1) {
|
|
835
|
-
selValues.add(values.get(j));
|
|
836
|
-
}
|
|
837
|
-
}
|
|
838
|
-
}
|
|
839
|
-
}
|
|
840
|
-
|
|
841
|
-
JSObject retObj = prepareSQL(stmt, selValues, false, "no");
|
|
842
|
-
long lastId = retObj.getLong("lastId");
|
|
843
|
-
if (lastId == -1) {
|
|
844
|
-
String msg = "UPDATE sql_deleted failed for references " + "table: " + refTable + ";";
|
|
845
|
-
throw new Exception(msg);
|
|
846
|
-
}
|
|
847
|
-
}
|
|
848
|
-
return;
|
|
849
|
-
} catch (JSONException e) {
|
|
850
|
-
throw new Exception(e.getMessage());
|
|
851
|
-
} catch (Exception e) {
|
|
852
|
-
throw new Exception(e.getMessage());
|
|
853
|
-
}
|
|
854
|
-
}
|
|
855
|
-
|
|
856
|
-
public String getReferenceTableName(String refValue) {
|
|
857
|
-
String tableName = "";
|
|
858
|
-
if (refValue.length() > 0) {
|
|
859
|
-
String[] arr = refValue.split("(?i)REFERENCES", -1);
|
|
860
|
-
if (arr.length == 2) {
|
|
861
|
-
int oPar = arr[1].indexOf("(");
|
|
862
|
-
|
|
863
|
-
tableName = arr[1].substring(0, oPar).trim();
|
|
864
|
-
}
|
|
865
|
-
}
|
|
866
|
-
return tableName;
|
|
867
|
-
}
|
|
868
|
-
|
|
869
|
-
public String[] getWithRefsColumnName(String refValue) {
|
|
870
|
-
String[] colNames = new String[0];
|
|
871
|
-
if (refValue.length() > 0) {
|
|
872
|
-
String[] arr = refValue.split("(?i)REFERENCES", -1);
|
|
873
|
-
if (arr.length == 2) {
|
|
874
|
-
int oPar = arr[0].indexOf("(");
|
|
875
|
-
int cPar = arr[0].indexOf(")");
|
|
876
|
-
String colStr = arr[0].substring(oPar + 1, cPar).trim();
|
|
877
|
-
colNames = colStr.split(",");
|
|
878
|
-
}
|
|
879
|
-
}
|
|
880
|
-
return colNames;
|
|
881
|
-
}
|
|
882
|
-
|
|
883
|
-
public String[] getReferencedColumnName(String refValue) {
|
|
884
|
-
String[] colNames = new String[0];
|
|
885
|
-
if (refValue.length() > 0) {
|
|
886
|
-
String[] arr = refValue.split("(?i)REFERENCES", -1);
|
|
887
|
-
if (arr.length == 2) {
|
|
888
|
-
int oPar = arr[1].indexOf("(");
|
|
889
|
-
int cPar = arr[1].indexOf(")");
|
|
890
|
-
String colStr = arr[1].substring(oPar + 1, cPar).trim();
|
|
891
|
-
colNames = colStr.split(",");
|
|
892
|
-
}
|
|
893
|
-
}
|
|
894
|
-
return colNames;
|
|
895
|
-
}
|
|
896
|
-
|
|
897
|
-
public String updateWhere(String whStmt, String[] withRefsNames, String[] colNames) {
|
|
898
|
-
String whereStmt = "";
|
|
899
|
-
if (whStmt.length() > 0) {
|
|
900
|
-
Integer index = whStmt.toLowerCase().indexOf("WHERE".toLowerCase());
|
|
901
|
-
String stmt = whStmt.substring(index + 6);
|
|
902
|
-
if (withRefsNames.length == colNames.length) {
|
|
903
|
-
for (int i = 0; i < withRefsNames.length; i++) {
|
|
904
|
-
String colType = "withRefsNames";
|
|
905
|
-
int idx = stmt.indexOf(withRefsNames[i]);
|
|
906
|
-
if (idx == -1) {
|
|
907
|
-
idx = stmt.indexOf(colNames[i]);
|
|
908
|
-
colType = "colNames";
|
|
909
|
-
}
|
|
910
|
-
if (idx > -1) {
|
|
911
|
-
String valStr = "";
|
|
912
|
-
int fEqual = stmt.indexOf("=", idx);
|
|
913
|
-
if (fEqual > -1) {
|
|
914
|
-
int iAnd = stmt.indexOf("AND", fEqual);
|
|
915
|
-
int ilAnd = stmt.indexOf("and", fEqual);
|
|
916
|
-
if (iAnd > -1) {
|
|
917
|
-
valStr = (stmt.substring(fEqual + 1, iAnd - 1)).trim();
|
|
918
|
-
} else if (ilAnd > -1) {
|
|
919
|
-
valStr = (stmt.substring(fEqual + 1, ilAnd - 1)).trim();
|
|
920
|
-
} else {
|
|
921
|
-
valStr = (stmt.substring(fEqual + 1)).trim();
|
|
922
|
-
}
|
|
923
|
-
if (i > 0) {
|
|
924
|
-
whereStmt += " AND ";
|
|
925
|
-
}
|
|
926
|
-
if (colType.equals("withRefsNames")) {
|
|
927
|
-
whereStmt += colNames[i] + " = " + valStr;
|
|
928
|
-
} else {
|
|
929
|
-
whereStmt += withRefsNames[i] + " = " + valStr;
|
|
930
|
-
}
|
|
931
|
-
}
|
|
932
|
-
}
|
|
933
|
-
}
|
|
934
|
-
whereStmt = "WHERE " + whereStmt;
|
|
935
|
-
}
|
|
936
|
-
}
|
|
937
|
-
return whereStmt;
|
|
938
|
-
}
|
|
939
|
-
|
|
940
|
-
public ArrayList<String> getReferences(Database mDB, String tableName) throws Exception {
|
|
941
|
-
String sqlStmt =
|
|
942
|
-
"SELECT sql FROM sqlite_master " +
|
|
943
|
-
"WHERE sql LIKE('%FOREIGN KEY%') AND sql LIKE('%REFERENCES%') AND " +
|
|
944
|
-
"sql LIKE('%" +
|
|
945
|
-
tableName +
|
|
946
|
-
"%') AND sql LIKE('%ON DELETE%');";
|
|
947
|
-
|
|
948
|
-
try {
|
|
949
|
-
JSArray references = mDB.selectSQL(sqlStmt, new ArrayList<Object>());
|
|
950
|
-
ArrayList<String> retRefs = new ArrayList<String>();
|
|
951
|
-
if (references.length() > 0) {
|
|
952
|
-
retRefs = getRefs(references.getJSONObject(0).getString("sql"));
|
|
953
|
-
}
|
|
954
|
-
return retRefs;
|
|
955
|
-
} catch (Exception e) {
|
|
956
|
-
throw new Exception(e.getMessage());
|
|
957
|
-
}
|
|
958
|
-
}
|
|
959
|
-
|
|
960
|
-
private ArrayList<String> getRefs(String str) throws Exception {
|
|
961
|
-
ArrayList<String> retRefs = new ArrayList<String>();
|
|
962
|
-
String[] arrFor = str.split("(?i)FOREIGN KEY", -1);
|
|
963
|
-
// Loop through Foreign Keys
|
|
964
|
-
for (int i = 1; i < arrFor.length; i++) {
|
|
965
|
-
retRefs.add((arrFor[i].split("(?i)ON DELETE", -1))[0].trim());
|
|
966
|
-
}
|
|
967
|
-
// find table name with references
|
|
968
|
-
if (str.substring(0, 12).toLowerCase().equals("CREATE TABLE".toLowerCase())) {
|
|
969
|
-
int oPar = str.indexOf("(");
|
|
970
|
-
String tableName = str.substring(13, oPar).trim();
|
|
971
|
-
retRefs.add(tableName);
|
|
972
|
-
}
|
|
973
|
-
|
|
974
|
-
return retRefs;
|
|
975
|
-
}
|
|
976
|
-
|
|
977
|
-
*/
|
|
978
821
|
/**
|
|
979
822
|
* SelectSQL Method
|
|
980
823
|
* Query a raw sql statement with or without binding values
|