Change the one use of that method to use the new syntax.
Add some tests.
Change-Id: I9ad4e5b5e97d1d13cfd858105167ca5d20ffaf83
'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(*)',
* 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) ";
}
}
/**
- * @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(),
) ), $sqlText );
}
- function dataSQL() {
+ function dataSelectSQLText() {
return array(
array(
array(
),
);
}
+
+ /**
+ * @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