@capacitor-community/sqlite 3.5.2-dev1 → 4.0.0-0

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.
@@ -11,7 +11,7 @@ Pod::Spec.new do |s|
11
11
  s.author = package['author']
12
12
  s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
13
13
  s.source_files = 'ios/Plugin/**/*.{swift,h,m,c,cc,mm,cpp}'
14
- s.ios.deployment_target = '12.0'
14
+ s.ios.deployment_target = '13.0'
15
15
  s.dependency 'Capacitor'
16
16
  s.dependency 'SQLCipher'
17
17
  s.dependency 'ZIPFoundation'
@@ -1,8 +1,8 @@
1
1
  ext {
2
- junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.1'
3
- androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.3.0'
4
- androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.2'
5
- androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.3.0'
2
+ junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
3
+ androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.4.2'
4
+ androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.3'
5
+ androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.4.0'
6
6
  }
7
7
 
8
8
  buildscript {
@@ -11,17 +11,17 @@ buildscript {
11
11
  mavenCentral()
12
12
  }
13
13
  dependencies {
14
- classpath 'com.android.tools.build:gradle:4.2.1'
14
+ classpath 'com.android.tools.build:gradle:7.2.1'
15
15
  }
16
16
  }
17
17
 
18
18
  apply plugin: 'com.android.library'
19
19
 
