@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.
@@ -342,7 +342,9 @@ class UtilsSQLite {
342
342
  if (trimStmt === 'DELETE FROM' &&
343
343
  stmt.toLowerCase().includes('WHERE'.toLowerCase())) {
344
344
  const whereStmt = `${stmt.trim()};`;
345
+ console.log(`in utilsSQLite execute whereStmt ${whereStmt}`);
345
346
  const rStmt = await this.deleteSQL(mDB, whereStmt, []);
347
+ console.log(`in utilsSQLite execute rStmt ${rStmt}`);
346
348
  resArr.push(rStmt);
347
349
  }
348
350
  else {
@@ -351,12 +353,14 @@ class UtilsSQLite {
351
353
  }
352
354
  sqlStmt = resArr.join(';');
353
355
  }
356
+ console.log(`in utilsSQLite execute sqlStmt ${sqlStmt}`);
354
357
  await this.execDB(mDB, sqlStmt);
355
358
  changes = (await this.dbChanges(mDB)) - initChanges;
356
359
  return Promise.resolve(changes);
357
360
  }
358
361
  catch (err) {
359
- return Promise.reject(`Execute: ${err.message}`);
362
+ const msg = err.message ? err.message : err;
363
+ return Promise.reject(`Execute: ${msg}`);
360
364
  }
361
365
  }
