Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix USE statement, add tests
  • Loading branch information
JanJakes committed Oct 15, 2025
commit 98d1ed8d11ca1a1d05e7df81e787188dbfe23b35
42 changes: 42 additions & 0 deletions tests/WP_SQLite_Driver_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -9325,4 +9325,46 @@ function ( $name ) {
$this->assertEquals( 'information_schema', $result[0]->SCHEMA_NAME );
$this->assertEquals( 'wp_test_new', $result[1]->SCHEMA_NAME );
}

public function testDynamicDatabaseNameWithUseStatement(): void {
// Ensure "information_schema.tables" is empty.
$this->assertQuery( 'DROP TABLE _options, _dates' );
$result = $this->assertQuery( 'SELECT * FROM information_schema.tables' );
$this->assertCount( 0, $result );

// Create a "tables" table in the "wp" database.
$this->assertQuery( 'CREATE TABLE tables (id INT)' );
$this->assertQuery( 'INSERT INTO tables (id) VALUES (1), (2)' );

// Now, unqualified "tables" refers to the "wp.tables".
$result = $this->assertQuery( 'SELECT * FROM tables' );
$this->assertCount( 2, $result );
$this->assertEquals( array( (object) array( 'id' => '1' ), (object) array( 'id' => '2' ) ), $result );

// Qualified references should work as well.
$result = $this->assertQuery( 'SELECT * FROM wp.tables' );
$this->assertCount( 2, $result );
$this->assertEquals( array( (object) array( 'id' => '1' ), (object) array( 'id' => '2' ) ), $result );

$result = $this->assertQuery( 'SELECT * FROM information_schema.tables' );
$this->assertCount( 1, $result );
$this->assertEquals( 'tables', $result[0]->TABLE_NAME );

// Switch to the "information_schema" database.
$this->assertQuery( 'USE information_schema' );

// Now, unqualified "tables" refers to the "information_schema.tables".
$result = $this->assertQuery( 'SELECT * FROM tables' );
$this->assertCount( 1, $result );
$this->assertEquals( 'tables', $result[0]->TABLE_NAME );

// Qualified references should still work.
$result = $this->assertQuery( 'SELECT * FROM wp.tables' );
$this->assertCount( 2, $result );
$this->assertEquals( array( (object) array( 'id' => '1' ), (object) array( 'id' => '2' ) ), $result );

$result = $this->assertQuery( 'SELECT * FROM information_schema.tables' );
$this->assertCount( 1, $result );
$this->assertEquals( 'tables', $result[0]->TABLE_NAME );
}
}
2 changes: 1 addition & 1 deletion wp-includes/sqlite-ast/class-wp-sqlite-driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3353,7 +3353,7 @@ private function translate_qualified_identifier(
);
if ( 'information_schema' === strtolower( $schema_name ) ) {
$is_information_schema = true;
} elseif ( $this->db_name === $schema_name ) {
} elseif ( $this->main_db_name === $schema_name ) {
$is_information_schema = false;
} else {
throw $this->new_not_supported_exception(
Expand Down
Loading