Skip to content

Commit 5984afe

Browse files
committed
Fix LIKE in SHOW COLUMN being applied to table name instead of column name
1 parent b170a8c commit 5984afe

2 files changed

Lines changed: 36 additions & 5 deletions

File tree

tests/WP_SQLite_Driver_Metadata_Tests.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,32 @@ public function testShowTableStatus() {
412412
);
413413
}
414414

415+
public function testShowColumnsLike(): void {
416+
$this->assertQuery( 'CREATE TABLE t (id INT, val1 INT, val2 INT, name TEXT)' );
417+
$result = $this->assertQuery( "SHOW COLUMNS FROM t LIKE 'val%'" );
418+
$this->assertEquals(
419+
array(
420+
(object) array(
421+
'Field' => 'val1',
422+
'Type' => 'int',
423+
'Null' => 'YES',
424+
'Key' => '',
425+
'Default' => null,
426+
'Extra' => '',
427+
),
428+
(object) array(
429+
'Field' => 'val2',
430+
'Type' => 'int',
431+
'Null' => 'YES',
432+
'Key' => '',
433+
'Default' => null,
434+
'Extra' => '',
435+
),
436+
),
437+
$result
438+
);
439+
}
440+
415441
private function assertTableEmpty( $table_name, $empty_var ) {
416442

417443
$this->assertQuery(

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,7 +1713,7 @@ private function execute_show_table_status_statement( WP_Parser_Node $node ): vo
17131713
// LIKE and WHERE clauses.
17141714
$like_or_where = $node->get_first_child_node( 'likeOrWhere' );
17151715
if ( null !== $like_or_where ) {
1716-
$condition = $this->translate_show_like_or_where_condition( $like_or_where );
1716+
$condition = $this->translate_show_like_or_where_condition( $like_or_where, 'table_name' );
17171717
}
17181718

17191719
// Fetch table information.
@@ -1782,7 +1782,7 @@ private function execute_show_tables_statement( WP_Parser_Node $node ): void {
17821782
// LIKE and WHERE clauses.
17831783
$like_or_where = $node->get_first_child_node( 'likeOrWhere' );
17841784
if ( null !== $like_or_where ) {
1785-
$condition = $this->translate_show_like_or_where_condition( $like_or_where );
1785+
$condition = $this->translate_show_like_or_where_condition( $like_or_where, 'table_name' );
17861786
}
17871787

17881788
// Fetch table information.
@@ -1867,7 +1867,7 @@ private function execute_show_columns_statement( WP_Parser_Node $node ): void {
18671867
// LIKE and WHERE clauses.
18681868
$like_or_where = $node->get_first_child_node( 'likeOrWhere' );
18691869
if ( null !== $like_or_where ) {
1870-
$condition = $this->translate_show_like_or_where_condition( $like_or_where );
1870+
$condition = $this->translate_show_like_or_where_condition( $like_or_where, 'column_name' );
18711871
}
18721872

18731873
// Fetch column information.
@@ -3003,16 +3003,21 @@ private function recreate_table_from_information_schema(
30033003
* Translate a MySQL SHOW LIKE ... or SHOW WHERE ... condition to SQLite.
30043004
*
30053005
* @param WP_Parser_Node $like_or_where The "likeOrWhere" AST node.
3006+
* @param string $like_column The column name to use in the LIKE clause ("table_name", "column_name", etc.).
30063007
* @return string The translated value.
30073008
* @throws WP_SQLite_Driver_Exception When the translation fails.
30083009
*/
3009-
private function translate_show_like_or_where_condition( WP_Parser_Node $like_or_where ): string {
3010+
private function translate_show_like_or_where_condition( WP_Parser_Node $like_or_where, string $like_column ): string {
30103011
$like_clause = $like_or_where->get_first_child_node( 'likeClause' );
30113012
if ( null !== $like_clause ) {
30123013
$value = $this->translate(
30133014
$like_clause->get_first_child_node( 'textStringLiteral' )
30143015
);
3015-
return sprintf( "AND table_name LIKE %s ESCAPE '\\'", $value );
3016+
return sprintf(
3017+
"AND %s LIKE %s ESCAPE '\\'",
3018+
$this->quote_sqlite_identifier( $like_column ),
3019+
$value
3020+
);
30163021
}
30173022

30183023
$where_clause = $like_or_where->get_first_child_node( 'whereClause' );

0 commit comments

Comments
 (0)