Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fuzz/fuzz_seeds/mysql_index_option_comment_escape.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE t (a INT, INDEX idx (a) COMMENT 'a''b');
2 changes: 1 addition & 1 deletion src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1684,7 +1684,7 @@ impl fmt::Display for IndexOption {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Using(index_type) => write!(f, "USING {index_type}"),
Self::Comment(s) => write!(f, "COMMENT '{s}'"),
Self::Comment(s) => write!(f, "COMMENT '{}'", escape_single_quote_string(s)),
}
}
}
Expand Down
42 changes: 42 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5254,3 +5254,45 @@ fn parse_key_constraint_index_type_before_columns() {
);
}
}

#[test]
fn test_index_option_comment_escape() {
match mysql_and_generic()
.verified_stmt("CREATE TABLE t (a INT, INDEX idx (a) COMMENT 'foo''bar')")
{
Statement::CreateTable(CreateTable { constraints, .. }) => match &constraints[..] {
[TableConstraint::Index(c)] => {
assert_eq!(
c.index_options,
vec![IndexOption::Comment("foo'bar".to_string())]
);
}
_ => unreachable!(),
},
_ => unreachable!(),
}

match mysql_and_generic()
.verified_stmt("CREATE TABLE t (a INT, UNIQUE KEY uk (a) COMMENT 'it''s an ''index''')")
{
Statement::CreateTable(CreateTable { constraints, .. }) => match &constraints[..] {
[TableConstraint::Unique(c)] => {
assert_eq!(
c.index_options,
vec![IndexOption::Comment("it's an 'index'".to_string())]
);
}
_ => unreachable!(),
},
_ => unreachable!(),
}

assert_eq!(
mysql_and_generic()
.parse_sql_statements("CREATE TABLE t (a INT, INDEX idx (a) COMMENT 'unterminated)")
.unwrap_err(),
ParserError::TokenizerError(
"Unterminated string literal at Line: 1, Column: 46".to_string()
)
);
}
Loading