From 7a5f4b431da693ab14e16e77e603d08034cd3fac Mon Sep 17 00:00:00 2001 From: umherirrender Date: Sat, 25 Aug 2012 20:24:59 +0200 Subject: [PATCH] Allow array condition for Database::conditional Change the one use of that method to use the new syntax. Add some tests. Change-Id: I9ad4e5b5e97d1d13cfd858105167ca5d20ffaf83 --- includes/Category.php | 4 +- includes/db/Database.php | 5 ++- tests/phpunit/includes/db/DatabaseSQLTest.php | 38 +++++++++++++++++-- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/includes/Category.php b/includes/Category.php index d9ca234ef8..b7b12e8a31 100644 --- a/includes/Category.php +++ b/includes/Category.php @@ -297,8 +297,8 @@ class Category { 'IGNORE' ); - $cond1 = $dbw->conditional( 'page_namespace=' . NS_CATEGORY, 1, 'NULL' ); - $cond2 = $dbw->conditional( 'page_namespace=' . NS_FILE, 1, 'NULL' ); + $cond1 = $dbw->conditional( array( 'page_namespace' => NS_CATEGORY ), 1, 'NULL' ); + $cond2 = $dbw->conditional( array( 'page_namespace' => NS_FILE ), 1, 'NULL' ); $result = $dbw->selectRow( array( 'categorylinks', 'page' ), array( 'pages' => 'COUNT(*)', diff --git a/includes/db/Database.php b/includes/db/Database.php index 61061b2012..ae5335b63a 100644 --- a/includes/db/Database.php +++ b/includes/db/Database.php @@ -2621,12 +2621,15 @@ abstract class DatabaseBase implements DatabaseType { * Returns an SQL expression for a simple conditional. This doesn't need * to be overridden unless CASE isn't supported in your DBMS. * - * @param $cond String: SQL expression which will result in a boolean value + * @param $cond string|array SQL expression which will result in a boolean value * @param $trueVal String: SQL expression to return if true * @param $falseVal String: SQL expression to return if false * @return String: SQL fragment */ public function conditional( $cond, $trueVal, $falseVal ) { + if ( is_array( $cond ) ) { + $cond = $this->makeList( $cond, LIST_AND ); + } return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) "; } diff --git a/tests/phpunit/includes/db/DatabaseSQLTest.php b/tests/phpunit/includes/db/DatabaseSQLTest.php index d56e632c30..0df5a46080 100644 --- a/tests/phpunit/includes/db/DatabaseSQLTest.php +++ b/tests/phpunit/includes/db/DatabaseSQLTest.php @@ -16,9 +16,9 @@ class DatabaseSQLTest extends MediaWikiTestCase { } /** - * @dataProvider dataSQL + * @dataProvider dataSelectSQLText */ - function testSQL( $sql, $sqlText ) { + function testSelectSQLText( $sql, $sqlText ) { $this->assertEquals( trim( $this->db->selectSQLText( isset( $sql['tables'] ) ? $sql['tables'] : array(), isset( $sql['fields'] ) ? $sql['fields'] : array(), @@ -29,7 +29,7 @@ class DatabaseSQLTest extends MediaWikiTestCase { ) ), $sqlText ); } - function dataSQL() { + function dataSelectSQLText() { return array( array( array( @@ -72,4 +72,36 @@ class DatabaseSQLTest extends MediaWikiTestCase { ), ); } + + /** + * @dataProvider dataConditional + */ + function testConditional( $sql, $sqlText ) { + $this->assertEquals( trim( $this->db->conditional( + $sql['conds'], + $sql['true'], + $sql['false'] + ) ), $sqlText ); + } + + function dataConditional() { + return array( + array( + array( + 'conds' => array( 'field' => 'text' ), + 'true' => 1, + 'false' => 'NULL', + ), + "(CASE WHEN field = 'text' THEN 1 ELSE NULL END)" + ), + array( + array( + 'conds' => 'field=1', + 'true' => 1, + 'false' => 'NULL', + ), + "(CASE WHEN field=1 THEN 1 ELSE NULL END)" + ), + ); + } } \ No newline at end of file -- 2.20.1