Repository navigation
Fix Statement#execute parameters persisting into later executions - #757
Closed
flavorjones wants to merge 1 commit into
Closed
flavorjones wants to merge 1 commit into
flavorjones wants to merge 1 commit into
Conversation
A parameter passed to `Statement#execute` stayed bound, so a later `#execute` that omitted it reused the old value, and an `#execute` value overrode a `#bind_param` value for good. Record each value bound by `#bind_param` and `#bind_params`, keyed by parameter index, copying unfrozen Strings. When the next `#execute` or binding follows an `#execute` with arguments, clear the statement's bindings and rebind the recorded values. Code that relied on the old behavior should bind persistent values with `#bind_params`. [Fix #751]
flavorjones
force-pushed
the
card-793-execute-bind-params-persist
branch
from
October 24, 2026 14:49
4294c76 to
900972a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
A parameter passed to
Statement#executestays bound after that execution, as @jeremyevans reported in #740:Statement#executebinds its arguments with#bind_paramsand never undoes them. Values bound with#bind_paramsbefore#executemust still persist, so#executecannot simply clear every binding:Details
SQLite has no API to read a bound value, so
Statement#bind_paramand#bind_paramsnow record each value in a Hash on the statement, keyed by the parameter index SQLite resolved. An unfrozen String is recorded as a frozen copy made withString.new(orSQLite3::Blob.new), because SQLite copies its bytes at bind time and the caller may mutate it afterwards.String.newruns no subclass hooks, and the copy is made before binding.#executestill binds its arguments through#bind_paramsand#bind_param, so subclasses that override them keep working, but those values are not recorded. After such an#execute, the next#executeor the next value bound by#bind_paramor#bind_paramscallssqlite3_clear_bindings()and rebinds the recorded values, so placeholders set only by the earlier#executebecome NULL.#bind_parambinds the new value before restoring, so a bind that raises on a running statement leaves that statement's bindings alone.With this change, the first example returns
[[3]],[[nil]],[[nil]]. A value passed to#executealso overrides a#bind_paramvalue only for that execution.Calling
#bind_paramswith no values binds nothing and restores nothing.ResultSet#resetwith no arguments therefore still rewinds with the values passed to#execute, as it did before.#clear_bindings!also forgets the recorded values. Its documentation said it reset the statement; it now says it sets every placeholder to NULL.#bind_paramand#clear_bindings!are now Ruby methods wrapping the C implementations, which are private.Additional information
Code that passes a value to
#executeand expects a later#executeto reuse it must now bind that value with#bind_params.The recorded Hash keeps the last value bound to each parameter alive until that parameter is rebound or
#clear_bindings!is called. A cached statement therefore holds the last values bound with#bind_paramor#bind_params. Except while an#executeoverrides them, SQLite holds its own copy of the same values. On the path Rails uses (reset!,bind_params,to_aon a cached statement), recording costs one allocation per unfrozen String bound. On a one-row in-memory query binding an Integer and a String, that path takes 1.52 µs per call, against 1.23 µs with the recording removed (best of 4 runs of 500,000 calls).Resolves #751.