Skip to content

Commit

Permalink
Add identifier quote style to Dialect trait (#1170)
Browse files Browse the repository at this point in the history
  • Loading branch information
backkem authored Mar 11, 2024
1 parent 11899fd commit 929c646
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ pub trait Dialect: Debug + Any {
fn is_delimited_identifier_start(&self, ch: char) -> bool {
ch == '"' || ch == '`'
}
/// Return the character used to quote identifiers.
fn identifier_quote_style(&self, _identifier: &str) -> Option<char> {
None
}
/// Determine if quoted characters are proper for identifier
fn is_proper_identifier_inside_quotes(&self, mut _chars: Peekable<Chars<'_>>) -> bool {
true
Expand Down Expand Up @@ -262,6 +266,21 @@ mod tests {
dialect_from_str(v).unwrap()
}

#[test]
fn identifier_quote_style() {
let tests: Vec<(&dyn Dialect, &str, Option<char>)> = vec![
(&GenericDialect {}, "id", None),
(&SQLiteDialect {}, "id", Some('`')),
(&PostgreSqlDialect {}, "id", Some('"')),
];

for (dialect, ident, expected) in tests {
let actual = dialect.identifier_quote_style(ident);

assert_eq!(actual, expected);
}
}

#[test]
fn parse_with_wrapped_dialect() {
/// Wrapper for a dialect. In a real-world example, this wrapper
Expand All @@ -283,6 +302,10 @@ mod tests {
self.0.is_delimited_identifier_start(ch)
}

fn identifier_quote_style(&self, identifier: &str) -> Option<char> {
self.0.identifier_quote_style(identifier)
}

fn is_proper_identifier_inside_quotes(
&self,
chars: std::iter::Peekable<std::str::Chars<'_>>,
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ impl Dialect for MySqlDialect {
ch == '`'
}

fn identifier_quote_style(&self, _identifier: &str) -> Option<char> {
Some('`')
}

fn parse_infix(
&self,
parser: &mut crate::parser::Parser,
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ use crate::tokenizer::Token;
pub struct PostgreSqlDialect {}

impl Dialect for PostgreSqlDialect {
fn identifier_quote_style(&self, _identifier: &str) -> Option<char> {
Some('"')
}

fn is_identifier_start(&self, ch: char) -> bool {
// See https://www.postgresql.org/docs/11/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
// We don't yet support identifiers beginning with "letters with
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ impl Dialect for SQLiteDialect {
ch == '`' || ch == '"' || ch == '['
}

fn identifier_quote_style(&self, _identifier: &str) -> Option<char> {
Some('`')
}

fn is_identifier_start(&self, ch: char) -> bool {
// See https://www.sqlite.org/draft/tokenreq.html
ch.is_ascii_lowercase()
Expand Down

0 comments on commit 929c646

Please sign in to comment.