Skip to content

Commit 24201b7

Browse files
committed
fixup! Lex qualified names with MySQL's lexer states
Join the tokens around "." without whitespace in stored CHECK and DEFAULT expressions. The driver parses these again to create the SQLite table, and with whitespace, "order . id" and "t . 1" are not qualified names, so CREATE TABLE failed with an internal error.
1 parent b8f2378 commit 24201b7

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3352,12 +3352,16 @@ private function serialize_mysql_expression( WP_Parser_Node $node ): string {
33523352
// Do not insert whitespace around parentheses. This is primarily to
33533353
// avoid inserting whitespace before '(', which may break function
33543354
// calls, depending on the value of the "IGNORE_SPACE" SQL mode.
3355+
// Around '.', whitespace changes how qualified names are lexed:
3356+
// "t . 1" has a number, and "order . id" has a keyword.
33553357
if (
33563358
0 === $i
33573359
|| WP_MySQL_Lexer::OPEN_PAR_SYMBOL === $token->id
33583360
|| WP_MySQL_Lexer::CLOSE_PAR_SYMBOL === $token->id
3361+
|| WP_MySQL_Lexer::DOT_SYMBOL === $token->id
33593362
|| WP_MySQL_Lexer::OPEN_PAR_SYMBOL === $last_token_id
33603363
|| WP_MySQL_Lexer::CLOSE_PAR_SYMBOL === $last_token_id
3364+
|| WP_MySQL_Lexer::DOT_SYMBOL === $last_token_id
33613365
) {
33623366
$value .= $token->get_bytes();
33633367
} else {

‎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)