@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.
@@ -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 < " + "OLD.last_modified BEGIN ")
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=OLD.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 = new ArrayList<>();
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
- if (tableNamesTypes.has("types")) {
391
- tColTypes = _uJson.getColumnNames(tableNamesTypes.get("types"));
392
- } else {
393
- throw new Exception("GetValues: Table " + tableName + " no types");
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
- // Loop on Table's Values
397
- for (int j = 0; j < values.size(); j++) {
398
- // Check the row number of columns
399
- ArrayList<Object> row = values.get(j);
400
- // Check row validity remove to accept RDBMS types
401
- // _uJson.checkRowValidity(mDb, tColNames, tColTypes, row, j, tableName);
402
- // Create INSERT or UPDATE Statements
403
- Boolean isRun = true;
404
- String stmt = createRowStatement(mDb, tColNames, tColTypes, row, j, tableName, mode);
405
- isRun = checkUpdate(mDb, stmt, row, tableName, tColNames, tColTypes);
406
- if (isRun) {
407
- // load the values
408
- if (stmt.substring(0, 6).toUpperCase().equals("DELETE")) {
409
- row = new ArrayList<>();
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
- return;
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
- * Check when UPDATE if the values are updated
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 createRowStatement(
520
- Database mDb,
521
- ArrayList<String> tColNames,
522
- ArrayList<String> tColTypes,
523
- ArrayList<Object> row,
524
- int j,
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
- boolean retIsIdExists = _uJson.isIdExists(mDb, tableName, tColNames.get(0), row.get(0));
535
- String stmt = "";
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
- if (key instanceof Integer) sbQuery.append(row.get(0)).append(";");
569
- if (key instanceof String) sbQuery.append("'").append(row.get(0)).append("';");
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
- if (key instanceof Integer) sbQuery.append(row.get(0)).append(";");
589
- if (key instanceof String) sbQuery.append("'").append(row.get(0)).append("';");
590
- stmt = sbQuery.toString();
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
- return stmt;
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
  /**
@@ -5760,7 +5760,6 @@ class CapacitorSQLite {
5760
5760
  this.versionUpgrades[dbName] = upgradeVersionDict;
5761
5761
  }
5762
5762
  }
5763
- console.log(`this.versionUpgrades: ${JSON.stringify(this.versionUpgrades)}`);
5764
5763
  return;
5765
5764
  }
5766
5765
  async copyFromAssets(options) {