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: 2 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2147,12 +2147,12 @@ parameters:

-
message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertEmpty\\(\\)\\.$#"
count: 2
count: 3
path: tests/Builder/CreateStatementTest.php

-
message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertEquals\\(\\)\\.$#"
count: 35
count: 36
path: tests/Builder/CreateStatementTest.php

-
Expand Down
9 changes: 7 additions & 2 deletions src/Components/CreateDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
* 0 -----------------------[ ( ]------------------------> 1
*
* 1 --------------------[ CONSTRAINT ]------------------> 1
* 1 ----------------------[ CHECK ]---------------------> 4
* 1 -----------------------[ key ]----------------------> 2
* 1 -------------[ constraint / column name ]-----------> 2
*
Expand Down Expand Up @@ -244,6 +245,10 @@ public static function parse(Parser $parser, TokensList $list, array $options =
} elseif ($state === 1) {
if ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'CONSTRAINT') {
$expr->isConstraint = true;
} elseif ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'CHECK') {
// Table check constraint: [CONSTRAINT [symbol]] CHECK (expr)
$expr->options = OptionsArray::parse($parser, $list, static::$FIELD_OPTIONS);
$state = 4;
} elseif (($token->type === Token::TYPE_KEYWORD) && ($token->flags & Token::FLAG_KEYWORD_KEY)) {
$expr->key = Key::parse($parser, $list);
$state = 4;
Expand Down Expand Up @@ -290,7 +295,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =

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

Expand All @@ -310,7 +315,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) || $expr->options !== null) {
$ret[] = $expr;
}

Expand Down
15 changes: 15 additions & 0 deletions tests/Builder/CreateStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -895,4 +895,19 @@ public function testBuildCreateTableWithUppercaseHexDefault(): void
$this->assertEmpty($parser->errors);
$this->assertStringContainsString("DEFAULT X'FF'", $parser->statements[0]->build());
}

public function testBuildCreateTableCheckConstraint(): void
{
$sql = "CREATE TABLE `t` (\n"
. " `a` int(11) DEFAULT NULL,\n"
. " `b` int(11) DEFAULT NULL,\n"
. " CONSTRAINT `chk_ab` CHECK (`a` < `b`),\n"
. " CHECK (`b` > 0),\n"
. " `c` int(11) DEFAULT NULL\n"
. ') ENGINE=InnoDB';

$parser = new Parser($sql);
$this->assertEmpty($parser->errors);
$this->assertEquals($sql, $parser->statements[0]->build());
}
}
1 change: 1 addition & 0 deletions tests/Parser/CreateStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public static function createProvider(): array
['parser/parseCreateTableTimestampWithPrecision'],
['parser/parseCreateTableEnforcedCheck'],
['parser/parseCreateTableNotEnforcedCheck'],
['parser/parseCreateTableCheckConstraint'],
['parser/parseCreateTableWithInvisibleKey'],
['parser/parseCreateTableWithParser'],
['parser/parseCreateTableUuidColumn_mariadb_100700'],
Expand Down
9 changes: 9 additions & 0 deletions tests/data/parser/parseCreateTableCheckConstraint.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE `t` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL CHECK (`b` > 0),
CONSTRAINT `chk_ab` CHECK (`a` < `b`),
CONSTRAINT CHECK (`a` <> 42),
CHECK (`b` != 0xDEAD),
CONSTRAINT `chk_not_enforced` CHECK (`a` > 0) NOT ENFORCED,
`c` int(11) DEFAULT NULL
) ENGINE=InnoDB;
Loading