-
Notifications
You must be signed in to change notification settings - Fork 628
fix: mysql ON UPDATE not taking an expression #586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -383,6 +383,7 @@ define_keywords!( | |
NOSUPERUSER, | ||
NOT, | ||
NOTHING, | ||
NOW, | ||
NOWAIT, | ||
NTH_VALUE, | ||
NTILE, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -497,9 +497,8 @@ impl<'a> Parser<'a> { | |
| Keyword::CURRENT_TIME | ||
| Keyword::CURRENT_DATE | ||
| Keyword::LOCALTIME | ||
| Keyword::LOCALTIMESTAMP => { | ||
self.parse_time_functions(ObjectName(vec![w.to_ident()])) | ||
} | ||
| Keyword::LOCALTIMESTAMP | ||
| Keyword::NOW => self.parse_time_functions(ObjectName(vec![w.to_ident()])), | ||
Keyword::CASE => self.parse_case_expr(), | ||
Keyword::CAST => self.parse_cast_expr(), | ||
Keyword::TRY_CAST => self.parse_try_cast_expr(), | ||
|
@@ -3254,9 +3253,9 @@ impl<'a> Parser<'a> { | |
} else if self.parse_keywords(&[Keyword::ON, Keyword::UPDATE]) | ||
&& dialect_of!(self is MySqlDialect) | ||
{ | ||
Ok(Some(ColumnOption::DialectSpecific(vec![ | ||
Token::make_keyword("ON UPDATE"), | ||
]))) | ||
let expr = self.parse_expr()?; | ||
Self::check_on_update_expr_is_valid(&expr)?; | ||
Ok(Some(ColumnOption::OnUpdate(expr))) | ||
} else { | ||
Ok(None) | ||
} | ||
|
@@ -6318,6 +6317,31 @@ impl<'a> Parser<'a> { | |
pub fn index(&self) -> usize { | ||
self.index | ||
} | ||
|
||
fn check_on_update_expr_is_valid(expr: &Expr) -> Result<(), ParserError> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my preference is to leave this type of "semantic" check downstream -- so I would prefer that sqlparser accepts any expression in this place, and then downstream crates can enforce limits like the expr must be one of the specified time functions. @AugustoFKL and I tried to explain this difference in the readme recently https://github.com/sqlparser-rs/sqlparser-rs#extensible-sql-lexer-and-parser-for-rust
|
||
const VALID_FN_NAMES: [&str; 4] = [ | ||
keywords::CURRENT_TIMESTAMP, | ||
keywords::LOCALTIME, | ||
keywords::LOCALTIMESTAMP, | ||
keywords::NOW, | ||
]; | ||
|
||
if let Expr::Function(f) = expr { | ||
if let Some(fn_name_ident) = f.name.0.first() { | ||
if VALID_FN_NAMES | ||
.iter() | ||
.any(|x| x.eq_ignore_ascii_case(&fn_name_ident.value.as_str())) | ||
{ | ||
return Ok(()); | ||
} | ||
} | ||
} | ||
|
||
Err(ParserError::ParserError(format!( | ||
"Expected one of '{}' after ON UPDATE in column definition", | ||
VALID_FN_NAMES.map(|x| x.to_string()).join("', '") | ||
))) | ||
} | ||
} | ||
|
||
impl Word { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recommend just accepting
expr
here