1
- import type {
2
- capSQLiteVersionUpgrade ,
3
- JsonSQLite ,
4
- EncryptJson ,
5
- Changes ,
6
- } from '../../../src/definitions' ;
1
+ import type { capSQLiteVersionUpgrade , JsonSQLite , EncryptJson , Changes } from '../../../src/definitions' ;
7
2
import { GlobalSQLite } from '../GlobalSQLite' ;
8
3
9
4
import { ExportToJson } from './ImportExportJson/exportToJson' ;
@@ -49,7 +44,7 @@ export class Database {
49
44
isEncryption : boolean ,
50
45
readonly : boolean ,
51
46
upgDict : Record < number , capSQLiteVersionUpgrade > ,
52
- globalUtil ?: GlobalSQLite ,
47
+ globalUtil ?: GlobalSQLite
53
48
) {
54
49
this . dbName = dbName ;
55
50
this . _encrypted = encrypted ;
@@ -63,8 +58,7 @@ export class Database {
63
58
this . isTransactionActive = false ;
64
59
this . globalUtil = globalUtil ? globalUtil : new GlobalSQLite ( ) ;
65
60
66
- if ( this . pathDB . length === 0 )
67
- throw new Error ( 'Could not generate a path to ' + dbName ) ;
61
+ if ( this . pathDB . length === 0 ) throw new Error ( 'Could not generate a path to ' + dbName ) ;
68
62
console . log ( `&&& Databases path: ${ this . pathDB } ` ) ;
69
63
}
70
64
/**
@@ -86,12 +80,7 @@ export class Database {
86
80
this . _isDbOpen = false ;
87
81
let password = '' ;
88
82
try {
89
- if (
90
- this . _encrypted &&
91
- ( this . _mode === 'secret' ||
92
- this . _mode === 'encryption' ||
93
- this . _mode === 'decryption' )
94
- ) {
83
+ if ( this . _encrypted && ( this . _mode === 'secret' || this . _mode === 'encryption' || this . _mode === 'decryption' ) ) {
95
84
password = this . secretUtil . getPassphrase ( ) ;
96
85
97
86
if ( password . length <= 0 ) {
@@ -105,31 +94,16 @@ export class Database {
105
94
await this . encryptionUtil . decryptDatabase ( this . pathDB , password ) ;
106
95
password = '' ;
107
96
}
108
- this . database = this . sqliteUtil . openOrCreateDatabase (
109
- this . pathDB ,
110
- password ,
111
- this . readonly ,
112
- ) ;
97
+ this . database = this . sqliteUtil . openOrCreateDatabase ( this . pathDB , password , this . readonly ) ;
113
98
this . _isDbOpen = true ;
114
99
if ( ! this . readonly ) {
115
100
const curVersion : number = this . sqliteUtil . getVersion ( this . database ) ;
116
- if (
117
- this . version > curVersion &&
118
- Object . keys ( this . upgradeVersionDict ) . length > 0
119
- ) {
101
+ if ( this . version > curVersion && Object . keys ( this . upgradeVersionDict ) . length > 0 ) {
120
102
try {
121
- await this . fileUtil . copyFileName (
122
- this . dbName ,
123
- `backup-${ this . dbName } ` ,
124
- ) ;
103
+ await this . fileUtil . copyFileName ( this . dbName , `backup-${ this . dbName } ` ) ;
125
104
126
105
// execute the upgrade flow process
127
- await this . upgradeUtil . onUpgrade (
128
- this ,
129
- this . upgradeVersionDict ,
130
- curVersion ,
131
- this . version ,
132
- ) ;
106
+ await this . upgradeUtil . onUpgrade ( this , this . upgradeVersionDict , curVersion , this . version ) ;
133
107
// delete the backup database
134
108
await this . fileUtil . deleteFileName ( `backup-${ this . dbName } ` ) ;
135
109
} catch ( err ) {
@@ -309,11 +283,7 @@ export class Database {
309
283
const isOpen : boolean = this . _isDbOpen ;
310
284
311
285
try {
312
- const tableExistsResult = this . jsonUtil . isTableExists (
313
- this . database ,
314
- isOpen ,
315
- tableName ,
316
- ) ;
286
+ const tableExistsResult = this . jsonUtil . isTableExists ( this . database , isOpen , tableName ) ;
317
287
return tableExistsResult ;
318
288
} catch ( err ) {
319
289
throw new Error ( `IsTableExists: ${ err } ` ) ;
@@ -331,20 +301,10 @@ export class Database {
331
301
const isOpen = this . _isDbOpen ;
332
302
// check if the table has already being created
333
303
try {
334
- const retB = this . jsonUtil . isTableExists (
335
- this . database ,
336
- isOpen ,
337
- 'sync_table' ,
338
- ) ;
304
+ const retB = this . jsonUtil . isTableExists ( this . database , isOpen , 'sync_table' ) ;
339
305
if ( ! retB ) {
340
- const isLastModified = this . sqliteUtil . isLastModified (
341
- this . database ,
342
- isOpen ,
343
- ) ;
344
- const isSqlDeleted = this . sqliteUtil . isSqlDeleted (
345
- this . database ,
346
- isOpen ,
347
- ) ;
306
+ const isLastModified = this . sqliteUtil . isLastModified ( this . database , isOpen ) ;
307
+ const isSqlDeleted = this . sqliteUtil . isSqlDeleted ( this . database , isOpen ) ;
348
308
if ( isLastModified && isSqlDeleted ) {
349
309
const date : number = Math . round ( new Date ( ) . getTime ( ) / 1000 ) ;
350
310
let stmts = `
@@ -354,12 +314,7 @@ export class Database {
354
314
);` ;
355
315
stmts += `INSERT INTO sync_table (sync_date) VALUES (
356
316
${ date } );` ;
357
- const results = this . sqliteUtil . execute (
358
- this . database ,
359
- stmts ,
360
- false ,
361
- true ,
362
- ) ;
317
+ const results = this . sqliteUtil . execute ( this . database , stmts , false , true ) ;
363
318
changes = results . changes ;
364
319
if ( results . changes < 0 ) {
365
320
throw new Error ( `CreateSyncTable: failed changes < 0` ) ;
@@ -385,17 +340,11 @@ export class Database {
385
340
this . ensureDatabaseIsOpen ( ) ;
386
341
387
342
try {
388
- const isTable = this . jsonUtil . isTableExists (
389
- this . database ,
390
- this . _isDbOpen ,
391
- 'sync_table' ,
392
- ) ;
343
+ const isTable = this . jsonUtil . isTableExists ( this . database , this . _isDbOpen , 'sync_table' ) ;
393
344
if ( ! isTable ) {
394
345
throw new Error ( 'No sync_table available' ) ;
395
346
}
396
- const syncDateUnixTimestamp : number = Math . round (
397
- new Date ( syncDate ) . getTime ( ) / 1000 ,
398
- ) ;
347
+ const syncDateUnixTimestamp : number = Math . round ( new Date ( syncDate ) . getTime ( ) / 1000 ) ;
399
348
let stmt = `UPDATE sync_table SET sync_date = ` ;
400
349
stmt += `${ syncDateUnixTimestamp } WHERE id = 1;` ;
401
350
@@ -419,11 +368,7 @@ export class Database {
419
368
this . ensureDatabaseIsOpen ( ) ;
420
369
421
370
try {
422
- const isTable = this . jsonUtil . isTableExists (
423
- this . database ,
424
- this . _isDbOpen ,
425
- 'sync_table' ,
426
- ) ;
371
+ const isTable = this . jsonUtil . isTableExists ( this . database , this . _isDbOpen , 'sync_table' ) ;
427
372
if ( ! isTable ) {
428
373
throw new Error ( 'No sync_table available' ) ;
429
374
}
@@ -455,12 +400,7 @@ export class Database {
455
400
console . log ( `$$$ in executeSQL journal_mode: ${ mode } $$$` ) ;
456
401
this . sqliteUtil . beginTransaction ( this . database , this . _isDbOpen ) ;
457
402
}
458
- const results = this . sqliteUtil . execute (
459
- this . database ,
460
- sql ,
461
- false ,
462
- isSQL92 ,
463
- ) ;
403
+ const results = this . sqliteUtil . execute ( this . database , sql , false , isSQL92 ) ;
464
404
465
405
if ( results . changes < 0 ) {
466
406
throw new Error ( 'ExecuteSQL: changes < 0' ) ;
@@ -495,12 +435,7 @@ export class Database {
495
435
this . ensureDatabaseIsOpen ( ) ;
496
436
497
437
try {
498
- const selectResult = this . sqliteUtil . queryAll (
499
- this . database ,
500
- sql ,
501
- values ,
502
- isSQL92 ,
503
- ) ;
438
+ const selectResult = this . sqliteUtil . queryAll ( this . database , sql , values , isSQL92 ) ;
504
439
return selectResult ;
505
440
} catch ( err ) {
506
441
throw new Error ( `SelectSQL: ${ err } ` ) ;
@@ -516,13 +451,7 @@ export class Database {
516
451
* @param isSQL92: boolean,
517
452
* @returns Promise<{changes:number, lastId:number}>
518
453
*/
519
- runSQL (
520
- statement : string ,
521
- values : any [ ] ,
522
- transaction : boolean ,
523
- returnMode : string ,
524
- isSQL92 : boolean ,
525
- ) : Changes {
454
+ runSQL ( statement : string , values : any [ ] , transaction : boolean , returnMode : string , isSQL92 : boolean ) : Changes {
526
455
this . ensureDatabaseIsOpen ( ) ;
527
456
try {
528
457
// start a transaction
@@ -539,13 +468,7 @@ export class Database {
539
468
if ( ! isSQL92 && values . length === 0 ) {
540
469
nStmt = this . sql92Utils . compatibleSQL92 ( statement ) ;
541
470
}
542
- const results = this . sqliteUtil . prepareRun (
543
- this . database ,
544
- nStmt ,
545
- values ,
546
- false ,
547
- returnMode ,
548
- ) ;
471
+ const results = this . sqliteUtil . prepareRun ( this . database , nStmt , values , false , returnMode ) ;
549
472
if ( results . lastId < 0 ) {
550
473
if ( transaction ) {
551
474
this . sqliteUtil . rollbackTransaction ( this . database , this . _isDbOpen ) ;
@@ -574,12 +497,7 @@ export class Database {
574
497
* @param isSQL92: boolean,
575
498
* @returns Promise<{changes:number, lastId:number}>
576
499
*/
577
- execSet (
578
- set : any [ ] ,
579
- transaction : boolean ,
580
- returnMode : string ,
581
- isSQL92 : boolean ,
582
- ) : Changes {
500
+ execSet ( set : any [ ] , transaction : boolean , returnMode : string , isSQL92 : boolean ) : Changes {
583
501
this . ensureDatabaseIsOpen ( ) ;
584
502
585
503
let results : Changes = { changes : 0 , lastId : - 1 } ;
@@ -594,13 +512,7 @@ export class Database {
594
512
throw new Error ( `ExecSet: ${ err } ` ) ;
595
513
}
596
514
try {
597
- results = this . sqliteUtil . executeSet (
598
- this . database ,
599
- set ,
600
- false ,
601
- returnMode ,
602
- isSQL92 ,
603
- ) ;
515
+ results = this . sqliteUtil . executeSet ( this . database , set , false , returnMode , isSQL92 ) ;
604
516
if ( transaction ) {
605
517
this . sqliteUtil . commitTransaction ( this . database , this . _isDbOpen ) ;
606
518
}
@@ -680,38 +592,26 @@ export class Database {
680
592
this . ensureDatabaseIsOpen ( ) ;
681
593
682
594
try {
683
- const isTable = this . jsonUtil . isTableExists (
684
- this . database ,
685
- this . _isDbOpen ,
686
- 'sync_table' ,
687
- ) ;
595
+ const isTable = this . jsonUtil . isTableExists ( this . database , this . _isDbOpen , 'sync_table' ) ;
688
596
if ( isTable ) {
689
- this . exportToJsonUtil . setLastExportDate (
690
- this . database ,
691
- new Date ( ) . toISOString ( ) ,
692
- ) ;
597
+ this . exportToJsonUtil . setLastExportDate ( this . database , new Date ( ) . toISOString ( ) ) ;
693
598
} else {
694
599
if ( inJson . mode === 'partial' ) {
695
600
throw new Error ( `No sync_table available` ) ;
696
601
}
697
602
}
698
- let jsonResult : any = this . exportToJsonUtil . createExportObject (
699
- this . database ,
700
- inJson ,
701
- ) ;
603
+ let jsonResult : any = this . exportToJsonUtil . createExportObject ( this . database , inJson ) ;
702
604
const keys = Object . keys ( jsonResult ) ;
703
605
if ( keys . length === 0 ) {
704
- const msg =
705
- `ExportJson: return Object is empty ` + `No data to synchronize` ;
606
+ const msg = `ExportJson: return Object is empty ` + `No data to synchronize` ;
706
607
throw new Error ( msg ) ;
707
608
}
708
609
let isValid = this . jsonUtil . isJsonSQLite ( jsonResult ) ;
709
610
710
611
if ( this . _encrypted && this . _isEncryption && encrypted ) {
711
612
jsonResult . overwrite = true ;
712
613
jsonResult . encrypted = true ;
713
- const base64Str : string =
714
- this . jsonEncryptUtil . encryptJSONObject ( jsonResult ) ;
614
+ const base64Str : string = this . jsonEncryptUtil . encryptJSONObject ( jsonResult ) ;
715
615
jsonResult = { } as EncryptJson ;
716
616
jsonResult . expData = base64Str ;
717
617
isValid = true ;
@@ -732,9 +632,7 @@ export class Database {
732
632
*/
733
633
private ensureDatabaseIsOpen ( ) {
734
634
if ( ! this . _isDbOpen || ! this . database ) {
735
- throw new Error (
736
- `getVersion: Database ${ this . dbName } is not open yet. You should open it first.` ,
737
- ) ;
635
+ throw new Error ( `getVersion: Database ${ this . dbName } is not open yet. You should open it first.` ) ;
738
636
}
739
637
}
740
638
}
0 commit comments