MDL-17645 delete_records_list - Tidied a bit return values. Fix behaviour when no values are passed + new dml tests for all *_list() functions.
This commit is contained in:
@@ -769,6 +769,10 @@ abstract class moodle_database {
|
||||
*/
|
||||
public function get_recordset_list($table, $field, array $values, $sort='', $fields='*', $limitfrom=0, $limitnum=0) {
|
||||
list($select, $params) = $this->where_clause_list($field, $values);
|
||||
if (empty($select)) {
|
||||
$select = '? = ?'; /// Fake condition, won't return rows ever. MDL-17645
|
||||
$params = array(1, 2);
|
||||
}
|
||||
return $this->get_recordset_select($table, $select, $params, $sort, $fields, $limitfrom, $limitnum);
|
||||
}
|
||||
|
||||
@@ -1360,11 +1364,11 @@ abstract class moodle_database {
|
||||
* @return bool true.
|
||||
* @throws dml_exception if error
|
||||
*/
|
||||
public function delete_records_list($table, $field, array $values, $sort='', $fields='*', $limitfrom=0, $limitnum=0) {
|
||||
public function delete_records_list($table, $field, array $values) {
|
||||
list($select, $params) = $this->where_clause_list($field, $values);
|
||||
if (empty($select)) {
|
||||
// nothing to delete
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
return $this->delete_records_select($table, $select, $params);
|
||||
}
|
||||
|
||||
@@ -597,6 +597,16 @@ class dml_test extends UnitTestCase {
|
||||
$counter++;
|
||||
}
|
||||
$this->assertEqual(3, $counter);
|
||||
$rs->close();
|
||||
|
||||
$rs = $DB->get_recordset_list($tablename, 'course',array()); /// Must return 0 rows without conditions. MDL-17645
|
||||
|
||||
$counter = 0;
|
||||
foreach ($rs as $record) {
|
||||
$counter++;
|
||||
}
|
||||
$this->assertEqual(0, $counter);
|
||||
$rs->close();
|
||||
}
|
||||
|
||||
public function test_get_recordset_select() {
|
||||
@@ -747,6 +757,9 @@ class dml_test extends UnitTestCase {
|
||||
$this->assertEqual(2, next($records)->id);
|
||||
$this->assertEqual(4, next($records)->id);
|
||||
|
||||
$this->assertIdentical(array(), $records = $DB->get_records_list($tablename, 'course', array())); /// Must return 0 rows without conditions. MDL-17645
|
||||
$this->assertEqual(0, count($records));
|
||||
|
||||
}
|
||||
|
||||
public function test_get_records_sql() {
|
||||
@@ -1488,6 +1501,30 @@ class dml_test extends UnitTestCase {
|
||||
$this->assertEqual(1, $DB->count_records($tablename));
|
||||
}
|
||||
|
||||
public function test_delete_records_list() {
|
||||
$DB = $this->tdb;
|
||||
$dbman = $DB->get_manager();
|
||||
|
||||
$table = $this->get_test_table();
|
||||
$tablename = $table->getName();
|
||||
|
||||
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
|
||||
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
|
||||
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
|
||||
$dbman->create_table($table);
|
||||
$this->tables[$tablename] = $table;
|
||||
|
||||
$DB->insert_record($tablename, array('course' => 1));
|
||||
$DB->insert_record($tablename, array('course' => 2));
|
||||
$DB->insert_record($tablename, array('course' => 3));
|
||||
|
||||
$this->assertTrue($DB->delete_records_list($tablename, 'course', array(2, 3)));
|
||||
$this->assertEqual(1, $DB->count_records($tablename));
|
||||
|
||||
$this->assertTrue($DB->delete_records_list($tablename, 'course', array())); /// Must delete 0 rows without conditions. MDL-17645
|
||||
$this->assertEqual(1, $DB->count_records($tablename));
|
||||
}
|
||||
|
||||
function test_sql_null_from_clause() {
|
||||
$DB = $this->tdb;
|
||||
$sql = "SELECT 1 AS id ".$DB->sql_null_from_clause();
|
||||
|
||||
Reference in New Issue
Block a user