From 2b2f9e229d2772d680393c1e7d7e4a41dae5b114 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Wed, 29 Nov 2017 15:42:27 -0500 Subject: [PATCH] Database: Fix degenerate parenthesized joins The SQL standard supports parenthesized joins like a JOIN (b JOIN c ON (...)) ON (...) But it doesn't support parenthesizing a single table name, i.e. a one-table "join", like a JOIN (b) ON (...) Detect the degenerate single-table case and omit the parentheses. Bug: T181674 Change-Id: I82cacd80465092aa67ff19bdcfd6682001bf12ab --- includes/libs/rdbms/database/Database.php | 16 +++++++++++++--- .../libs/rdbms/database/DatabaseTest.php | 7 +++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/includes/libs/rdbms/database/Database.php b/includes/libs/rdbms/database/Database.php index e04566eb49..b9367791c7 100644 --- a/includes/libs/rdbms/database/Database.php +++ b/includes/libs/rdbms/database/Database.php @@ -2019,9 +2019,19 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware if ( is_array( $table ) ) { // A parenthesized group - $joinedTable = '(' - . $this->tableNamesWithIndexClauseOrJOIN( $table, $use_index, $ignore_index, $join_conds ) - . ')'; + if ( count( $table ) > 1 ) { + $joinedTable = '(' + . $this->tableNamesWithIndexClauseOrJOIN( $table, $use_index, $ignore_index, $join_conds ) + . ')'; + } else { + // Degenerate case + $innerTable = reset( $table ); + $innerAlias = key( $table ); + $joinedTable = $this->tableNameWithAlias( + $innerTable, + is_string( $innerAlias ) ? $innerAlias : $innerTable + ); + } } else { $joinedTable = $this->tableNameWithAlias( $table, $alias ); } diff --git a/tests/phpunit/includes/libs/rdbms/database/DatabaseTest.php b/tests/phpunit/includes/libs/rdbms/database/DatabaseTest.php index ee7ad2f2a2..7933f19f49 100644 --- a/tests/phpunit/includes/libs/rdbms/database/DatabaseTest.php +++ b/tests/phpunit/includes/libs/rdbms/database/DatabaseTest.php @@ -120,6 +120,13 @@ class DatabaseTest extends PHPUnit_Framework_TestCase { ], 'table1 LEFT JOIN (table2 JOIN table3 ON ((t2_id = t3_id))) ON ((t1_id = t2_id))' ], + 'join with degenerate parenthesized group' => [ + [ 'table1', 'n' => [ 't2' => 'table2' ] ], + [ + 'n' => [ 'LEFT JOIN', 't1_id = t2_id' ], + ], + 'table1 LEFT JOIN table2 t2 ON ((t1_id = t2_id))' + ], ]; } -- 2.20.1