From 59e06474577f7b0dc8e73a0d19233b1955372ec9 Mon Sep 17 00:00:00 2001 From: umherirrender Date: Fri, 31 Aug 2012 20:12:19 +0200 Subject: [PATCH] Allow array condition for HAVING in database interface Current no use in core. Change-Id: I8ed86c8238146285dd1caed4d8586c877cbecf44 --- includes/db/Database.php | 9 ++++- tests/phpunit/includes/db/DatabaseSQLTest.php | 40 +++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/includes/db/Database.php b/includes/db/Database.php index 3354f984c3..a46f33d35d 100644 --- a/includes/db/Database.php +++ b/includes/db/Database.php @@ -1103,7 +1103,10 @@ abstract class DatabaseBase implements DatabaseType { } if ( isset( $options['HAVING'] ) ) { - $preLimitTail .= " HAVING {$options['HAVING']}"; + $having = is_array( $options['HAVING'] ) + ? $this->makeList( $options['HAVING'], LIST_AND ) + : $options['HAVING']; + $preLimitTail .= " HAVING {$having}"; } if ( isset( $options['ORDER BY'] ) ) { @@ -1264,7 +1267,9 @@ abstract class DatabaseBase implements DatabaseType { * - GROUP BY: May be either an SQL fragment string naming a field or * expression to group by, or an array of such SQL fragments. * - * - HAVING: A string containing a HAVING clause. + * - HAVING: May be either an string containing a HAVING clause or an array of + * conditions building the HAVING clause. If an array is given, the conditions + * constructed from each element are combined with AND. * * - ORDER BY: May be either an SQL fragment giving a field name or * expression to order by, or an array of such SQL fragments. diff --git a/tests/phpunit/includes/db/DatabaseSQLTest.php b/tests/phpunit/includes/db/DatabaseSQLTest.php index 0df5a46080..e37cd445fa 100644 --- a/tests/phpunit/includes/db/DatabaseSQLTest.php +++ b/tests/phpunit/includes/db/DatabaseSQLTest.php @@ -70,6 +70,38 @@ class DatabaseSQLTest extends MediaWikiTestCase { "ORDER BY field " . "LIMIT 1" ), + array( + array( + 'tables' => array( 'table', 't2' => 'table2' ), + 'fields' => array( 'tid', 'field', 'alias' => 'field2', 't2.id' ), + 'conds' => array( 'alias' => 'text' ), + 'options' => array( 'LIMIT' => 1, 'GROUP BY' => 'field', 'HAVING' => 'COUNT(*) > 1' ), + 'join_conds' => array( 't2' => array( + 'LEFT JOIN', 'tid = t2.id' + )), + ), + "SELECT tid,field,field2 AS alias,t2.id " . + "FROM `unittest_table` LEFT JOIN `unittest_table2` `t2` ON ((tid = t2.id)) " . + "WHERE alias = 'text' " . + "GROUP BY field HAVING COUNT(*) > 1 " . + "LIMIT 1" + ), + array( + array( + 'tables' => array( 'table', 't2' => 'table2' ), + 'fields' => array( 'tid', 'field', 'alias' => 'field2', 't2.id' ), + 'conds' => array( 'alias' => 'text' ), + 'options' => array( 'LIMIT' => 1, 'GROUP BY' => array( 'field', 'field2' ), 'HAVING' => array( 'COUNT(*) > 1', 'field' => 1 ) ), + 'join_conds' => array( 't2' => array( + 'LEFT JOIN', 'tid = t2.id' + )), + ), + "SELECT tid,field,field2 AS alias,t2.id " . + "FROM `unittest_table` LEFT JOIN `unittest_table2` `t2` ON ((tid = t2.id)) " . + "WHERE alias = 'text' " . + "GROUP BY field,field2 HAVING (COUNT(*) > 1) AND field = '1' " . + "LIMIT 1" + ), ); } @@ -94,6 +126,14 @@ class DatabaseSQLTest extends MediaWikiTestCase { ), "(CASE WHEN field = 'text' THEN 1 ELSE NULL END)" ), + array( + array( + 'conds' => array( 'field' => 'text', 'field2' => 'anothertext' ), + 'true' => 1, + 'false' => 'NULL', + ), + "(CASE WHEN field = 'text' AND field2 = 'anothertext' THEN 1 ELSE NULL END)" + ), array( array( 'conds' => 'field=1', -- 2.20.1