Skip to content

fix: dbExecute() runs every parameter set of a statement with RETURNING - #948

Draft
krlmlr wants to merge 4 commits into
claude/rb-fetch-error-rowsfrom
claude/rb-returning-params
Draft

krlmlr wants to merge 4 commits into
claude/rb-fetch-error-rowsfrom
claude/rb-returning-params

Conversation

@krlmlr

@krlmlr krlmlr commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

dbExecute() now runs every parameter set of a statement that writes and returns rows, such as INSERT ... RETURNING or UPDATE ... RETURNING, raises an error from any of them (classed as RSQLite_error_constraint etc.), and returns the number of changed rows instead of 0. The same holds for dbSendStatement() followed by dbBind() and dbGetRowsAffected(). dbFetch() on such a result returns zero rows, the first time after each bind with a warning that the rows of a statement sent with dbSendStatement() were discarded and that dbSendQuery() returns them; this also applies to a PRAGMA that writes and returns a row, such as PRAGMA journal_mode = WAL (dbExecute() is unchanged for it, dbGetQuery() still returns the row). EXPLAIN is never run to the end, so dbSendStatement() + dbFetch() returns its rows. A long run of parameter sets can now be interrupted, for any statement. dbGetQuery(), dbSendQuery() + dbFetch(), and dbSendStatement() with a read-only statement return their rows as before. An SQLite error in dbExecute() now names dbSendStatement() as its call instead of dbSendQuery().

Why: dbBind() stepped such a statement until its first row and left the rest to dbFetch(), which dbExecute() never calls, so the later parameter sets never ran. SQLite also counts the changes of a statement only when it finishes, so even a single parameter set counted 0. Closes #946.

How: RSQLite had no dbSendStatement() method, so DBI's default sent statements through dbSendQuery(). A new method passes is_statement = TRUE to result_create(). For a statement that SQLite does not report as read-only (sqlite3_stmt_readonly()), SqliteResultImpl::step() then keeps stepping past rows until the last parameter set is done, both in dbBind() and right away for a statement without parameters. Errors go through the existing paths: a failed set leaves the result unbound with the changes since the bind counted (#936), and the condition is classed (#943). EXPLAIN (sqlite3_stmt_isexplain()) is left out too, since SQLite reports it as writing although it writes nothing; for a PRAGMA that writes and returns a row there is no such test, so it runs to the end and the result remembers that it dropped rows, which the first dbFetch() after the bind reports with a warning (only if rows were actually dropped). The stepping loop checks for an interrupt every 1000 steps with cpp11::check_user_interrupt(), which throws through the same catch blocks as an SQLite error: an interrupt in dbBind() leaves the result unbound with the changes counted (#936), one in dbFetch() completes it (#935). The read-ahead past the last row of a fetch, which skips parameter sets that return no row, checks for an interrupt the same way; an interrupt there ends the fetch instead of being kept for the next one like an SQLite error. Read-only statements are left out so that dbSendStatement() + dbFetch() on a SELECT keeps returning rows and dbExecute() on a large SELECT doesn't read it to the end; a question for the maintainer is whether dbSendStatement() should run every statement to the end regardless.

Tests: new tests/testthat/test-returning.R with the reprex of #946 (all sets inserted or updated, counts 3 and 2, a constraint violation in a later set raised from dbBind(), the state after a failed later set, a second bind counting its own changes) and pins for dbGetQuery(), dbSendQuery() + dbFetch() and a read-only dbSendStatement(). Red on the base (10+ failures, counts 0, later sets missing, no error), green with the fix. Full suite with NOT_CRAN=true: FAIL 0, WARN 3 (locale), SKIP 63, PASS 11228 before the review follow-up, PASS 11257 after it. Review follow-up: tests for EXPLAIN keeping its rows, PRAGMA journal_mode via dbSendStatement() + dbFetch() (warning, zero rows), dbExecute() (sets WAL, returns 0) and dbGetQuery() (row returned), no warning when nothing was dropped, and a callr test that interrupts dbBind() of an INSERT ... RETURNING with 1e7 parameter sets and checks the state of a failed bind and a working rebind; the EXPLAIN, warning and interrupt tests fail on the previous head of this PR. Second review follow-up: a callr test that interrupts the read-ahead of dbFetch(n = 2) over 1e7 parameter sets (red on the previous head: the interrupt isn't seen for over a minute and the fetch keeps its two rows; green: the result is completed with zero rows counted), and checks that the warning about discarded rows comes once per bind. Full suite after it: FAIL 0, WARN 3 (locale), SKIP 63, PASS 11270. Internal callers of result_create() in tests pass the new argument; the call expectation in test-conditions.R changes to dbSendStatement.

Layer B8 of the rebuilt chain, planned in #880; based on #945. New layer, not ported from an old PR.

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


Generated by Claude Code

…RNING`

A statement that writes and returns rows stopped on its first row in `dbBind()`,
and `dbExecute()` never ran its later parameter sets,
missed their errors and counted 0 changed rows,
because SQLite counts the changes of such a statement when it finishes.
`dbSendStatement()` now has its own method that marks the result:
for a statement that writes, each execution runs to the end and drops the rows.
Read-only statements, `dbSendQuery()` and `dbGetQuery()` keep their rows.
An error in `dbExecute()` now names `dbSendStatement()`.

Closes #946.

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

krlmlr commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Question: should dbSendStatement() run read-only statements to the end too?

To fix #946, this pull request gives RSQLite its own dbSendStatement() method, which runs every parameter set of a statement to the end, discarding the rows it returns, so that dbExecute() and dbGetRowsAffected() see every change of an INSERT … RETURNING or UPDATE … RETURNING. It does this only for statements that SQLite reports as writing (sqlite3_stmt_readonly() false). A read-only statement sent with dbSendStatement(), such as a SELECT, still behaves as before: dbFetch() returns its rows, and dbExecute() on it reads only up to the first row.

If nobody decides, it stays as it is.


Generated by Claude Code

…dropped rows

`EXPLAIN` writes nothing although SQLite doesn't report it read-only,
so it is no longer run to the end and `dbFetch()` returns its rows.
A `PRAGMA` that writes and returns a row still runs to the end,
which keeps `dbExecute()` right,
and `dbFetch()` warns if rows were dropped and points to `dbSendQuery()`.
The stepping loop checks for an interrupt every 1000 steps,
which ends the execution like an error of a parameter set.

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

The step past the last row of a fetch skips the parameter sets that return no row,
and now checks for an interrupt every 1000 of them, as dbBind() does.
The interrupt ends the fetch, unlike an SQLite error that is kept for the next one.
The warning that `dbSendStatement()` discarded rows comes once per bind
instead of on every `dbFetch()`.

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.

dbExecute() with RETURNING and several parameter sets runs only the first set and returns 0

2 participants