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

SQLite: Allow dollar signs in placeholder names #1620

Merged
merged 3 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,10 @@ pub trait Dialect: Debug + Any {
false
}

fn supports_dollar_quoted_string(&self) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to add some documents for this method. (Just like others).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondering would it make more sense to flip the condition to e.g. supports_dollar_placeholder(), setting that to true for sqlite and default false for the others? Thinking that might be the more conservative change of the two given that its the placeholder behavior that is more subtle/specific, (most of the other dialects technically don't support dollar quoted string so its less misleading if they don't have to explicitly flag otherwise)

true
}

/// Does the dialect support with clause in create index statement?
/// e.g. `CREATE INDEX idx ON t WITH (key = value, key2)`
fn supports_create_index_with_clause(&self) -> bool {
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,8 @@ impl Dialect for SQLiteDialect {
fn supports_asc_desc_in_column_definition(&self) -> bool {
true
}

fn supports_dollar_quoted_string(&self) -> bool {
false
}
}
37 changes: 33 additions & 4 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1523,7 +1523,9 @@ impl<'a> Tokenizer<'a> {

chars.next();

if let Some('$') = chars.peek() {
// Check if the second character is a dollar sign
let next_is_dollar = matches!(chars.peek(), Some('$'));
if next_is_dollar && self.dialect.supports_dollar_quoted_string() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Check if the second character is a dollar sign
let next_is_dollar = matches!(chars.peek(), Some('$'));
if next_is_dollar && self.dialect.supports_dollar_quoted_string() {
// If the dialect does not support dollar-quoted string, then `$$` is rather a placeholder.
if matches!(chars.peek(), Some('$')) && self.dialect.supports_dollar_quoted_string() {

if that makes sense, figured we could mention in the comment what the condition implies?

chars.next();

let mut is_terminated = false;
Expand Down Expand Up @@ -1557,10 +1559,13 @@ impl<'a> Tokenizer<'a> {
};
} else {
value.push_str(&peeking_take_while(chars, |ch| {
ch.is_alphanumeric() || ch == '_'
ch.is_alphanumeric()
|| ch == '_'
|| matches!(ch, '$' if !self.dialect.supports_dollar_quoted_string())
}));

if let Some('$') = chars.peek() {
let next_is_dollar = matches!(chars.peek(), Some('$'));
if next_is_dollar && self.dialect.supports_dollar_quoted_string() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let next_is_dollar = matches!(chars.peek(), Some('$'));
if next_is_dollar && self.dialect.supports_dollar_quoted_string() {
if matches!(chars.peek(), Some('$')) && self.dialect.supports_dollar_quoted_string() {

chars.next();

'searching_for_end: loop {
Expand Down Expand Up @@ -2151,7 +2156,7 @@ fn take_char_from_hex_digits(
mod tests {
use super::*;
use crate::dialect::{
BigQueryDialect, ClickHouseDialect, HiveDialect, MsSqlDialect, MySqlDialect,
BigQueryDialect, ClickHouseDialect, HiveDialect, MsSqlDialect, MySqlDialect, SQLiteDialect,
};
use core::fmt::Debug;

Expand Down Expand Up @@ -2604,6 +2609,30 @@ mod tests {
);
}

#[test]
fn tokenize_dollar_placeholder_sqlite() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn tokenize_dollar_placeholder_sqlite() {
fn tokenize_dollar_placeholder() {

let sql = String::from("SELECT $$, $$ABC$$, $ABC$, $ABC");
let dialect = SQLiteDialect {};
let tokens = Tokenizer::new(&dialect, &sql).tokenize().unwrap();
assert_eq!(
tokens,
vec![
Token::make_keyword("SELECT"),
Token::Whitespace(Whitespace::Space),
Token::Placeholder("$$".into()),
Token::Comma,
Token::Whitespace(Whitespace::Space),
Token::Placeholder("$$ABC$$".into()),
Token::Comma,
Token::Whitespace(Whitespace::Space),
Token::Placeholder("$ABC$".into()),
Token::Comma,
Token::Whitespace(Whitespace::Space),
Token::Placeholder("$ABC".into()),
]
);
}

#[test]
fn tokenize_dollar_quoted_string_untagged() {
let sql =
Expand Down
10 changes: 10 additions & 0 deletions tests/sqlparser_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,16 @@ fn test_dollar_identifier_as_placeholder() {
}
_ => unreachable!(),
}

// $$ is a valid placeholder in SQLite
match sqlite().verified_expr("id = $$") {
Expr::BinaryOp { op, left, right } => {
assert_eq!(op, BinaryOperator::Eq);
assert_eq!(left, Box::new(Expr::Identifier(Ident::new("id"))));
assert_eq!(right, Box::new(Expr::Value(Placeholder("$$".to_string()))));
}
_ => unreachable!(),
}
}

fn sqlite() -> TestedDialects {
Expand Down
Loading