@@ -39,6 +39,7 @@ import { GlobalSQLite } from './GlobalSQLite';
3939import { Database } from './electron-utils/Database' ;
4040import { UtilsJson } from './electron-utils/ImportExportJson/utilsJson' ;
4141import { UtilsFile } from './electron-utils/utilsFile' ;
42+ import { UtilsSQLite } from './electron-utils/utilsSQLite' ;
4243
4344export class CapacitorSQLite implements CapacitorSQLitePlugin {
4445 private versionUpgrades : Record <
@@ -48,7 +49,9 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
4849 private databases : { [ databasename : string ] : Database } = { } ;
4950 private fileUtil : UtilsFile = new UtilsFile ( ) ;
5051 private jsonUtil : UtilsJson = new UtilsJson ( ) ;
52+ private sqliteUtil : UtilsSQLite = new UtilsSQLite ( ) ;
5153 private globalUtil : GlobalSQLite = new GlobalSQLite ( ) ;
54+ private isEncryption : boolean = this . fileUtil . getIsEncryption ( ) ;
5255
5356 async createConnection ( options : capConnectionOptions ) : Promise < void > {
5457 const optionKeys = Object . keys ( options ) ;
@@ -59,16 +62,21 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
5962
6063 const dbName : string = options . database ;
6164 const version : number = options . version ? options . version : 1 ;
62- // const encrypted = false;
63- // const inMode = "no-encryption";
6465
65- const encrypted : boolean = options . encrypted ? options . encrypted : false ;
66- const inMode : string =
67- options . mode === 'secret'
66+ let encrypted = options . encrypted ? options . encrypted : false ;
67+ if ( ! this . isEncryption && encrypted ) {
68+ throw new Error ( 'Must set electronIsEncryption = true in capacitor.config.ts' ) ;
69+ }
70+ let inMode : string =
71+ encrypted && options . mode === 'secret'
6872 ? 'secret'
69- : options . mode === 'encryption'
73+ : encrypted && options . mode === 'encryption'
7074 ? 'encryption'
7175 : 'no-encryption' ;
76+ if ( ! this . isEncryption ) {
77+ encrypted = false ;
78+ inMode = 'no-encryption' ;
79+ }
7280 const readonly : boolean = options . readonly ? options . readonly : false ;
7381
7482 let upgrades : Record < number , capSQLiteVersionUpgrade > = { } ;
@@ -460,6 +468,9 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
460468 const overwrite : boolean = vJsonObj . overwrite ?? false ;
461469 const encrypted : boolean = vJsonObj . encrypted ?? false ;
462470 const mode : string = vJsonObj . mode ?? 'no-encryption' ;
471+ if ( ! this . isEncryption && encrypted ) {
472+ throw new Error ( 'Must set electronIsEncryption = true in capacitor.config.ts' ) ;
473+ }
463474
464475 // Create the database
465476 const database : Database = new Database (
@@ -865,6 +876,10 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
865876 }
866877
867878 async isSecretStored ( ) : Promise < capSQLiteResult > {
879+ if ( ! this . isEncryption ) {
880+ return Promise . reject ( `isSecretStored: Not available electronIsEncryption = false in capacitor.config.ts` ) ;
881+ }
882+
868883 if ( this . globalUtil != null ) {
869884 let capSQLiteResult = { result : false } ;
870885 if (
@@ -880,6 +895,9 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
880895 }
881896
882897 async setEncryptionSecret ( options : capSetSecretOptions ) : Promise < void > {
898+ if ( ! this . isEncryption ) {
899+ return Promise . reject ( `setEncryptionSecret: Not available electronIsEncryption = false in capacitor.config.ts` ) ;
900+ }
883901 if ( this . globalUtil != null ) {
884902 this . globalUtil . secret = options . passphrase ;
885903 Promise . resolve ( ) ;
@@ -889,6 +907,9 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
889907 }
890908
891909 async changeEncryptionSecret ( options : capChangeSecretOptions ) : Promise < void > {
910+ if ( ! this . isEncryption ) {
911+ return Promise . reject ( `changeEncryptionSecret: Not available electronIsEncryption = false in capacitor.config.ts` ) ;
912+ }
892913 if ( this . globalUtil != null ) {
893914 this . globalUtil . secret = options . oldpassphrase ;
894915 this . globalUtil . newsecret = options . passphrase ;
@@ -909,6 +930,9 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
909930 }
910931
911932 async clearEncryptionSecret ( ) : Promise < void > {
933+ if ( ! this . isEncryption ) {
934+ return Promise . reject ( `clearEncryptionSecret: Not available electronIsEncryption = false in capacitor.config.ts` ) ;
935+ }
912936 if ( this . globalUtil != null ) {
913937 this . globalUtil . secret = '' ;
914938 Promise . resolve ( ) ;
@@ -917,6 +941,40 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
917941 }
918942 }
919943
944+ async isInConfigEncryption ( ) : Promise < capSQLiteResult > {
945+ console . log ( `in electron plugin this.isEncryption: ${ this . isEncryption } ` ) ;
946+ return Promise . resolve ( { result : this . isEncryption } ) ;
947+ }
948+
949+ async isDatabaseEncrypted (
950+ options : capSQLiteOptions ,
951+ ) : Promise < capSQLiteResult > {
952+ const dbName : string = this . getOptionValue ( options , 'database' ) ;
953+ const isExists : boolean = this . fileUtil . isFileExists ( dbName + 'SQLite.db' ) ;
954+ console . log ( `@@@ isDatabaseEncrypted dbName: ${ dbName } isExists: ${ isExists } ` )
955+ if ( isExists ) {
956+ const filePath = this . fileUtil . getFilePath ( dbName + 'SQLite.db' ) ;
957+ try {
958+ const mDB = await this . sqliteUtil . openOrCreateDatabase (
959+ filePath ,
960+ "" ,
961+ true ,
962+ ) ;
963+ console . log ( `@@@@ dbName: ${ dbName } , mDB ${ mDB } ` )
964+ if ( mDB !== null ) {
965+ mDB . close ( ) ;
966+ return Promise . resolve ( { result : false } ) ;
967+ } else {
968+ return Promise . resolve ( { result : true } ) ;
969+ }
970+ } catch ( error ) {
971+ return Promise . resolve ( { result : true } ) ;
972+ }
973+ } else {
974+ return Promise . reject ( `isDatabaseEncryptedt: Database ${ dbName } does not exist` ) ;
975+ }
976+ }
977+
920978 ////////////////////////////////
921979 //// UNIMPLEMENTED METHODS
922980 ////////////////////////////////
@@ -997,16 +1055,6 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
9971055 throw new Error ( 'Method not implemented.' ) ;
9981056 }
9991057
1000- async isDatabaseEncrypted (
1001- options : capSQLiteOptions ,
1002- ) : Promise < capSQLiteResult > {
1003- console . log ( 'isDatabaseEncrypted' , options ) ;
1004- throw new Error ( 'Not implemented on web.' ) ;
1005- }
1006-
1007- async isInConfigEncryption ( ) : Promise < capSQLiteResult > {
1008- throw new Error ( 'Not implemented on web.' ) ;
1009- }
10101058
10111059 async isInConfigBiometricAuth ( ) : Promise < capSQLiteResult > {
10121060 throw new Error ( 'Not implemented on web.' ) ;
0 commit comments