Skip to content

Commit b7f92f6

Browse files
committedSep 12, 2024
style: format
1 parent 2bfc7dd commit b7f92f6

22 files changed

+359
-1192
lines changed
 

‎android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLitePlugin.java

+12-16
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,15 @@ public void changeEncryptionSecret(PluginCall call) {
146146
oldpassphrase = call.getString("oldpassphrase");
147147
if (implementation != null) {
148148
getActivity()
149-
.runOnUiThread(
150-
() -> {
151-
try {
152-
implementation.changeEncryptionSecret(call, passphrase, oldpassphrase);
153-
rHandler.retResult(call, null, null);
154-
} catch (Exception e) {
155-
String msg = "ChangeEncryptionSecret: " + e.getMessage();
156-
rHandler.retResult(call, null, msg);
157-
}
149+
.runOnUiThread(() -> {
150+
try {
151+
implementation.changeEncryptionSecret(call, passphrase, oldpassphrase);
152+
rHandler.retResult(call, null, null);
153+
} catch (Exception e) {
154+
String msg = "ChangeEncryptionSecret: " + e.getMessage();
155+
rHandler.retResult(call, null, msg);
158156
}
159-
);
157+
});
160158
} else {
161159
rHandler.retResult(call, null, loadMessage);
162160
}
@@ -1535,12 +1533,10 @@ public void getFromHTTPRequest(PluginCall call) {
15351533
getActivity().runOnUiThread(() -> rHandler.retResult(call, null, null));
15361534
} catch (Exception e) {
15371535
getActivity()
1538-
.runOnUiThread(
1539-
() -> {
1540-
String msg = "GetFromHTTPRequest: " + e.getMessage();
1541-
rHandler.retResult(call, null, msg);
1542-
}
1543-
);
1536+
.runOnUiThread(() -> {
1537+
String msg = "GetFromHTTPRequest: " + e.getMessage();
1538+
rHandler.retResult(call, null, msg);
1539+
});
15441540
}
15451541
};
15461542
Thread myHttpThread = new Thread(setHTTPRunnable);

‎android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ImportFromJson.java

+10-12
Original file line numberDiff line numberDiff line change
@@ -746,20 +746,18 @@ private JSONObject generateInsertAndDeletedStrings(ArrayList<String> tColNames,
746746
", ",
747747
rowIndex
748748
.stream()
749-
.map(
750-
item -> {
751-
if (item instanceof String) {
752-
String val = (String) item;
753-
String rVal = val;
754-
if (val.contains("'")) {
755-
rVal = val.replace("'", "''");
756-
}
757-
return "'" + rVal + "'";
758-
} else {
759-
return item.toString();
749+
.map(item -> {
750+
if (item instanceof String) {
751+
String val = (String) item;
752+
String rVal = val;
753+
if (val.contains("'")) {
754+
rVal = val.replace("'", "''");
760755
}
756+
return "'" + rVal + "'";
757+
} else {
758+
return item.toString();
761759
}
762-
)
760+
})
763761
.toArray(String[]::new)
764762
);
765763
insertValues.append("(").append(formattedRow).append("), ");

‎electron/rollup.config.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ export default {
2929
nodeResolve(),
3030
commonjs({
3131
ignoreDynamicRequires: true,
32-
dynamicRequireTargets: [
33-
'node_modules/@capacitor-community/sqlite/electron/dist/plugin.js',
34-
],
32+
dynamicRequireTargets: ['node_modules/@capacitor-community/sqlite/electron/dist/plugin.js'],
3533
}),
3634
],
3735
};

‎electron/src/electron-utils/Database.ts

+28-130
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
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';
72
import { GlobalSQLite } from '../GlobalSQLite';
83

94
import { ExportToJson } from './ImportExportJson/exportToJson';
@@ -49,7 +44,7 @@ export class Database {
4944
isEncryption: boolean,
5045
readonly: boolean,
5146
upgDict: Record<number, capSQLiteVersionUpgrade>,
52-
globalUtil?: GlobalSQLite,
47+
globalUtil?: GlobalSQLite
5348
) {
5449
this.dbName = dbName;
5550
this._encrypted = encrypted;
@@ -63,8 +58,7 @@ export class Database {
6358
this.isTransactionActive = false;
6459
this.globalUtil = globalUtil ? globalUtil : new GlobalSQLite();
6560

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);
6862
console.log(`&&& Databases path: ${this.pathDB}`);
6963
}
7064
/**
@@ -86,12 +80,7 @@ export class Database {
8680
this._isDbOpen = false;
8781
let password = '';
8882
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')) {
9584
password = this.secretUtil.getPassphrase();
9685

9786
if (password.length <= 0) {
@@ -105,31 +94,16 @@ export class Database {
10594
await this.encryptionUtil.decryptDatabase(this.pathDB, password);
10695
password = '';
10796
}
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);
11398
this._isDbOpen = true;
11499
if (!this.readonly) {
115100
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) {
120102
try {
121-
await this.fileUtil.copyFileName(
122-
this.dbName,
123-
`backup-${this.dbName}`,
124-
);
103+
await this.fileUtil.copyFileName(this.dbName, `backup-${this.dbName}`);
125104

126105
// 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);
133107
// delete the backup database
134108
await this.fileUtil.deleteFileName(`backup-${this.dbName}`);
135109
} catch (err) {
@@ -309,11 +283,7 @@ export class Database {
309283
const isOpen: boolean = this._isDbOpen;
310284

311285
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);
317287
return tableExistsResult;
318288
} catch (err) {
319289
throw new Error(`IsTableExists: ${err}`);
@@ -331,20 +301,10 @@ export class Database {
331301
const isOpen = this._isDbOpen;
332302
// check if the table has already being created
333303
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');
339305
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);
348308
if (isLastModified && isSqlDeleted) {
349309
const date: number = Math.round(new Date().getTime() / 1000);
350310
let stmts = `
@@ -354,12 +314,7 @@ export class Database {
354314
);`;
355315
stmts += `INSERT INTO sync_table (sync_date) VALUES (
356316
${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);
363318
changes = results.changes;
364319
if (results.changes < 0) {
365320
throw new Error(`CreateSyncTable: failed changes < 0`);
@@ -385,17 +340,11 @@ export class Database {
385340
this.ensureDatabaseIsOpen();
386341

387342
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');
393344
if (!isTable) {
394345
throw new Error('No sync_table available');
395346
}
396-
const syncDateUnixTimestamp: number = Math.round(
397-
new Date(syncDate).getTime() / 1000,
398-
);
347+
const syncDateUnixTimestamp: number = Math.round(new Date(syncDate).getTime() / 1000);
399348
let stmt = `UPDATE sync_table SET sync_date = `;
400349
stmt += `${syncDateUnixTimestamp} WHERE id = 1;`;
401350

@@ -419,11 +368,7 @@ export class Database {
419368
this.ensureDatabaseIsOpen();
420369

421370
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');
427372
if (!isTable) {
428373
throw new Error('No sync_table available');
429374
}
@@ -455,12 +400,7 @@ export class Database {
455400
console.log(`$$$ in executeSQL journal_mode: ${mode} $$$`);
456401
this.sqliteUtil.beginTransaction(this.database, this._isDbOpen);
457402
}
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);
464404

465405
if (results.changes < 0) {
466406
throw new Error('ExecuteSQL: changes < 0');
@@ -495,12 +435,7 @@ export class Database {
495435
this.ensureDatabaseIsOpen();
496436

497437
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);
504439
return selectResult;
505440
} catch (err) {
506441
throw new Error(`SelectSQL: ${err}`);
@@ -516,13 +451,7 @@ export class Database {
516451
* @param isSQL92: boolean,
517452
* @returns Promise<{changes:number, lastId:number}>
518453
*/
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 {
526455
this.ensureDatabaseIsOpen();
527456
try {
528457
// start a transaction
@@ -539,13 +468,7 @@ export class Database {
539468
if (!isSQL92 && values.length === 0) {
540469
nStmt = this.sql92Utils.compatibleSQL92(statement);
541470
}
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);
549472
if (results.lastId < 0) {
550473
if (transaction) {
551474
this.sqliteUtil.rollbackTransaction(this.database, this._isDbOpen);
@@ -574,12 +497,7 @@ export class Database {
574497
* @param isSQL92: boolean,
575498
* @returns Promise<{changes:number, lastId:number}>
576499
*/
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 {
583501
this.ensureDatabaseIsOpen();
584502

585503
let results: Changes = { changes: 0, lastId: -1 };
@@ -594,13 +512,7 @@ export class Database {
594512
throw new Error(`ExecSet: ${err}`);
595513
}
596514
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);
604516
if (transaction) {
605517
this.sqliteUtil.commitTransaction(this.database, this._isDbOpen);
606518
}
@@ -680,38 +592,26 @@ export class Database {
680592
this.ensureDatabaseIsOpen();
681593

682594
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');
688596
if (isTable) {
689-
this.exportToJsonUtil.setLastExportDate(
690-
this.database,
691-
new Date().toISOString(),
692-
);
597+
this.exportToJsonUtil.setLastExportDate(this.database, new Date().toISOString());
693598
} else {
694599
if (inJson.mode === 'partial') {
695600
throw new Error(`No sync_table available`);
696601
}
697602
}
698-
let jsonResult: any = this.exportToJsonUtil.createExportObject(
699-
this.database,
700-
inJson,
701-
);
603+
let jsonResult: any = this.exportToJsonUtil.createExportObject(this.database, inJson);
702604
const keys = Object.keys(jsonResult);
703605
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`;
706607
throw new Error(msg);
707608
}
708609
let isValid = this.jsonUtil.isJsonSQLite(jsonResult);
709610

710611
if (this._encrypted && this._isEncryption && encrypted) {
711612
jsonResult.overwrite = true;
712613
jsonResult.encrypted = true;
713-
const base64Str: string =
714-
this.jsonEncryptUtil.encryptJSONObject(jsonResult);
614+
const base64Str: string = this.jsonEncryptUtil.encryptJSONObject(jsonResult);
715615
jsonResult = {} as EncryptJson;
716616
jsonResult.expData = base64Str;
717617
isValid = true;
@@ -732,9 +632,7 @@ export class Database {
732632
*/
733633
private ensureDatabaseIsOpen() {
734634
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.`);
738636
}
739637
}
740638
}

0 commit comments

Comments
 (0)