Skip to content

Commit 1ff2e70

Browse files
ehoeveserprex
authored andcommitted
Add support for MySQL auto_increment offset (apache#950)
1 parent aa9f991 commit 1ff2e70

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

src/ast/helpers/stmt_create_table.rs

+10
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 auto_increment_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+
auto_increment_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 auto_increment_offset(mut self, offset: Option<u32>) -> Self {
210+
self.auto_increment_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+
auto_increment_offset: self.auto_increment_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+
auto_increment_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+
auto_increment_offset,
327337
default_charset,
328338
collation,
329339
on_commit,

src/ast/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,7 @@ pub enum Statement {
14181418
clone: Option<ObjectName>,
14191419
engine: Option<String>,
14201420
comment: Option<String>,
1421+
auto_increment_offset: Option<u32>,
14211422
default_charset: Option<String>,
14221423
collation: Option<String>,
14231424
on_commit: Option<OnCommit>,
@@ -2451,6 +2452,7 @@ impl fmt::Display for Statement {
24512452
default_charset,
24522453
engine,
24532454
comment,
2455+
auto_increment_offset,
24542456
collation,
24552457
on_commit,
24562458
on_cluster,
@@ -2605,6 +2607,9 @@ impl fmt::Display for Statement {
26052607
if let Some(comment) = comment {
26062608
write!(f, " COMMENT '{comment}'")?;
26072609
}
2610+
if let Some(auto_increment_offset) = auto_increment_offset {
2611+
write!(f, " AUTO_INCREMENT {auto_increment_offset}")?;
2612+
}
26082613
if let Some(order_by) = order_by {
26092614
write!(f, " ORDER BY ({})", display_comma_separated(order_by))?;
26102615
}

src/parser/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -3586,6 +3586,17 @@ impl<'a> Parser<'a> {
35863586
None
35873587
};
35883588

3589+
let auto_increment_offset = if self.parse_keyword(Keyword::AUTO_INCREMENT) {
3590+
let _ = self.consume_token(&Token::Eq);
3591+
let next_token = self.next_token();
3592+
match next_token.token {
3593+
Token::Number(s, _) => Some(s.parse::<u32>().expect("literal int")),
3594+
_ => self.expected("literal int", next_token)?,
3595+
}
3596+
} else {
3597+
None
3598+
};
3599+
35893600
let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
35903601
if self.consume_token(&Token::LParen) {
35913602
let columns = if self.peek_token() != Token::RParen {
@@ -3667,6 +3678,7 @@ impl<'a> Parser<'a> {
36673678
.clone_clause(clone)
36683679
.engine(engine)
36693680
.comment(comment)
3681+
.auto_increment_offset(auto_increment_offset)
36703682
.order_by(order_by)
36713683
.default_charset(default_charset)
36723684
.collation(collation)

tests/sqlparser_mysql.rs

+25
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,31 @@ fn parse_create_table_comment() {
312312
}
313313
}
314314

315+
#[test]
316+
fn parse_create_table_auto_increment_offset() {
317+
let canonical =
318+
"CREATE TABLE foo (bar INT NOT NULL AUTO_INCREMENT) ENGINE=InnoDB AUTO_INCREMENT 123";
319+
let with_equal =
320+
"CREATE TABLE foo (bar INT NOT NULL AUTO_INCREMENT) ENGINE=InnoDB AUTO_INCREMENT=123";
321+
322+
for sql in [canonical, with_equal] {
323+
match mysql().one_statement_parses_to(sql, canonical) {
324+
Statement::CreateTable {
325+
name,
326+
auto_increment_offset,
327+
..
328+
} => {
329+
assert_eq!(name.to_string(), "foo");
330+
assert_eq!(
331+
auto_increment_offset.expect("Should exist").to_string(),
332+
"123"
333+
);
334+
}
335+
_ => unreachable!(),
336+
}
337+
}
338+
}
339+
315340
#[test]
316341
fn parse_create_table_set_enum() {
317342
let sql = "CREATE TABLE foo (bar SET('a', 'b'), baz ENUM('a', 'b'))";

0 commit comments

Comments
 (0)