Skip to content

Commit 1754169

Browse files
authored
Support LIKE/WHERE clauses in all SHOW statements (#286)
Adds support for `WHERE` and `LIKE` conditions to `SHOW`, where it was missing: - Add support for `LIKE` and `WHERE` clauses in `SHOW COLLATION`. - Add support for `WHERE` clause in `SHOW INDEX`. This fixes some failures in the activation of the top 100 plugins.
1 parent d982450 commit 1754169

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

tests/WP_SQLite_Driver_Metadata_Tests.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ public function testRepairTable() {
468468
}
469469

470470
public function testShowCollation(): void {
471+
// Simple.
471472
$this->assertQuery( 'SHOW COLLATION' );
472473
$actual = $this->engine->get_query_results();
473474
$this->assertCount( 7, $actual );
@@ -478,6 +479,21 @@ public function testShowCollation(): void {
478479
$this->assertEquals( 'utf8mb4_bin', $actual[4]->Collation );
479480
$this->assertEquals( 'utf8mb4_unicode_ci', $actual[5]->Collation );
480481
$this->assertEquals( 'utf8mb4_0900_ai_ci', $actual[6]->Collation );
482+
483+
// With LIKE clause.
484+
$this->assertQuery( "SHOW COLLATION LIKE 'utf8%'" );
485+
$actual = $this->engine->get_query_results();
486+
$this->assertCount( 6, $actual );
487+
$this->assertEquals( 'utf8_bin', $actual[0]->Collation );
488+
$this->assertEquals( 'utf8_general_ci', $actual[1]->Collation );
489+
$this->assertEquals( 'utf8_unicode_ci', $actual[2]->Collation );
490+
$this->assertEquals( 'utf8mb4_bin', $actual[3]->Collation );
491+
492+
// With WHERE clause.
493+
$this->assertQuery( "SHOW COLLATION WHERE Collation = 'utf8_bin'" );
494+
$actual = $this->engine->get_query_results();
495+
$this->assertCount( 1, $actual );
496+
$this->assertEquals( 'utf8_bin', $actual[0]->Collation );
481497
}
482498

483499
public function testShowDatabases(): void {

tests/WP_SQLite_Driver_Tests.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3018,6 +3018,19 @@ public function testShowIndex() {
30183018
),
30193019
$this->engine->get_query_results()
30203020
);
3021+
3022+
// With WHERE clause.
3023+
$this->assertQuery( "SHOW INDEX FROM wptests_term_relationships WHERE Key_name = 'PRIMARY'" );
3024+
$actual = $this->engine->get_query_results();
3025+
$this->assertCount( 2, $actual );
3026+
3027+
$this->assertQuery( 'SHOW INDEX FROM wptests_term_relationships WHERE Non_unique = 0' );
3028+
$actual = $this->engine->get_query_results();
3029+
$this->assertCount( 2, $actual );
3030+
3031+
$this->assertQuery( "SHOW INDEX FROM wptests_term_relationships WHERE Index_type = 'FULLTEXT'" );
3032+
$actual = $this->engine->get_query_results();
3033+
$this->assertCount( 2, $actual );
30213034
}
30223035

30233036
public function testShowVarianles(): void {

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

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2230,7 +2230,7 @@ private function execute_show_statement( WP_Parser_Node $node ): void {
22302230

22312231
switch ( $keyword1->id ) {
22322232
case WP_MySQL_Lexer::COLLATION_SYMBOL:
2233-
$this->execute_show_collation_statement();
2233+
$this->execute_show_collation_statement( $node );
22342234
return;
22352235
case WP_MySQL_Lexer::DATABASES_SYMBOL:
22362236
$this->execute_show_databases_statement( $node );
@@ -2359,12 +2359,18 @@ private function execute_show_statement( WP_Parser_Node $node ): void {
23592359

23602360
/**
23612361
* Translate and execute a MySQL SHOW COLLATION statement in SQLite.
2362+
*
2363+
* @param WP_Parser_Node $node The "showStatement" AST node.
23622364
*/
2363-
private function execute_show_collation_statement(): void {
2365+
private function execute_show_collation_statement( WP_Parser_Node $node ): void {
23642366
$definition = $this->information_schema_builder
23652367
->get_computed_information_schema_table_definition( 'collations' );
23662368

2367-
// TODO: LIKE and WHERE clauses.
2369+
// LIKE and WHERE clauses.
2370+
$like_or_where = $node->get_first_child_node( 'likeOrWhere' );
2371+
if ( $like_or_where ) {
2372+
$condition = $this->translate_show_like_or_where_condition( $like_or_where, 'collation_name' );
2373+
}
23682374

23692375
$stmt = $this->execute_sqlite_query(
23702376
sprintf(
@@ -2376,8 +2382,10 @@ private function execute_show_collation_statement(): void {
23762382
IS_COMPILED AS `Compiled`,
23772383
SORTLEN AS `Sortlen`,
23782384
PAD_ATTRIBUTE AS `Pad_attribute`
2379-
FROM (%s)',
2380-
$definition
2385+
FROM (%s)
2386+
WHERE TRUE %s',
2387+
$definition,
2388+
$condition ?? ''
23812389
)
23822390
);
23832391
$this->store_last_column_meta_from_statement( $stmt );
@@ -2394,10 +2402,9 @@ private function execute_show_databases_statement( WP_Parser_Node $node ): void
23942402

23952403
// LIKE and WHERE clauses.
23962404
$like_or_where = $node->get_first_child_node( 'likeOrWhere' );
2397-
if ( null !== $like_or_where ) {
2405+
if ( $like_or_where ) {
23982406
$condition = $this->translate_show_like_or_where_condition( $like_or_where, 'schema_name' );
23992407
}
2400-
24012408
$stmt = $this->execute_sqlite_query(
24022409
sprintf(
24032410
'SELECT SCHEMA_NAME AS Database
@@ -2424,10 +2431,9 @@ private function execute_show_databases_statement( WP_Parser_Node $node ): void
24242431
* @param WP_Parser_Node $node The "showStatement" AST node.
24252432
*/
24262433
private function execute_show_index_statement( WP_Parser_Node $node ): void {
2434+
// Get database and table name.
24272435
$table_ref = $node->get_first_child_node( 'tableRef' );
24282436
$in_db = $node->get_first_child_node( 'inDb' );
2429-
2430-
// Get database and table name.
24312437
if ( $in_db ) {
24322438
// FROM/IN database.
24332439
$database = $this->get_database_name( $in_db );
@@ -2436,7 +2442,14 @@ private function execute_show_index_statement( WP_Parser_Node $node ): void {
24362442
}
24372443
$table_name = $this->unquote_sqlite_identifier( $this->translate( $table_ref ) );
24382444

2439-
// TODO: WHERE
2445+
// WHERE clause.
2446+
$where = $node->get_first_child_node( 'whereClause' );
2447+
if ( null !== $where ) {
2448+
$value = $this->translate( $where->get_first_child_node( 'expr' ) );
2449+
$condition = sprintf( 'AND %s', $value );
2450+
} else {
2451+
$condition = '';
2452+
}
24402453

24412454
$table_is_temporary = $this->information_schema_builder->temporary_table_exists( $table_name );
24422455

@@ -2480,6 +2493,7 @@ private function execute_show_index_statement( WP_Parser_Node $node ): void {
24802493
FROM ' . $this->quote_sqlite_identifier( $statistics_table ) . "
24812494
WHERE table_schema = ?
24822495
AND table_name = ?
2496+
$condition
24832497
ORDER BY
24842498
INDEX_NAME = 'PRIMARY' DESC,
24852499
NON_UNIQUE = '0' DESC,
@@ -2624,10 +2638,10 @@ private function execute_show_tables_statement( WP_Parser_Node $node ): void {
26242638
*/
26252639
private function execute_show_columns_statement( WP_Parser_Node $node ): void {
26262640
// TODO: EXTENDED, FULL
2627-
$table_ref = $node->get_first_child_node( 'tableRef' );
2628-
$in_db = $node->get_first_child_node( 'inDb' );
26292641

26302642
// Get database and table name.
2643+
$table_ref = $node->get_first_child_node( 'tableRef' );
2644+
$in_db = $node->get_first_child_node( 'inDb' );
26312645
if ( $in_db ) {
26322646
// FROM/IN database.
26332647
$database = $this->get_database_name( $in_db );

0 commit comments

Comments
 (0)