Skip to content

Commit 51c1b21

Browse files
committed
Added support for mysql auto_increment offset, should also fix issue apache#920
1 parent 9a39afb commit 51c1b21

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

src/ast/helpers/stmt_create_table.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ pub struct CreateTableBuilder {
6666
pub clone: Option<ObjectName>,
6767
pub engine: Option<String>,
6868
pub comment: Option<String>,
69+
pub autoincrement_offset: Option<u32>,
6970
pub default_charset: Option<String>,
7071
pub collation: Option<String>,
7172
pub on_commit: Option<OnCommit>,
@@ -98,6 +99,7 @@ impl CreateTableBuilder {
9899
clone: None,
99100
engine: None,
100101
comment: None,
102+
autoincrement_offset: None,
101103
default_charset: None,
102104
collation: None,
103105
on_commit: None,
@@ -204,6 +206,11 @@ impl CreateTableBuilder {
204206
self
205207
}
206208

209+
pub fn autoincrement_offset(mut self, offset: Option<u32>) -> Self {
210+
self.autoincrement_offset = offset;
211+
self
212+
}
213+
207214
pub fn default_charset(mut self, default_charset: Option<String>) -> Self {
208215
self.default_charset = default_charset;
209216
self
@@ -257,6 +264,7 @@ impl CreateTableBuilder {
257264
clone: self.clone,
258265
engine: self.engine,
259266
comment: self.comment,
267+
autoincrement_offset: self.autoincrement_offset,
260268
default_charset: self.default_charset,
261269
collation: self.collation,
262270
on_commit: self.on_commit,
@@ -296,6 +304,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
296304
clone,
297305
engine,
298306
comment,
307+
autoincrement_offset,
299308
default_charset,
300309
collation,
301310
on_commit,
@@ -324,6 +333,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
324333
clone,
325334
engine,
326335
comment,
336+
autoincrement_offset,
327337
default_charset,
328338
collation,
329339
on_commit,

src/ast/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,7 @@ pub enum Statement {
13221322
clone: Option<ObjectName>,
13231323
engine: Option<String>,
13241324
comment: Option<String>,
1325+
autoincrement_offset: Option<u32>,
13251326
default_charset: Option<String>,
13261327
collation: Option<String>,
13271328
on_commit: Option<OnCommit>,
@@ -2263,6 +2264,7 @@ impl fmt::Display for Statement {
22632264
default_charset,
22642265
engine,
22652266
comment,
2267+
autoincrement_offset,
22662268
collation,
22672269
on_commit,
22682270
on_cluster,
@@ -2417,6 +2419,9 @@ impl fmt::Display for Statement {
24172419
if let Some(comment) = comment {
24182420
write!(f, " COMMENT '{comment}'")?;
24192421
}
2422+
if let Some(autoincrement_offset) = autoincrement_offset {
2423+
write!(f, " AUTO_INCREMENT {autoincrement_offset}")?;
2424+
}
24202425
if let Some(order_by) = order_by {
24212426
write!(f, " ORDER BY ({})", display_comma_separated(order_by))?;
24222427
}

src/parser/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3550,6 +3550,17 @@ impl<'a> Parser<'a> {
35503550
None
35513551
};
35523552

3553+
let autoincrement_offset = if self.parse_keyword(Keyword::AUTO_INCREMENT) {
3554+
let _ = self.consume_token(&Token::Eq);
3555+
let next_token = self.next_token();
3556+
match next_token.token {
3557+
Token::Number(s, _) => Some(s.parse::<u32>().expect("literal int")),
3558+
_ => self.expected("literal int", next_token)?,
3559+
}
3560+
} else {
3561+
None
3562+
};
3563+
35533564
let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
35543565
if self.consume_token(&Token::LParen) {
35553566
let columns = if self.peek_token() != Token::RParen {
@@ -3631,6 +3642,7 @@ impl<'a> Parser<'a> {
36313642
.clone_clause(clone)
36323643
.engine(engine)
36333644
.comment(comment)
3645+
.autoincrement_offset(autoincrement_offset)
36343646
.order_by(order_by)
36353647
.default_charset(default_charset)
36363648
.collation(collation)

tests/sqlparser_mysql.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,22 @@ fn parse_create_table_comment() {
312312
}
313313
}
314314

315+
#[test]
316+
fn parse_create_table_autoincrement_offset() {
317+
let canonical = "CREATE TABLE foo (bar INT NOT NULL AUTO_INCREMENT) ENGINE=InnoDB AUTO_INCREMENT 123";
318+
let with_equal = "CREATE TABLE foo (bar INT NOT NULL AUTO_INCREMENT) ENGINE=InnoDB AUTO_INCREMENT=123";
319+
320+
for sql in [canonical, with_equal] {
321+
match mysql().one_statement_parses_to(sql, canonical) {
322+
Statement::CreateTable { name, autoincrement_offset, .. } => {
323+
assert_eq!(name.to_string(), "foo");
324+
assert_eq!(autoincrement_offset.expect("Should exist").to_string(), "123");
325+
}
326+
_ => unreachable!(),
327+
}
328+
}
329+
}
330+
315331
#[test]
316332
fn parse_create_table_set_enum() {
317333
let sql = "CREATE TABLE foo (bar SET('a', 'b'), baz ENUM('a', 'b'))";

0 commit comments

Comments
 (0)