Merge "Allow array condition for Database::conditional"
authorAaron Schulz <aschulz@wikimedia.org>
Tue, 28 Aug 2012 00:01:05 +0000 (00:01 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 28 Aug 2012 00:01:05 +0000 (00:01 +0000)
includes/Category.php
includes/db/Database.php
tests/phpunit/includes/db/DatabaseSQLTest.php

index d9ca234..b7b12e8 100644 (file)
@@ -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(*)',
index 61061b2..ae5335b 100644 (file)
@@ -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) ";
        }
 
index d56e632..0df5a46 100644 (file)
@@ -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