Skip to content

Commit 7eef338

Browse files
committed
Join qualified names without whitespace in stored expressions
The driver stores CHECK and DEFAULT expressions by joining their tokens with spaces, and it parses them again to create the SQLite table. With spaces, "t . order" and "order . id" contain the ORDER keyword, and "t . 1" contains a number, so the stored text no longer parses. For example, "CHECK (t.order > 0)" failed with an internal error, while MySQL accepts it. Join the tokens around "." without whitespace, as the serializer already does for parentheses, and list each of these token types once.
1 parent bc38094 commit 7eef338

2 files changed

Lines changed: 40 additions & 15 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-information-schema-builder.php‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3347,23 +3347,21 @@ private function serialize_mysql_expression( WP_Parser_Node $node ): string {
33473347
}
33483348

33493349
$value = '';
3350-
$last_token_id = null;
3351-
foreach ( $node->get_descendant_tokens() as $i => $token ) {
3352-
// Do not insert whitespace around parentheses. This is primarily to
3353-
// avoid inserting whitespace before '(', which may break function
3354-
// calls, depending on the value of the "IGNORE_SPACE" SQL mode.
3355-
if (
3356-
0 === $i
3357-
|| WP_MySQL_Lexer::OPEN_PAR_SYMBOL === $token->id
3350+
$last_no_space = true;
3351+
foreach ( $node->get_descendant_tokens() as $token ) {
3352+
// Do not insert whitespace around parentheses and dots. Before '(',
3353+
// whitespace may break function calls, depending on the value of the
3354+
// "IGNORE_SPACE" SQL mode. Around '.', it changes how qualified names
3355+
// are lexed: "t . 1" has a number, and "order . id" has a keyword.
3356+
$no_space = WP_MySQL_Lexer::OPEN_PAR_SYMBOL === $token->id
33583357
|| WP_MySQL_Lexer::CLOSE_PAR_SYMBOL === $token->id
3359-
|| WP_MySQL_Lexer::OPEN_PAR_SYMBOL === $last_token_id
3360-
|| WP_MySQL_Lexer::CLOSE_PAR_SYMBOL === $last_token_id
3361-
) {
3362-
$value .= $token->get_bytes();
3363-
} else {
3364-
$value .= ' ' . $token->get_bytes();
3358+
|| WP_MySQL_Lexer::DOT_SYMBOL === $token->id;
3359+
3360+
if ( ! $no_space && ! $last_no_space ) {
3361+
$value .= ' ';
33653362
}
3366-
$last_token_id = $token->id;
3363+
$value .= $token->get_bytes();
3364+
$last_no_space = $no_space;
33673365
}
33683366
return $value;
33693367
}

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14520,6 +14520,33 @@ public function testReservedWordsAsQualifiers(): void {
1452014520
);
1452114521
}
1452214522

14523+
public function testCheckConstraintsWithQualifiedNames(): void {
14524+
// The stored check clauses are parsed again to create the SQLite table.
14525+
$this->assertQuery( 'CREATE TABLE `order` (id INT, `1` INT, CHECK (order.id > 0), CHECK (wp.order.1 < 10))' );
14526+
14527+
$result = $this->assertQuery( 'SHOW CREATE TABLE `order`' );
14528+
$this->assertEquals(
14529+
implode(
14530+
"\n",
14531+
array(
14532+
'CREATE TABLE `order` (',
14533+
' `id` int DEFAULT NULL,',
14534+
' `1` int DEFAULT NULL,',
14535+
' CONSTRAINT `order_chk_1` CHECK (order.id > 0),',
14536+
' CONSTRAINT `order_chk_2` CHECK (wp.order.1 < 10)',
14537+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
14538+
)
14539+
),
14540+
$result[0]->{'Create Table'}
14541+
);
14542+
14543+
$this->assertQuery( 'INSERT INTO `order` VALUES (1, 9)' );
14544+
14545+
$this->expectException( WP_MySQL_On_SQLite_Exception::class );
14546+
$this->expectExceptionMessage( 'SQLSTATE[23000]: Integrity constraint violation: 19 CHECK constraint failed: order_chk_2' );
14547+
$this->assertQuery( 'INSERT INTO `order` VALUES (1, 10)' );
14548+
}
14549+
1452314550
public function testWriteWithUsageOfInformationSchemaTables(): void {
1452414551
// Ensure "information_schema.tables" is empty.
1452514552
$this->assertQuery( 'DROP TABLE _options, _dates' );

0 commit comments

Comments
 (0)