Expand description
psql-style variable resolution for SQL files.
Resolves :foo, :'foo', and :"foo" syntax in raw SQL text before
it reaches the SQL parser. Variables are defined per-profile in
[<profile>.variables] in project.toml.
§Variable Syntax
| Syntax | Semantics | Example output |
|---|---|---|
:name | Raw substitution — value inserted verbatim | analytics |
:'name' | SQL literal — value wrapped in single quotes, ' doubled | 'it''s-a-host' |
:"name" | SQL identifier — value wrapped in double quotes, " doubled | "my""col" |
§Resolution Rules
- Variables are looked up in the
BTreeMap<String, String>passed toresolve_variables. This map is populated fromproject.toml’s[<profile>.variables]section. - If a referenced variable has no definition, it is left as-is in the
output and an
UnresolvedVariable(with byte offset and length) is recorded inResolvedSql::unresolved. Each occurrence is tracked separately (not deduplicated) so the LSP can highlight every reference. - Every resolved substitution is recorded as a
SubstitutioninResolvedSql::substitutions, enablingresolved_to_originalto map byte offsets in the resolved text back to the original source positions. - The caller decides whether unresolved variables are errors or warnings
based on the
PRAGMA WARN_ON_MISSING_VARIABLES;directive.
§Context Awareness
Variable references are not resolved inside:
- Single-quoted string literals (
'...') - Double-quoted identifiers (
"...") - Line comments (
-- ...) - Block comments (
/* ... */), including nested blocks - Dollar-quoted strings (
$$...$$or$tag$...$tag$)
The :: token (PostgreSQL type cast) is never interpreted as a variable
reference.
Structs§
- Resolved
Sql 🔒 - Result of resolving psql-style variables in SQL text.
- Substitution 🔒
- A resolved variable substitution, for mapping offsets between original and resolved text.
- Unresolved
Variable - An unresolved variable reference with its location in the original SQL.
- Variable
Error - Error returned when SQL contains variable references that have no definition.
Enums§
- VarKind 🔒
- The kind of variable reference found in the SQL text.
Constants§
- PRAGMA 🔒
Functions§
- consume_
block_ 🔒comment - Consume a block comment (with nesting).
iis the position after/*. Returns the position after the final*/. - consume_
dollar_ 🔒quoted - Consume a dollar-quoted string.
iis the position after the opening tag. Scans for the matching closing tag. - consume_
double_ 🔒quoted - Consume a double-quoted identifier.
iis the position after the opening". Returns the position after the closing". - consume_
line_ 🔒comment - Consume a line comment.
iis the position after--. Returns the position after\n(or end of input). - consume_
single_ 🔒quoted - Consume a single-quoted string.
iis the position after the opening'. Returns the position after the closing'. - detect_
warn_ 🔒pragma - Check whether the first non-whitespace content in
sqlis a comment containing the warn-on-missing-variables pragma. - find_
variable_ 🔒at_ position - Find the variable reference (if any) that contains the given byte offset.
- push_
sql_ 🔒escaped - Push
valueintoout, doubling any occurrence ofquote. - resolve_
variables 🔒 - Resolve psql-style variables (
:foo,:'foo',:"foo") in SQL text. - resolved_
to_ 🔒original - Map a byte offset in resolved text back to the corresponding offset in original text.
- starts_
with 🔒 - Check if
bytes[i..]starts withneedle. - try_
dollar_ 🔒tag - Check if
$at positionistarts a dollar-quote tag. Returns(end_pos, tag_bytes)whereend_posis the position after the closing$of the tag. - try_
read_ 🔒variable - Try to read a variable reference starting at position
i(which must be:).