From 9620a26c4eeb34baddce3a841ffd686d82cd87af Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 24 Jun 2021 20:29:14 +0200 Subject: [PATCH] fix: Capacitor driver PRAGMA requests failing on Android (#7728) * - fix: PRAGMA needs to be run through the `query` method, since it returns data - added WAL journal mode * added journalMode as an optional setting * reverted auto-format --- docs/connection-options.md | 2 ++ src/driver/capacitor/CapacitorConnectionOptions.ts | 11 +++++++++++ src/driver/capacitor/CapacitorDriver.ts | 10 +++++++++- src/driver/capacitor/CapacitorQueryRunner.ts | 14 ++++---------- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/docs/connection-options.md b/docs/connection-options.md index bb96cbdedc..34d587728d 100644 --- a/docs/connection-options.md +++ b/docs/connection-options.md @@ -203,6 +203,8 @@ See [SSL options](https://github.com/mysqljs/mysql#ssl-options). * `driver` - The capacitor-sqlite instance. For example, `new SQLiteConnection(CapacitorSQLite)`. +* `journalMode` - The SQLite journal mode (optional) + ## `cordova` connection options * `database` - Database name diff --git a/src/driver/capacitor/CapacitorConnectionOptions.ts b/src/driver/capacitor/CapacitorConnectionOptions.ts index 6c30a52cd2..7121af4adc 100644 --- a/src/driver/capacitor/CapacitorConnectionOptions.ts +++ b/src/driver/capacitor/CapacitorConnectionOptions.ts @@ -18,4 +18,15 @@ export interface CapacitorConnectionOptions extends BaseConnectionOptions { * The capacitor-sqlite instance. For example, `new SQLiteConnection(CapacitorSQLite)`. */ readonly driver: any; + + /** + * The SQLite journal mode (optional) + */ + readonly journalMode?: + | "DELETE" + | "TRUNCATE" + | "PERSIST" + | "MEMORY" + | "WAL" + | "OFF"; } diff --git a/src/driver/capacitor/CapacitorDriver.ts b/src/driver/capacitor/CapacitorDriver.ts index ebe7ac9879..1e394e27c5 100644 --- a/src/driver/capacitor/CapacitorDriver.ts +++ b/src/driver/capacitor/CapacitorDriver.ts @@ -81,9 +81,17 @@ export class CapacitorDriver extends AbstractSqliteDriver { 1 ); await connection.open(); + // we need to enable foreign keys in sqlite to make sure all foreign key related features // working properly. this also makes onDelete to work with sqlite. - await connection.execute(`PRAGMA foreign_keys = ON;`); + await connection.query(`PRAGMA foreign_keys = ON`, []); + + if (this.options.journalMode) { + await connection.query(`PRAGMA journal_mode = ?`, [ + this.options.journalMode, + ]); + } + return connection; } diff --git a/src/driver/capacitor/CapacitorQueryRunner.ts b/src/driver/capacitor/CapacitorQueryRunner.ts index b72a0c2373..271890521f 100644 --- a/src/driver/capacitor/CapacitorQueryRunner.ts +++ b/src/driver/capacitor/CapacitorQueryRunner.ts @@ -47,15 +47,9 @@ export class CapacitorQueryRunner extends AbstractSqliteQueryRunner { const command = query.substr(0, query.indexOf(" ")); if ( - [ - "PRAGMA", - "BEGIN", - "ROLLBACK", - "COMMIT", - "CREATE", - "ALTER", - "DROP", - ].indexOf(command) !== -1 + ["BEGIN", "ROLLBACK", "COMMIT", "CREATE", "ALTER", "DROP"].indexOf( + command + ) !== -1 ) { pResult = databaseConnection.execute(query, false); } else if (["INSERT", "UPDATE", "DELETE"].indexOf(command) !== -1) { @@ -70,7 +64,7 @@ export class CapacitorQueryRunner extends AbstractSqliteQueryRunner { ); } else { pResult = databaseConnection - .query(query, parameters) + .query(query, parameters || []) .then(({ values }: { values: any[] }) => values); }