@capacitor-community/sqlite 5.4.1 → 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
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;
|
|
@@ -455,42 +458,30 @@ public class Database {
|
|
|
455
458
|
if (transaction) beginTransaction();
|
|
456
459
|
for (int i = 0; i < set.length(); i++) {
|
|
457
460
|
JSONObject row = set.getJSONObject(i);
|
|
461
|
+
JSObject respSet = new JSObject();
|
|
458
462
|
String statement = row.getString("statement");
|
|
459
463
|
JSONArray valuesJson = row.getJSONArray("values");
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
values.add(valuesJson.get(j));
|
|
463
|
-
}
|
|
464
|
-
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;
|
|
465
466
|
if (isArray) {
|
|
466
|
-
|
|
467
|
-
JSONArray valsJson = (JSONArray) values.get(j);
|
|
468
|
-
ArrayList<Object> vals = new ArrayList<>();
|
|
469
|
-
for (int k = 0; k < valsJson.length(); k++) {
|
|
470
|
-
vals.add(valsJson.get(k));
|
|
471
|
-
}
|
|
472
|
-
JSObject respSet = prepareSQL(statement, vals, false, returnMode);
|
|
473
|
-
lastId = respSet.getLong("lastId");
|
|
474
|
-
if (lastId == -1) break;
|
|
475
|
-
response = addToResponse(response, respSet);
|
|
476
|
-
}
|
|
467
|
+
respSet = multipleRowsStatement( statement, valuesJson, returnMode);
|
|
477
468
|
} else {
|
|
478
|
-
|
|
479
|
-
lastId = respSet.getLong("lastId");
|
|
480
|
-
if (lastId == -1) break;
|
|
481
|
-
response = addToResponse(response, respSet);
|
|
469
|
+
respSet = oneRowStatement( statement, valuesJson, returnMode);
|
|
482
470
|
}
|
|
471
|
+
lastId = respSet.getLong("lastId");
|
|
472
|
+
if (lastId.equals(-1L)) break;
|
|
473
|
+
response = addToResponse(response, respSet);
|
|
474
|
+
|
|
483
475
|
}
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
if (transaction) commitTransaction();
|
|
487
|
-
changes = _uSqlite.dbChanges(_db) - initChanges;
|
|
488
|
-
retObj.put("changes", changes);
|
|
489
|
-
retObj.put("lastId", lastId);
|
|
490
|
-
retObj.put("values", response.getJSONArray("values"));
|
|
491
|
-
return retObj;
|
|
476
|
+
if (lastId.equals(-1L)) {
|
|
477
|
+
throw new Exception("lastId equals -1");
|
|
492
478
|
} else {
|
|
493
|
-
|
|
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;
|
|
494
485
|
}
|
|
495
486
|
} else {
|
|
496
487
|
throw new Exception("Database not opened");
|
|
@@ -501,7 +492,77 @@ public class Database {
|
|
|
501
492
|
if (_db != null && transaction && _db.inTransaction()) rollbackTransaction();
|
|
502
493
|
}
|
|
503
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
|
+
}
|
|
504
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
|
+
}
|
|
505
566
|
public JSObject addToResponse(JSObject response, JSObject respSet) throws JSONException {
|
|
506
567
|
JSObject retResp = response;
|
|
507
568
|
long lastId = respSet.getLong("lastId");
|
|
@@ -698,33 +759,6 @@ public class Database {
|
|
|
698
759
|
return retVals;
|
|
699
760
|
}
|
|
700
761
|
|
|
701
|
-
/*
|
|
702
|
-
private static String extractTableName(String statement) {
|
|
703
|
-
Pattern pattern = Pattern.compile("(?i)(?:INSERT\\s+INTO|UPDATE|DELETE\\s+FROM)\\s+(\\w+)");
|
|
704
|
-
Matcher matcher = pattern.matcher(statement);
|
|
705
|
-
if (matcher.find()) {
|
|
706
|
-
return matcher.group(1);
|
|
707
|
-
}
|
|
708
|
-
return null;
|
|
709
|
-
}
|
|
710
|
-
|
|
711
|
-
private static String extractWhereClause(String sqlStatement) {
|
|
712
|
-
// Regular expression pattern to match the WHERE clause and removing the
|
|
713
|
-
// ORDER BY and LIMIT if any
|
|
714
|
-
Pattern pattern = Pattern.compile("(?i)\\bWHERE\\b\\s*(.*?)(?:\\s*\\b(?:ORDER\\s+BY|LIMIT)\\b|$)");
|
|
715
|
-
Matcher matcher = pattern.matcher(sqlStatement);
|
|
716
|
-
|
|
717
|
-
if (matcher.find()) {
|
|
718
|
-
String whereClause = matcher.group(1);
|
|
719
|
-
if (whereClause != null && whereClause.endsWith(";")) {
|
|
720
|
-
whereClause = whereClause.substring(0, whereClause.length() - 1);
|
|
721
|
-
}
|
|
722
|
-
return whereClause.trim();
|
|
723
|
-
}
|
|
724
|
-
|
|
725
|
-
return null;
|
|
726
|
-
}
|
|
727
|
-
*/
|
|
728
762
|
/**
|
|
729
763
|
* DeleteSQL method
|
|
730
764
|
*
|
|
@@ -784,199 +818,6 @@ public class Database {
|
|
|
784
818
|
}
|
|
785
819
|
}
|
|
786
820
|
|
|
787
|
-
/*
|
|
788
|
-
public void findReferencesAndUpdate(Database mDB, String tableName, String whereStmt, ArrayList<Object> values) throws Exception {
|
|
789
|
-
try {
|
|
790
|
-
ArrayList<String> references = getReferences(mDB, tableName);
|
|
791
|
-
if (references.size() == 0) {
|
|
792
|
-
return;
|
|
793
|
-
}
|
|
794
|
-
String tableNameWithRefs = references.get(references.size() - 1);
|
|
795
|
-
references.remove(references.size() - 1);
|
|
796
|
-
for (String refe : references) {
|
|
797
|
-
// get the tableName of the reference
|
|
798
|
-
String refTable = getReferenceTableName(refe);
|
|
799
|
-
if (refTable.length() <= 0) {
|
|
800
|
-
continue;
|
|
801
|
-
}
|
|
802
|
-
// get the withRefsNames
|
|
803
|
-
String[] withRefsNames = getWithRefsColumnName(refe);
|
|
804
|
-
if (withRefsNames.length <= 0) {
|
|
805
|
-
continue;
|
|
806
|
-
}
|
|
807
|
-
// get the columnNames
|
|
808
|
-
String[] colNames = getReferencedColumnName(refe);
|
|
809
|
-
if (colNames.length <= 0) {
|
|
810
|
-
continue;
|
|
811
|
-
}
|
|
812
|
-
// update the where clause
|
|
813
|
-
String uWhereStmt = updateWhere(whereStmt, withRefsNames, colNames);
|
|
814
|
-
|
|
815
|
-
if (uWhereStmt.length() <= 0) {
|
|
816
|
-
continue;
|
|
817
|
-
}
|
|
818
|
-
String updTableName = tableNameWithRefs;
|
|
819
|
-
String[] updColNames = colNames;
|
|
820
|
-
if (tableNameWithRefs.equals(tableName)) {
|
|
821
|
-
updTableName = refTable;
|
|
822
|
-
updColNames = withRefsNames;
|
|
823
|
-
}
|
|
824
|
-
//update sql_deleted for this reference
|
|
825
|
-
String stmt = "UPDATE " + updTableName + " SET sql_deleted = 1 " + uWhereStmt;
|
|
826
|
-
ArrayList<Object> selValues = new ArrayList<Object>();
|
|
827
|
-
if (values != null && values.size() > 0) {
|
|
828
|
-
String[] arrVal = whereStmt.split("\\?");
|
|
829
|
-
String[] modArr = arrVal;
|
|
830
|
-
if (arrVal[arrVal.length - 1].equals(";")) {
|
|
831
|
-
modArr = Arrays.copyOf(arrVal, arrVal.length - 1);
|
|
832
|
-
}
|
|
833
|
-
for (int j = 0; j < modArr.length; j++) {
|
|
834
|
-
for (String updVal : updColNames) {
|
|
835
|
-
int idxVal = arrVal[j].indexOf(updVal);
|
|
836
|
-
if (idxVal > -1) {
|
|
837
|
-
selValues.add(values.get(j));
|
|
838
|
-
}
|
|
839
|
-
}
|
|
840
|
-
}
|
|
841
|
-
}
|
|
842
|
-
|
|
843
|
-
JSObject retObj = prepareSQL(stmt, selValues, false, "no");
|
|
844
|
-
long lastId = retObj.getLong("lastId");
|
|
845
|
-
if (lastId == -1) {
|
|
846
|
-
String msg = "UPDATE sql_deleted failed for references " + "table: " + refTable + ";";
|
|
847
|
-
throw new Exception(msg);
|
|
848
|
-
}
|
|
849
|
-
}
|
|
850
|
-
return;
|
|
851
|
-
} catch (JSONException e) {
|
|
852
|
-
throw new Exception(e.getMessage());
|
|
853
|
-
} catch (Exception e) {
|
|
854
|
-
throw new Exception(e.getMessage());
|
|
855
|
-
}
|
|
856
|
-
}
|
|
857
|
-
|
|
858
|
-
public String getReferenceTableName(String refValue) {
|
|
859
|
-
String tableName = "";
|
|
860
|
-
if (refValue.length() > 0) {
|
|
861
|
-
String[] arr = refValue.split("(?i)REFERENCES", -1);
|
|
862
|
-
if (arr.length == 2) {
|
|
863
|
-
int oPar = arr[1].indexOf("(");
|
|
864
|
-
|
|
865
|
-
tableName = arr[1].substring(0, oPar).trim();
|
|
866
|
-
}
|
|
867
|
-
}
|
|
868
|
-
return tableName;
|
|
869
|
-
}
|
|
870
|
-
|
|
871
|
-
public String[] getWithRefsColumnName(String refValue) {
|
|
872
|
-
String[] colNames = new String[0];
|
|
873
|
-
if (refValue.length() > 0) {
|
|
874
|
-
String[] arr = refValue.split("(?i)REFERENCES", -1);
|
|
875
|
-
if (arr.length == 2) {
|
|
876
|
-
int oPar = arr[0].indexOf("(");
|
|
877
|
-
int cPar = arr[0].indexOf(")");
|
|
878
|
-
String colStr = arr[0].substring(oPar + 1, cPar).trim();
|
|
879
|
-
colNames = colStr.split(",");
|
|
880
|
-
}
|
|
881
|
-
}
|
|
882
|
-
return colNames;
|
|
883
|
-
}
|
|
884
|
-
|
|
885
|
-
public String[] getReferencedColumnName(String refValue) {
|
|
886
|
-
String[] colNames = new String[0];
|
|
887
|
-
if (refValue.length() > 0) {
|
|
888
|
-
String[] arr = refValue.split("(?i)REFERENCES", -1);
|
|
889
|
-
if (arr.length == 2) {
|
|
890
|
-
int oPar = arr[1].indexOf("(");
|
|
891
|
-
int cPar = arr[1].indexOf(")");
|
|
892
|
-
String colStr = arr[1].substring(oPar + 1, cPar).trim();
|
|
893
|
-
colNames = colStr.split(",");
|
|
894
|
-
}
|
|
895
|
-
}
|
|
896
|
-
return colNames;
|
|
897
|
-
}
|
|
898
|
-
|
|
899
|
-
public String updateWhere(String whStmt, String[] withRefsNames, String[] colNames) {
|
|
900
|
-
String whereStmt = "";
|
|
901
|
-
if (whStmt.length() > 0) {
|
|
902
|
-
Integer index = whStmt.toLowerCase().indexOf("WHERE".toLowerCase());
|
|
903
|
-
String stmt = whStmt.substring(index + 6);
|
|
904
|
-
if (withRefsNames.length == colNames.length) {
|
|
905
|
-
for (int i = 0; i < withRefsNames.length; i++) {
|
|
906
|
-
String colType = "withRefsNames";
|
|
907
|
-
int idx = stmt.indexOf(withRefsNames[i]);
|
|
908
|
-
if (idx == -1) {
|
|
909
|
-
idx = stmt.indexOf(colNames[i]);
|
|
910
|
-
colType = "colNames";
|
|
911
|
-
}
|
|
912
|
-
if (idx > -1) {
|
|
913
|
-
String valStr = "";
|
|
914
|
-
int fEqual = stmt.indexOf("=", idx);
|
|
915
|
-
if (fEqual > -1) {
|
|
916
|
-
int iAnd = stmt.indexOf("AND", fEqual);
|
|
917
|
-
int ilAnd = stmt.indexOf("and", fEqual);
|
|
918
|
-
if (iAnd > -1) {
|
|
919
|
-
valStr = (stmt.substring(fEqual + 1, iAnd - 1)).trim();
|
|
920
|
-
} else if (ilAnd > -1) {
|
|
921
|
-
valStr = (stmt.substring(fEqual + 1, ilAnd - 1)).trim();
|
|
922
|
-
} else {
|
|
923
|
-
valStr = (stmt.substring(fEqual + 1)).trim();
|
|
924
|
-
}
|
|
925
|
-
if (i > 0) {
|
|
926
|
-
whereStmt += " AND ";
|
|
927
|
-
}
|
|
928
|
-
if (colType.equals("withRefsNames")) {
|
|
929
|
-
whereStmt += colNames[i] + " = " + valStr;
|
|
930
|
-
} else {
|
|
931
|
-
whereStmt += withRefsNames[i] + " = " + valStr;
|
|
932
|
-
}
|
|
933
|
-
}
|
|
934
|
-
}
|
|
935
|
-
}
|
|
936
|
-
whereStmt = "WHERE " + whereStmt;
|
|
937
|
-
}
|
|
938
|
-
}
|
|
939
|
-
return whereStmt;
|
|
940
|
-
}
|
|
941
|
-
|
|
942
|
-
public ArrayList<String> getReferences(Database mDB, String tableName) throws Exception {
|
|
943
|
-
String sqlStmt =
|
|
944
|
-
"SELECT sql FROM sqlite_master " +
|
|
945
|
-
"WHERE sql LIKE('%FOREIGN KEY%') AND sql LIKE('%REFERENCES%') AND " +
|
|
946
|
-
"sql LIKE('%" +
|
|
947
|
-
tableName +
|
|
948
|
-
"%') AND sql LIKE('%ON DELETE%');";
|
|
949
|
-
|
|
950
|
-
try {
|
|
951
|
-
JSArray references = mDB.selectSQL(sqlStmt, new ArrayList<Object>());
|
|
952
|
-
ArrayList<String> retRefs = new ArrayList<String>();
|
|
953
|
-
if (references.length() > 0) {
|
|
954
|
-
retRefs = getRefs(references.getJSONObject(0).getString("sql"));
|
|
955
|
-
}
|
|
956
|
-
return retRefs;
|
|
957
|
-
} catch (Exception e) {
|
|
958
|
-
throw new Exception(e.getMessage());
|
|
959
|
-
}
|
|
960
|
-
}
|
|
961
|
-
|
|
962
|
-
private ArrayList<String> getRefs(String str) throws Exception {
|
|
963
|
-
ArrayList<String> retRefs = new ArrayList<String>();
|
|
964
|
-
String[] arrFor = str.split("(?i)FOREIGN KEY", -1);
|
|
965
|
-
// Loop through Foreign Keys
|
|
966
|
-
for (int i = 1; i < arrFor.length; i++) {
|
|
967
|
-
retRefs.add((arrFor[i].split("(?i)ON DELETE", -1))[0].trim());
|
|
968
|
-
}
|
|
969
|
-
// find table name with references
|
|
970
|
-
if (str.substring(0, 12).toLowerCase().equals("CREATE TABLE".toLowerCase())) {
|
|
971
|
-
int oPar = str.indexOf("(");
|
|
972
|
-
String tableName = str.substring(13, oPar).trim();
|
|
973
|
-
retRefs.add(tableName);
|
|
974
|
-
}
|
|
975
|
-
|
|
976
|
-
return retRefs;
|
|
977
|
-
}
|
|
978
|
-
|
|
979
|
-
*/
|
|
980
821
|
/**
|
|
981
822
|
* SelectSQL Method
|
|
982
823
|
* Query a raw sql statement with or without binding values
|
|
@@ -13,6 +13,7 @@ import java.util.ArrayList;
|
|
|
13
13
|
import java.util.HashMap;
|
|
14
14
|
import java.util.Map;
|
|
15
15
|
import org.json.JSONException;
|
|
16
|
+
import org.json.JSONObject;
|
|
16
17
|
|
|
17
18
|
public class ImportFromJson {
|
|
18
19
|
|
|
@@ -234,11 +235,11 @@ public class ImportFromJson {
|
|
|
234
235
|
.append(" AFTER UPDATE ON ")
|
|
235
236
|
.append(tableName)
|
|
236
237
|
.append(" FOR EACH ROW ")
|
|
237
|
-
.append("WHEN NEW.last_modified
|
|
238
|
+
.append("WHEN NEW.last_modified <= " + "OLD.last_modified BEGIN ")
|
|
238
239
|
.append("UPDATE ")
|
|
239
240
|
.append(tableName)
|
|
240
241
|
.append(" SET last_modified = (strftime('%s','now')) ")
|
|
241
|
-
.append("WHERE id=
|
|
242
|
+
.append("WHERE id=NEW.id; ")
|
|
242
243
|
.append("END;")
|
|
243
244
|
.toString();
|
|
244
245
|
statements.add(stmtTrigger);
|
|
@@ -380,42 +381,52 @@ public class ImportFromJson {
|
|
|
380
381
|
if (tableNamesTypes.length() == 0) {
|
|
381
382
|
throw new Exception("CreateTableData: no column names & types returned");
|
|
382
383
|
}
|
|
383
|
-
ArrayList<String> tColNames
|
|
384
|
-
ArrayList<String> tColTypes = new ArrayList<>();
|
|
384
|
+
ArrayList<String> tColNames;
|
|
385
385
|
if (tableNamesTypes.has("names")) {
|
|
386
386
|
tColNames = _uJson.getColumnNames(tableNamesTypes.get("names"));
|
|
387
387
|
} else {
|
|
388
388
|
throw new Exception("GetValues: Table " + tableName + " no names");
|
|
389
389
|
}
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
390
|
+
// New process flow
|
|
391
|
+
JSONObject retObjStrs = generateInsertAndDeletedStrings(tColNames,values);
|
|
392
|
+
// Create the statement for INSERT
|
|
393
|
+
String namesString = _uJson.convertToString(tColNames, ',');
|
|
394
|
+
if(retObjStrs.has("insert")) {
|
|
395
|
+
String stmtInsert =
|
|
396
|
+
new StringBuilder("INSERT OR REPLACE INTO ")
|
|
397
|
+
.append(tableName)
|
|
398
|
+
.append("(")
|
|
399
|
+
.append(namesString)
|
|
400
|
+
.append(") ")
|
|
401
|
+
.append(retObjStrs.get("insert"))
|
|
402
|
+
.append(";")
|
|
403
|
+
.toString();
|
|
404
|
+
JSObject retObj = mDb.prepareSQL(stmtInsert, new ArrayList<>(), true,
|
|
405
|
+
"no");
|
|
406
|
+
long lastId = retObj.getLong("lastId");
|
|
407
|
+
if (lastId < 0) {
|
|
408
|
+
throw new Exception("CreateTableData: INSERT lastId < 0");
|
|
409
|
+
}
|
|
394
410
|
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
JSObject retObj = mDb.prepareSQL(stmt, row, true, "no");
|
|
412
|
-
long lastId = retObj.getLong("lastId");
|
|
413
|
-
if (lastId < 0) {
|
|
414
|
-
throw new Exception("CreateTableData: lastId < 0");
|
|
415
|
-
}
|
|
416
|
-
}
|
|
411
|
+
if(retObjStrs.has("delete")) {
|
|
412
|
+
String stmtDelete =
|
|
413
|
+
new StringBuilder("DELETE FROM ")
|
|
414
|
+
.append(tableName)
|
|
415
|
+
.append(" WHERE ")
|
|
416
|
+
.append(tColNames.get(0))
|
|
417
|
+
.append(" ")
|
|
418
|
+
.append(retObjStrs.get("delete"))
|
|
419
|
+
.append(";")
|
|
420
|
+
.toString();
|
|
421
|
+
JSObject retObj = mDb.prepareSQL(stmtDelete, new ArrayList<>(), true,
|
|
422
|
+
"no");
|
|
423
|
+
long lastId = retObj.getLong("lastId");
|
|
424
|
+
if (lastId < 0) {
|
|
425
|
+
throw new Exception("CreateTableData: INSERT lastId < 0");
|
|
426
|
+
}
|
|
417
427
|
}
|
|
418
|
-
|
|
428
|
+
|
|
429
|
+
|
|
419
430
|
} catch (JSONException e) {
|
|
420
431
|
throw new Exception("CreateTableData: " + e.getMessage());
|
|
421
432
|
} catch (Exception e) {
|
|
@@ -424,173 +435,72 @@ public class ImportFromJson {
|
|
|
424
435
|
}
|
|
425
436
|
|
|
426
437
|
/**
|
|
427
|
-
*
|
|
428
|
-
* @param mDb
|
|
429
|
-
* @param stmt
|
|
430
|
-
* @param values
|
|
431
|
-
* @param tableName
|
|
438
|
+
* GenerateInsertAndDeletedStrings
|
|
432
439
|
* @param tColNames
|
|
433
|
-
* @param tColTypes
|
|
434
|
-
* @return
|
|
435
|
-
* @throws Exception
|
|
436
|
-
*/
|
|
437
|
-
private Boolean checkUpdate(
|
|
438
|
-
Database mDb,
|
|
439
|
-
String stmt,
|
|
440
|
-
ArrayList<Object> values,
|
|
441
|
-
String tableName,
|
|
442
|
-
ArrayList<String> tColNames,
|
|
443
|
-
ArrayList<String> tColTypes
|
|
444
|
-
) throws Exception {
|
|
445
|
-
Boolean isRun = true;
|
|
446
|
-
if (stmt.substring(0, 6).equals("UPDATE")) {
|
|
447
|
-
StringBuilder sbQuery = new StringBuilder("SELECT * FROM ").append(tableName).append(" WHERE ").append(tColNames.get(0));
|
|
448
|
-
|
|
449
|
-
if (values.get(0) instanceof String) {
|
|
450
|
-
sbQuery.append(" = '").append(values.get(0)).append("';");
|
|
451
|
-
} else {
|
|
452
|
-
sbQuery.append(" = ").append(values.get(0)).append(";");
|
|
453
|
-
}
|
|
454
|
-
String query = sbQuery.toString();
|
|
455
|
-
|
|
456
|
-
try {
|
|
457
|
-
ArrayList<ArrayList<Object>> resValues = _uJson.getValues(mDb, query, tableName);
|
|
458
|
-
if (resValues.size() > 0) {
|
|
459
|
-
isRun = checkValues(values, resValues.get(0));
|
|
460
|
-
} else {
|
|
461
|
-
throw new Exception("CheckUpdate: CheckUpdate statement returns nothing");
|
|
462
|
-
}
|
|
463
|
-
} catch (Exception e) {
|
|
464
|
-
throw new Exception("CheckUpdate: " + e.getMessage());
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
return isRun;
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
/**
|
|
471
|
-
* Check Values
|
|
472
440
|
* @param values
|
|
473
|
-
* @param nValues
|
|
474
|
-
* @return
|
|
475
|
-
* @throws Exception
|
|
476
|
-
*/
|
|
477
|
-
private Boolean checkValues(ArrayList<Object> values, ArrayList<Object> nValues) throws Exception {
|
|
478
|
-
if (values.size() > 0 && nValues.size() > 0 && values.size() == nValues.size()) {
|
|
479
|
-
for (int i = 0; i < values.size(); i++) {
|
|
480
|
-
if (nValues.get(i) instanceof String) {
|
|
481
|
-
if (!values.get(i).equals(nValues.get(i))) {
|
|
482
|
-
return true;
|
|
483
|
-
}
|
|
484
|
-
} else if (nValues.get(i) instanceof Long && values.get(i) instanceof Integer) {
|
|
485
|
-
// int iVal = (Integer) values.get(i);
|
|
486
|
-
long lVal = (Integer) values.get(i);
|
|
487
|
-
// long nlVal = (Long) nValues.get(i);
|
|
488
|
-
if (lVal != (Long) nValues.get(i)) {
|
|
489
|
-
return true;
|
|
490
|
-
}
|
|
491
|
-
} else if (nValues.get(i) instanceof Double && values.get(i) instanceof Integer) {
|
|
492
|
-
double dVal = (Integer) values.get(i);
|
|
493
|
-
if (dVal != (Double) nValues.get(i)) {
|
|
494
|
-
return true;
|
|
495
|
-
}
|
|
496
|
-
} else {
|
|
497
|
-
if (values.get(i) != nValues.get(i)) {
|
|
498
|
-
return true;
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
return false;
|
|
503
|
-
} else {
|
|
504
|
-
throw new Exception("CheckValues: Both arrays not the same length");
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
/**
|
|
509
|
-
* Create the Row Statement to load the data
|
|
510
|
-
* @param mDb
|
|
511
|
-
* @param tColNames
|
|
512
|
-
* @param tColTypes
|
|
513
|
-
* @param row
|
|
514
|
-
* @param j
|
|
515
|
-
* @param tableName
|
|
516
|
-
* @param mode
|
|
517
441
|
* @return
|
|
442
|
+
* @throws JSONException
|
|
518
443
|
*/
|
|
519
|
-
private String
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
String tableName,
|
|
526
|
-
String mode
|
|
527
|
-
) throws Exception {
|
|
528
|
-
String msg = "CreateRowStatement: ";
|
|
529
|
-
msg += "Table" + tableName + " values row";
|
|
530
|
-
if (tColNames.size() != row.size() || row.size() == 0 || tColNames.size() == 0) {
|
|
531
|
-
throw new Exception(msg + j + " not correct length");
|
|
532
|
-
}
|
|
444
|
+
private JSONObject generateInsertAndDeletedStrings(ArrayList<String> tColNames,
|
|
445
|
+
ArrayList<ArrayList<Object>> values)
|
|
446
|
+
throws JSONException {
|
|
447
|
+
JSONObject retObj = new JSONObject();
|
|
448
|
+
StringBuilder insertValues = new StringBuilder();
|
|
449
|
+
StringBuilder deletedIds = new StringBuilder();
|
|
533
450
|
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
// Create INSERT or UPDATE Statements
|
|
537
|
-
if (mode.equals("full") || (mode.equals("partial") && !retIsIdExists)) {
|
|
538
|
-
// Insert
|
|
539
|
-
String namesString = _uJson.convertToString(tColNames, ',');
|
|
540
|
-
String questionMarkString = _uJson.createQuestionMarkString(tColNames.size());
|
|
541
|
-
if (questionMarkString.length() == 0) {
|
|
542
|
-
throw new Exception(msg + j + "questionMarkString is empty");
|
|
543
|
-
}
|
|
544
|
-
stmt =
|
|
545
|
-
new StringBuilder("INSERT INTO ")
|
|
546
|
-
.append(tableName)
|
|
547
|
-
.append("(")
|
|
548
|
-
.append(namesString)
|
|
549
|
-
.append(")")
|
|
550
|
-
.append(" VALUES (")
|
|
551
|
-
.append(questionMarkString)
|
|
552
|
-
.append(");")
|
|
553
|
-
.toString();
|
|
554
|
-
} else {
|
|
555
|
-
Boolean isUpdate = true;
|
|
556
|
-
Integer idxDelete = tColNames.indexOf("sql_deleted");
|
|
557
|
-
if (idxDelete >= 0) {
|
|
558
|
-
if (row.get(idxDelete).equals(1)) {
|
|
559
|
-
// Delete
|
|
560
|
-
isUpdate = false;
|
|
561
|
-
Object key = tColNames.get(0);
|
|
562
|
-
StringBuilder sbQuery = new StringBuilder("DELETE FROM ")
|
|
563
|
-
.append(tableName)
|
|
564
|
-
.append(" WHERE ")
|
|
565
|
-
.append(tColNames.get(0))
|
|
566
|
-
.append(" = ");
|
|
451
|
+
for (ArrayList<Object> rowIndex : values) {
|
|
452
|
+
int colIndex = tColNames.indexOf("sql_deleted");
|
|
567
453
|
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
stmt = sbQuery.toString();
|
|
571
|
-
}
|
|
572
|
-
}
|
|
573
|
-
if (isUpdate) {
|
|
574
|
-
// Update
|
|
575
|
-
String setString = _uJson.setNameForUpdate(tColNames);
|
|
576
|
-
if (setString.length() == 0) {
|
|
577
|
-
throw new Exception(msg + j + "setString is empty");
|
|
578
|
-
}
|
|
579
|
-
Object key = tColNames.get(0);
|
|
580
|
-
StringBuilder sbQuery = new StringBuilder("UPDATE ")
|
|
581
|
-
.append(tableName)
|
|
582
|
-
.append(" SET ")
|
|
583
|
-
.append(setString)
|
|
584
|
-
.append(" WHERE ")
|
|
585
|
-
.append(tColNames.get(0))
|
|
586
|
-
.append(" = ");
|
|
454
|
+
// Check if the column "sql_deleted" is 0
|
|
455
|
+
if (colIndex == -1 || (int) rowIndex.get(colIndex) == 0) {
|
|
587
456
|
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
457
|
+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
|
|
458
|
+
String formattedRow = null;
|
|
459
|
+
formattedRow = String.join(", ", rowIndex.stream().map(item -> {
|
|
460
|
+
if (item instanceof String) {
|
|
461
|
+
return "'" + item + "'";
|
|
462
|
+
} else {
|
|
463
|
+
return item.toString();
|
|
464
|
+
}
|
|
465
|
+
}).toArray(String[]::new));
|
|
466
|
+
insertValues.append("(").append(formattedRow).append("), ");
|
|
467
|
+
} else {
|
|
468
|
+
StringBuilder formattedRow = new StringBuilder();
|
|
469
|
+
for (int i = 0; i < rowIndex.size(); i++) {
|
|
470
|
+
if (i > 0) {
|
|
471
|
+
formattedRow.append(", ");
|
|
472
|
+
}
|
|
473
|
+
Object item = rowIndex.get(i);
|
|
474
|
+
if (item instanceof String) {
|
|
475
|
+
formattedRow.append("'").append(item).append("'");
|
|
476
|
+
} else {
|
|
477
|
+
formattedRow.append(item);
|
|
478
|
+
}
|
|
591
479
|
}
|
|
480
|
+
insertValues.append("(").append(formattedRow).append("), ");
|
|
481
|
+
}
|
|
482
|
+
} else if ((int) rowIndex.get(colIndex) == 1) {
|
|
483
|
+
deletedIds.append(rowIndex.get(0)).append(", ");
|
|
592
484
|
}
|
|
593
|
-
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// Remove the trailing comma and space from insertValues and deletedIds
|
|
488
|
+
if (insertValues.length() > 0) {
|
|
489
|
+
insertValues.setLength(insertValues.length() - 2); // Remove trailing comma and space
|
|
490
|
+
insertValues.insert(0, "VALUES ");
|
|
491
|
+
}
|
|
492
|
+
if (deletedIds.length() > 0) {
|
|
493
|
+
deletedIds.setLength(deletedIds.length() - 2); // Remove trailing comma and space
|
|
494
|
+
deletedIds.insert(0, "IN (");
|
|
495
|
+
deletedIds.append(")");
|
|
496
|
+
}
|
|
497
|
+
if (insertValues.length() > 0) {
|
|
498
|
+
retObj.put("insert", insertValues.toString());
|
|
499
|
+
}
|
|
500
|
+
if (deletedIds.length() > 0) {
|
|
501
|
+
retObj.put("delete", deletedIds.toString());
|
|
502
|
+
}
|
|
503
|
+
return retObj;
|
|
594
504
|
}
|
|
595
505
|
|
|
596
506
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capacitor-community/sqlite",
|
|
3
|
-
"version": "5.4.1",
|
|
3
|
+
"version": "5.4.2-1",
|
|
4
4
|
"description": "Community plugin for native & electron SQLite databases",
|
|
5
5
|
"main": "dist/plugin.cjs.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -59,11 +59,11 @@
|
|
|
59
59
|
"prepublishOnly": "npm run build && npm run build-electron && npm run docgen"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
|
-
"@capacitor/android": "^5.4.
|
|
63
|
-
"@capacitor/cli": "^5.4.
|
|
64
|
-
"@capacitor/core": "^5.4.
|
|
62
|
+
"@capacitor/android": "^5.4.2",
|
|
63
|
+
"@capacitor/cli": "^5.4.2",
|
|
64
|
+
"@capacitor/core": "^5.4.2",
|
|
65
65
|
"@capacitor/docgen": "^0.0.17",
|
|
66
|
-
"@capacitor/ios": "^5.4.
|
|
66
|
+
"@capacitor/ios": "^5.4.2",
|
|
67
67
|
"@ionic/eslint-config": "^0.3.0",
|
|
68
68
|
"@ionic/prettier-config": "^1.0.1",
|
|
69
69
|
"@ionic/swiftlint-config": "^1.1.2",
|