Skip to content

Commit 45fc758

Browse files
committed
Fix identifier starts with $ should be regarded as a placeholder in SQLite
Currently, `$var` would be parsed as an identifier instead of a placeholder in SQLite dialect, which is unexpected from the SQLite's documentation: https://www.sqlite.org/lang_expr.html#varparam. This relates to the issue comment: apache#291 (comment)
1 parent 222b7d1 commit 45fc758

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/dialect/sqlite.rs

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ impl Dialect for SQLiteDialect {
4141
ch.is_ascii_lowercase()
4242
|| ch.is_ascii_uppercase()
4343
|| ch == '_'
44-
|| ch == '$'
4544
|| ('\u{007f}'..='\u{ffff}').contains(&ch)
4645
}
4746

tests/sqlparser_sqlite.rs

+17
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mod test_utils;
2020
use test_utils::*;
2121

2222
use sqlparser::ast::SelectItem::UnnamedExpr;
23+
use sqlparser::ast::Value::Placeholder;
2324
use sqlparser::ast::*;
2425
use sqlparser::dialect::{GenericDialect, SQLiteDialect};
2526
use sqlparser::parser::{ParserError, ParserOptions};
@@ -470,6 +471,22 @@ fn parse_start_transaction_with_modifier() {
470471
);
471472
}
472473

474+
#[test]
475+
fn test_dollar_identifier_as_placeholder() {
476+
// This relates to the discussion in issue #291. The `$id` should be treated as a placeholder,
477+
// not as an identifier in SQLite dialect.
478+
//
479+
// Reference: https://www.sqlite.org/lang_expr.html#varparam
480+
match sqlite().verified_expr("id = $id") {
481+
Expr::BinaryOp { op, left, right } => {
482+
assert_eq!(op, BinaryOperator::Eq);
483+
assert_eq!(left, Box::new(Expr::Identifier(Ident::new("id"))));
484+
assert_eq!(right, Box::new(Expr::Value(Placeholder("$id".to_string()))));
485+
}
486+
_ => unreachable!(),
487+
}
488+
}
489+
473490
fn sqlite() -> TestedDialects {
474491
TestedDialects {
475492
dialects: vec![Box::new(SQLiteDialect {})],

0 commit comments

Comments
 (0)