Skip to content
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

Support full range of sqlite prepared statement placeholders #604

Merged
merged 1 commit into from Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/parser.rs
Expand Up @@ -570,7 +570,7 @@ impl<'a> Parser<'a> {
})
}
}
Token::Placeholder(_) => {
Token::Placeholder(_) | Token::Colon | Token::AtSign => {
self.prev_token();
Ok(Expr::Value(self.parse_value()?))
}
Expand Down Expand Up @@ -1774,7 +1774,7 @@ impl<'a> Parser<'a> {
.iter()
.any(|d| kw.keyword == *d) =>
{
break
break;
}
Token::RParen | Token::EOF => break,
_ => continue,
Expand Down Expand Up @@ -3026,6 +3026,11 @@ impl<'a> Parser<'a> {
Token::EscapedStringLiteral(ref s) => Ok(Value::EscapedStringLiteral(s.to_string())),
Token::HexStringLiteral(ref s) => Ok(Value::HexStringLiteral(s.to_string())),
Token::Placeholder(ref s) => Ok(Value::Placeholder(s.to_string())),
tok @ Token::Colon | tok @ Token::AtSign => {
let ident = self.parse_identifier()?;
let placeholder = tok.to_string() + &ident.value;
Ok(Value::Placeholder(placeholder))
}
unexpected => self.expected("a value", unexpected),
}
}
Expand Down Expand Up @@ -4844,12 +4849,12 @@ impl<'a> Parser<'a> {
Some(_) => {
return Err(ParserError::ParserError(
"expected UPDATE, DELETE or INSERT in merge clause".to_string(),
))
));
}
None => {
return Err(ParserError::ParserError(
"expected UPDATE, DELETE or INSERT in merge clause".to_string(),
))
));
}
},
);
Expand Down
11 changes: 6 additions & 5 deletions src/tokenizer.rs
Expand Up @@ -677,13 +677,14 @@ impl<'a> Tokenizer<'a> {
}
}
'@' => self.consume_and_return(chars, Token::AtSign),
'?' => self.consume_and_return(chars, Token::Placeholder(String::from("?"))),
'?' => {
chars.next();
let s = peeking_take_while(chars, |ch| ch.is_numeric());
Ok(Some(Token::Placeholder(String::from("?") + &s)))
}
'$' => {
chars.next();
let s = peeking_take_while(
chars,
|ch| matches!(ch, '0'..='9' | 'A'..='Z' | 'a'..='z'),
);
let s = peeking_take_while(chars, |ch| ch.is_alphanumeric() || ch == '_');
Ok(Some(Token::Placeholder(String::from("$") + &s)))
}
//whitespace check (including unicode chars) should be last as it covers some of the chars above
Expand Down
12 changes: 12 additions & 0 deletions tests/sqlparser_common.rs
Expand Up @@ -22,6 +22,7 @@
mod test_utils;

use matches::assert_matches;
use sqlparser::ast::SelectItem::UnnamedExpr;
use sqlparser::ast::*;
use sqlparser::dialect::{
AnsiDialect, BigQueryDialect, ClickHouseDialect, GenericDialect, HiveDialect, MsSqlDialect,
Expand Down Expand Up @@ -5213,6 +5214,17 @@ fn test_placeholder() {
rows: OffsetRows::None,
}),
);

let sql = "SELECT $fromage_français, :x, ?123";
let ast = dialects.verified_only_select(sql);
assert_eq!(
ast.projection,
vec![
UnnamedExpr(Expr::Value(Value::Placeholder("$fromage_français".into()))),
UnnamedExpr(Expr::Value(Value::Placeholder(":x".into()))),
UnnamedExpr(Expr::Value(Value::Placeholder("?123".into()))),
]
);
}

#[test]
Expand Down
21 changes: 18 additions & 3 deletions tests/sqlparser_sqlite.rs
Expand Up @@ -16,8 +16,10 @@

#[macro_use]
mod test_utils;

use test_utils::*;

use sqlparser::ast::SelectItem::UnnamedExpr;
use sqlparser::ast::*;
use sqlparser::dialect::{GenericDialect, SQLiteDialect};
use sqlparser::tokenizer::Token;
Expand Down Expand Up @@ -73,14 +75,14 @@ fn parse_create_table_auto_increment() {
options: vec![
ColumnOptionDef {
name: None,
option: ColumnOption::Unique { is_primary: true }
option: ColumnOption::Unique { is_primary: true },
},
ColumnOptionDef {
name: None,
option: ColumnOption::DialectSpecific(vec![Token::make_keyword(
"AUTOINCREMENT"
)])
}
)]),
},
],
}],
columns
Expand Down Expand Up @@ -118,6 +120,19 @@ fn parse_create_sqlite_quote() {
}
}

#[test]
fn test_placeholder() {
// In postgres, this would be the absolute value operator '@' applied to the column 'xxx'
// But in sqlite, this is a named parameter.
// see https://www.sqlite.org/lang_expr.html#varparam
let sql = "SELECT @xxx";
lovasoa marked this conversation as resolved.
Show resolved Hide resolved
let ast = sqlite().verified_only_select(sql);
assert_eq!(
ast.projection[0],
UnnamedExpr(Expr::Value(Value::Placeholder("@xxx".into()))),
);
}

fn sqlite() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(SQLiteDialect {})],
Expand Down