named: support PostgreSQL :: cast directly after a named param#985
Open
c-tonneslan wants to merge 1 commit into
Open
named: support PostgreSQL :: cast directly after a named param#985c-tonneslan wants to merge 1 commit into
c-tonneslan wants to merge 1 commit into
Conversation
sqlx.Named was bailing with "unexpected ':' while reading named param"
on perfectly valid Postgres queries like:
SELECT :boundary::jsonb FROM ...
because the colon parser only knew about :: as a way to escape a single
colon in a string literal — it had no story for the cast operator glued
directly to the trailing edge of a named parameter.
Closes jmoiron#983.
After this change, when ':' lands while we've already built up a name,
we emit the bindvar for the name and then look at the next byte. If
it's another ':' we treat the whole '::' as a Postgres cast and pass
both bytes through to the rebound query (and skip the second one).
Otherwise the trailing ':' is written as literal text.
Also pulled the bindvar-emission switch into an emitName helper so the
new path and the existing end-of-name path don't drift.
The 'a::b::c' escape behavior inside SQL string literals is unchanged —
that case is still ambiguous between Postgres cast and escaped colon
and isn't what jmoiron#983 is about.
Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
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.
Closes #983.
sqlx.Named was bailing with `unexpected ':' while reading named param at N` on Postgres queries like:
```sql
SELECT :boundary::jsonb FROM ...
```
because the colon parser only knew about `::` as a way to escape a single colon — it had no story for the cast operator glued directly to the trailing edge of a named parameter.
When `:` lands while we've already built up a name, the new code emits the bindvar for that name and then peeks at the next byte. If it's another `:`, the whole `::` is treated as a Postgres cast and passed through to the rebound query. Otherwise the trailing `:` is written as literal text.
The bindvar-emission switch is now factored into an `emitName` helper so the new path and the existing end-of-name path stay in sync.
The `'a::b::c'` escape behavior inside SQL string literals is intentionally left alone — that case is ambiguous between Postgres cast and escaped colon and isn't what #983 is about.
Test plan
```
go test ./...
```
Added a case to TestCompileQuery covering `:boundary::jsonb, :id::text` across all bind types.