Skip to content

Commit 9e7e302

Browse files
authored
Support identifiers quoted with backticks in the MySQL dialect (#247)
Per https://dev.mysql.com/doc/refman/8.0/en/identifiers.html MySQL historically supports `identifiers quoted in backticks` in addition to the ANSI "quoting style" (assuming ANSI_QUOTES mode).
1 parent 1337820 commit 9e7e302

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/dialect/mysql.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@ impl Dialect for MySqlDialect {
3030
fn is_identifier_part(&self, ch: char) -> bool {
3131
self.is_identifier_start(ch) || (ch >= '0' && ch <= '9')
3232
}
33+
34+
fn is_delimited_identifier_start(&self, ch: char) -> bool {
35+
ch == '`'
36+
}
3337
}

tests/sqlparser_mysql.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ fn parse_create_table_auto_increment() {
106106
assert_eq!(name.to_string(), "foo");
107107
assert_eq!(
108108
vec![ColumnDef {
109-
name: "bar".into(),
109+
name: Ident::new("bar"),
110110
data_type: DataType::Int,
111111
collation: None,
112112
options: vec![
@@ -129,6 +129,29 @@ fn parse_create_table_auto_increment() {
129129
}
130130
}
131131

132+
#[test]
133+
fn parse_quote_identifiers() {
134+
let sql = "CREATE TABLE `PRIMARY` (`BEGIN` INT PRIMARY KEY)";
135+
match mysql().verified_stmt(sql) {
136+
Statement::CreateTable { name, columns, .. } => {
137+
assert_eq!(name.to_string(), "`PRIMARY`");
138+
assert_eq!(
139+
vec![ColumnDef {
140+
name: Ident::with_quote('`', "BEGIN"),
141+
data_type: DataType::Int,
142+
collation: None,
143+
options: vec![ColumnOptionDef {
144+
name: None,
145+
option: ColumnOption::Unique { is_primary: true }
146+
}],
147+
}],
148+
columns
149+
);
150+
}
151+
_ => unreachable!(),
152+
}
153+
}
154+
132155
fn mysql() -> TestedDialects {
133156
TestedDialects {
134157
dialects: vec![Box::new(MySqlDialect {})],

0 commit comments

Comments
 (0)