Skip to content

fix: One state after every failed dbBind() - #936

Draft
krlmlr wants to merge 5 commits into
claude/rb-error-ends-fetchfrom
claude/rb-failed-bind-state
Draft

krlmlr wants to merge 5 commits into
claude/rb-error-ends-fetchfrom
claude/rb-failed-bind-state

Conversation

@krlmlr

@krlmlr krlmlr commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

After a dbBind() whose parameters fail to bind or to run, the result is in one state: it is left unbound, dbFetch() refuses with "Query needs to be bound before fetching", dbHasCompleted() returns FALSE, and dbGetRowsAffected() counts the rows changed since that bind. This holds whichever row fails, and whether it fails to bind or in the statement. Input that dbBind() refuses before binding (the wrong number of parameters, columns of different lengths, placeholder or name errors, or a busy connection) keeps the previous binding usable, as on #935. For example, with CREATE TABLE u (x INTEGER UNIQUE) and res <- dbSendStatement(con, "INSERT INTO u VALUES (?)"), dbBind(res, list(c(1L, 2L, 1L, 3L))) fails on the third row and leaves FALSE and 2; dbBind(res, list(list(1))) fails to bind the first row and leaves FALSE and 0. RSQLite 3.54.1 returned TRUE and NA when the first row failed to bind (from #868), and FALSE plus a count for an error from the statement. On #935, a dbBind() whose first step fails left a completed result whose fetch returned zero rows; that is now the unbound state too.

This follows the maintainer's decision in #880 (comment) (option 1). In SqliteResultImpl::bind(), one catch around the bind and the first step drops the parameters and resets ready_ and complete_ to false, and a new flag bind_started_ lets n_rows_affected() count from the latest bind also after it failed. It is still NA before the first bind. peek_first_row() reads column values only for a bound result, so dbColumnInfo() after a failed bind reads no values from a statement that is on no row, and gives the same as before the first bind.

Tests: the five #868 expectations in tests/testthat/test-dbBind.R change (three dbHasCompleted() from TRUE to FALSE, two dbGetRowsAffected() from NA to 0), and "an unsupported parameter type leaves a completed, unbound result" loses "completed". The #935 test "a fetch doesn't run a write again after its dbBind() failed" in tests/testthat/test-fetch-error.R now expects the unbound state. Two new tests cover the reprex above (a failure on the third row and on the first row of the same statement) and a query whose first row fails in the statement. Two more pin dbColumnInfo() after a failed bind and the kept binding after input refused before binding. Red on #935: the rewritten and new expectations fail in both files (dbHasCompleted() TRUE, dbGetRowsAffected() NA, dbFetch() not refusing). The last two tests pass on #935 too: they pin behavior that this change keeps. Green with this change: the full suite with NOT_CRAN=true gives FAIL 0, WARN 3, SKIP 63, PASS 10988.

Layer B5 of the rebuilt chain, planned in #880; based on #935. Ported from #830 (now a draft), commits 527c277, d928047 and 1edd8f4, with its NEWS bullet.

Coordinating session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4


Generated by Claude Code

A `dbBind()` whose first row failed to bind left a completed result:
`dbHasCompleted()` returned `TRUE` and `dbGetRowsAffected()` returned `NA`.
A `dbBind()` whose statement failed left a completed result too,
whose fetch returned zero rows.
Every failed `dbBind()` now leaves the result unbound,
whichever row fails and whether it fails to bind or in the statement.
`dbFetch()` asks for a new bind with "Query needs to be bound before fetching",
and `dbHasCompleted()` returns `FALSE`.
A flag records that a bind has started, separately from the readiness to fetch,
so that `dbGetRowsAffected()` counts the rows changed since that bind:
0 when the first row fails to bind, 2 when the third of four inserts fails.
It is still `NA` before the first bind.
The tests from #868 and #935 that pinned the completed state now pin the unbound one.

Ported from #830, as decided in
#880 (comment) (option 1).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
@krlmlr
krlmlr added this pull request to stack #928 December 1, 2026 00:36
After a failed bind, the statement is on no row,
so peeking at the first row reads column values only for a bound result.
Before the first bind, the column info is the same as before.

NEWS states that input refused before binding keeps the previous binding,
with a test that pins it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants