MDL-26542 get_in_or_equal - adding $onemptyitems + tests

This commit is contained in:
Eloy Lafuente (stronk7)
2011-03-30 18:22:53 +02:00
parent dae6b38c51
commit c7ce62f9c8
2 changed files with 86 additions and 3 deletions
+15 -2
View File
@@ -569,12 +569,25 @@ abstract class moodle_database {
* @param int $type bound param type SQL_PARAMS_QM or SQL_PARAMS_NAMED
* @param string named param placeholder start
* @param bool true means equal, false not equal
* @param mixed $onemptyitems defines the behavior when the array of items is empty. Defaults to false,
* meaning throw exceptions. Other values will become part of the returned SQL fragment.
* @return array - $sql and $params
*/
public function get_in_or_equal($items, $type=SQL_PARAMS_QM, $start='param0000', $equal=true) {
if (is_array($items) and empty($items)) {
public function get_in_or_equal($items, $type=SQL_PARAMS_QM, $start='param0000', $equal=true, $onemptyitems=false) {
// default behavior, throw exception on empty array
if (is_array($items) and empty($items) and $onemptyitems === false) {
throw new coding_exception('moodle_database::get_in_or_equal() does not accept empty arrays');
}
// handle $onemptyitems on empty array of items
if (is_array($items) and empty($items)) {
if (is_null($onemptyitems)) { // Special case, NULL value
$sql = $equal ? ' IS NULL' : ' IS NOT NULL';
return (array($sql, array()));
} else {
$items = array($onemptyitems); // Rest of cases, prepare $items for std processing
}
}
if ($type == SQL_PARAMS_QM) {
if (!is_array($items) or count($items) == 1) {
$sql = $equal ? '= ?' : '<> ?';
+71 -1
View File
@@ -223,11 +223,81 @@ class dml_test extends UnitTestCase {
// Correct usage of single value
$in_value = 'value1';
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param01', false);
list($usql, $params) = $DB->get_in_or_equal($in_value, SQL_PARAMS_NAMED, 'param01', false);
$this->assertEqual("<> :param01", $usql);
$this->assertEqual(1, count($params));
$this->assertEqual($in_value, $params['param01']);
// Some incorrect tests
// Incorrect usage passing not-allowed params type
$in_values = array(1, 2, 3);
try {
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_DOLLAR, 'param01', false);
$this->fail('An Exception is missing, expected due to not supported SQL_PARAMS_DOLLAR');
} catch (exception $e) {
$this->assertTrue($e instanceof dml_exception);
$this->assertEqual($e->errorcode, 'typenotimplement');
}
// Incorrect usage passing empty array
$in_values = array();
try {
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param01', false);
$this->fail('An Exception is missing, expected due to empty array of items');
} catch (exception $e) {
$this->assertTrue($e instanceof coding_exception);
}
// Test using $onemptyitems
// Correct usage passing empty array and $onemptyitems = NULL (equal = true, QM)
$in_values = array();
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, 'param01', true, NULL);
$this->assertEqual(' IS NULL', $usql);
$this->assertIdentical(array(), $params);
// Correct usage passing empty array and $onemptyitems = NULL (equal = false, NAMED)
$in_values = array();
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param01', false, NULL);
$this->assertEqual(' IS NOT NULL', $usql);
$this->assertIdentical(array(), $params);
// Correct usage passing empty array and $onemptyitems = true (equal = true, QM)
$in_values = array();
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, 'param01', true, true);
$this->assertEqual('= ?', $usql);
$this->assertIdentical(array(true), $params);
// Correct usage passing empty array and $onemptyitems = true (equal = false, NAMED)
$in_values = array();
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param01', false, true);
$this->assertEqual('<> :param01', $usql);
$this->assertIdentical(array('param01' => true), $params);
// Correct usage passing empty array and $onemptyitems = -1 (equal = true, QM)
$in_values = array();
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, 'param01', true, -1);
$this->assertEqual('= ?', $usql);
$this->assertIdentical(array(-1), $params);
// Correct usage passing empty array and $onemptyitems = -1 (equal = false, NAMED)
$in_values = array();
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param01', false, -1);
$this->assertEqual('<> :param01', $usql);
$this->assertIdentical(array('param01' => -1), $params);
// Correct usage passing empty array and $onemptyitems = 'onevalue' (equal = true, QM)
$in_values = array();
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, 'param01', true, 'onevalue');
$this->assertEqual('= ?', $usql);
$this->assertIdentical(array('onevalue'), $params);
// Correct usage passing empty array and $onemptyitems = 'onevalue' (equal = false, NAMED)
$in_values = array();
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param01', false, 'onevalue');
$this->assertEqual('<> :param01', $usql);
$this->assertIdentical(array('param01' => 'onevalue'), $params);
}
public function test_fix_table_names() {