Repository navigation
feat: type query placeholders by their SQL cast - #57
Merged
Merged
Conversation
A $N or :name placeholder that is the direct operand of ::type or CAST(... AS type) inside WHERE, INSERT values, or UPDATE assignments takes the cast's type, including declared enums and one-dimensional arrays. An indexed cast placeholder keeps the column name an uncast one would take and is otherwise named param<N>; a named one keeps its own name. Unmapped cast types, casts in unanalyzed locations, and quoted, qualified, or &name cast operands are rejected.
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.
Summary
Lets a query state a placeholder's type with a cast, so common idioms such as an optional filter
(:name::text IS NULL OR name = :name), a computed pattern'%' || :term::text || '%', andSET bio = COALESCE(:bio::text, bio)compile as written. The cast type is resolved by the same declared-type mapping the schema parser uses, now extracted into a sharedColumnTypeMapping.Changes
$Nor:nameplaceholder that is the direct operand of::typeorCAST(... AS type)anywhere insideWHERE, anINSERTvalue, or anUPDATEassignment takes the cast's type, including declared enums,char(n), and one-dimensional arrays such astags && $1::varchar[].IN,BETWEEN, plainLIKE/ILIKEpattern, inserted, or assigned column), and is otherwise namedparam<N>. A cast pattern underNOT LIKE,SIMILAR TO,ESCAPE, orBINARYis accepted and namedparam<N>.$1::intervalor$1::int[][], a cast placeholder in an unanalyzed location such asORDER BY $1::int, a placeholder that is not the cast's direct operand, and quoted, qualified, or&namecast operands, which previously reached the executable SQL unreplaced.DefaultSchemaParserintoColumnTypeMappingwithout changing schema parsing.Scope and non-goals
ORDER BY,HAVING, join conditions, pagination values, and subqueries stay rejected; computed or cast projections are not added.LIKEpattern restrictions and their messages are unchanged, as are queries without casts.