Skip to content

Commit fa10497

Browse files
committed
Implement SHOW DATABASES statement
1 parent 574ed2d commit fa10497

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

tests/WP_SQLite_Driver_Metadata_Tests.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,64 @@ public function testRepairTable() {
371371
);
372372
}
373373

374+
public function testShowDatabases(): void {
375+
// Simple.
376+
$this->assertQuery( 'SHOW DATABASES' );
377+
$actual = $this->engine->get_query_results();
378+
$this->assertEquals(
379+
array(
380+
(object) array( 'Database' => 'information_schema' ),
381+
(object) array( 'Database' => 'wp' ),
382+
),
383+
$actual
384+
);
385+
386+
// With LIKE clause.
387+
$this->assertQuery( 'SHOW DATABASES LIKE "w%"' );
388+
$actual = $this->engine->get_query_results();
389+
$this->assertEquals(
390+
array( (object) array( 'Database' => 'wp' ) ),
391+
$actual
392+
);
393+
394+
// With WHERE clause.
395+
$this->assertQuery( 'SHOW DATABASES WHERE `Database` = "wp"' );
396+
$actual = $this->engine->get_query_results();
397+
$this->assertEquals(
398+
array( (object) array( 'Database' => 'wp' ) ),
399+
$actual
400+
);
401+
}
402+
403+
public function testShowTableSchemas(): void {
404+
$this->assertQuery( 'SHOW SCHEMAS' );
405+
406+
$actual = $this->engine->get_query_results();
407+
$this->assertEquals(
408+
array(
409+
(object) array( 'Database' => 'information_schema' ),
410+
(object) array( 'Database' => 'wp' ),
411+
),
412+
$actual
413+
);
414+
415+
// With LIKE clause.
416+
$this->assertQuery( 'SHOW DATABASES LIKE "inf%"' );
417+
$actual = $this->engine->get_query_results();
418+
$this->assertEquals(
419+
array( (object) array( 'Database' => 'information_schema' ) ),
420+
$actual
421+
);
422+
423+
// With WHERE clause.
424+
$this->assertQuery( 'SHOW DATABASES WHERE `Database` = "information_schema"' );
425+
$actual = $this->engine->get_query_results();
426+
$this->assertEquals(
427+
array( (object) array( 'Database' => 'information_schema' ) ),
428+
$actual
429+
);
430+
}
431+
374432
public function testShowTableStatus() {
375433
$this->assertQuery( 'CREATE TABLE t ( comment_author TEXT, comment_content TEXT )' );
376434

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,9 @@ private function execute_show_statement( WP_Parser_Node $node ): void {
15941594
$keyword2 = $tokens[2] ?? null;
15951595

15961596
switch ( $keyword1->id ) {
1597+
case WP_MySQL_Lexer::DATABASES_SYMBOL:
1598+
$this->execute_show_databases_statement( $node );
1599+
break;
15971600
case WP_MySQL_Lexer::COLUMNS_SYMBOL:
15981601
case WP_MySQL_Lexer::FIELDS_SYMBOL:
15991602
$this->execute_show_columns_statement( $node );
@@ -1658,6 +1661,31 @@ private function execute_show_statement( WP_Parser_Node $node ): void {
16581661
}
16591662
}
16601663

1664+
/**
1665+
* Translate and execute a MySQL SHOW DATABASES statement in SQLite.
1666+
*
1667+
* @param WP_Parser_Node $node The "showStatement" AST node.
1668+
*/
1669+
private function execute_show_databases_statement( WP_Parser_Node $node ): void {
1670+
$schemata_table = $this->information_schema_builder->get_table_name( false, 'schemata' );
1671+
1672+
// LIKE and WHERE clauses.
1673+
$like_or_where = $node->get_first_child_node( 'likeOrWhere' );
1674+
if ( null !== $like_or_where ) {
1675+
$condition = $this->translate_show_like_or_where_condition( $like_or_where, 'schema_name' );
1676+
}
1677+
1678+
$databases = $this->execute_sqlite_query(
1679+
sprintf(
1680+
'SELECT SCHEMA_NAME AS Database FROM %s%s ORDER BY SCHEMA_NAME',
1681+
$this->quote_sqlite_identifier( $schemata_table ),
1682+
isset( $condition ) ? ( ' WHERE TRUE ' . $condition ) : ''
1683+
)
1684+
)->fetchAll( PDO::FETCH_OBJ );
1685+
1686+
$this->set_results_from_fetched_data( $databases );
1687+
}
1688+
16611689
/**
16621690
* Translate and execute a MySQL SHOW INDEX statement in SQLite.
16631691
*

0 commit comments

Comments
 (0)