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
13 changes: 11 additions & 2 deletions src/Parsers/CreateDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
* 1 --------------------[ CONSTRAINT ]------------------> 1
* 1 -----------------------[ key ]----------------------> 2
* 1 -------------[ constraint / column name ]-----------> 2
* 1 ----------------------[ CHECK ]---------------------> 4
*
* 2 --------------------[ data type ]-------------------> 3
*
Expand Down Expand Up @@ -165,6 +166,14 @@ public static function parse(Parser $parser, TokensList $list, array $options =
} elseif (($token->type === TokenType::Keyword) && ($token->flags & Token::FLAG_KEYWORD_KEY)) {
$expr->key = Keys::parse($parser, $list);
$state = 4;
} elseif ($token->type === TokenType::Keyword && $token->keyword === 'CHECK') {
// Table-level CHECK constraint, optionally named:
// [CONSTRAINT [symbol]] CHECK (expr) [[NOT] ENFORCED]
// It has neither a data type nor a key, so it is
// parsed with the field options, the same way a
// column-level CHECK is parsed in state 3.
$expr->options = OptionsArrays::parse($parser, $list, self::FIELD_OPTIONS);
$state = 4;
} elseif ($token->type === TokenType::Symbol || $token->type === TokenType::None) {
$expr->name = $token->value;
if (! $expr->isConstraint) {
Expand Down Expand Up @@ -208,7 +217,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =

$state = 5;
} else {
if (! empty($expr->type) || ! empty($expr->key)) {
if (! empty($expr->type) || ! empty($expr->key) || ! empty($expr->options)) {
$ret[] = $expr;
}

Expand All @@ -228,7 +237,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
}

// Last iteration was not saved.
if (! empty($expr->type) || ! empty($expr->key)) {
if (! empty($expr->type) || ! empty($expr->key) || ! empty($expr->options)) {
$ret[] = $expr;
}

Expand Down
1 change: 1 addition & 0 deletions tests/Parser/CreateStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public static function createProvider(): array
['parser/parseCreateTableTimestampWithPrecision'],
['parser/parseCreateTableEnforcedCheck'],
['parser/parseCreateTableNotEnforcedCheck'],
['parser/parseCreateTableWithCheckConstraint'],
['parser/parseCreateTableWithInvisibleKey'],
['parser/parseCreateTableWithParser'],
['parser/parseCreateTableUuidColumn_mariadb_100700'],
Expand Down
11 changes: 11 additions & 0 deletions tests/data/parser/parseCreateTableWithCheckConstraint.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE table_name (
PrimaryKeyId INT UNSIGNED NOT NULL,
NextSequence INT UNSIGNED NOT NULL DEFAULT 1,
CreatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UpdatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (PrimaryKeyId),
CONSTRAINT chk_next_sequence CHECK (NextSequence >= 1),
CONSTRAINT fk_name FOREIGN KEY (PrimaryKeyId) REFERENCES foreign_table_name (PrimaryKeyId)
ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE=InnoDB;
Loading