Skip to content

Commit 3e99046

Browse files
authored
Support CREATE TABLE ON UPDATE <expr> Function (#685)
* feat : OnUpdate Function Implement * feat : add GenericDialect Options
1 parent b1a000f commit 3e99046

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

src/ast/ddl.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ pub enum ColumnOption {
558558
DialectSpecific(Vec<Token>),
559559
CharacterSet(ObjectName),
560560
Comment(String),
561+
OnUpdate(Expr),
561562
}
562563

563564
impl fmt::Display for ColumnOption {
@@ -592,6 +593,7 @@ impl fmt::Display for ColumnOption {
592593
DialectSpecific(val) => write!(f, "{}", display_separated(val, " ")),
593594
CharacterSet(n) => write!(f, "CHARACTER SET {}", n),
594595
Comment(v) => write!(f, "COMMENT '{}'", escape_single_quote_string(v)),
596+
OnUpdate(expr) => write!(f, "ON UPDATE {}", expr),
595597
}
596598
}
597599
}

src/parser.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3474,11 +3474,10 @@ impl<'a> Parser<'a> {
34743474
Token::make_keyword("AUTOINCREMENT"),
34753475
])))
34763476
} else if self.parse_keywords(&[Keyword::ON, Keyword::UPDATE])
3477-
&& dialect_of!(self is MySqlDialect)
3477+
&& dialect_of!(self is MySqlDialect | GenericDialect)
34783478
{
3479-
Ok(Some(ColumnOption::DialectSpecific(vec![
3480-
Token::make_keyword("ON UPDATE"),
3481-
])))
3479+
let expr = self.parse_expr()?;
3480+
Ok(Some(ColumnOption::OnUpdate(expr)))
34823481
} else {
34833482
Ok(None)
34843483
}

tests/sqlparser_mysql.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ fn parse_kill() {
10311031

10321032
#[test]
10331033
fn parse_table_colum_option_on_update() {
1034-
let sql1 = "CREATE TABLE foo (`modification_time` DATETIME ON UPDATE)";
1034+
let sql1 = "CREATE TABLE foo (`modification_time` DATETIME ON UPDATE CURRENT_TIMESTAMP())";
10351035
match mysql().verified_stmt(sql1) {
10361036
Statement::CreateTable { name, columns, .. } => {
10371037
assert_eq!(name.to_string(), "foo");
@@ -1042,9 +1042,13 @@ fn parse_table_colum_option_on_update() {
10421042
collation: None,
10431043
options: vec![ColumnOptionDef {
10441044
name: None,
1045-
option: ColumnOption::DialectSpecific(vec![Token::make_keyword(
1046-
"ON UPDATE"
1047-
)]),
1045+
option: ColumnOption::OnUpdate(Expr::Function(Function {
1046+
name: ObjectName(vec![Ident::new("CURRENT_TIMESTAMP")]),
1047+
args: vec![],
1048+
over: None,
1049+
distinct: false,
1050+
special: false,
1051+
})),
10481052
},],
10491053
}],
10501054
columns

0 commit comments

Comments
 (0)