Skip to content

Commit ff50b67

Browse files
Nandan108claude
andcommitted
Fix CREATE TEMPORARY TABLE IF NOT EXISTS as the first temporary table
The IF NOT EXISTS check looked the table up in the temporary information schema tables, which are only created together with the first temporary table of a session. When the first temporary table of a session was created with IF NOT EXISTS, the lookup failed with "Table '_wp_sqlite_mysql_information_schema_tmp_tables' doesn't exist". For temporary tables, check for the table in sqlite_temp_master instead, as temporary_table_exists() already does for this reason. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent be0bdf8 commit ff50b67

2 files changed

Lines changed: 43 additions & 8 deletions

File tree

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2925,14 +2925,22 @@ private function execute_create_table_statement( WP_Parser_Node $node ): void {
29252925

29262926
// Handle IF NOT EXISTS.
29272927
if ( $subnode->has_child_node( 'ifNotExists' ) ) {
2928-
$tables_table = $this->information_schema_builder->get_table_name( $table_is_temporary, 'tables' );
2929-
$table_exists = $this->execute_sqlite_query(
2930-
sprintf(
2931-
'SELECT 1 FROM %s WHERE table_schema = ? AND table_name = ?',
2932-
$this->quote_sqlite_identifier( $tables_table )
2933-
),
2934-
array( $this->get_saved_db_name(), $table_name )
2935-
)->fetchColumn();
2928+
if ( $table_is_temporary ) {
2929+
/*
2930+
* The temporary information schema tables are created with the
2931+
* first temporary table of a session, so they may not exist yet.
2932+
*/
2933+
$table_exists = $this->information_schema_builder->temporary_table_exists( $table_name );
2934+
} else {
2935+
$tables_table = $this->information_schema_builder->get_table_name( false, 'tables' );
2936+
$table_exists = $this->execute_sqlite_query(
2937+
sprintf(
2938+
'SELECT 1 FROM %s WHERE table_schema = ? AND table_name = ?',
2939+
$this->quote_sqlite_identifier( $tables_table )
2940+
),
2941+
array( $this->get_saved_db_name(), $table_name )
2942+
)->fetchColumn();
2943+
}
29362944

29372945
if ( $table_exists ) {
29382946
$this->last_result_statement = $this->create_result_statement_from_data( array(), array() );

‎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
@@ -4433,6 +4433,33 @@ public function testCreateTemporaryTableIfNotExists(): void {
44334433
);
44344434
}
44354435

4436+
public function testCreateTemporaryTableIfNotExistsAsFirstTemporaryTable(): void {
4437+
// No temporary table exists in this session yet.
4438+
$this->assertQuery(
4439+
'CREATE TEMPORARY TABLE IF NOT EXISTS t (ID INTEGER, name TEXT)'
4440+
);
4441+
$this->assertQuery( "INSERT INTO t (ID, name) VALUES (1, 'a')" );
4442+
4443+
// A repeated statement is a no-op and keeps the existing data,
4444+
// also when the table name is cased differently.
4445+
$this->assertQuery(
4446+
'CREATE TEMPORARY TABLE IF NOT EXISTS t (ID INTEGER, name TEXT)'
4447+
);
4448+
$this->assertQuery(
4449+
'CREATE TEMPORARY TABLE IF NOT EXISTS T (ID INTEGER, name TEXT)'
4450+
);
4451+
$result = $this->assertQuery( 'SELECT ID, name FROM t' );
4452+
$this->assertEquals(
4453+
array(
4454+
(object) array(
4455+
'ID' => '1',
4456+
'name' => 'a',
4457+
),
4458+
),
4459+
$result
4460+
);
4461+
}
4462+
44364463
public function testTranslatesComplexDelete() {
44374464
$this->sqlite->query(
44384465
"CREATE TABLE wptests_dummy (

0 commit comments

Comments
 (0)