Skip to content

Commit 2bea72c

Browse files
Update Capacitor community SQLite. Restore Android Execute method functionality.
1 parent 600f0d5 commit 2bea72c

File tree

4 files changed

+48
-31
lines changed

4 files changed

+48
-31
lines changed

packages/capacitor/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ const db = new PowerSyncDatabase({
8282

8383
- Encryption for native mobile platforms is not yet supported.
8484
- Multiple tab support is not available for native Android and iOS targets.
85-
- `executeRaw` does not support results where multiple columns would have the same name in SQLite
85+
- `PowerSyncDatabase.executeRaw` does not support results where multiple columns would have the same name in SQLite
86+
- `PowerSyncDatabase.execute` has limited support on Android. The SQLCipher Android driver exposes queries and executions as separate APIs, so there is no single method that handles both. While `PowerSyncDatabase.execute` accepts both, on Android we treat a statement as a query only when the SQL starts with `select` (case-insensitive).
8687

8788
## Examples
8889

packages/capacitor/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"test:exports": "attw --pack ."
5353
},
5454
"devDependencies": {
55-
"@capacitor-community/sqlite": "^7.0.1",
55+
"@capacitor-community/sqlite": "^7.0.2",
5656
"@capacitor/android": "^7.0.0",
5757
"@capacitor/core": "^7.0.0",
5858
"@capacitor/docgen": "^0.3.0",
@@ -68,7 +68,7 @@
6868
"swiftlint": "^2.0.0"
6969
},
7070
"peerDependencies": {
71-
"@capacitor-community/sqlite": "^7.0.1",
71+
"@capacitor-community/sqlite": "^7.0.2",
7272
"@powersync/web": "workspace:^"
7373
},
7474
"swiftlint": "@ionic/swiftlint-config",

packages/capacitor/src/adapter/CapacitorSQLiteAdapter.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CapacitorSQLite, SQLiteConnection, SQLiteDBConnection } from '@capacitor-community/sqlite';
2+
import { Capacitor } from '@capacitor/core';
23

34
import {
45
BaseObserver,
@@ -14,7 +15,6 @@ import Lock from 'async-lock';
1415
import { PowerSyncCore } from '../plugin/PowerSyncCore';
1516
import { messageForErrorCode } from '../plugin/PowerSyncPlugin';
1617
import { CapacitorSQLiteOpenFactoryOptions, DEFAULT_SQLITE_OPTIONS } from './CapacitorSQLiteOpenFactory';
17-
1818
/**
1919
* Monitors the execution time of a query and logs it to the performance timeline.
2020
*/
@@ -107,23 +107,37 @@ export class CapacitorSQLiteAdapter extends BaseObserver<DBAdapterListener> impl
107107

108108
protected generateLockContext(db: SQLiteDBConnection): LockContext {
109109
const _execute = async (query: string, params: any[] = []): Promise<QueryResult> => {
110-
let result = await db.run(query, params, false, 'all');
111-
/**
112-
* This is a sample response for `SELECT powersync_control(?, ?)`
113-
* ```json
114-
* {
115-
* "changes": {
116-
* "changes": 0,
117-
* "values": [
118-
* {
119-
* "powersync_control(?, ?)": "[]"
120-
* }
121-
* ],
122-
* "lastId": 0
123-
* }
124-
* }
125-
* ```
126-
*/
110+
const platform = Capacitor.getPlatform();
111+
if (platform == 'android') {
112+
// Android: use query for SELECT and executeSet for mutations
113+
// We cannot use `run` here for both cases.
114+
if (query.toLowerCase().trim().startsWith('select')) {
115+
const result = await db.query(query, params);
116+
const arrayResult = result.values ?? [];
117+
return {
118+
rowsAffected: 0,
119+
rows: {
120+
_array: arrayResult,
121+
length: arrayResult.length,
122+
item: (idx: number) => arrayResult[idx]
123+
}
124+
};
125+
} else {
126+
const result = await db.executeSet([{ statement: query, values: params }], false);
127+
return {
128+
insertId: result.changes?.lastId,
129+
rowsAffected: result.changes?.changes ?? 0,
130+
rows: {
131+
_array: [],
132+
length: 0,
133+
item: () => null
134+
}
135+
};
136+
}
137+
}
138+
139+
// iOS (and other platforms): use run("all")
140+
const result = await db.run(query, params, false, 'all');
127141
const resultSet = result.changes?.values ?? [];
128142
return {
129143
insertId: result.changes?.lastId,

pnpm-lock.yaml

Lines changed: 12 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)