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 @@ -223,4 +223,8 @@ impl Dialect for GenericDialect {
fn supports_lambda_functions(&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 @@ -9246,7 +9246,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))
{
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 @@ -3438,6 +3438,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))";
let normalized = "CREATE TABLE t (x INT, CHECK (x > 1))";
mysql_and_generic().one_statement_parses_to(sql, normalized);
}

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