Skip to content

Commit 6b22c0e

Browse files
0xABCD01JanJakes
andauthored
Quote SQLite identifiers in LOCK TABLES, SHOW TABLES, and compound primary key indexes (#599)
### Summary Fixes #598. This PR ensures generated SQLite identifiers are consistently quoted via `$this->quote_sqlite_identifier()` in three query paths inside `WP_MySQL_On_SQLite`: 1. **`LOCK TABLES` (`execute_transaction_or_locking_statement()`)**: Quote `$table_name` in `SELECT 1 FROM %s LIMIT 0` so persistent and `TEMPORARY` tables whose names contain spaces, keywords, or special characters can be locked without falsely raising `1146 Table '...' doesn't exist`. 2. **`SHOW [FULL] TABLES` (`execute_show_tables_statement()`)**: Quote the `Tables_in_<database>` column alias via `$this->quote_sqlite_identifier( 'Tables_in_' . $database )` so database identifiers containing escaped backticks do not break the generated SQLite `SELECT` query. 3. **Compound `PRIMARY KEY` with `AUTO_INCREMENT` (`get_sqlite_create_table_statement()`)**: Quote `self::RESERVED_PREFIX . $sqlite_index_name` in `CREATE UNIQUE INDEX %s ON %s (%s)` (matching regular index generation right below it) so `CREATE TABLE` and `ALTER TABLE` table rebuilds succeed on quoted table names. ### Testing Added unit tests in `packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php`: - `testShowTablesWithQuotedDatabaseIdentifier` - `testCompoundPrimaryKeyWithAutoincrementAndQuotedIdentifiers` - `testLockTablesWithQuotedIdentifiers` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Improved handling of quoted database and table names when listing tables, locking tables, and using compound primary keys with auto-increment columns. * Improved table-lock behavior for names containing dots and for tables that do not exist. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Jan Jakeš <jan@jakes.pro>
1 parent 2716348 commit 6b22c0e

2 files changed

Lines changed: 109 additions & 7 deletions

File tree

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2301,7 +2301,10 @@ private function execute_transaction_or_locking_statement( WP_Parser_Node $node
23012301
* can handle persistent and temporary tables in one query.
23022302
*/
23032303
$this->execute_sqlite_query(
2304-
sprintf( 'SELECT 1 FROM %s LIMIT 0', $table_name )
2304+
sprintf(
2305+
'SELECT 1 FROM %s LIMIT 0',
2306+
$this->quote_sqlite_identifier( $table_name )
2307+
)
23052308
);
23062309
} catch ( PDOException $e ) {
23072310
throw $this->new_table_not_found_exception(
@@ -3722,19 +3725,20 @@ private function execute_show_tables_statement( WP_Parser_Node $node ): void {
37223725
$is_full = $command_type && $command_type->has_child_token( WP_MySQL_Lexer::FULL_SYMBOL );
37233726

37243727
// Fetch table information.
3725-
$table_tables = $this->information_schema_builder->get_table_name(
3728+
$table_tables = $this->information_schema_builder->get_table_name(
37263729
false, // SHOW TABLES lists only non-temporary tables.
37273730
'tables'
37283731
);
3729-
$query = sprintf(
3732+
$tables_column_alias = $this->quote_sqlite_identifier( 'Tables_in_' . $database );
3733+
$query = sprintf(
37303734
'SELECT %s FROM %s WHERE table_schema = ? %s ORDER BY table_name',
37313735
$is_full
3732-
? sprintf( 'table_name AS `Tables_in_%s`, table_type AS `Table_type`', $database )
3733-
: sprintf( 'table_name AS `Tables_in_%s`', $database ),
3736+
? sprintf( 'table_name AS %s, table_type AS `Table_type`', $tables_column_alias )
3737+
: sprintf( 'table_name AS %s', $tables_column_alias ),
37343738
$this->quote_sqlite_identifier( $table_tables ),
37353739
$condition ?? ''
37363740
);
3737-
$params = array(
3741+
$params = array(
37383742
$this->get_saved_db_name( $database ),
37393743
);
37403744

@@ -7451,7 +7455,7 @@ function ( $column ) {
74517455
$sqlite_index_name = $this->get_sqlite_index_name( $table_name, 'primary' );
74527456
$create_index_queries[] = sprintf(
74537457
'CREATE UNIQUE INDEX %s ON %s (%s)',
7454-
self::RESERVED_PREFIX . $sqlite_index_name,
7458+
$this->quote_sqlite_identifier( self::RESERVED_PREFIX . $sqlite_index_name ),
74557459
$this->quote_sqlite_identifier( $table_name ),
74567460
implode( ', ', $column_list )
74577461
);

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

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,44 @@ public function testShowTablesLike() {
14021402
);
14031403
}
14041404

1405+
public function testShowTablesWithQuotedDatabaseIdentifier(): void {
1406+
$pdo_class = PHP_VERSION_ID >= 80400 ? Pdo\Sqlite::class : PDO::class;
1407+
$this->sqlite = new $pdo_class( 'sqlite::memory:' );
1408+
$this->engine = new WP_MySQL_On_SQLite(
1409+
'mysql-on-sqlite:dbname=db`name',
1410+
null,
1411+
null,
1412+
array( 'sqlite_pdo' => $this->sqlite )
1413+
);
1414+
$this->engine->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true );
1415+
1416+
$this->assertQuery( 'CREATE TABLE `table name` (id INT)' );
1417+
1418+
$result = $this->assertQuery( 'SHOW TABLES' );
1419+
$this->assertSame( 'Tables_in_db`name', $this->last_statement->getColumnMeta( 0 )['name'] );
1420+
$this->assertEquals(
1421+
array(
1422+
(object) array(
1423+
'Tables_in_db`name' => 'table name',
1424+
),
1425+
),
1426+
$result
1427+
);
1428+
1429+
$result = $this->assertQuery( 'SHOW FULL TABLES FROM `db``name`' );
1430+
$this->assertSame( 'Tables_in_db`name', $this->last_statement->getColumnMeta( 0 )['name'] );
1431+
$this->assertSame( 'Table_type', $this->last_statement->getColumnMeta( 1 )['name'] );
1432+
$this->assertEquals(
1433+
array(
1434+
(object) array(
1435+
'Tables_in_db`name' => 'table name',
1436+
'Table_type' => 'BASE TABLE',
1437+
),
1438+
),
1439+
$result
1440+
);
1441+
}
1442+
14051443
public function testShowTableStatusFrom() {
14061444
// Created in setUp() function
14071445
$this->assertQuery( 'DROP TABLE _options' );
@@ -8161,6 +8199,44 @@ public function testCompoundPrimaryKeyWithAutoincrement(): void {
81618199
);
81628200
}
81638201

8202+
public function testCompoundPrimaryKeyWithAutoincrementAndQuotedIdentifiers(): void {
8203+
$this->assertQuery(
8204+
'CREATE TABLE `table ``name` (`id ``col` INT AUTO_INCREMENT, `name ``col` VARCHAR(32), PRIMARY KEY(`id ``col`, `name ``col`))'
8205+
);
8206+
8207+
$this->assertQuery( "INSERT INTO `table ``name` (`name ``col`) VALUES ('A'), ('B')" );
8208+
8209+
// Rebuilding the table via ALTER TABLE must also quote the generated index identifier.
8210+
$this->assertQuery( 'ALTER TABLE `table ``name` ADD COLUMN extra INT DEFAULT 0' );
8211+
$this->assertQuery( "INSERT INTO `table ``name` (`name ``col`) VALUES ('C')" );
8212+
8213+
$results = $this->assertQuery( 'SELECT `id ``col`, `name ``col` FROM `table ``name` ORDER BY `id ``col`' );
8214+
$this->assertEquals(
8215+
array(
8216+
(object) array(
8217+
'id `col' => '1',
8218+
'name `col' => 'A',
8219+
),
8220+
(object) array(
8221+
'id `col' => '2',
8222+
'name `col' => 'B',
8223+
),
8224+
(object) array(
8225+
'id `col' => '3',
8226+
'name `col' => 'C',
8227+
),
8228+
),
8229+
$results
8230+
);
8231+
8232+
$result = $this->engine
8233+
->execute_sqlite_query( "SELECT * FROM pragma_index_list('table `name')" )
8234+
->fetchAll( PDO::FETCH_ASSOC );
8235+
$this->assertCount( 1, $result );
8236+
$this->assertSame( '_wp_sqlite_table `name__primary', $result[0]['name'] );
8237+
$this->assertSame( '1', (string) $result[0]['unique'] );
8238+
}
8239+
81648240
/**
81658241
* @dataProvider getReservedPrefixTestData
81668242
*/
@@ -10546,6 +10622,28 @@ public function testLockTemporaryTables(): void {
1054610622
$this->assertQuery( 'UNLOCK TABLES' );
1054710623
}
1054810624

10625+
public function testLockTablesWithQuotedIdentifiers(): void {
10626+
$this->assertQuery( 'CREATE TABLE `table name` (id INT)' );
10627+
$this->assertQuery( 'CREATE TABLE `select` (id INT)' );
10628+
$this->assertQuery( 'CREATE TEMPORARY TABLE `temp``table` (id INT)' );
10629+
10630+
$this->assertQuery( 'LOCK TABLES `table name` READ, `select` WRITE, `temp``table` WRITE' );
10631+
$this->assertQuery( 'UNLOCK TABLES' );
10632+
10633+
$this->expectException( 'WP_MySQL_On_SQLite_Exception' );
10634+
$this->expectExceptionMessage( "Table 'wp.missing table' doesn't exist" );
10635+
$this->assertQuery( 'LOCK TABLES `missing table` READ' );
10636+
}
10637+
10638+
public function testLockTableWithDotInName(): void {
10639+
// "main.t" is a table name, not table "t" in the SQLite "main" schema.
10640+
$this->assertQuery( 'CREATE TABLE t (id INT)' );
10641+
10642+
$this->expectException( 'WP_MySQL_On_SQLite_Exception' );
10643+
$this->expectExceptionMessage( "Table 'wp.main.t' doesn't exist" );
10644+
$this->assertQuery( 'LOCK TABLES `main.t` READ' );
10645+
}
10646+
1054910647
public function testTransactionSavepoints(): void {
1055010648
$this->assertQuery( 'CREATE TABLE t (id INT)' );
1055110649

0 commit comments

Comments
 (0)