From f2c5eada7acb690eb04dcf55b4c5c651992da9e2 Mon Sep 17 00:00:00 2001 From: jamaljsr <1356600+jamaljsr@users.noreply.github.com> Date: Wed, 17 Jun 2026 00:58:05 -0500 Subject: [PATCH] bridge: bind positional exec params by number runExec sent positional-array params to db.exec({ bind: [...] }), which binds the array by SQLite slot order (order of first appearance) rather than by the $N number. Statements whose $N appear out of textual order -- e.g. UPDATE ... SET col = $3 WHERE k = $1 -- therefore bound the WHERE key against the wrong value and silently matched zero rows. runQuery already routes positional arrays through normalizePositionalParams (added in "Fix numbered SQLite parameter binding"), which remaps each $N to its slot by number. This applies the same remap on the exec path by sending any non-empty params (array or object) through bindParams, leaving only the empty-params case on db.exec. Adds TestBrowserExecNumberedParamsOutOfOrderE2E covering the exec path; it fails (0 rows updated) against the old worker and passes with this change. The existing UPDATE...RETURNING test only exercised the query path. --- bridge/sqlite-worker.js | 16 +++++-- driver_browser_js_wasm_test.go | 80 ++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 4 deletions(-) diff --git a/bridge/sqlite-worker.js b/bridge/sqlite-worker.js index e305f62..ed5c350 100644 --- a/bridge/sqlite-worker.js +++ b/bridge/sqlite-worker.js @@ -221,7 +221,17 @@ function runQuery(db, sql, params) { function runExec(db, sql, params) { const beforeChanges = db.changes(true); - if (params && !Array.isArray(params) && Object.keys(params).length) { + // Route any non-empty params (positional array OR named object) through + // bindParams, exactly like runQuery. bindParams sends positional arrays to + // normalizePositionalParams, which remaps each $N to its slot BY NUMBER. + // db.exec({ bind: [...] }) would instead bind the array by SQLite slot order + // (i.e. order of first appearance), silently mis-binding any statement whose + // $N are out of textual order -- e.g. UPDATE ... SET col = $3 WHERE k = $1 + // binds k against the value meant for col and matches zero rows. Only the + // empty-params case falls through to db.exec. + const hasParams = + params && (Array.isArray(params) ? params.length : Object.keys(params).length); + if (hasParams) { const stmt = db.prepare(sql); try { bindParams(stmt, params); @@ -230,9 +240,7 @@ function runExec(db, sql, params) { stmt.finalize(); } } else { - const options = { sql }; - if (params && params.length) options.bind = params.map(normalizeValue); - db.exec(options); + db.exec({ sql }); } return { diff --git a/driver_browser_js_wasm_test.go b/driver_browser_js_wasm_test.go index 4c37c36..c54d4e1 100644 --- a/driver_browser_js_wasm_test.go +++ b/driver_browser_js_wasm_test.go @@ -324,6 +324,86 @@ RETURNING id, mailbox_id, lease_token, lease_until, attempts;` } } +// TestBrowserExecNumberedParamsOutOfOrderE2E exercises the database/sql exec +// path (ExecContext, no RETURNING) with positional $N placeholders whose +// numbers appear out of textual order: the SET clause references $3/$4 before +// the WHERE clause references $1/$2. The driver passes these as a positional +// array, which must be remapped to SQLite parameter slots BY NUMBER. A +// regression binds the WHERE keys against the SET values, so the statement +// matches no rows and silently updates nothing. This mirrors the sqlc :exec +// UPDATEs emitted for the PostgreSQL engine (e.g. UpdateVTXOStatus) that run +// against this driver in the browser. It complements +// TestBrowserUpdateReturningWithNumberedParamsAndSubqueryE2E, which only +// covers the query (RETURNING) path. +func TestBrowserExecNumberedParamsOutOfOrderE2E(t *testing.T) { + db, err := Open(&Options{File: ":memory:", VFS: "memory"}) + if err != nil { + t.Fatalf("open memory db: %v", err) + } + defer db.Close() + + if _, err := db.Exec(` +CREATE TABLE vtxos ( + outpoint_hash TEXT NOT NULL, + outpoint_index INTEGER NOT NULL, + status INTEGER NOT NULL, + last_update_time TEXT NOT NULL, + PRIMARY KEY (outpoint_hash, outpoint_index) +);`); err != nil { + t.Fatalf("create vtxos schema: %v", err) + } + + if _, err := db.Exec( + `INSERT INTO vtxos (outpoint_hash, outpoint_index, status, last_update_time) + VALUES (?, ?, ?, ?)`, + "abcd", 0, 0, "2026-06-17T00:00:00Z", + ); err != nil { + t.Fatalf("seed vtxo row: %v", err) + } + + // SET references $3,$4 before WHERE references $1,$2 -- the exact shape that + // mis-binds when a positional array is bound by appearance order instead of + // by number. + const updateStatus = ` +UPDATE vtxos +SET status = $3, + last_update_time = $4 +WHERE outpoint_hash = $1 AND outpoint_index = $2` + + res, err := db.Exec(updateStatus, "abcd", 0, 4, "2026-06-17T01:00:00Z") + if err != nil { + t.Fatalf("exec out-of-order update: %v", err) + } + affected, err := res.RowsAffected() + if err != nil { + t.Fatalf("rows affected: %v", err) + } + if affected != 1 { + t.Fatalf( + "expected 1 row updated, got %d: positional $N mis-bound on the exec path", + affected, + ) + } + + var ( + status int + lastUpdate string + ) + if err := db.QueryRow( + `SELECT status, last_update_time FROM vtxos + WHERE outpoint_hash = ? AND outpoint_index = ?`, + "abcd", 0, + ).Scan(&status, &lastUpdate); err != nil { + t.Fatalf("read back vtxo row: %v", err) + } + if status != 4 || lastUpdate != "2026-06-17T01:00:00Z" { + t.Fatalf( + "unexpected row after update: status=%d last_update_time=%q", + status, lastUpdate, + ) + } +} + func TestBrowserProtocolMetadataE2E(t *testing.T) { bridgeProtocol := js.Global().Get("sqliteBridge").Get("protocolVersion") if bridgeProtocol.IsUndefined() {