-
Notifications
You must be signed in to change notification settings - Fork 670
Require space after -- to start single line comment in MySQL #1705
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 2 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 | ||||
---|---|---|---|---|---|---|
|
@@ -880,6 +880,15 @@ pub trait Dialect: Debug + Any { | |||||
fn supports_table_hints(&self) -> bool { | ||||||
false | ||||||
} | ||||||
|
||||||
/// Returns whether it's the start of a single line comment | ||||||
/// e.g. MySQL requires a space after `--` to be a single line comment | ||||||
/// Otherwise it's interpreted as a double minus operator | ||||||
/// | ||||||
/// MySQL: <https://dev.mysql.com/doc/refman/8.4/en/ansi-diff-comments.html> | ||||||
fn requires_whitespace_to_start_comment(&self) -> bool { | ||||||
|
fn requires_whitespace_to_start_comment(&self) -> bool { | |
fn requires_single_line_comment_whitespace(&self) -> bool { |
thinking something like this to flag that this is only for the --
comment style
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,6 +125,15 @@ impl Dialect for MySqlDialect { | |
fn supports_table_hints(&self) -> bool { | ||
true | ||
} | ||
|
||
/// Returns whether it's the start of a single line comment | ||
/// e.g. MySQL requires a space after `--` to be a single line comment | ||
/// Otherwise it's interpreted as a double minus operator | ||
/// | ||
|
||
/// MySQL: <https://dev.mysql.com/doc/refman/8.4/en/ansi-diff-comments.html> | ||
fn requires_whitespace_to_start_comment(&self) -> bool { | ||
true | ||
} | ||
} | ||
|
||
/// `LOCK TABLES` | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1229,15 +1229,31 @@ impl<'a> Tokenizer<'a> { | |||||||||||
// operators | ||||||||||||
'-' => { | ||||||||||||
chars.next(); // consume the '-' | ||||||||||||
match chars.peek() { | ||||||||||||
Some('-') => { | ||||||||||||
chars.next(); // consume the second '-', starting a single-line comment | ||||||||||||
|
||||||||||||
if let Some('-') = chars.peek() { | ||||||||||||
|
||||||||||||
let mut is_comment = true; | ||||||||||||
if self.dialect.requires_whitespace_to_start_comment() { | ||||||||||||
// MySQL requires a space after the -- for a single-line comment | ||||||||||||
// Otherwise it's interpreted as two minus signs | ||||||||||||
// e.g. UPDATE account SET balance=balance--1 | ||||||||||||
// WHERE account_id=5752; | ||||||||||||
|
||||||||||||
match chars.peekable.clone().nth(1) { | ||||||||||||
Some(' ') => (), | ||||||||||||
_ => is_comment = false, | ||||||||||||
} | ||||||||||||
|
match chars.peekable.clone().nth(1) { | |
Some(' ') => (), | |
_ => is_comment = false, | |
} | |
is_comment = Some(' ') == chars.peekable.clone().nth(1); |
looks like this can be simplified?
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3244,3 +3244,62 @@ fn parse_double_precision() { | |
"CREATE TABLE foo (bar DOUBLE(11,0))", | ||
); | ||
} | ||
|
||
#[test] | ||
fn parse_looks_like_single_line_comment() { | ||
// See https://dev.mysql.com/doc/refman/8.4/en/ansi-diff-comments.html | ||
match mysql().parse_sql_statements( | ||
r#" | ||
UPDATE account SET balance=balance--1 | ||
WHERE account_id=5752 | ||
"#, | ||
|
||
) { | ||
Ok(statement) => match statement.first() { | ||
Some(Statement::Update { assignments, .. }) => { | ||
assert_eq!(assignments.len(), 1); | ||
assert_eq!( | ||
assignments[0], | ||
Assignment { | ||
value: Expr::BinaryOp { | ||
left: Box::new(Expr::Identifier(Ident::new("balance"))), | ||
op: BinaryOperator::Minus, | ||
right: Box::new(Expr::UnaryOp { | ||
op: UnaryOperator::Minus, | ||
expr: Box::new(Expr::Value(number("1"))) | ||
}), | ||
}, | ||
target: AssignmentTarget::ColumnName(ObjectName::from(vec![Ident::new( | ||
"balance".to_string() | ||
)])), | ||
} | ||
); | ||
} | ||
_ => panic!("expected update statement"), | ||
}, | ||
_ => panic!("expected error"), | ||
} | ||
|
||
match mysql().parse_sql_statements( | ||
r#" | ||
UPDATE account SET balance=balance-- 1 | ||
WHERE account_id=5752 | ||
"#, | ||
) { | ||
Ok(statement) => match statement.first() { | ||
Some(Statement::Update { assignments, .. }) => { | ||
assert_eq!(assignments.len(), 1); | ||
assert_eq!( | ||
assignments[0], | ||
Assignment { | ||
value: Expr::Identifier(Ident::new("balance")), | ||
target: AssignmentTarget::ColumnName(ObjectName::from(vec![Ident::new( | ||
"balance".to_string() | ||
)])), | ||
} | ||
); | ||
} | ||
_ => panic!("expected update statement"), | ||
}, | ||
_ => panic!("expected error"), | ||
} | ||
} |
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.