Improve database query handling with automatic fallback for RETURNING clauses

This commit is contained in:
2025-11-07 01:39:44 +01:00
parent 63849d86e1
commit 2b739b9e79
2 changed files with 31 additions and 11 deletions

View File

@ -1,7 +1,7 @@
{ {
"name": "haex-hub", "name": "haex-hub",
"private": true, "private": true,
"version": "0.1.11", "version": "0.1.12",
"type": "module", "type": "module",
"scripts": { "scripts": {
"build": "nuxt build", "build": "nuxt build",

View File

@ -365,6 +365,7 @@ async function handleDatabaseMethodAsync(
switch (request.method) { switch (request.method) {
case 'haextension.db.query': { case 'haextension.db.query': {
try {
const rows = await invoke<unknown[]>('extension_sql_select', { const rows = await invoke<unknown[]>('extension_sql_select', {
sql: params.query || '', sql: params.query || '',
params: params.params || [], params: params.params || [],
@ -377,6 +378,25 @@ async function handleDatabaseMethodAsync(
rowsAffected: 0, rowsAffected: 0,
lastInsertId: undefined, lastInsertId: undefined,
} }
} catch (error: any) {
// If error is about non-SELECT statements (INSERT/UPDATE/DELETE with RETURNING),
// automatically retry with execute
if (error?.message?.includes('Only SELECT statements are allowed')) {
const rows = await invoke<unknown[]>('extension_sql_execute', {
sql: params.query || '',
params: params.params || [],
publicKey: extension.publicKey,
name: extension.name,
})
return {
rows,
rowsAffected: rows.length,
lastInsertId: undefined,
}
}
throw error
}
} }
case 'haextension.db.execute': { case 'haextension.db.execute': {