-
Notifications
You must be signed in to change notification settings - Fork 102
fix: add support for named param type $name
#475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -186,9 +186,29 @@ impl Cursor<'_> { | |||||
'$' => { | ||||||
// Dollar quoted strings | ||||||
if is_ident_start(self.first()) || self.first() == '$' { | ||||||
self.dollar_quoted_string() | ||||||
// Get the start sequence of the dollar quote, i.e., 'foo' in $foo$hello$foo$ | ||||||
// if ident does not continue and there is no terminating dollar | ||||||
// sign, we have a positional param `$name` | ||||||
let mut start = vec![]; | ||||||
loop { | ||||||
match self.first() { | ||||||
'$' => { | ||||||
self.bump(); | ||||||
break self.dollar_quoted_string(start); | ||||||
} | ||||||
c if is_ident_cont(c) => { | ||||||
self.bump(); | ||||||
start.push(c); | ||||||
} | ||||||
_ => { | ||||||
break TokenKind::NamedParam { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this would actually behave differently. we need break, and the value is then returned from the loop. with return, we would return the value from the function. |
||||||
kind: NamedParamKind::DollarRaw, | ||||||
}; | ||||||
} | ||||||
} | ||||||
} | ||||||
} else { | ||||||
// Parameters | ||||||
// positional parameter, e.g. `$1` | ||||||
while self.first().is_ascii_digit() { | ||||||
self.bump(); | ||||||
} | ||||||
|
@@ -490,22 +510,7 @@ impl Cursor<'_> { | |||||
} | ||||||
|
||||||
// https://www.postgresql.org/docs/16/sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING | ||||||
fn dollar_quoted_string(&mut self) -> TokenKind { | ||||||
// Get the start sequence of the dollar quote, i.e., 'foo' in | ||||||
// $foo$hello$foo$ | ||||||
let mut start = vec![]; | ||||||
while let Some(c) = self.bump() { | ||||||
match c { | ||||||
'$' => { | ||||||
self.bump(); | ||||||
break; | ||||||
} | ||||||
_ => { | ||||||
start.push(c); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
fn dollar_quoted_string(&mut self, start: Vec<char>) -> TokenKind { | ||||||
// we have a dollar quoted string deliminated with `$$` | ||||||
if start.is_empty() { | ||||||
loop { | ||||||
|
@@ -658,6 +663,12 @@ mod tests { | |||||
assert_debug_snapshot!(result); | ||||||
} | ||||||
|
||||||
#[test] | ||||||
fn named_param_dollar_raw() { | ||||||
let result = lex("select 1 from c where id = $id;"); | ||||||
assert_debug_snapshot!(result); | ||||||
} | ||||||
|
||||||
#[test] | ||||||
fn named_param_colon_raw() { | ||||||
let result = lex("select 1 from c where id = :id;"); | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
source: crates/pgt_tokenizer/src/lib.rs | ||
expression: result | ||
snapshot_kind: text | ||
--- | ||
[ | ||
"select" @ Ident, | ||
" " @ Space, | ||
"1" @ Literal { kind: Int { base: Decimal, empty_int: false } }, | ||
" " @ Space, | ||
"from" @ Ident, | ||
" " @ Space, | ||
"c" @ Ident, | ||
" " @ Space, | ||
"where" @ Ident, | ||
" " @ Space, | ||
"id" @ Ident, | ||
" " @ Space, | ||
"=" @ Eq, | ||
" " @ Space, | ||
"$id" @ NamedParam { kind: DollarRaw }, | ||
";" @ Semi, | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hat mich kurz irritiert, weil der breaked value ja auch aus der obrigen fn returned wird