Skip to content

Commit

Permalink
fix(client): Use execute Capacitor driver API to fix parsing issue (#…
Browse files Browse the repository at this point in the history
…1052)

Using the `execute` API rather than the `run` one for when no bind
values are provided as it has less pre-processing and solves a parsing
issue that `run` has for statements like `BEGIN` and `ROLLBACK`.

Not strictly necessary but provides an immediate solution to a bug that
is blocking the use of this driver on Android without requiring changes
from the driver side.

See this issue: capacitor-community/sqlite#526
  • Loading branch information
msfstef committed Mar 13, 2024
1 parent 452361d commit 95dbb6b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-schools-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"electric-sql": patch
---

Fix Capacitor driver issue where `BEGIN` statements failed to run on Android by using driver's `execute` API.
11 changes: 6 additions & 5 deletions clients/typescript/src/drivers/capacitor-sqlite/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ export class DatabaseAdapter extends GenericDatabaseAdapter {

async _run(statement: Statement): Promise<RunResult> {
const wrapInTransaction = false
const result = await this.db.run(
statement.sql,
statement.args,
wrapInTransaction
)

// if no bind values are provided, use `execute` API which
// has less overhead and native side pre-processing
const result = await (statement.args && statement.args.length > 0
? this.db.run(statement.sql, statement.args, wrapInTransaction)
: this.db.execute(statement.sql, wrapInTransaction))

const rowsAffected = result.changes?.changes ?? 0
return { rowsAffected: rowsAffected }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ type OriginalDatabase = SQLiteDBConnection
// The relevant subset of the SQLitePlugin database client API
// that we need to ensure the client we're electrifying provides.
export interface Database
extends Pick<OriginalDatabase, 'executeSet' | 'run' | 'query'> {
extends Pick<OriginalDatabase, 'executeSet' | 'execute' | 'run' | 'query'> {
dbname?: DbName
}
4 changes: 4 additions & 0 deletions clients/typescript/src/drivers/capacitor-sqlite/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export class MockDatabase implements Database {
})
}

execute(): Promise<capSQLiteChanges> {
return this.run()
}

query(): Promise<DBSQLiteValues> {
return this.resolveIfNotFail({
values: [
Expand Down

0 comments on commit 95dbb6b

Please sign in to comment.