Skip to content

Commit 052f789

Browse files
committed
Add support for USE <database> statement (supports main DB and information schema)
1 parent c11d981 commit 052f789

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

wp-includes/sqlite-ast/class-wp-sqlite-driver.php

+45-3
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,19 @@ class WP_SQLite_Driver {
239239
private static $mysql_grammar;
240240

241241
/**
242-
* The database name.
242+
* The main database name.
243+
*
244+
* The name of the main database that is used by the driver.
245+
*
246+
* @var string|null
247+
*/
248+
private $main_db_name;
249+
250+
/**
251+
* The name of the current database in use.
252+
*
253+
* This can be set with the USE statement. At the moment, we support only
254+
* the main driver database and the INFORMATION_SCHEMA database.
243255
*
244256
* @var string
245257
*/
@@ -335,7 +347,8 @@ public function __construct( array $options ) {
335347
if ( ! isset( $options['database'] ) || ! is_string( $options['database'] ) ) {
336348
throw $this->new_driver_exception( 'Option "database" is required.' );
337349
}
338-
$this->db_name = $options['database'];
350+
$this->main_db_name = $options['database'];
351+
$this->db_name = $this->main_db_name;
339352

340353
// Database connection.
341354
if ( isset( $options['connection'] ) && $options['connection'] instanceof PDO ) {
@@ -757,6 +770,9 @@ private function execute_mysql_query( WP_Parser_Node $node ): void {
757770
case 'describeStatement':
758771
$this->execute_describe_statement( $subtree );
759772
break;
773+
case 'useCommand':
774+
$this->execute_use_statement( $subtree );
775+
break;
760776
default:
761777
throw $this->new_not_supported_exception(
762778
sprintf(
@@ -1594,6 +1610,32 @@ private function execute_describe_statement( WP_Parser_Node $node ): void {
15941610
$this->set_results_from_fetched_data( $column_info );
15951611
}
15961612

1613+
/**
1614+
* Translate and execute a MySQL USE statement in SQLite.
1615+
*
1616+
* @param WP_Parser_Node $node The "useStatement" AST node.
1617+
* @throws WP_SQLite_Driver_Exception When the query execution fails.
1618+
*/
1619+
private function execute_use_statement( WP_Parser_Node $node ): void {
1620+
$database_name = $this->unquote_sqlite_identifier(
1621+
$this->translate( $node->get_first_child_node( 'identifier' ) )
1622+
);
1623+
1624+
if ( 'information_schema' === strtolower( $database_name ) ) {
1625+
$this->db_name = 'information_schema';
1626+
} elseif ( $this->db_name === $database_name ) {
1627+
$this->db_name = $database_name;
1628+
} else {
1629+
throw $this->new_not_supported_exception(
1630+
sprintf(
1631+
"can't use database '%s', only '%s' and 'information_schema' are supported",
1632+
$database_name,
1633+
$this->db_name
1634+
)
1635+
);
1636+
}
1637+
}
1638+
15971639
/**
15981640
* Translate a MySQL AST node or token to an SQLite query fragment.
15991641
*
@@ -1947,7 +1989,7 @@ private function translate_qualified_identifier(
19471989
$parts = array();
19481990

19491991
// Database name.
1950-
$is_information_schema = false;
1992+
$is_information_schema = 'information_schema' === $this->db_name;
19511993
if ( null !== $schema_node ) {
19521994
$schema_name = $this->unquote_sqlite_identifier(
19531995
$this->translate_sequence( $schema_node->get_children() )

0 commit comments

Comments
 (0)