]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-17645 delete_records_list - Tidied a bit return values. Fix behaviour when no...
authorstronk7 <stronk7>
Mon, 15 Dec 2008 21:41:09 +0000 (21:41 +0000)
committerstronk7 <stronk7>
Mon, 15 Dec 2008 21:41:09 +0000 (21:41 +0000)
lib/dml/moodle_database.php
lib/dml/simpletest/testdml.php

index 67afc02cd76154a7835d46a7f7dc797dc106956d..658de1c41250621085766a40c84862bc6adf5245 100644 (file)
@@ -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);
     }
index fcdd17f4232ef2118cd6609923f54f76f2cf648b..b6d64b9a065a43fc0d9ed05eea75f6555e659397 100755 (executable)
@@ -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();