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
4 changes: 4 additions & 0 deletions src/dialect/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,8 @@ impl Dialect for GenericDialect {
fn supports_select_format(&self) -> bool {
true
}

fn supports_constraint_keyword_without_name(&self) -> bool {
true
}
}
17 changes: 17 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,23 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if the dialect supports the `CONSTRAINT` keyword without a name
/// in table constraint definitions.
///
/// Example:
/// ```sql
/// CREATE TABLE t (a INT, CONSTRAINT CHECK (a > 0))
/// ```
///
/// This is a MySQL extension; the SQL standard requires a name after `CONSTRAINT`.
/// When the name is omitted, the output normalizes to just the constraint type
/// without the `CONSTRAINT` keyword (e.g., `CHECK (a > 0)`).
///
/// <https://dev.mysql.com/doc/refman/8.4/en/create-table.html>
fn supports_constraint_keyword_without_name(&self) -> bool {
false
}

/// Returns true if the specified keyword is reserved and cannot be
/// used as an identifier without special handling like quoting.
fn is_reserved_for_identifier(&self, kw: Keyword) -> bool {
Expand Down
5 changes: 5 additions & 0 deletions src/dialect/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ impl Dialect for MySqlDialect {
fn supports_binary_kw_as_cast(&self) -> bool {
true
}

/// See: <https://dev.mysql.com/doc/refman/8.4/en/create-table.html>
fn supports_constraint_keyword_without_name(&self) -> bool {
true
}
}

/// `LOCK TABLES`
Expand Down
11 changes: 10 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9252,7 +9252,16 @@ impl<'a> Parser<'a> {
&mut self,
) -> Result<Option<TableConstraint>, ParserError> {
let name = if self.parse_keyword(Keyword::CONSTRAINT) {
Some(self.parse_identifier()?)
if self.dialect.supports_constraint_keyword_without_name()
&& (self.peek_keyword(Keyword::CHECK)
|| self.peek_keyword(Keyword::PRIMARY)
|| self.peek_keyword(Keyword::UNIQUE)
|| self.peek_keyword(Keyword::FOREIGN))
Comment on lines +9256 to +9259
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use self.peek_one_of_keywords() here?

{
None
} else {
Some(self.parse_identifier()?)
}
} else {
None
};
Expand Down
7 changes: 7 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3457,6 +3457,13 @@ fn parse_create_table_unallow_constraint_then_index() {
assert!(mysql_and_generic().parse_sql_statements(sql).is_ok());
}

#[test]
fn parse_create_table_constraint_check_without_name() {
let sql = "CREATE TABLE t (x INT, CONSTRAINT CHECK (x > 1))";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also add cases for primary/unique/foreign ?

let normalized = "CREATE TABLE t (x INT, CHECK (x > 1))";
mysql_and_generic().one_statement_parses_to(sql, normalized);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use all_dialects_where() to select the dialects?

}

#[test]
fn parse_create_table_with_fulltext_definition() {
mysql_and_generic().verified_stmt("CREATE TABLE tb (id INT, FULLTEXT (id))");
Expand Down
Loading