Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/gorgeous-scissors-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@powersync/common': patch
---

Fixed readTransaction method throwing "not allowed in read-only mode" errors
2 changes: 1 addition & 1 deletion packages/capacitor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const db = new PowerSyncDatabase({

- Encryption for native mobile platforms is not yet supported.
- `PowerSyncDatabase.executeRaw` does not support results where multiple columns would have the same name in SQLite
- `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).
- `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). Queries such as `INSERT into customers (id, name) VALUES (uuid(), 'name') RETURNING *` do not work on Android.
- Multiple tab support is not available for native Android and iOS targets. If you're not opening a second webview in your native app using something like `@jackobo/capacitor-webview`, you are unaffected by this.

## Examples
Expand Down
48 changes: 21 additions & 27 deletions packages/capacitor/src/adapter/CapacitorSQLiteAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,31 @@ export class CapacitorSQLiteAdapter extends BaseObserver<DBAdapterListener> impl
}

protected generateLockContext(db: SQLiteDBConnection): LockContext {
const _query = async (query: string, params: any[] = []) => {
const result = await db.query(query, params);
const arrayResult = result.values ?? [];
return {
rowsAffected: 0,
rows: {
_array: arrayResult,
length: arrayResult.length,
item: (idx: number) => arrayResult[idx]
}
};
};

const _execute = async (query: string, params: any[] = []): Promise<QueryResult> => {
const platform = Capacitor.getPlatform();

if (db.getConnectionReadOnly()) {
return _query(query, params);
}

if (platform == 'android') {
// Android: use query for SELECT and executeSet for mutations
// We cannot use `run` here for both cases.
if (query.toLowerCase().trim().startsWith('select')) {
const result = await db.query(query, params);
const arrayResult = result.values ?? [];
return {
rowsAffected: 0,
rows: {
_array: arrayResult,
length: arrayResult.length,
item: (idx: number) => arrayResult[idx]
}
};
return _query(query, params);
} else {
const result = await db.executeSet([{ statement: query, values: params }], false);
return {
Expand Down Expand Up @@ -166,24 +175,9 @@ export class CapacitorSQLiteAdapter extends BaseObserver<DBAdapterListener> impl
? (sql: string, params?: any[]) => monitorQuery(sql, () => _execute(sql, params))
: _execute;

const _executeQuery = async (query: string, params?: any[]): Promise<QueryResult> => {
let result = await db.query(query, params);

let arrayResult = result.values ?? [];

return {
rowsAffected: 0,
rows: {
_array: arrayResult,
length: arrayResult.length,
item: (idx: number) => arrayResult[idx]
}
};
};

const executeQuery = this.options.debugMode
? (sql: string, params?: any[]) => monitorQuery(sql, () => _executeQuery(sql, params))
: _executeQuery;
? (sql: string, params?: any[]) => monitorQuery(sql, () => _query(sql, params))
: _query;

const getAll = async <T>(query: string, params?: any[]): Promise<T[]> => {
const result = await executeQuery(query, params);
Expand Down