fix(any): rewrite ? placeholders to $N for Postgres backend (#3000)#4351
Open
allocsys wants to merge 1 commit into
Open
fix(any): rewrite ? placeholders to $N for Postgres backend (#3000)#4351allocsys wants to merge 1 commit into
allocsys wants to merge 1 commit into
Conversation
…-rs#3000) QueryBuilder<Any>::push_bind() always writes '?' placeholders since AnyArguments has no backend context at build time. This adds a SQL-aware rewrite (skipping string literals, quoted identifiers, dollar-quoted strings, and comments) applied on the Postgres leg of the Any driver, right before the query hits the wire protocol in fetch_many/fetch_optional/prepare_with/describe. MySQL and SQLite are unaffected since they already use '?' natively. Fixes transact-rs#3000
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.
Fixes #3000.
The
Anydriver always wrote?-style placeholders, which is correct for MySQL/SQLite but not Postgres ($1,$2, ...). This caused a syntax error on any query issued throughAny+ Postgres that had more than a trivial shape (e.g. bound params combined withLIMIT/OFFSET).The originally proposed fix (adding an
AnyKindfield toAnyArgumentsplus aformat_placeholderoverride) doesn't work:QueryBuilder<Any>::push_bind()writes placeholders into the SQL string before any backend is known, sinceAnyArgumentsis built viaDefault::default(). The backend is only resolved later, inAnyConnectionBackend::fetch_many().This PR instead rewrites
?->$1, $2, ...insidesqlx-postgres/src/any.rs, right before the query is dispatched toPgConnection(coversfetch_many,fetch_optional,prepare_with, anddescribe). Only the Postgres leg needs this, since MySQL/SQLite already use?natively.The rewrite is SQL-aware (not a blind
str::replace) so it skips?that appears inside:''escaping)""escaping)$$...$$and$tag$...$tag$)--line comments and/* */block commentsAdded unit tests covering the placeholder-numbering happy path and each of the above edge cases.
CI (Examples, SQLx, SQLx CLI) passes clean on the fork: allocsys#1.