Repository navigation
Conversation
…esult codes Errors from SQLite in dbSendQuery(), dbBind() and dbFetch() are raised by the new signal_sqlite_error() with rlang::abort(), with the classes "RSQLite_error" and "RSQLite_error_<name>" for the primary result code, and the fields code, extended_code and code_name. The messages are unchanged. The C++ exception step_error becomes SqliteError in SqliteError.h, which also carries the extended result code, and the prepare error is thrown as one too. with_sqlite_errors() at the entry points result_create(), result_bind() and result_fetch() catches it after the C++ frames of the body have unwound and calls signal_sqlite_error() through cpp11, so that no R error jumps over live C++ objects. rethrow(), as in RPostgres, sets the call of the condition to the DBI method. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
Question: should SQLite's extended result codes get condition subclasses too?This pull request classes an SQLite error by its primary result code,
Implemented on 2026-12-01: the recommended option is in 2f8f761; no box is ticked, and ticking another option changes it. If nobody decides, the recommended option goes in with the fixes of this pull request's review. Generated by Claude Code |
Question: which call should an SQLite error report, the method that hit it or the verb the user called?
If nobody decides, it stays as it is. Generated by Claude Code |
Question: should connection-level SQLite errors be classed as well?This pull request classes the errors of statements: prepare, bind, step and the error kept from reading ahead. Errors that SQLite reports outside a statement stay ordinary errors: opening a database in
Implemented on 2026-12-01: the recommended option is in 2f8f761; no box is ticked, and ticking another option changes it. If nobody decides, the recommended option goes in with the fixes of this pull request's review. Generated by Claude Code |
SQLite errors are now raised with `stop()` on a condition from `rlang::error_cnd()` instead of `rlang::abort()`, which captured a useless backtrace on every error; a caught constraint error takes about 0.2 ms instead of 3.4 ms. `dbi_call()` searches the frames from the top of the stack. The conditions gain the field `extended_code_name`, such as `"SQLITE_CONSTRAINT_UNIQUE"`, from a table of the extended result codes in sqlite3.h. Opening the database in `dbConnect()`, `sqliteCopyDatabase()` and loading extensions raise `RSQLite_error` conditions with unchanged messages and name `dbConnect()`, `sqliteCopyDatabase()` or `initExtension()` as their call. `sqliteCopyDatabase()` fails with SQLite's error instead of crashing R when the copy can't start, and releases the copy when it fails. The deprecated `dbGetPreparedQuery()` and `dbSendPreparedQuery()` name themselves in SQLite errors from their second bind, which runs in a handler. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
…laude/rb-rlang-conditions Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
…value `sqlite3_load_extension()`, `sqlite3_backup_step()` and `sqlite3_backup_finish()` don't set the error code on the connection the error was built from, whose extended code could be left over from an earlier error, such as `SQLITE_ERROR_MISSING_COLLSEQ` for a failed load. The conditions now carry the code these calls return. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
Errors that SQLite reports in
dbSendQuery(),dbBind()anddbFetch(), when opening the database indbConnect(), insqliteCopyDatabase()and when loading extensions are now rlang conditions of classc("RSQLite_error_<name>", "RSQLite_error", "rlang_error", "error", "condition"), where<name>is SQLite's primary result code in lowercase, such asRSQLite_error_constraint,RSQLite_error_busyorRSQLite_error_cantopen. The condition carries the fieldscode(primary result code),extended_code(such as 2067 forSQLITE_CONSTRAINT_UNIQUE),code_name(such as"SQLITE_CONSTRAINT") andextended_code_name(such as"SQLITE_CONSTRAINT_UNIQUE",NAfor a code RSQLite doesn't know), and its call is the DBI method or the RSQLite function (dbConnect(),sqliteCopyDatabase(),initExtension()), as in RPostgres. Messages are unchanged, so existing expectations keep passing. The only other visible change is that these errors print with the call instead of a bareError:. Documented in the new?"RSQLite-conditions"page, NEWS bullet under Features.Covered: errors when a statement is prepared, when a parameter fails to bind (including a parameter of a type that can't be bound, raised with
SQLITE_ERRORas today), when a step fails, the error kept from reading ahead that the next fetch raises, opening a database, each step of the backup insqliteCopyDatabase(), andsqlite3_load_extension()ininitExtension()(also called bydbConnect(default.extensions = TRUE)). Errors from invalid or busy results, value conversion and argument checks stay ordinary errors; the help page lists them.sqliteSetBusyHandler()has no SQLite error to class:sqlite3_busy_handler()andsqlite3_busy_timeout()only fail for a misused handle, which the connection check already rules out.How: the C++ exception
step_errorbecomesSqliteError(newsrc/SqliteError.h), which also carries the extended result code; the prepare error and the connection-level errors are thrown as one too.with_sqlite_errors()wraps the bodies of the entry pointsresult_create(),result_bind(),result_fetch(),connection_connect(),connection_copy_database()andextension_load(), catches aSqliteErrorafter the body's C++ frames have unwound and the busy flag is reset, and only then calls the R functionsignal_sqlite_error()throughcpp11::package("RSQLite"). That R error unwinds the remaining frame through cpp11 like any R error from R code, so no longjmp crosses live C++ objects.signal_sqlite_error()raises the condition withstop(rlang::error_cnd(...))rather thanrlang::abort(), so that no backtrace is captured: it would be detached from the user's frames anyway, and capturing it cost about 1.3 ms per error.rethrow()anddbi_call()are taken from RPostgres'sR/rethrow.Rand set the call;dbi_call()searches the frames from the top of the stack. The deprecateddbGetPreparedQuery()anddbSendPreparedQuery()capture their call before thetryCatch()whose handler binds a second time, so that they name themselves. SQLite has no API for the symbolic name of an extended result code, soR/conditions.Rholds a table of the extended codes from the vendoredsqlite3.h.Performance of a caught error, 2000 ×
tryCatch(dbExecute(con, "INSERT …duplicate…"), error = function(e) NULL): 40 µs per error on the base branch, 3.4 ms withrlang::abort(), 0.2 ms now (0.3 ms instead of 7.9 ms at a call depth of 100). The remaining overhead over the base is mostlyconditionMessage()of anrlang_error, whichstop()evaluates when the condition is signaled and again whenrethrow()signals it with the call. The path without an error is unchanged.The fix for
sqliteCopyDatabase(con, con), which crashed R becausesqlite3_backup_init()returnedNULL, and for releasing the locks after a failed backup step moved to #942; this PR raises those errors as classed conditions (RSQLite_error_errorfor the same connection).sqlite3_load_extension(),sqlite3_backup_step()andsqlite3_backup_finish()don't set the error code on the connection, so these errors take their code from the call's return value rather than fromsqlite3_extended_errcode(), which could report an earlier error.Tests: new
test-conditions.Rchecks class and fields for a unique constraint error, a busy database, a syntax error at prepare, a value over the length limit at bind, a parameter of an unbindable type, and the kept read-ahead error, plusconditionCall(),tryCatch(RSQLite_error_constraint = ), and that RSQLite's own argument checks aren't classed. Further tests coverextended_code_nameand unknown codes, the call of the deprecated prepared-query functions,dbConnect()on a missing file withSQLITE_ROand on a directory (RSQLite_error_cantopen),sqliteCopyDatabase()into a read-only destination (RSQLite_error_readonly), the class of the same-connection error in #942's test,initExtension()with a library that can't be loaded, and a failed extension load after an unrelated error, which reported that error's extended code (SQLITE_ERROR_MISSING_COLLSEQ) before. Full suite withNOT_CRAN=true: FAIL 0, WARN 3 (locale), SKIP 63, PASS 11138.R CMD check --no-manualon the built tarball: Status OK.Layer R0 of the rebuilt chain, planned in #880 (asked for in #880 (comment)); based on #942. It is the base for B2 (#872), which will attach the rows read before an error to this condition. New in the rebuilt chain, not ported from an old PR.
Coordinating session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
Generated by Claude Code