diff --git a/lib/dml/moodle_database.php b/lib/dml/moodle_database.php index 5779592275bb4..67afc02cd7615 100644 --- a/lib/dml/moodle_database.php +++ b/lib/dml/moodle_database.php @@ -368,6 +368,32 @@ protected function where_clause(array $conditions=null) { return array($where, $params); } + /** + * Returns SQL WHERE conditions for the ..._list methods. + * + * @param string $field the name of a field. + * @param array $values the values field might take. + * @return array sql part and params + */ + protected function where_clause_list($field, array $values) { + $params = array(); + $select = array(); + $values = (array)$values; + foreach ($values as $value) { + if (is_bool($value)) { + $value = (int)$value; + } + if (is_null($value)) { + $select[] = "$field IS NULL"; + } else { + $select[] = "$field = ?"; + $params[] = $value; + } + } + $select = implode(" OR ", $select); + return array($select, $params); + } + /** * Constructs IN() or = sql fragment * @param mixed $items single or array of values @@ -742,20 +768,7 @@ public function get_recordset($table, array $conditions=null, $sort='', $fields= * @throws dml_exception if error */ public function get_recordset_list($table, $field, array $values, $sort='', $fields='*', $limitfrom=0, $limitnum=0) { - $params = array(); - $select = array(); - foreach ($values as $value) { - if (is_bool($value)) { - $value = (int)$value; - } - if (is_null($value)) { - $select[] = "$field IS NULL"; - } else { - $select[] = "$field = ?"; - $params[] = $value; - } - } - $select = implode(" OR ", $select); + list($select, $params) = $this->where_clause_list($field, $values); return $this->get_recordset_select($table, $select, $params, $sort, $fields, $limitfrom, $limitnum); } @@ -847,25 +860,11 @@ public function get_records($table, array $conditions=null, $sort='', $fields='* * @throws dml_exception if error */ public function get_records_list($table, $field, array $values, $sort='', $fields='*', $limitfrom=0, $limitnum=0) { - $params = array(); - $select = array(); - $values = (array)$values; - foreach ($values as $value) { - if (is_bool($value)) { - $value = (int)$value; - } - if (is_null($value)) { - $select[] = "$field IS NULL"; - } else { - $select[] = "$field = ?"; - $params[] = $value; - } - } + list($select, $params) = $this->where_clause_list($field, $values); if (empty($select)) { // nothing to return return array(); } - $select = implode(" OR ", $select); return $this->get_records_select($table, $select, $params, $sort, $fields, $limitfrom, $limitnum); } @@ -1352,6 +1351,24 @@ public function delete_records($table, array $conditions=null) { return $this->delete_records_select($table, $select, $params); } + /** + * Delete the records from a table where one field match one list of values. + * + * @param string $table the table to delete from. + * @param string $field The field to search + * @param string $values array of values + * @return bool true. + * @throws dml_exception if error + */ + public function delete_records_list($table, $field, array $values, $sort='', $fields='*', $limitfrom=0, $limitnum=0) { + list($select, $params) = $this->where_clause_list($field, $values); + if (empty($select)) { + // nothing to delete + return; + } + return $this->delete_records_select($table, $select, $params); + } + /** * Delete one or more records from a table which match a particular WHERE clause. *