From 51c1b215a33e909dbb7409105ccca844358a5251 Mon Sep 17 00:00:00 2001 From: ehoeve Date: Thu, 17 Aug 2023 22:24:44 +0200 Subject: [PATCH 1/3] Added support for mysql auto_increment offset, should also fix issue #920 --- src/ast/helpers/stmt_create_table.rs | 10 ++++++++++ src/ast/mod.rs | 5 +++++ src/parser/mod.rs | 12 ++++++++++++ tests/sqlparser_mysql.rs | 16 ++++++++++++++++ 4 files changed, 43 insertions(+) diff --git a/src/ast/helpers/stmt_create_table.rs b/src/ast/helpers/stmt_create_table.rs index a00555fd8..2a47e516a 100644 --- a/src/ast/helpers/stmt_create_table.rs +++ b/src/ast/helpers/stmt_create_table.rs @@ -66,6 +66,7 @@ pub struct CreateTableBuilder { pub clone: Option, pub engine: Option, pub comment: Option, + pub autoincrement_offset: Option, pub default_charset: Option, pub collation: Option, pub on_commit: Option, @@ -98,6 +99,7 @@ impl CreateTableBuilder { clone: None, engine: None, comment: None, + autoincrement_offset: None, default_charset: None, collation: None, on_commit: None, @@ -204,6 +206,11 @@ impl CreateTableBuilder { self } + pub fn autoincrement_offset(mut self, offset: Option) -> Self { + self.autoincrement_offset = offset; + self + } + pub fn default_charset(mut self, default_charset: Option) -> Self { self.default_charset = default_charset; self @@ -257,6 +264,7 @@ impl CreateTableBuilder { clone: self.clone, engine: self.engine, comment: self.comment, + autoincrement_offset: self.autoincrement_offset, default_charset: self.default_charset, collation: self.collation, on_commit: self.on_commit, @@ -296,6 +304,7 @@ impl TryFrom for CreateTableBuilder { clone, engine, comment, + autoincrement_offset, default_charset, collation, on_commit, @@ -324,6 +333,7 @@ impl TryFrom for CreateTableBuilder { clone, engine, comment, + autoincrement_offset, default_charset, collation, on_commit, diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 47419a893..4d92f3050 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -1322,6 +1322,7 @@ pub enum Statement { clone: Option, engine: Option, comment: Option, + autoincrement_offset: Option, default_charset: Option, collation: Option, on_commit: Option, @@ -2263,6 +2264,7 @@ impl fmt::Display for Statement { default_charset, engine, comment, + autoincrement_offset, collation, on_commit, on_cluster, @@ -2417,6 +2419,9 @@ impl fmt::Display for Statement { if let Some(comment) = comment { write!(f, " COMMENT '{comment}'")?; } + if let Some(autoincrement_offset) = autoincrement_offset { + write!(f, " AUTO_INCREMENT {autoincrement_offset}")?; + } if let Some(order_by) = order_by { write!(f, " ORDER BY ({})", display_comma_separated(order_by))?; } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 94814627d..38926755e 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -3550,6 +3550,17 @@ impl<'a> Parser<'a> { None }; + let autoincrement_offset = if self.parse_keyword(Keyword::AUTO_INCREMENT) { + let _ = self.consume_token(&Token::Eq); + let next_token = self.next_token(); + match next_token.token { + Token::Number(s, _) => Some(s.parse::().expect("literal int")), + _ => self.expected("literal int", next_token)?, + } + } else { + None + }; + let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) { if self.consume_token(&Token::LParen) { let columns = if self.peek_token() != Token::RParen { @@ -3631,6 +3642,7 @@ impl<'a> Parser<'a> { .clone_clause(clone) .engine(engine) .comment(comment) + .autoincrement_offset(autoincrement_offset) .order_by(order_by) .default_charset(default_charset) .collation(collation) diff --git a/tests/sqlparser_mysql.rs b/tests/sqlparser_mysql.rs index c0a51edab..22ca7baa4 100644 --- a/tests/sqlparser_mysql.rs +++ b/tests/sqlparser_mysql.rs @@ -312,6 +312,22 @@ fn parse_create_table_comment() { } } +#[test] +fn parse_create_table_autoincrement_offset() { + let canonical = "CREATE TABLE foo (bar INT NOT NULL AUTO_INCREMENT) ENGINE=InnoDB AUTO_INCREMENT 123"; + let with_equal = "CREATE TABLE foo (bar INT NOT NULL AUTO_INCREMENT) ENGINE=InnoDB AUTO_INCREMENT=123"; + + for sql in [canonical, with_equal] { + match mysql().one_statement_parses_to(sql, canonical) { + Statement::CreateTable { name, autoincrement_offset, .. } => { + assert_eq!(name.to_string(), "foo"); + assert_eq!(autoincrement_offset.expect("Should exist").to_string(), "123"); + } + _ => unreachable!(), + } + } +} + #[test] fn parse_create_table_set_enum() { let sql = "CREATE TABLE foo (bar SET('a', 'b'), baz ENUM('a', 'b'))"; From 9368e98d08f783cc3ae6baf3737e842a36195d6d Mon Sep 17 00:00:00 2001 From: ehoeve Date: Thu, 17 Aug 2023 22:34:28 +0200 Subject: [PATCH 2/3] cargo fmt --- tests/sqlparser_mysql.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/tests/sqlparser_mysql.rs b/tests/sqlparser_mysql.rs index 22ca7baa4..fac88e904 100644 --- a/tests/sqlparser_mysql.rs +++ b/tests/sqlparser_mysql.rs @@ -314,14 +314,23 @@ fn parse_create_table_comment() { #[test] fn parse_create_table_autoincrement_offset() { - let canonical = "CREATE TABLE foo (bar INT NOT NULL AUTO_INCREMENT) ENGINE=InnoDB AUTO_INCREMENT 123"; - let with_equal = "CREATE TABLE foo (bar INT NOT NULL AUTO_INCREMENT) ENGINE=InnoDB AUTO_INCREMENT=123"; + let canonical = + "CREATE TABLE foo (bar INT NOT NULL AUTO_INCREMENT) ENGINE=InnoDB AUTO_INCREMENT 123"; + let with_equal = + "CREATE TABLE foo (bar INT NOT NULL AUTO_INCREMENT) ENGINE=InnoDB AUTO_INCREMENT=123"; for sql in [canonical, with_equal] { match mysql().one_statement_parses_to(sql, canonical) { - Statement::CreateTable { name, autoincrement_offset, .. } => { + Statement::CreateTable { + name, + autoincrement_offset, + .. + } => { assert_eq!(name.to_string(), "foo"); - assert_eq!(autoincrement_offset.expect("Should exist").to_string(), "123"); + assert_eq!( + autoincrement_offset.expect("Should exist").to_string(), + "123" + ); } _ => unreachable!(), } From 75cea6b90e483c0f2214e628ae2e199c644f33c9 Mon Sep 17 00:00:00 2001 From: ehoeve Date: Mon, 21 Aug 2023 21:04:34 +0200 Subject: [PATCH 3/3] Rename autoincrement to auto_increment to more closely follow convention --- src/ast/helpers/stmt_create_table.rs | 14 +++++++------- src/ast/mod.rs | 8 ++++---- src/parser/mod.rs | 4 ++-- tests/sqlparser_mysql.rs | 6 +++--- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/ast/helpers/stmt_create_table.rs b/src/ast/helpers/stmt_create_table.rs index 2a47e516a..17327e7f8 100644 --- a/src/ast/helpers/stmt_create_table.rs +++ b/src/ast/helpers/stmt_create_table.rs @@ -66,7 +66,7 @@ pub struct CreateTableBuilder { pub clone: Option, pub engine: Option, pub comment: Option, - pub autoincrement_offset: Option, + pub auto_increment_offset: Option, pub default_charset: Option, pub collation: Option, pub on_commit: Option, @@ -99,7 +99,7 @@ impl CreateTableBuilder { clone: None, engine: None, comment: None, - autoincrement_offset: None, + auto_increment_offset: None, default_charset: None, collation: None, on_commit: None, @@ -206,8 +206,8 @@ impl CreateTableBuilder { self } - pub fn autoincrement_offset(mut self, offset: Option) -> Self { - self.autoincrement_offset = offset; + pub fn auto_increment_offset(mut self, offset: Option) -> Self { + self.auto_increment_offset = offset; self } @@ -264,7 +264,7 @@ impl CreateTableBuilder { clone: self.clone, engine: self.engine, comment: self.comment, - autoincrement_offset: self.autoincrement_offset, + auto_increment_offset: self.auto_increment_offset, default_charset: self.default_charset, collation: self.collation, on_commit: self.on_commit, @@ -304,7 +304,7 @@ impl TryFrom for CreateTableBuilder { clone, engine, comment, - autoincrement_offset, + auto_increment_offset, default_charset, collation, on_commit, @@ -333,7 +333,7 @@ impl TryFrom for CreateTableBuilder { clone, engine, comment, - autoincrement_offset, + auto_increment_offset, default_charset, collation, on_commit, diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 4d92f3050..50c11ba59 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -1322,7 +1322,7 @@ pub enum Statement { clone: Option, engine: Option, comment: Option, - autoincrement_offset: Option, + auto_increment_offset: Option, default_charset: Option, collation: Option, on_commit: Option, @@ -2264,7 +2264,7 @@ impl fmt::Display for Statement { default_charset, engine, comment, - autoincrement_offset, + auto_increment_offset, collation, on_commit, on_cluster, @@ -2419,8 +2419,8 @@ impl fmt::Display for Statement { if let Some(comment) = comment { write!(f, " COMMENT '{comment}'")?; } - if let Some(autoincrement_offset) = autoincrement_offset { - write!(f, " AUTO_INCREMENT {autoincrement_offset}")?; + if let Some(auto_increment_offset) = auto_increment_offset { + write!(f, " AUTO_INCREMENT {auto_increment_offset}")?; } if let Some(order_by) = order_by { write!(f, " ORDER BY ({})", display_comma_separated(order_by))?; diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 38926755e..1b11237ff 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -3550,7 +3550,7 @@ impl<'a> Parser<'a> { None }; - let autoincrement_offset = if self.parse_keyword(Keyword::AUTO_INCREMENT) { + let auto_increment_offset = if self.parse_keyword(Keyword::AUTO_INCREMENT) { let _ = self.consume_token(&Token::Eq); let next_token = self.next_token(); match next_token.token { @@ -3642,7 +3642,7 @@ impl<'a> Parser<'a> { .clone_clause(clone) .engine(engine) .comment(comment) - .autoincrement_offset(autoincrement_offset) + .auto_increment_offset(auto_increment_offset) .order_by(order_by) .default_charset(default_charset) .collation(collation) diff --git a/tests/sqlparser_mysql.rs b/tests/sqlparser_mysql.rs index fac88e904..209143b18 100644 --- a/tests/sqlparser_mysql.rs +++ b/tests/sqlparser_mysql.rs @@ -313,7 +313,7 @@ fn parse_create_table_comment() { } #[test] -fn parse_create_table_autoincrement_offset() { +fn parse_create_table_auto_increment_offset() { let canonical = "CREATE TABLE foo (bar INT NOT NULL AUTO_INCREMENT) ENGINE=InnoDB AUTO_INCREMENT 123"; let with_equal = @@ -323,12 +323,12 @@ fn parse_create_table_autoincrement_offset() { match mysql().one_statement_parses_to(sql, canonical) { Statement::CreateTable { name, - autoincrement_offset, + auto_increment_offset, .. } => { assert_eq!(name.to_string(), "foo"); assert_eq!( - autoincrement_offset.expect("Should exist").to_string(), + auto_increment_offset.expect("Should exist").to_string(), "123" ); }