@capacitor-community/sqlite 4.1.0-6 → 4.1.0-8

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.
@@ -97,7 +97,7 @@ class SQLiteConnection {
97
97
  return Promise.reject(err);
98
98
  }
99
99
  }
100
- async createConnection(database, encrypted, mode, version) {
100
+ async createConnection(database, encrypted, mode, version, readonly) {
101
101
  try {
102
102
  if (database.endsWith('.db'))
103
103
  database = database.slice(0, -3);
@@ -106,39 +106,44 @@ class SQLiteConnection {
106
106
  encrypted,
107
107
  mode,
108
108
  version,
109
+ readonly,
109
110
  });
110
- const conn = new SQLiteDBConnection(database, this.sqlite);
111
- this._connectionDict.set(database, conn);
111
+ const conn = new SQLiteDBConnection(database, readonly, this.sqlite);
112
+ const connName = readonly ? `RO_${database}` : `RW_${database}`;
113
+ this._connectionDict.set(connName, conn);
112
114
  return Promise.resolve(conn);
113
115
  }
114
116
  catch (err) {
115
117
  return Promise.reject(err);
116
118
  }
117
119
  }
118
- async closeConnection(database) {
120
+ async closeConnection(database, readonly) {
119
121
  try {
120
122
  if (database.endsWith('.db'))
121
123
  database = database.slice(0, -3);
122
- await this.sqlite.closeConnection({ database });
123
- this._connectionDict.delete(database);
124
+ await this.sqlite.closeConnection({ database, readonly });
125
+ const connName = readonly ? `RO_${database}` : `RW_${database}`;
126
+ this._connectionDict.delete(connName);
124
127
  return Promise.resolve();
125
128
  }
126
129
  catch (err) {
127
130
  return Promise.reject(err);
128
131
  }
129
132
  }
130
- async isConnection(database) {
133
+ async isConnection(database, readonly) {
131
134
  const res = {};
132
135
  if (database.endsWith('.db'))
133
136
  database = database.slice(0, -3);
134
- res.result = this._connectionDict.has(database);
137
+ const connName = readonly ? `RO_${database}` : `RW_${database}`;
138
+ res.result = this._connectionDict.has(connName);
135
139
  return Promise.resolve(res);
136
140
  }
137
- async retrieveConnection(database) {
141
+ async retrieveConnection(database, readonly) {
138
142
  if (database.endsWith('.db'))
139
143
  database = database.slice(0, -3);
140
- if (this._connectionDict.has(database)) {
141
- const conn = this._connectionDict.get(database);
144
+ const connName = readonly ? `RO_${database}` : `RW_${database}`;
145
+ if (this._connectionDict.has(connName)) {
146
+ const conn = this._connectionDict.get(connName);
142
147
  if (typeof conn != 'undefined')
143
148
  return Promise.resolve(conn);
144
149
  else {
@@ -167,8 +172,9 @@ class SQLiteConnection {
167
172
  databasePath,
168
173
  version,
169
174
  });
170
- const conn = new SQLiteDBConnection(databasePath, this.sqlite);
171
- this._connectionDict.set(databasePath, conn);
175
+ const conn = new SQLiteDBConnection(databasePath, true, this.sqlite);
176
+ const connName = `RO_${databasePath})`;
177
+ this._connectionDict.set(connName, conn);
172
178
  return Promise.resolve(conn);
173
179
  }
174
180
  catch (err) {
@@ -178,7 +184,8 @@ class SQLiteConnection {
178
184
  async closeNCConnection(databasePath) {
179
185
  try {
180
186
  await this.sqlite.closeNCConnection({ databasePath });
181
- this._connectionDict.delete(databasePath);
187
+ const connName = `RO_${databasePath})`;
188
+ this._connectionDict.delete(connName);
182
189
  return Promise.resolve();
183
190
  }
184
191
  catch (err) {
@@ -187,12 +194,14 @@ class SQLiteConnection {
187
194
  }
188
195
  async isNCConnection(databasePath) {
189
196
  const res = {};
190
- res.result = this._connectionDict.has(databasePath);
197
+ const connName = `RO_${databasePath})`;
198
+ res.result = this._connectionDict.has(connName);
191
199
  return Promise.resolve(res);
192
200
  }
193
201
  async retrieveNCConnection(databasePath) {
194
202
  if (this._connectionDict.has(databasePath)) {
195
- const conn = this._connectionDict.get(databasePath);
203
+ const connName = `RO_${databasePath})`;
204
+ const conn = this._connectionDict.get(connName);
196
205
  if (typeof conn != 'undefined')
197
206
  return Promise.resolve(conn);
198
207
  else {
@@ -218,12 +227,14 @@ class SQLiteConnection {
218
227
  async closeAllConnections() {
219
228
  const delDict = new Map();
220
229
  try {
221
- for (const database of this._connectionDict.keys()) {
222
- await this.sqlite.closeConnection({ database });
223
- delDict.set(database, null);
230
+ for (const key of this._connectionDict.keys()) {
231
+ const database = key.substring(3);
232
+ const readonly = key.substring(0, 3) === ("RO_") ? true : false;
233
+ await this.sqlite.closeConnection({ database, readonly });
234
+ delDict.set(key, null);
224
235
  }
225
- for (const database of delDict.keys()) {
226
- this._connectionDict.delete(database);
236
+ for (const key of delDict.keys()) {
237
+ this._connectionDict.delete(key);
227
238
  }
228
239
  return Promise.resolve();
229
240
  }
@@ -347,16 +358,23 @@ class SQLiteConnection {
347
358
  * SQLiteDBConnection Class
348
359
  */
349
360
  class SQLiteDBConnection {
350
- constructor(dbName, sqlite) {
361
+ constructor(dbName, readonly, sqlite) {
351
362
  this.dbName = dbName;
363
+ this.readonly = readonly;
352
364
  this.sqlite = sqlite;
353
365
  }
354
366
  getConnectionDBName() {
355
367
  return this.dbName;
356
368
  }
369
+ getConnectionReadOnly() {
370
+ return this.readonly;
371
+ }
357
372
  async open() {
358
373
  try {
359
- await this.sqlite.open({ database: this.dbName });
374
+ await this.sqlite.open({
375
+ database: this.dbName,
376
+ readonly: this.readonly,
377
+ });
360
378
  return Promise.resolve();
361
379
  }
362
380
  catch (err) {
@@ -365,7 +383,10 @@ class SQLiteDBConnection {
365
383
  }
366
384
  async close() {
367
385
  try {
368
- await this.sqlite.close({ database: this.dbName });
386
+ await this.sqlite.close({
387
+ database: this.dbName,
388
+ readonly: this.readonly,
389
+ });
369
390
  return Promise.resolve();
370
391
  }
371
392
  catch (err) {
@@ -376,6 +397,7 @@ class SQLiteDBConnection {
376
397
  try {
377
398
  const res = await this.sqlite.getUrl({
378
399
  database: this.dbName,
400
+ readonly: this.readonly,
379
401
  });
380
402
  return Promise.resolve(res);
381
403
  }
@@ -387,6 +409,7 @@ class SQLiteDBConnection {
387
409
  try {
388
410
  const version = await this.sqlite.getVersion({
389
411
  database: this.dbName,
412
+ readonly: this.readonly,
390
413
  });
391
414
  return Promise.resolve(version);
392
415
  }
@@ -398,6 +421,7 @@ class SQLiteDBConnection {
398
421
  try {
399
422
  const res = await this.sqlite.getTableList({
400
423
  database: this.dbName,
424
+ readonly: this.readonly,
401
425
  });
402
426
  return Promise.resolve(res);
403
427
  }
@@ -407,12 +431,18 @@ class SQLiteDBConnection {
407
431
  }
408
432
  async execute(statements, transaction = true) {
409
433
  try {
410
- const res = await this.sqlite.execute({
411
- database: this.dbName,
412
- statements: statements,
413
- transaction: transaction,
414
- });
415
- return Promise.resolve(res);
434
+ if (!this.readonly) {
435
+ const res = await this.sqlite.execute({
436
+ database: this.dbName,
437
+ statements: statements,
438
+ transaction: transaction,
439
+ readonly: false,
440
+ });
441
+ return Promise.resolve(res);
442
+ }
443
+ else {
444
+ return Promise.reject('not allowed in read-only mode');
445
+ }
416
446
  }
417
447
  catch (err) {
418
448
  return Promise.reject(err);
@@ -426,6 +456,7 @@ class SQLiteDBConnection {
426
456
  database: this.dbName,
427
457
  statement: statement,
428
458
  values: values,
459
+ readonly: this.readonly,
429
460
  });
430
461
  }
431
462
  else {
@@ -433,6 +464,7 @@ class SQLiteDBConnection {
433
464
  database: this.dbName,
434
465
  statement: statement,
435
466
  values: [],
467
+ readonly: this.readonly,
436
468
  });
437
469
  }
438
470
  if (res && typeof res.values[0] === 'object') {
@@ -460,24 +492,31 @@ class SQLiteDBConnection {
460
492
  async run(statement, values, transaction = true) {
461
493
  let res;
462
494
  try {
463
- if (values && values.length > 0) {
464
- res = await this.sqlite.run({
465
- database: this.dbName,
466
- statement: statement,
467
- values: values,
468
- transaction: transaction,
469
- });
470
- // }
495
+ if (!this.readonly) {
496
+ if (values && values.length > 0) {
497
+ res = await this.sqlite.run({
498
+ database: this.dbName,
499
+ statement: statement,
500
+ values: values,
501
+ transaction: transaction,
502
+ readonly: false,
503
+ });
504
+ // }
505
+ }
506
+ else {
507
+ res = await this.sqlite.run({
508
+ database: this.dbName,
509
+ statement: statement,
510
+ values: [],
511
+ transaction: transaction,
512
+ readonly: false,
513
+ });
514
+ }
515
+ return Promise.resolve(res);
471
516
  }
472
517
  else {
473
- res = await this.sqlite.run({
474
- database: this.dbName,
475
- statement: statement,
476
- values: [],
477
- transaction: transaction,
478
- });
518
+ return Promise.reject('not allowed in read-only mode');
479
519
  }
480
- return Promise.resolve(res);
481
520
  }
482
521
  catch (err) {
483
522
  return Promise.reject(err);
@@ -485,13 +524,19 @@ class SQLiteDBConnection {
485
524
  }
486
525
  async executeSet(set, transaction = true) {
487
526
  try {
488
- const res = await this.sqlite.executeSet({
489
- database: this.dbName,
490
- set: set,
491
- transaction: transaction,
492
- });
493
- // }
494
- return Promise.resolve(res);
527
+ if (!this.readonly) {
528
+ const res = await this.sqlite.executeSet({
529
+ database: this.dbName,
530
+ set: set,
531
+ transaction: transaction,
532
+ readonly: false,
533
+ });
534
+ // }
535
+ return Promise.resolve(res);
536
+ }
537
+ else {
538
+ return Promise.reject('not allowed in read-only mode');
539
+ }
495
540
  }
496
541
  catch (err) {
497
542
  return Promise.reject(err);
@@ -501,6 +546,7 @@ class SQLiteDBConnection {
501
546
  try {
502
547
  const res = await this.sqlite.isDBExists({
503
548
  database: this.dbName,
549
+ readonly: this.readonly,
504
550
  });
505
551
  return Promise.resolve(res);
506
552
  }
@@ -513,6 +559,7 @@ class SQLiteDBConnection {
513
559
  const res = await this.sqlite.isTableExists({
514
560
  database: this.dbName,
515
561
  table: table,
562
+ readonly: this.readonly,
516
563
  });
517
564
  return Promise.resolve(res);
518
565
  }
@@ -524,6 +571,7 @@ class SQLiteDBConnection {
524
571
  try {
525
572
  const res = await this.sqlite.isDBOpen({
526
573
  database: this.dbName,
574
+ readonly: this.readonly,
527
575
  });
528
576
  return Promise.resolve(res);
529
577
  }
@@ -533,8 +581,16 @@ class SQLiteDBConnection {
533
581
  }
534
582
  async delete() {
535
583
  try {
536
- await this.sqlite.deleteDatabase({ database: this.dbName });
537
- return Promise.resolve();
584
+ if (!this.readonly) {
585
+ await this.sqlite.deleteDatabase({
586
+ database: this.dbName,
587
+ readonly: false,
588
+ });
589
+ return Promise.resolve();
590
+ }
591
+ else {
592
+ return Promise.reject('not allowed in read-only mode');
593
+ }
538
594
  }
539
595
  catch (err) {
540
596
  return Promise.reject(err);
@@ -542,10 +598,16 @@ class SQLiteDBConnection {
542
598
  }
543
599
  async createSyncTable() {
544
600
  try {
545
- const res = await this.sqlite.createSyncTable({
546
- database: this.dbName,
547
- });
548
- return Promise.resolve(res);
601
+ if (!this.readonly) {
602
+ const res = await this.sqlite.createSyncTable({
603
+ database: this.dbName,
604
+ readonly: false,
605
+ });
606
+ return Promise.resolve(res);
607
+ }
608
+ else {
609
+ return Promise.reject('not allowed in read-only mode');
610
+ }
549
611
  }
550
612
  catch (err) {
551
613
  return Promise.reject(err);
@@ -553,11 +615,17 @@ class SQLiteDBConnection {
553
615
  }
554
616
  async setSyncDate(syncdate) {
555
617
  try {
556
- await this.sqlite.setSyncDate({
557
- database: this.dbName,
558
- syncdate: syncdate,
559
- });
560
- return Promise.resolve();
618
+ if (!this.readonly) {
619
+ await this.sqlite.setSyncDate({
620
+ database: this.dbName,
621
+ syncdate: syncdate,
622
+ readonly: false,
623
+ });
624
+ return Promise.resolve();
625
+ }
626
+ else {
627
+ return Promise.reject('not allowed in read-only mode');
628
+ }
561
629
  }
562
630
  catch (err) {
563
631
  return Promise.reject(err);
@@ -567,6 +635,7 @@ class SQLiteDBConnection {
567
635
  try {
568
636
  const res = await this.sqlite.getSyncDate({
569
637
  database: this.dbName,
638
+ readonly: this.readonly,
570
639
  });
571
640
  let retDate = '';
572
641
  if (res.syncDate > 0)
@@ -582,6 +651,7 @@ class SQLiteDBConnection {
582
651
  const res = await this.sqlite.exportToJson({
583
652
  database: this.dbName,
584
653
  jsonexportmode: mode,
654
+ readonly: this.readonly,
585
655
  });
586
656
  return Promise.resolve(res);
587
657
  }
@@ -591,8 +661,16 @@ class SQLiteDBConnection {
591
661
  }
592
662
  async deleteExportedRows() {
593
663
  try {
594
- await this.sqlite.deleteExportedRows({ database: this.dbName });
595
- return Promise.resolve();
664
+ if (!this.readonly) {
665
+ await this.sqlite.deleteExportedRows({
666
+ database: this.dbName,
667
+ readonly: false,
668
+ });
669
+ return Promise.resolve();
670
+ }
671
+ else {
672
+ return Promise.reject('not allowed in read-only mode');
673
+ }
596
674
  }
597
675
  catch (err) {
598
676
  return Promise.reject(err);
@@ -600,55 +678,65 @@ class SQLiteDBConnection {
600
678
  }
601
679
  async executeTransaction(txn) {
602
680
  try {
603
- const ret = await this.sqlite.execute({
604
- database: this.dbName,
605
- statements: 'BEGIN TRANSACTION;',
606
- transaction: false,
607
- });
608
- if (ret.changes.changes < 0) {
609
- return Promise.reject('Error in BEGIN TRANSACTION');
610
- }
611
- for (const task of txn) {
612
- if (task.values && task.values.length > 0) {
613
- const ret = await this.sqlite.run({
614
- database: this.dbName,
615
- statement: task.statement,
616
- values: task.values,
617
- transaction: false,
618
- });
619
- if (ret.changes.lastId === -1) {
620
- await this.execute('ROLLBACK;', false);
621
- return Promise.reject('Error in transaction run ');
622
- }
681
+ if (!this.readonly) {
682
+ const ret = await this.sqlite.execute({
683
+ database: this.dbName,
684
+ statements: 'BEGIN TRANSACTION;',
685
+ transaction: false,
686
+ });
687
+ if (ret.changes.changes < 0) {
688
+ return Promise.reject('Error in BEGIN TRANSACTION');
623
689
  }
624
- else {
625
- const ret = await this.sqlite.execute({
626
- database: this.dbName,
627
- statements: task.statement,
628
- transaction: false,
629
- });
630
- if (ret.changes.changes < 0) {
631
- await this.sqlite.execute({
690
+ for (const task of txn) {
691
+ if (task.values && task.values.length > 0) {
692
+ const ret = await this.sqlite.run({
693
+ database: this.dbName,
694
+ statement: task.statement,
695
+ values: task.values,
696
+ transaction: false,
697
+ readonly: false,
698
+ });
699
+ if (ret.changes.lastId === -1) {
700
+ await this.execute('ROLLBACK;', false);
701
+ return Promise.reject('Error in transaction run ');
702
+ }
703
+ }
704
+ else {
705
+ const ret = await this.sqlite.execute({
632
706
  database: this.dbName,
633
- statements: 'ROLLBACK;',
707
+ statements: task.statement,
634
708
  transaction: false,
709
+ readonly: false,
635
710
  });
636
- return Promise.reject('Error in transaction execute ');
711
+ if (ret.changes.changes < 0) {
712
+ await this.sqlite.execute({
713
+ database: this.dbName,
714
+ statements: 'ROLLBACK;',
715
+ transaction: false,
716
+ readonly: false,
717
+ });
718
+ return Promise.reject('Error in transaction execute ');
719
+ }
637
720
  }
638
721
  }
722
+ await this.sqlite.execute({
723
+ database: this.dbName,
724
+ statements: 'COMMIT;',
725
+ transaction: false,
726
+ readonly: false,
727
+ });
728
+ return Promise.resolve();
729
+ }
730
+ else {
731
+ return Promise.reject('not allowed in read-only mode');
639
732
  }
640
- await this.sqlite.execute({
641
- database: this.dbName,
642
- statements: 'COMMIT;',
643
- transaction: false,
644
- });
645
- return Promise.resolve();
646
733
  }
647
734
  catch (err) {
648
735
  await this.sqlite.execute({
649
736
  database: this.dbName,
650
737
  statements: 'ROLLBACK;',
651
738
  transaction: false,
739
+ readonly: false,
652
740
  });
653
741
  return Promise.reject(err);
654
742
  }