Add identifier quote style to Dialect trait (#1170)

This commit is contained in:
Michiel De Backker 2024-03-11 21:27:25 +01:00 committed by GitHub
parent 11899fd0cb
commit 929c646bba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 35 additions and 0 deletions

View file

@ -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
@ -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
@ -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<'_>>,

View file

@ -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,

View file

@ -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

View file

@ -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()