Skip to content

Commit

Permalink
dml: MDL-17645 New database method delete_records_list.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjhunt committed Dec 15, 2008
1 parent 464f3d8 commit 0e8e23c
Showing 1 changed file with 46 additions and 29 deletions.
75 changes: 46 additions & 29 deletions lib/dml/moodle_database.php
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 0e8e23c

Please sign in to comment.