From f3c00511b54e8e3055500f2e49069a9ec23944b0 Mon Sep 17 00:00:00 2001 From: mashuai Date: Wed, 29 Jul 2020 13:54:41 +0800 Subject: [PATCH 1/2] support MySQL quote style like `BEGIN` --- src/dialect/mysql.rs | 4 ++++ tests/sqlparser_mysql.rs | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/dialect/mysql.rs b/src/dialect/mysql.rs index e0b4e21c46..a4aaafe6b3 100644 --- a/src/dialect/mysql.rs +++ b/src/dialect/mysql.rs @@ -30,4 +30,8 @@ impl Dialect for MySqlDialect { fn is_identifier_part(&self, ch: char) -> bool { self.is_identifier_start(ch) || (ch >= '0' && ch <= '9') } + + fn is_delimited_identifier_start(&self, ch: char) -> bool { + ch == '`' + } } diff --git a/tests/sqlparser_mysql.rs b/tests/sqlparser_mysql.rs index 4e49a6348e..20d38baad7 100644 --- a/tests/sqlparser_mysql.rs +++ b/tests/sqlparser_mysql.rs @@ -100,13 +100,13 @@ fn parse_show_columns() { #[test] fn parse_create_table_auto_increment() { - let sql = "CREATE TABLE foo (bar INT PRIMARY KEY AUTO_INCREMENT)"; + let sql = "CREATE TABLE `foo` (`bar` INT PRIMARY KEY AUTO_INCREMENT)"; match mysql().verified_stmt(sql) { Statement::CreateTable { name, columns, .. } => { - assert_eq!(name.to_string(), "foo"); + assert_eq!(name.to_string(), "`foo`"); assert_eq!( vec![ColumnDef { - name: "bar".into(), + name: Ident::with_quote('`', "bar"), data_type: DataType::Int, collation: None, options: vec![ From 344ac3f6296c69fa98e638ac45b768e663a12889 Mon Sep 17 00:00:00 2001 From: mashuai Date: Thu, 30 Jul 2020 08:30:00 +0800 Subject: [PATCH 2/2] use a seperate test to test parse quote identifier --- tests/sqlparser_mysql.rs | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/tests/sqlparser_mysql.rs b/tests/sqlparser_mysql.rs index 20d38baad7..c0fc8c8ba6 100644 --- a/tests/sqlparser_mysql.rs +++ b/tests/sqlparser_mysql.rs @@ -100,13 +100,13 @@ fn parse_show_columns() { #[test] fn parse_create_table_auto_increment() { - let sql = "CREATE TABLE `foo` (`bar` INT PRIMARY KEY AUTO_INCREMENT)"; + let sql = "CREATE TABLE foo (bar INT PRIMARY KEY AUTO_INCREMENT)"; match mysql().verified_stmt(sql) { Statement::CreateTable { name, columns, .. } => { - assert_eq!(name.to_string(), "`foo`"); + assert_eq!(name.to_string(), "foo"); assert_eq!( vec![ColumnDef { - name: Ident::with_quote('`', "bar"), + name: Ident::new("bar"), data_type: DataType::Int, collation: None, options: vec![ @@ -129,6 +129,29 @@ fn parse_create_table_auto_increment() { } } +#[test] +fn parse_quote_identifiers() { + let sql = "CREATE TABLE `PRIMARY` (`BEGIN` INT PRIMARY KEY)"; + match mysql().verified_stmt(sql) { + Statement::CreateTable { name, columns, .. } => { + assert_eq!(name.to_string(), "`PRIMARY`"); + assert_eq!( + vec![ColumnDef { + name: Ident::with_quote('`', "BEGIN"), + data_type: DataType::Int, + collation: None, + options: vec![ColumnOptionDef { + name: None, + option: ColumnOption::Unique { is_primary: true } + }], + }], + columns + ); + } + _ => unreachable!(), + } +} + fn mysql() -> TestedDialects { TestedDialects { dialects: vec![Box::new(MySqlDialect {})],