Skip to content

Commit be0bdf8

Browse files
Nandan108claude
andcommitted
Fix temporary table lookup without stringified fetches and with a different case
The temporary table lookup compared the fetched value to the string "1". PDO SQLite returns native types unless fetches are stringified, so on PHP 8.1 and newer, the lookup never found a temporary table for callers that don't set PDO::ATTR_STRINGIFY_FETCHES. Statements like ALTER TABLE, SHOW COLUMNS, or DESCRIBE then failed or used a standard table of the same name instead. The lookup also matched the table name case-sensitively, unlike the information schema tables, so a temporary table was not found when the statement used a differently cased name. Match the name case-insensitively and check the fetched value for presence rather than comparing it to a string. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 6546e45 commit be0bdf8

3 files changed

Lines changed: 38 additions & 3 deletions

File tree

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,12 +381,16 @@ public function temporary_table_exists( string $table_name ): bool {
381381
/*
382382
* We could search in the "{$this->temporary_table_prefix}tables" table,
383383
* but it may not exist yet, so using "sqlite_temp_master" is simpler.
384+
*
385+
* The name is matched case-insensitively, as it is in the information
386+
* schema tables. The fetched value is not compared to a string, because
387+
* PDO SQLite returns native types when fetches are not stringified.
384388
*/
385389
$stmt = $this->connection->query(
386-
"SELECT 1 FROM sqlite_temp_master WHERE type = 'table' AND name = ?",
390+
"SELECT 1 FROM sqlite_temp_master WHERE type = 'table' AND name = ? COLLATE NOCASE",
387391
array( $table_name )
388392
);
389-
return $stmt->fetchColumn() === '1';
393+
return false !== $stmt->fetchColumn();
390394
}
391395

392396
/**

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5842,6 +5842,37 @@ public function getInformationSchemaIsReadonlyWithUseTestData(): array {
58425842
);
58435843
}
58445844

5845+
public function testTemporaryTableIsFoundWithoutStringifiedFetches(): void {
5846+
// PDO SQLite returns native types unless fetches are stringified.
5847+
$this->engine->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, false );
5848+
5849+
// Create a standard and a temporary table with the same name.
5850+
$this->assertQuery( 'CREATE TABLE t (a INT)' );
5851+
$this->assertQuery( 'CREATE TEMPORARY TABLE t (b INT)' );
5852+
5853+
// ALTER TABLE will use the temporary table.
5854+
$this->assertQuery( 'ALTER TABLE t ADD COLUMN c INT' );
5855+
$result = $this->assertQuery( 'SHOW COLUMNS FROM t' );
5856+
$this->assertEquals( 'b', $result[0]->Field );
5857+
$this->assertEquals( 'c', $result[1]->Field );
5858+
5859+
// The standard table is unchanged.
5860+
$this->assertQuery( 'DROP TEMPORARY TABLE t' );
5861+
$result = $this->assertQuery( 'SHOW COLUMNS FROM t' );
5862+
$this->assertCount( 1, $result );
5863+
$this->assertEquals( 'a', $result[0]->Field );
5864+
}
5865+
5866+
public function testTemporaryTableIsFoundByDifferentlyCasedName(): void {
5867+
$this->assertQuery( 'CREATE TEMPORARY TABLE t (a INT)' );
5868+
5869+
// ALTER TABLE will use the temporary table.
5870+
$this->assertQuery( 'ALTER TABLE T ADD COLUMN b INT' );
5871+
$result = $this->assertQuery( 'SHOW COLUMNS FROM T' );
5872+
$this->assertEquals( 'a', $result[0]->Field );
5873+
$this->assertEquals( 'b', $result[1]->Field );
5874+
}
5875+
58455876
public function testTemporaryTableHasPriorityOverStandardTable(): void {
58465877
// Create a standard and a temporary table with the same name.
58475878
$this->assertQuery( 'CREATE TABLE t (a INT, INDEX ia(a))' );

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2157,7 +2157,7 @@ private function assertQuery( $expected, string $query ): void {
21572157
array_filter(
21582158
$executed_queries,
21592159
function ( $query ) {
2160-
return "SELECT 1 FROM sqlite_temp_master WHERE type = 'table' AND name = ?" !== $query;
2160+
return "SELECT 1 FROM sqlite_temp_master WHERE type = 'table' AND name = ? COLLATE NOCASE" !== $query;
21612161
}
21622162
)
21632163
);

0 commit comments

Comments
 (0)