@capacitor-community/sqlite 5.6.1-3 → 5.6.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.
@@ -12,6 +12,8 @@ import com.getcapacitor.community.database.sqlite.SQLite.UtilsSQLite;
12
12
  import java.util.ArrayList;
13
13
  import java.util.HashMap;
14
14
  import java.util.Map;
15
+
16
+ import org.json.JSONArray;
15
17
  import org.json.JSONException;
16
18
  import org.json.JSONObject;
17
19
 
@@ -375,7 +377,7 @@ public class ImportFromJson {
375
377
  if (!isTable) {
376
378
  throw new Exception("createTableData: Table " + tableName + "does not exist");
377
379
  }
378
- // Get the Column's Name and Type
380
+ // Get the Column's Name and Types
379
381
  try {
380
382
  JSObject tableNamesTypes = _uJson.getTableColumnNamesTypes(mDb, tableName);
381
383
  if (tableNamesTypes.length() == 0) {
@@ -387,19 +389,120 @@ public class ImportFromJson {
387
389
  } else {
388
390
  throw new Exception("GetValues: Table " + tableName + " no names");
389
391
  }
390
- // New process flow
392
+ ArrayList<String> tColTypes;
393
+ if (tableNamesTypes.has("types")) {
394
+ tColTypes = _uJson.getColumnNames(tableNamesTypes.get("types"));
395
+ } else {
396
+ throw new Exception("GetValues: Table " + tableName + " no types");
397
+ }
398
+ if (isBlob(tColTypes)) {
399
+ // Old process flow
400
+ oldProcessFow(mDb, values, tableName,tColNames, tColTypes, mode);
401
+ } else {
402
+ // New process flow
403
+ newProcessFlow(mDb, values, tableName,tColNames);
404
+ }
405
+ } catch (JSONException e) {
406
+ throw new Exception("CreateTableData: " + e.getMessage());
407
+ } catch (Exception e) {
408
+ throw new Exception("CreateTableData: " + e.getMessage());
409
+ }
410
+ }
411
+
412
+ /**
413
+ * Use the old process flow for INSERT, UPDATE and DELETE
414
+ * One by one row values
415
+ * @param mDb
416
+ * @param values
417
+ * @param tableName
418
+ * @param tColNames
419
+ * @param tColTypes
420
+ * @param mode
421
+ * @throws Exception
422
+ */
423
+ private void oldProcessFow( Database mDb, ArrayList<ArrayList<Object>> values,
424
+ String tableName, ArrayList<String> tColNames,
425
+ ArrayList<String> tColTypes, String mode) throws Exception {
426
+ try {
427
+ // Loop on table's value
428
+ for (int j = 0; j < values.size(); j++) {
429
+
430
+ // Check the row number of columns
431
+ ArrayList<Object> row = createRowValues(values.get(j));
432
+ //
433
+ // Create INSERT or UPDATE Statements
434
+ Boolean isRun = true;
435
+ String stmt = createRowStatement(mDb, tColNames, tColTypes, row, j, tableName, mode);
436
+ isRun = checkUpdate(mDb, stmt, row, tableName, tColNames, tColTypes);
437
+ if (isRun) {
438
+ // load the values
439
+ if (stmt.substring(0, 6).toUpperCase().equals("DELETE")) {
440
+ row = new ArrayList<>();
441
+ }
442
+ JSObject retObj = mDb.prepareSQL(stmt, row, true, "no");
443
+ long lastId = retObj.getLong("lastId");
444
+ if (lastId < 0) {
445
+ throw new Exception("CreateTableData: lastId < 0");
446
+ }
447
+ }
448
+ }
449
+ } catch (JSONException e) {
450
+ throw new Exception("oldProcessFlow: " + e.getMessage());
451
+ } catch (Exception e) {
452
+ throw new Exception("oldProcessFlow: " + e.getMessage());
453
+ }
454
+ }
455
+ private ArrayList<Object> createRowValues(ArrayList<Object> row )
456
+ throws Exception {
457
+ try {
458
+ // Iterate over the ArrayList and check for JSONArray objects
459
+ for (int i = 0; i < row.size(); i++) {
460
+ Object obj = row.get(i);
461
+ if (obj instanceof JSONArray) {
462
+ JSONArray jsonArrayObj = (JSONArray) obj;
463
+ byte[] byteArray = jsonArrayToByteArray(jsonArrayObj);
464
+ // Replace the JSONArray object with the corresponding byte[] in the ArrayList
465
+ row.set(i, byteArray);
466
+ }
467
+ }
468
+ return row;
469
+ } catch (JSONException e) {
470
+ throw new Exception("createRowValues: " + e.getMessage());
471
+ } catch (Exception e) {
472
+ throw new Exception("createRowValues: " + e.getMessage());
473
+ }
474
+ }
475
+ private byte[] jsonArrayToByteArray(JSONArray jsonArray) throws JSONException {
476
+ byte[] byteArray = new byte[jsonArray.length()];
477
+ for (int i = 0; i < jsonArray.length(); i++) {
478
+ byteArray[i] = (byte) jsonArray.getInt(i);
479
+ }
480
+ return byteArray;
481
+ }
482
+
483
+ /**
484
+ * Use the new process flow for INSERT, UPDATE and DELETE
485
+ * @param mDb
486
+ * @param values
487
+ * @param tableName
488
+ * @param tColNames
489
+ * @throws Exception
490
+ */
491
+ private void newProcessFlow(Database mDb, ArrayList<ArrayList<Object>> values,
492
+ String tableName, ArrayList<String> tColNames) throws Exception {
493
+ try {
391
494
  JSONObject retObjStrs = generateInsertAndDeletedStrings(tColNames, values);
392
495
  // Create the statement for INSERT
393
496
  String namesString = _uJson.convertToString(tColNames, ',');
394
497
  if (retObjStrs.has("insert")) {
395
498
  String stmtInsert = new StringBuilder("INSERT OR REPLACE INTO ")
396
- .append(tableName)
397
- .append("(")
398
- .append(namesString)
399
- .append(") ")
400
- .append(retObjStrs.get("insert"))
401
- .append(";")
402
- .toString();
499
+ .append(tableName)
500
+ .append("(")
501
+ .append(namesString)
502
+ .append(") ")
503
+ .append(retObjStrs.get("insert"))
504
+ .append(";")
505
+ .toString();
403
506
  JSObject retObj = mDb.prepareSQL(stmtInsert, new ArrayList<>(), true, "no");
404
507
  long lastId = retObj.getLong("lastId");
405
508
  if (lastId < 0) {
@@ -408,23 +511,207 @@ public class ImportFromJson {
408
511
  }
409
512
  if (retObjStrs.has("delete")) {
410
513
  String stmtDelete = new StringBuilder("DELETE FROM ")
411
- .append(tableName)
412
- .append(" WHERE ")
413
- .append(tColNames.get(0))
414
- .append(" ")
415
- .append(retObjStrs.get("delete"))
416
- .append(";")
417
- .toString();
514
+ .append(tableName)
515
+ .append(" WHERE ")
516
+ .append(tColNames.get(0))
517
+ .append(" ")
518
+ .append(retObjStrs.get("delete"))
519
+ .append(";")
520
+ .toString();
418
521
  JSObject retObj = mDb.prepareSQL(stmtDelete, new ArrayList<>(), true, "no");
419
522
  long lastId = retObj.getLong("lastId");
420
523
  if (lastId < 0) {
421
- throw new Exception("CreateTableData: INSERT lastId < 0");
524
+ throw new Exception("newProcessFlow: INSERT lastId < 0");
422
525
  }
423
526
  }
424
527
  } catch (JSONException e) {
425
- throw new Exception("CreateTableData: " + e.getMessage());
528
+ throw new Exception("newProcessFlow: " + e.getMessage());
426
529
  } catch (Exception e) {
427
- throw new Exception("CreateTableData: " + e.getMessage());
530
+ throw new Exception("newProcessFlow: " + e.getMessage());
531
+ }
532
+ }
533
+
534
+ /**
535
+ * Check if there is a BLOB types
536
+ * @param tColTypes
537
+ * @return
538
+ */
539
+ private Boolean isBlob(ArrayList<String> tColTypes) {
540
+ boolean containsBlob = false;
541
+ for (String str : tColTypes) {
542
+ if (str.equalsIgnoreCase("BLOB")) {
543
+ containsBlob = true;
544
+ break;
545
+ }
546
+ }
547
+ return containsBlob;
548
+ }
549
+ /**
550
+ * Create the Row Statement to load the data
551
+ * @param mDb
552
+ * @param tColNames
553
+ * @param tColTypes
554
+ * @param row
555
+ * @param j
556
+ * @param tableName
557
+ * @param mode
558
+ * @return
559
+ */
560
+ private String createRowStatement(
561
+ Database mDb,
562
+ ArrayList<String> tColNames,
563
+ ArrayList<String> tColTypes,
564
+ ArrayList<Object> row,
565
+ int j,
566
+ String tableName,
567
+ String mode
568
+ ) throws Exception {
569
+ String msg = "CreateRowStatement: ";
570
+ msg += "Table" + tableName + " values row";
571
+ if (tColNames.size() != row.size() || row.size() == 0 || tColNames.size() == 0) {
572
+ throw new Exception(msg + j + " not correct length");
573
+ }
574
+
575
+ boolean retIsIdExists = _uJson.isIdExists(mDb, tableName, tColNames.get(0), row.get(0));
576
+ String stmt = "";
577
+ // Create INSERT or UPDATE Statements
578
+ if (mode.equals("full") || (mode.equals("partial") && !retIsIdExists)) {
579
+ // Insert
580
+ String namesString = _uJson.convertToString(tColNames, ',');
581
+ String questionMarkString = _uJson.createQuestionMarkString(tColNames.size());
582
+ if (questionMarkString.length() == 0) {
583
+ throw new Exception(msg + j + "questionMarkString is empty");
584
+ }
585
+ stmt =
586
+ new StringBuilder("INSERT INTO ")
587
+ .append(tableName)
588
+ .append("(")
589
+ .append(namesString)
590
+ .append(")")
591
+ .append(" VALUES (")
592
+ .append(questionMarkString)
593
+ .append(");")
594
+ .toString();
595
+ } else {
596
+ Boolean isUpdate = true;
597
+ Integer idxDelete = tColNames.indexOf("sql_deleted");
598
+ if (idxDelete >= 0) {
599
+ if (row.get(idxDelete).equals(1)) {
600
+ // Delete
601
+ isUpdate = false;
602
+ Object key = tColNames.get(0);
603
+ StringBuilder sbQuery = new StringBuilder("DELETE FROM ")
604
+ .append(tableName)
605
+ .append(" WHERE ")
606
+ .append(tColNames.get(0))
607
+ .append(" = ");
608
+
609
+ if (key instanceof Integer) sbQuery.append(row.get(0)).append(";");
610
+ if (key instanceof String) sbQuery.append("'").append(row.get(0)).append("';");
611
+ stmt = sbQuery.toString();
612
+ }
613
+ }
614
+ if (isUpdate) {
615
+ // Update
616
+ String setString = _uJson.setNameForUpdate(tColNames);
617
+ if (setString.length() == 0) {
618
+ throw new Exception(msg + j + "setString is empty");
619
+ }
620
+ Object key = tColNames.get(0);
621
+ StringBuilder sbQuery = new StringBuilder("UPDATE ")
622
+ .append(tableName)
623
+ .append(" SET ")
624
+ .append(setString)
625
+ .append(" WHERE ")
626
+ .append(tColNames.get(0))
627
+ .append(" = ");
628
+
629
+ if (key instanceof Integer) sbQuery.append(row.get(0)).append(";");
630
+ if (key instanceof String) sbQuery.append("'").append(row.get(0)).append("';");
631
+ stmt = sbQuery.toString();
632
+ }
633
+ }
634
+ return stmt;
635
+ }
636
+
637
+ /**
638
+ * Check when UPDATE if the values are updated
639
+ * @param mDb
640
+ * @param stmt
641
+ * @param values
642
+ * @param tableName
643
+ * @param tColNames
644
+ * @param tColTypes
645
+ * @return
646
+ * @throws Exception
647
+ */
648
+ private Boolean checkUpdate(
649
+ Database mDb,
650
+ String stmt,
651
+ ArrayList<Object> values,
652
+ String tableName,
653
+ ArrayList<String> tColNames,
654
+ ArrayList<String> tColTypes
655
+ ) throws Exception {
656
+ Boolean isRun = true;
657
+ if (stmt.substring(0, 6).equals("UPDATE")) {
658
+ StringBuilder sbQuery = new StringBuilder("SELECT * FROM ").append(tableName).append(" WHERE ").append(tColNames.get(0));
659
+
660
+ if (values.get(0) instanceof String) {
661
+ sbQuery.append(" = '").append(values.get(0)).append("';");
662
+ } else {
663
+ sbQuery.append(" = ").append(values.get(0)).append(";");
664
+ }
665
+ String query = sbQuery.toString();
666
+
667
+ try {
668
+ ArrayList<ArrayList<Object>> resValues = _uJson.getValues(mDb, query, tableName);
669
+ if (resValues.size() > 0) {
670
+ isRun = checkValues(values, resValues.get(0));
671
+ } else {
672
+ throw new Exception("CheckUpdate: CheckUpdate statement returns nothing");
673
+ }
674
+ } catch (Exception e) {
675
+ throw new Exception("CheckUpdate: " + e.getMessage());
676
+ }
677
+ }
678
+ return isRun;
679
+ }
680
+ /**
681
+ * Check Values
682
+ * @param values
683
+ * @param nValues
684
+ * @return
685
+ * @throws Exception
686
+ */
687
+ private Boolean checkValues(ArrayList<Object> values, ArrayList<Object> nValues) throws Exception {
688
+ if (values.size() > 0 && nValues.size() > 0 && values.size() == nValues.size()) {
689
+ for (int i = 0; i < values.size(); i++) {
690
+ if (nValues.get(i) instanceof String) {
691
+ if (!values.get(i).equals(nValues.get(i))) {
692
+ return true;
693
+ }
694
+ } else if (nValues.get(i) instanceof Long && values.get(i) instanceof Integer) {
695
+ // int iVal = (Integer) values.get(i);
696
+ long lVal = (Integer) values.get(i);
697
+ // long nlVal = (Long) nValues.get(i);
698
+ if (lVal != (Long) nValues.get(i)) {
699
+ return true;
700
+ }
701
+ } else if (nValues.get(i) instanceof Double && values.get(i) instanceof Integer) {
702
+ double dVal = (Integer) values.get(i);
703
+ if (dVal != (Double) nValues.get(i)) {
704
+ return true;
705
+ }
706
+ } else {
707
+ if (values.get(i) != nValues.get(i)) {
708
+ return true;
709
+ }
710
+ }
711
+ }
712
+ return false;
713
+ } else {
714
+ throw new Exception("CheckValues: Both arrays not the same length");
428
715
  }
429
716
  }
430
717
 
@@ -617,7 +617,7 @@ export class SQLiteDBConnection {
617
617
  statement: statement,
618
618
  values: values,
619
619
  readonly: this.readonly,
620
- isSql92: true,
620
+ isSQL92: true,
621
621
  });
622
622
  }
623
623
  else {
@@ -623,7 +623,7 @@ class SQLiteDBConnection {
623
623
  statement: statement,
624
624
  values: values,
625
625
  readonly: this.readonly,
626
- isSql92: true,
626
+ isSQL92: true,
627
627
  });
628
628
  }
629
629
  else {