Skip to content

Commit 6756660

Browse files
fix executeRaw and execute limitations
1 parent 48e0f89 commit 6756660

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

packages/capacitor/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ 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-
- The Capacitor Community SQLite APIs differ for execution and query operations. The `.execute()` method cannot be used for `SELECT` queries or `RETURNING` queries.
85+
- `executeRaw` does not support results where multiple columns would have the same name in SQLite
8686

8787
## Examples
8888

packages/capacitor/src/adapter/CapacitorSQLiteAdapter.ts

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -107,30 +107,33 @@ 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-
// This driver does not support returning results for execute methods
111-
if (query.toLowerCase().trim().startsWith('select')) {
112-
let result = await db.query(query, params);
113-
let arrayResult = result.values ?? [];
114-
return {
115-
rowsAffected: 0,
116-
rows: {
117-
_array: arrayResult,
118-
length: arrayResult.length,
119-
item: (idx: number) => arrayResult[idx]
120-
}
121-
};
122-
} else {
123-
let result = await db.executeSet([{ statement: query, values: params }], false);
124-
return {
125-
insertId: result.changes?.lastId,
126-
rowsAffected: result.changes?.changes ?? 0,
127-
rows: {
128-
_array: [],
129-
length: 0,
130-
item: () => null
131-
}
132-
};
133-
}
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+
*/
127+
const resultSet = result.changes?.values ?? [];
128+
return {
129+
insertId: result.changes?.lastId,
130+
rowsAffected: result.changes?.changes ?? 0,
131+
rows: {
132+
_array: resultSet,
133+
length: resultSet.length,
134+
item: (idx) => resultSet[idx]
135+
}
136+
};
134137
};
135138

136139
const execute = this.options.debugMode
@@ -175,7 +178,9 @@ export class CapacitorSQLiteAdapter extends BaseObserver<DBAdapterListener> impl
175178
};
176179

177180
const executeRaw = async (query: string, params?: any[]): Promise<any[][]> => {
178-
throw new Error('Not supported');
181+
// This is a workaround, we don't support multiple columns of the same name
182+
const results = await execute(query, params);
183+
return results.rows?._array.map((row) => Object.values(row)) ?? [];
179184
};
180185

181186
return {

0 commit comments

Comments
 (0)