362
366
  /**
@@ -368,7 +372,8 @@ class UtilsSQLite {
368
372
  return new Promise((resolve, reject) => {
369
373
  mDB.exec(sql, async (err) => {
370
374
  if (err) {
371
- reject(`Execute: ${err.message}: `);
375
+ console.log(`in execDB err: ${JSON.stringify(err)}`);
376
+ reject(`Execute: ${err}: `);
372
377
  }
373
378
  resolve();
374
379
  });
@@ -440,9 +445,12 @@ class UtilsSQLite {
440
445
  async runExec(db, stmt, values = []) {
441
446
  return new Promise((resolve, reject) => {
442
447
  if (values != null && values.length > 0) {
448
+ console.log(`in runExec stmt: ${stmt} values: ${values}`);
443
449
  db.run(stmt, values, (err) => {
444
450
  if (err) {
445
- reject(err.message);
451
+ console.log(`in runExec err1: ${JSON.stringify(err)}`);
452
+ const msg = err.message ? err.message : err;
453
+ reject(msg);
446
454
  }
447
455
  else {
448
456
  resolve();
@@ -452,7 +460,9 @@ class UtilsSQLite {
452
460
  else {
453
461
  db.exec(stmt, (err) => {
454
462
  if (err) {
455
- reject(err.message);
463
+ console.log(`in runExec err2: ${JSON.stringify(err)}`);
464
+ const msg = err.message ? err.message : err;
465
+ reject(msg);
456
466
  }
457
467
  else {
458
468
  resolve();
@@ -499,13 +509,14 @@ class UtilsSQLite {
499
509
  .substring('DELETE FROM'.length)
500
510
  .trim();
501
511
  sqlStmt = `UPDATE ${tableName} SET sql_deleted = 1 ${clauseStmt}`;
512
+ console.log(`in deleteSQL sqlStmt: ${sqlStmt}`);
502
513
  // Find REFERENCES if any and update the sql_deleted column
503
514
  await this.findReferencesAndUpdate(db, tableName, clauseStmt, values);
504
515
  }
505
516
  return sqlStmt;
506
517
  }
507
518
  catch (err) {
508
- return Promise.reject(`DeleteSL: ${err}`);
519
+ return Promise.reject(`DeleteSQL: ${err}`);
509
520
  }
510
521
  }
511
522
  /**
@@ -519,27 +530,70 @@ class UtilsSQLite {
519
530
  async findReferencesAndUpdate(db, tableName, whereStmt, values) {
520
531
  try {
521
532
  const references = await this.getReferences(db, tableName);
533
+ if (references.length <= 0) {
534
+ return;
535
+ }
536
+ const tableNameWithRefs = references.pop();
537
+ console.log(`references: ${references}`);
538
+ console.log(`tableNameWithRefs: ${tableNameWithRefs}`);
522
539
  for (const refe of references) {
523
540
  // get the tableName of the reference
524
- const refTable = await this.getReferenceTableName(refe.sql);
541
+ const refTable = await this.getReferenceTableName(refe);
525
542
  if (refTable.length <= 0) {
526
543
  continue;
527
544
  }
528
- // get the columnName
529
- const colName = await this.getReferenceColumnName(refe.sql);
530
- if (colName.length <= 0) {
545
+ console.log(`refTable: ${refTable}`);
546
+ // get the with references columnName
547
+ const withRefsNames = await this.getWithRefsColumnName(refe);
548
+ console.log(`withRefsNames: ${withRefsNames}`);
549
+ if (withRefsNames.length <= 0) {
550
+ continue;
551
+ }
552
+ // get the referenced columnName
553
+ const colNames = await this.getReferencedColumnName(refe);
554
+ console.log(`colNames: ${colNames}`);
555
+ if (colNames.length <= 0) {
531
556
  continue;
532
557
  }
533
558
  // update the where clause
534
- const uWhereStmt = await this.updateWhere(whereStmt, colName);
559
+ const uWhereStmt = await this.updateWhere(whereStmt, withRefsNames, colNames);
560
+ console.log(`whereStmt: ${whereStmt}`);
561
+ console.log(`uWhereStmt: ${uWhereStmt}`);
535
562
  if (uWhereStmt.length <= 0) {
536
563
  continue;
537
564
  }
565
+ let updTableName = tableNameWithRefs;
566
+ let updColNames = colNames;
567
+ if (tableNameWithRefs === tableName) {
568
+ updTableName = refTable;
569
+ updColNames = withRefsNames;
570
+ }
571
+ console.log(`updTableName: ${updTableName}`);
572
+ console.log(`updColNames: ${updColNames}`);
538
573
  //update sql_deleted for this reference
539
- const stmt = `UPDATE ${refTable} SET sql_deleted = 1 ${uWhereStmt}`;
574
+ const stmt = 'UPDATE ' + updTableName + ' SET sql_deleted = 1 ' + uWhereStmt;
575
+ console.log(`stmt: ${stmt}`);
576
+ console.log(`values: ${values}`);
540
577
  if (values != null && values.length > 0) {
541
578
  const mVal = await this.replaceUndefinedByNull(values);
542
- await db.run(stmt, mVal);
579
+ let arrVal = whereStmt.split('?');
580
+ if (arrVal[arrVal.length - 1] === ';')
581
+ arrVal = arrVal.slice(0, -1);
582
+ console.log(`arrVal: ${arrVal}`);
583
+ const selValues = [];
584
+ for (const [j, val] of arrVal.entries()) {
585
+ console.log(`j: ${j} val: ${val}`);
586
+ for (const updVal of updColNames) {
587
+ const idxVal = val.indexOf(updVal);
588
+ console.log(`updVal: ${updVal} idxVal ${idxVal}`);
589
+ if (idxVal > -1) {
590
+ selValues.push(mVal[j]);
591
+ }
592
+ }
593
+ }
594
+ console.log(`*** stmt: ${selValues}`);
595
+ console.log(`*** selValues: ${selValues}`);
596
+ await db.run(stmt, selValues);
543
597
  }
544
598
  else {
545
599
  await db.exec(stmt);
@@ -558,51 +612,120 @@ class UtilsSQLite {
558
612
  }
559
613
  async getReferenceTableName(refValue) {
560
614
  let tableName = '';
561
- if (refValue.length > 0 &&
562
- refValue.substring(0, 12).toLowerCase() === 'CREATE TABLE'.toLowerCase()) {
563
- const oPar = refValue.indexOf('(');
564
- tableName = refValue.substring(13, oPar).trim();
615
+ if (refValue.length > 0) {
616
+ const arr = refValue.split(new RegExp('REFERENCES', 'i'));
617
+ if (arr.length === 2) {
618
+ const oPar = arr[1].indexOf('(');
619
+ tableName = arr[1].substring(0, oPar).trim();
620
+ }
565
621
  }
566
622
  return tableName;
567
623
  }
568
- async getReferenceColumnName(refValue) {
569
- let colName = '';
624
+ async getReferencedColumnName(refValue) {
625
+ let colNames = [];
570
626
  if (refValue.length > 0) {
571
- const index = refValue
572
- .toLowerCase()
573
- .indexOf('FOREIGN KEY'.toLowerCase());
574
- const stmt = refValue.substring(index + 12);
575
- const oPar = stmt.indexOf('(');
576
- const cPar = stmt.indexOf(')');
577
- colName = stmt.substring(oPar + 1, cPar).trim();
627
+ const arr = refValue.split(new RegExp('REFERENCES', 'i'));
628
+ if (arr.length === 2) {
629
+ const oPar = arr[1].indexOf('(');
630
+ const cPar = arr[1].indexOf(')');
631
+ const colStr = arr[1].substring(oPar + 1, cPar).trim();
632
+ colNames = colStr.split(',');
633
+ }
634
+ }
635
+ return colNames;
636
+ }
637
+ async getWithRefsColumnName(refValue) {
638
+ let colNames = [];
639
+ if (refValue.length > 0) {
640
+ const arr = refValue.split(new RegExp('REFERENCES', 'i'));
641
+ if (arr.length === 2) {
642
+ const oPar = arr[0].indexOf('(');
643
+ const cPar = arr[0].indexOf(')');
644
+ const colStr = arr[0].substring(oPar + 1, cPar).trim();
645
+ colNames = colStr.split(',');
646
+ }
578
647
  }
579
- return colName;
648
+ return colNames;
580
649
  }
581
- async updateWhere(whStmt, colName) {
650
+ async updateWhere(whStmt, withRefsNames, colNames) {
582
651
  let whereStmt = '';
583
652
  if (whStmt.length > 0) {
584
653
  const index = whStmt.toLowerCase().indexOf('WHERE'.toLowerCase());
585
654
  const stmt = whStmt.substring(index + 6);
586
- const fEqual = stmt.indexOf('=');
587
- const whereColName = stmt.substring(0, fEqual).trim();
588
- whereStmt = whStmt.replace(whereColName, colName);
655
+ if (withRefsNames.length === colNames.length) {
656
+ for (let i = 0; i < withRefsNames.length; i++) {
657
+ let colType = 'withRefsNames';
658
+ let idx = stmt.indexOf(withRefsNames[i]);
659
+ if (idx === -1) {
660
+ idx = stmt.indexOf(colNames[i]);
661
+ colType = 'colNames';
662
+ }
663
+ if (idx > -1) {
664
+ let valStr = '';
665
+ const fEqual = stmt.indexOf('=', idx);
666
+ if (fEqual > -1) {
667
+ const iAnd = stmt.indexOf('AND', fEqual);
668
+ const ilAnd = stmt.indexOf('and', fEqual);
669
+ if (iAnd > -1) {
670
+ valStr = stmt.substring(fEqual + 1, iAnd - 1).trim();
671
+ }
672
+ else if (ilAnd > -1) {
673
+ valStr = stmt.substring(fEqual + 1, ilAnd - 1).trim();
674
+ }
675
+ else {
676
+ valStr = stmt.substring(fEqual + 1, stmt.length).trim();
677
+ }
678
+ if (i > 0) {
679
+ whereStmt += ' AND ';
680
+ }
681
+ if (colType === 'withRefsNames') {
682
+ whereStmt += `${colNames[i]} = ${valStr}`;
683
+ }
684
+ else {
685
+ whereStmt += `${withRefsNames[i]} = ${valStr}`;
686
+ }
687
+ }
688
+ }
689
+ }
690
+ whereStmt = 'WHERE ' + whereStmt;
691
+ }
589
692
  }
590
693
  return whereStmt;
591
694
  }
592
695
  async getReferences(db, tableName) {
593
696
  const sqlStmt = 'SELECT sql FROM sqlite_master ' +
594
- "WHERE sql LIKE('%REFERENCES%') AND " +
697
+ "WHERE sql LIKE('%FOREIGN KEY%') AND sql LIKE('%REFERENCES%') AND " +
595
698
  "sql LIKE('%" +
596
699
  tableName +
597
700
  "%') AND sql LIKE('%ON DELETE%');";
598
701
  try {
599
702
  const res = await this.queryAll(db, sqlStmt, []);
600
- return Promise.resolve(res);
703
+ // get the reference's string(s)
704
+ let retRefs = [];
705
+ if (res.length > 0) {
706
+ retRefs = await this.getRefs(res[0].sql);
707
+ }
708
+ return Promise.resolve(retRefs);
601
709
  }
602
710
  catch (err) {
603
711
  return Promise.reject(new Error(`getReferences: ${err.message}`));
604
712
  }
605
713
  }
714
+ async getRefs(str) {
715
+ const retRefs = [];
716
+ const arrFor = str.split(new RegExp('FOREIGN KEY', 'i'));
717
+ // Loop through Foreign Keys
718
+ for (let i = 1; i < arrFor.length; i++) {
719
+ retRefs.push(arrFor[i].split(new RegExp('ON DELETE', 'i'))[0].trim());
720
+ }
721
+ // find table name with references
722
+ if (str.substring(0, 12).toLowerCase() === 'CREATE TABLE'.toLowerCase()) {
723
+ const oPar = str.indexOf('(');
724
+ const tableName = str.substring(13, oPar).trim();
725
+ retRefs.push(tableName);
726
+ }
727
+ return retRefs;
728
+ }
606
729
  /**
607
730
  * QueryAll
608
731
  * @param mDB
@@ -3509,7 +3632,7 @@ class Database {
3509
3632
  }
3510
3633
  }
3511
3634
  else {
3512
- throw new Error('No last_modified column in tables');
3635
+ throw new Error('No last_modified/sql_deleted columns in tables');
3513
3636
  }
3514
3637
  }
3515
3638
  else {
@@ -3768,7 +3891,10 @@ class Database {
3768
3891
  inJson.mode = mode;
3769
3892
  this.ensureDatabaseIsOpen();
3770
3893
  try {
3771
- await this.exportToJsonUtil.setLastExportDate(this.database, new Date().toISOString());
3894
+ const isTable = await this.jsonUtil.isTableExists(this.database, this._isDbOpen, 'sync_table');
3895
+ if (isTable) {
3896
+ await this.exportToJsonUtil.setLastExportDate(this.database, new Date().toISOString());
3897
+ }
3772
3898
  const jsonResult = await this.exportToJsonUtil.createExportObject(this.database, inJson);
3773
3899
  const keys = Object.keys(jsonResult);
3774
3900
  if (keys.length === 0) {