Skip to content

Commit 929c646

Browse files
authored
Add identifier quote style to Dialect trait (#1170)
1 parent 11899fd commit 929c646

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

src/dialect/mod.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ pub trait Dialect: Debug + Any {
108108
fn is_delimited_identifier_start(&self, ch: char) -> bool {
109109
ch == '"' || ch == '`'
110110
}
111+
/// Return the character used to quote identifiers.
112+
fn identifier_quote_style(&self, _identifier: &str) -> Option<char> {
113+
None
114+
}
111115
/// Determine if quoted characters are proper for identifier
112116
fn is_proper_identifier_inside_quotes(&self, mut _chars: Peekable<Chars<'_>>) -> bool {
113117
true
@@ -262,6 +266,21 @@ mod tests {
262266
dialect_from_str(v).unwrap()
263267
}
264268

269+
#[test]
270+
fn identifier_quote_style() {
271+
let tests: Vec<(&dyn Dialect, &str, Option<char>)> = vec![
272+
(&GenericDialect {}, "id", None),
273+
(&SQLiteDialect {}, "id", Some('`')),
274+
(&PostgreSqlDialect {}, "id", Some('"')),
275+
];
276+
277+
for (dialect, ident, expected) in tests {
278+
let actual = dialect.identifier_quote_style(ident);
279+
280+
assert_eq!(actual, expected);
281+
}
282+
}
283+
265284
#[test]
266285
fn parse_with_wrapped_dialect() {
267286
/// Wrapper for a dialect. In a real-world example, this wrapper
@@ -283,6 +302,10 @@ mod tests {
283302
self.0.is_delimited_identifier_start(ch)
284303
}
285304

305+
fn identifier_quote_style(&self, identifier: &str) -> Option<char> {
306+
self.0.identifier_quote_style(identifier)
307+
}
308+
286309
fn is_proper_identifier_inside_quotes(
287310
&self,
288311
chars: std::iter::Peekable<std::str::Chars<'_>>,

src/dialect/mysql.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ impl Dialect for MySqlDialect {
4444
ch == '`'
4545
}
4646

47+
fn identifier_quote_style(&self, _identifier: &str) -> Option<char> {
48+
Some('`')
49+
}
50+
4751
fn parse_infix(
4852
&self,
4953
parser: &mut crate::parser::Parser,

src/dialect/postgresql.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ use crate::tokenizer::Token;
2121
pub struct PostgreSqlDialect {}
2222

2323
impl Dialect for PostgreSqlDialect {
24+
fn identifier_quote_style(&self, _identifier: &str) -> Option<char> {
25+
Some('"')
26+
}
27+
2428
fn is_identifier_start(&self, ch: char) -> bool {
2529
// See https://www.postgresql.org/docs/11/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
2630
// We don't yet support identifiers beginning with "letters with

src/dialect/sqlite.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ impl Dialect for SQLiteDialect {
3232
ch == '`' || ch == '"' || ch == '['
3333
}
3434

35+
fn identifier_quote_style(&self, _identifier: &str) -> Option<char> {
36+
Some('`')
37+
}
38+
3539
fn is_identifier_start(&self, ch: char) -> bool {
3640
// See https://www.sqlite.org/draft/tokenreq.html
3741
ch.is_ascii_lowercase()

0 commit comments

Comments
 (0)