@@ -239,7 +239,19 @@ class WP_SQLite_Driver {
239
239
private static $ mysql_grammar ;
240
240
241
241
/**
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.
243
255
*
244
256
* @var string
245
257
*/
@@ -335,7 +347,8 @@ public function __construct( array $options ) {
335
347
if ( ! isset ( $ options ['database ' ] ) || ! is_string ( $ options ['database ' ] ) ) {
336
348
throw $ this ->new_driver_exception ( 'Option "database" is required. ' );
337
349
}
338
- $ this ->db_name = $ options ['database ' ];
350
+ $ this ->main_db_name = $ options ['database ' ];
351
+ $ this ->db_name = $ this ->main_db_name ;
339
352
340
353
// Database connection.
341
354
if ( isset ( $ options ['connection ' ] ) && $ options ['connection ' ] instanceof PDO ) {
@@ -757,6 +770,9 @@ private function execute_mysql_query( WP_Parser_Node $node ): void {
757
770
case 'describeStatement ' :
758
771
$ this ->execute_describe_statement ( $ subtree );
759
772
break ;
773
+ case 'useCommand ' :
774
+ $ this ->execute_use_statement ( $ subtree );
775
+ break ;
760
776
default :
761
777
throw $ this ->new_not_supported_exception (
762
778
sprintf (
@@ -1594,6 +1610,32 @@ private function execute_describe_statement( WP_Parser_Node $node ): void {
1594
1610
$ this ->set_results_from_fetched_data ( $ column_info );
1595
1611
}
1596
1612
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
+
1597
1639
/**
1598
1640
* Translate a MySQL AST node or token to an SQLite query fragment.
1599
1641
*
@@ -1947,7 +1989,7 @@ private function translate_qualified_identifier(
1947
1989
$ parts = array ();
1948
1990
1949
1991
// Database name.
1950
- $ is_information_schema = false ;
1992
+ $ is_information_schema = ' information_schema ' === $ this -> db_name ;
1951
1993
if ( null !== $ schema_node ) {
1952
1994
$ schema_name = $ this ->unquote_sqlite_identifier (
1953
1995
$ this ->translate_sequence ( $ schema_node ->get_children () )
0 commit comments