Skip to content

Commit

Permalink
follow the latest Dialect trait in sqlparser
Browse files Browse the repository at this point in the history
  • Loading branch information
goldmedal committed May 22, 2024
1 parent 736bc8e commit 66df8d3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
27 changes: 13 additions & 14 deletions datafusion/sql/src/unparser/dialect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,43 @@ use sqlparser::keywords::ALL_KEYWORDS;
///
/// See <https://github.com/sqlparser-rs/sqlparser-rs/pull/1170>
pub trait Dialect {
fn identifier_quote_style(&self) -> Option<char>;
fn identifier_needs_quote(&self, _: &str) -> bool {
true
}
fn identifier_quote_style(&self, _identifier: &str) -> Option<char>;
}
pub struct DefaultDialect {}

impl Dialect for DefaultDialect {
fn identifier_quote_style(&self) -> Option<char> {
Some('"')
}
fn identifier_needs_quote(&self, ident: &str) -> bool {
fn identifier_quote_style(&self, _identifier: &str) -> Option<char> {
let identifier_regex = Regex::new(r"^[a-zA-Z_][a-zA-Z0-9_]*$").unwrap();
ALL_KEYWORDS.contains(&ident.to_uppercase().as_str())
|| !identifier_regex.is_match(ident)
if ALL_KEYWORDS.contains(&_identifier.to_uppercase().as_str())
|| !identifier_regex.is_match(_identifier)
{
Some('"')
} else {
None
}
}
}

pub struct PostgreSqlDialect {}

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

pub struct MySqlDialect {}

impl Dialect for MySqlDialect {
fn identifier_quote_style(&self) -> Option<char> {
fn identifier_quote_style(&self, _: &str) -> Option<char> {
Some('`')
}
}

pub struct SqliteDialect {}

impl Dialect for SqliteDialect {
fn identifier_quote_style(&self) -> Option<char> {
fn identifier_quote_style(&self, _: &str) -> Option<char> {
Some('`')
}
}
Expand All @@ -81,7 +80,7 @@ impl CustomDialect {
}

impl Dialect for CustomDialect {
fn identifier_quote_style(&self) -> Option<char> {
fn identifier_quote_style(&self, _: &str) -> Option<char> {
self.identifier_quote_style
}
}
13 changes: 3 additions & 10 deletions datafusion/sql/src/unparser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,17 +538,10 @@ impl Unparser<'_> {

/// This function can create an identifier with or without quotes based on the dialect rules
pub(super) fn new_ident_quoted_if_needs(&self, ident: String) -> ast::Ident {
if self.dialect.identifier_needs_quote(&ident) {
self.new_ident(ident)
} else {
self.new_ident_without_quote_style(ident)
}
}

pub(super) fn new_ident(&self, str: String) -> ast::Ident {
let quote_style = self.dialect.identifier_quote_style(&ident);
ast::Ident {
value: str,
quote_style: self.dialect.identifier_quote_style(),
value: ident,
quote_style,
}
}

Expand Down

0 comments on commit 66df8d3

Please sign in to comment.