summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Starling <tstarling@wikimedia.org>2022-01-13 10:19:53 +1100
committerTim Starling <tstarling@wikimedia.org>2022-01-12 23:22:53 +0000
commitef46a9557e37d242f32fe33a335c2816373cca8f (patch)
treef77231da81b6f0f26e03bce13d2c4528e6be4bb2
parent7ba095ffa6eceb0b82e03c082932879c16fb9bcd (diff)
Database::factorConds(): fix insufficient parenthesization
Bug: T299095 Change-Id: I9746e69b7b4f79d0afb9b965da43b9ae36c6afe4
-rw-r--r--includes/libs/rdbms/database/Database.php9
-rw-r--r--tests/phpunit/unit/includes/libs/rdbms/database/DatabaseSQLTest.php10
2 files changed, 13 insertions, 6 deletions
diff --git a/includes/libs/rdbms/database/Database.php b/includes/libs/rdbms/database/Database.php
index eb7f7049db71..44e3374c6cd3 100644
--- a/includes/libs/rdbms/database/Database.php
+++ b/includes/libs/rdbms/database/Database.php
@@ -2798,10 +2798,12 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
}
}
+ $wrap = false;
$result = '';
foreach ( $expressionsByField1 as $value1 => $expressions ) {
if ( $result !== '' ) {
$result .= ' OR ';
+ $wrap = true;
}
$factored = $this->factorCondsWithCommonFields( $expressions );
$result .= "($field1 = " . $this->addQuotes( $value1 ) .
@@ -2811,10 +2813,15 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
$factored = $this->factorCondsWithCommonFields( $nullExpressions );
if ( $result !== '' ) {
$result .= ' OR ';
+ $wrap = true;
}
$result .= "($field1 IS NULL AND $factored)";
}
- return $result;
+ if ( $wrap ) {
+ return "($result)";
+ } else {
+ return $result;
+ }
}
/**
diff --git a/tests/phpunit/unit/includes/libs/rdbms/database/DatabaseSQLTest.php b/tests/phpunit/unit/includes/libs/rdbms/database/DatabaseSQLTest.php
index fc6695ae19d5..ac1c32fbdf97 100644
--- a/tests/phpunit/unit/includes/libs/rdbms/database/DatabaseSQLTest.php
+++ b/tests/phpunit/unit/includes/libs/rdbms/database/DatabaseSQLTest.php
@@ -1545,7 +1545,7 @@ class DatabaseSQLTest extends PHPUnit\Framework\TestCase {
if ( $expected === 'invalid' ) {
$this->expectException( InvalidArgumentException::class );
}
- $this->assertSame( $this->database->factorConds( $input ), $expected );
+ $this->assertSame( $expected, $this->database->factorConds( $input ) );
}
public static function provideFactorConds() {
@@ -1588,11 +1588,11 @@ class DatabaseSQLTest extends PHPUnit\Framework\TestCase {
],
[
[ [ 'a' => null, 'b' => 2 ], [ 'a' => 1, 'b' => 3 ] ],
- '(a = 1 AND b = 3) OR (a IS NULL AND b = 2)'
+ '((a = 1 AND b = 3) OR (a IS NULL AND b = 2))'
],
[
[ [ 'a' => 1, 'b' => 2 ], [ 'a' => 2, 'b' => 2 ] ],
- '(a = 1 AND b = 2) OR (a = 2 AND b = 2)'
+ '((a = 1 AND b = 2) OR (a = 2 AND b = 2))'
],
[
[
@@ -1605,8 +1605,8 @@ class DatabaseSQLTest extends PHPUnit\Framework\TestCase {
[ 'a' => 2, 'b' => 2, 'c' => 1 ],
[ 'a' => 2, 'b' => 2, 'c' => 2 ],
],
- '(a = 1 AND (b = 1 AND c IN (1,2) ) OR (b = 2 AND c IN (1,2) )) OR ' .
- '(a = 2 AND (b = 1 AND c IN (1,2) ) OR (b = 2 AND c IN (1,2) ))'
+ '((a = 1 AND ((b = 1 AND c IN (1,2) ) OR (b = 2 AND c IN (1,2) ))) OR ' .
+ '(a = 2 AND ((b = 1 AND c IN (1,2) ) OR (b = 2 AND c IN (1,2) ))))'
]
];
}