20
20
  android {
21
- compileSdkVersion project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 30
21
+ compileSdkVersion project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 32
22
22
  defaultConfig {
23
- minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 21
24
- targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 30
23
+ minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 22
24
+ targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 32
25
25
  versionCode 1
26
26
  versionName "1.0"
27
27
  testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
@@ -36,14 +36,14 @@ android {
36
36
  abortOnError false
37
37
  }
38
38
  compileOptions {
39
- sourceCompatibility JavaVersion.VERSION_1_8
40
- targetCompatibility JavaVersion.VERSION_1_8
39
+ sourceCompatibility JavaVersion.VERSION_11
40
+ targetCompatibility JavaVersion.VERSION_11
41
41
  }
42
42
  }
43
43
 
44
44
  repositories {
45
45
  google()
46
- jcenter()
46
+
47
47
  mavenCentral()
48
48
  }
49
49
 
@@ -26,6 +26,8 @@ import com.getcapacitor.community.database.sqlite.SQLite.UtilsSQLite;
26
26
  import java.io.File;
27
27
  import java.text.SimpleDateFormat;
28
28
  import java.util.ArrayList;
29
+ import java.util.Arrays;
30
+ import java.util.Collections;
29
31
  import java.util.Date;
30
32
  import java.util.Dictionary;
31
33
  import java.util.Hashtable;
@@ -329,7 +331,7 @@ public class Database {
329
331
  for (String cmd : statements) {
330
332
  if (!cmd.endsWith(";")) cmd += ";";
331
333
  String nCmd = cmd;
332
- String trimCmd = nCmd.trim().substring(0, 11).toUpperCase();
334
+ String trimCmd = nCmd.trim().substring(0, Math.min(nCmd.trim().length(), 11)).toUpperCase();
333
335
  if (trimCmd.equals("DELETE FROM") && nCmd.toLowerCase().contains("WHERE".toLowerCase())) {
334
336
  String whereStmt = nCmd.trim();
335
337
  nCmd = deleteSQL(this, whereStmt, new ArrayList<Object>());
@@ -545,27 +547,59 @@ public class Database {
545
547
  */
546
548
  public void findReferencesAndUpdate(Database mDB, String tableName, String whereStmt, ArrayList<Object> values) throws Exception {
547
549
  try {
548
- JSArray references = getReferences(mDB, tableName);
549
- for (int j = 0; j < references.length(); j++) {
550
+ ArrayList<String> references = getReferences(mDB, tableName);
551
+ if (references.size() == 0) {
552
+ return;
553
+ }
554
+ String tableNameWithRefs = references.get(references.size() - 1);
555
+ references.remove(references.size() - 1);
556
+ for (String refe : references) {
550
557
  // get the tableName of the reference
551
- String refTable = getReferenceTableName(references.getJSONObject(j).getString("sql"));
558
+ String refTable = getReferenceTableName(refe);
552
559
  if (refTable.length() <= 0) {
553
560
  continue;
554
561
  }
555
- // get the columnName
556
- String colName = getReferenceColumnName(references.getJSONObject(j).getString("sql"));
557
- if (refTable.length() <= 0) {
562
+ // get the withRefsNames
563
+ String[] withRefsNames = getWithRefsColumnName(refe);
564
+ if (withRefsNames.length <= 0) {
565
+ continue;
566
+ }
567
+ // get the columnNames
568
+ String[] colNames = getReferencedColumnName(refe);
569
+ if (colNames.length <= 0) {
558
570
  continue;
559
571
  }
560
572
  // update the where clause
561
- String uWhereStmt = updateWhere(whereStmt, colName);
573
+ String uWhereStmt = updateWhere(whereStmt, withRefsNames, colNames);
562
574
 
563
575
  if (uWhereStmt.length() <= 0) {
564
576
  continue;
565
577
  }
578
+ String updTableName = tableNameWithRefs;
579
+ String[] updColNames = colNames;
580
+ if (tableNameWithRefs.equals(tableName)) {
581
+ updTableName = refTable;
582
+ updColNames = withRefsNames;
583
+ }
566
584
  //update sql_deleted for this reference
567
- String stmt = "UPDATE " + refTable + " SET sql_deleted = 1 " + uWhereStmt;
568
- long lastId = prepareSQL(stmt, values, false);
585
+ String stmt = "UPDATE " + updTableName + " SET sql_deleted = 1 " + uWhereStmt;
586
+ ArrayList<Object> selValues = new ArrayList<Object>();
587
+ if (values != null && values.size() > 0) {
588
+ String[] arrVal = whereStmt.split("\\?");
589
+ if (arrVal[arrVal.length - 1].equals(";")) {
590
+ Arrays.copyOf(arrVal, arrVal.length - 1);
591
+ }
592
+ for (int j = 0; j < arrVal.length; j++) {
593
+ for (String updVal : updColNames) {
594
+ int idxVal = arrVal[j].indexOf(updVal);
595
+ if (idxVal > -1) {
596
+ selValues.add(values.get(j));
597
+ }
598
+ }
599
+ }
600
+ }
601
+
602
+ long lastId = prepareSQL(stmt, selValues, false);
569
603
  if (lastId == -1) {
570
604
  String msg = "UPDATE sql_deleted failed for references " + "table: " + refTable + ";";
571
605
  throw new Exception(msg);
@@ -587,47 +621,104 @@ public class Database {
587
621
  */
588
622
  public String getReferenceTableName(String refValue) {
589
623
  String tableName = "";
590
- if (refValue.length() > 0 && refValue.substring(0, 12).equalsIgnoreCase("CREATE TABLE")) {
591
- Integer oPar = refValue.indexOf("(");
592
- tableName = refValue.substring(13, oPar).trim();
593
- }
624
+ if (refValue.length() > 0) {
625
+ String[] arr = refValue.split("(?i)REFERENCES", -1);
626
+ if (arr.length == 2) {
627
+ int oPar = arr[1].indexOf("(");
594
628
 
629
+ tableName = arr[1].substring(0, oPar).trim();
630
+ }
631
+ }
595
632
  return tableName;
596
633
  }
597
634
 
598
635
  /**
599
- * GetReferenceColumnName method
636
+ * GetWithRefsColumnName
600
637
  *
601
638
  * @param refValue
602
639
  * @return
603
640
  */
604
- public String getReferenceColumnName(String refValue) {
605
- String colName = "";
641
+ public String[] getWithRefsColumnName(String refValue) {
642
+ String[] colNames = new String[0];
606
643
  if (refValue.length() > 0) {
607
- Integer index = refValue.toLowerCase().indexOf("FOREIGN KEY".toLowerCase());
608
- String stmt = refValue.substring(index + 12);
609
- Integer oPar = stmt.indexOf("(");
610
- Integer cPar = stmt.indexOf(")");
611
- colName = stmt.substring(oPar + 1, cPar).trim();
644
+ String[] arr = refValue.split("(?i)REFERENCES", -1);
645
+ if (arr.length == 2) {
646
+ int oPar = arr[0].indexOf("(");
647
+ int cPar = arr[0].indexOf(")");
648
+ String colStr = arr[0].substring(oPar + 1, cPar).trim();
649
+ colNames = colStr.split(",");
650
+ }
612
651
  }
613
- return colName;
652
+ return colNames;
653
+ }
654
+
655
+ /**
656
+ * GetReferencedColumnName method
657
+ *
658
+ * @param refValue
659
+ * @return
660
+ */
661
+ public String[] getReferencedColumnName(String refValue) {
662
+ String[] colNames = new String[0];
663
+ if (refValue.length() > 0) {
664
+ String[] arr = refValue.split("(?i)REFERENCES", -1);
665
+ if (arr.length == 2) {
666
+ int oPar = arr[1].indexOf("(");
667
+ int cPar = arr[1].indexOf(")");
668
+ String colStr = arr[1].substring(oPar + 1, cPar).trim();
669
+ colNames = colStr.split(",");
670
+ }
671
+ }
672
+ return colNames;
614
673
  }
615
674
 
616
675
  /**
617
676
  * UpdateWhere method
618
677
  *
619
678
  * @param whStmt
620
- * @param colName
679
+ * @param withRefsNames
680
+ * @param colNames
621
681
  * @return
622
682
  */
623
- public String updateWhere(String whStmt, String colName) {
683
+ public String updateWhere(String whStmt, String[] withRefsNames, String[] colNames) {
624
684
  String whereStmt = "";
625
685
  if (whStmt.length() > 0) {
626
686
  Integer index = whStmt.toLowerCase().indexOf("WHERE".toLowerCase());
627
687
  String stmt = whStmt.substring(index + 6);
628
- Integer fEqual = stmt.indexOf("=");
629
- String whereColName = stmt.substring(0, fEqual).trim();
630
- whereStmt = whStmt.replaceFirst(whereColName, colName);
688
+ if (withRefsNames.length == colNames.length) {
689
+ for (int i = 0; i < withRefsNames.length; i++) {
690
+ String colType = "withRefsNames";
691
+ int idx = stmt.indexOf(withRefsNames[i]);
692
+ if (idx == -1) {
693
+ idx = stmt.indexOf(colNames[i]);
694
+ colType = "colNames";
695
+ }
696
+ if (idx > -1) {
697
+ String valStr = "";
698
+ int fEqual = stmt.indexOf("=", idx);
699
+ if (fEqual > -1) {
700
+ int iAnd = stmt.indexOf("AND", fEqual);
701
+ int ilAnd = stmt.indexOf("and", fEqual);
702
+ if (iAnd > -1) {
703
+ valStr = (stmt.substring(fEqual + 1, iAnd - 1)).trim();
704
+ } else if (ilAnd > -1) {
705
+ valStr = (stmt.substring(fEqual + 1, ilAnd - 1)).trim();
706
+ } else {
707
+ valStr = (stmt.substring(fEqual + 1)).trim();
708
+ }
709
+ if (i > 0) {
710
+ whereStmt += " AND ";
711
+ }
712
+ if (colType.equals("withRefsNames")) {
713
+ whereStmt += colNames[i] + " = " + valStr;
714
+ } else {
715
+ whereStmt += withRefsNames[i] + " = " + valStr;
716
+ }
717
+ }
718
+ }
719
+ }
720
+ whereStmt = "WHERE " + whereStmt;
721
+ }
631
722
  }
632
723
  return whereStmt;
633
724
  }
@@ -640,21 +731,49 @@ public class Database {
640
731
  * @return
641
732
  * @throws Exception
642
733
  */
643
- public JSArray getReferences(Database mDB, String tableName) throws Exception {
734
+ public ArrayList<String> getReferences(Database mDB, String tableName) throws Exception {
644
735
  String sqlStmt =
645
736
  "SELECT sql FROM sqlite_master " +
646
- "WHERE sql LIKE('%REFERENCES%') AND " +
737
+ "WHERE sql LIKE('%FOREIGN KEY%') AND sql LIKE('%REFERENCES%') AND " +
647
738
  "sql LIKE('%" +
648
739
  tableName +
649
740
  "%') AND sql LIKE('%ON DELETE%');";
650
741
  try {
651
742
  JSArray references = mDB.selectSQL(sqlStmt, new ArrayList<Object>());
652
- return references;
743
+ ArrayList<String> retRefs = new ArrayList<String>();
744
+ if (references.length() > 0) {
745
+ retRefs = getRefs(references.getJSONObject(0).getString("sql"));
746
+ }
747
+ return retRefs;
653
748
  } catch (Exception e) {
654
749
  throw new Exception(e.getMessage());
655
750
  }
656
751
  }
657
752
 
753
+ /**
754
+ * GetRefs
755
+ *
756
+ * @param str
757
+ * @return
758
+ * @throws Exception
759
+ */
760
+ private ArrayList<String> getRefs(String str) throws Exception {
761
+ ArrayList<String> retRefs = new ArrayList<String>();
762
+ String[] arrFor = str.split("(?i)FOREIGN KEY", -1);
763
+ // Loop through Foreign Keys
764
+ for (int i = 1; i < arrFor.length; i++) {
765
+ retRefs.add((arrFor[i].split("(?i)ON DELETE", -1))[0].trim());
766
+ }
767
+ // find table name with references
768
+ if (str.substring(0, 12).toLowerCase().equals("CREATE TABLE".toLowerCase())) {
769
+ int oPar = str.indexOf("(");
770
+ String tableName = str.substring(13, oPar).trim();
771
+ retRefs.add(tableName);
772
+ }
773
+
774
+ return retRefs;
775
+ }
776
+
658
777
  /**
659
778
  * SelectSQL Method
660
779
  * Query a raw sql statement with or without binding values
@@ -786,7 +905,7 @@ public class Database {
786
905
  throw new Exception(e.getMessage());
787
906
  }
788
907
  } else {
789
- throw new Exception("No last_modified column in tables");
908
+ throw new Exception("No last_modified/sql_deleted columns in tables");
790
909
  }
791
910
  } else {
792
911
  retObj.put("changes", Integer.valueOf(0));
@@ -891,10 +1010,13 @@ public class Database {
891
1010
  inJson.setEncrypted(_encrypted);
892
1011
  inJson.setMode(mode);
893
1012
  try {
894
- // set the last export date
895
- Date date = new Date();
896
- long syncTime = date.getTime() / 1000L;
897
- toJson.setLastExportDate(this, syncTime);
1013
+ boolean isSyncTable = _uJson.isTableExists(this, "sync_table");
1014
+ if (isSyncTable) {
1015
+ // set the last export date
1016
+ Date date = new Date();
1017
+ long syncTime = date.getTime() / 1000L;
1018
+ toJson.setLastExportDate(this, syncTime);
1019
+ }
898
1020
  // launch the export process
899
1021
  JsonSQLite retJson = toJson.createExportObject(this, inJson);
900
1022
  // retJson.print();
@@ -74,7 +74,11 @@ public class UtilsMigrate {
74
74
  public String getFolder(Context context, String folderPath) throws Exception {
75
75
  String pathFiles = context.getFilesDir().getAbsolutePath();
76
76
  String pathDB = new File(context.getFilesDir().getParentFile(), "databases").getAbsolutePath();
77
- if (!folderPath.equals("default")) {
77
+ if (folderPath.equals("default")) {
78
+ pathFiles = pathDB;
79
+ } else if (folderPath.equalsIgnoreCase("cache")) {
80
+ pathFiles = context.getCacheDir().getAbsolutePath();
81
+ } else {
78
82
  String[] arr = folderPath.split("/", 2);
79
83
  if (arr.length == 2) {
80
84
  if (arr[0].equals("files")) {
@@ -85,8 +89,6 @@ public class UtilsMigrate {
85
89
  throw new Exception("Folder " + folderPath + " not allowed");
86
90
  }
87
91
  }
88
- } else {
89
- pathFiles = pathDB;
90
92
  }
91
93
  return pathFiles;
92
94
  }