$value) { switch (gettype($value)) { case 'integer': $escape = '%d'; break; case 'double': $escape = '%f'; break; case 'string': $escape = "'%s'"; break; case 'NULL': $escape = 'NULL'; break; default: continue; } $flat_fields[] = $field .'='. $escape; } list($where_string, $where_values) = db_where_clause($where, $where_type); $sql = 'UPDATE '. $table .' SET '. implode(',', $flat_fields) . $where_string; return db_query($sql, array_merge($update_values, $where_values)); } /** * Run a delete query on the active database. * * @param $table * The database table on which to run the delete query. * @param $where * The where rules for this delete query. * @param $where_type * Whether to AND or OR the where rules together. * @return * A database query result resource, or FALSE if the query was not * executed correctly. * */ function db_delete($table, $where, $where_type='AND') { list($where_string, $where_values) = db_where_clause($where, $where_type); $sql = 'DELETE FROM '. $table . $where_string; return db_query($sql, array_merge($where_string, $where_values)); } /** * Build the WHERE portion of an SQL query, based on the specified values. * * @param $where * Associative array of rules in the WHERE clause. If a key in the array * is numeric, the value is taken as a literal rule. If it is non-numeric, * then it is assumed to be a field name and the corresponding value is the * value that it must hold. * @param $where_type * Whether the values of the WHERE clause should be ANDed or ORed together. * * As an example, this $where clause would be translated as follows: * $where = array('name'=>'foo', 'type'=>'page', 'created < 1147567877') * * WHERE (name='foo') AND ('type'='page') AND (created < 1147567877') * @return * An array containing the where clause with sprintf() markers, and * an array of values to substitute for them. */ function db_where_clause($where, $where_type='AND', $where_keyword=TRUE) { $params = array(); $args = array(); foreach ($where as $key => $value) { if (is_numeric($key)) { $params[] = ' ('. $value .') '; } else { switch (gettype($value)) { case 'double': $escape = '%f'; break; case 'integer': $escape = '%d'; break; case 'string': $escape = "'%s'"; break; case 'NULL': $escape = 'NULL'; break; default: continue; } $params[] = ' ('. $key .'='. $escape .') '; $args[] = $value; } } $return = ''; if (sizeof($params)) { $return = ($where_keyword ? ' WHERE ' : ' ') . implode($where_type, $params); } return array($return, $args); } /** * Returns an array with all objects from a query * Does not do a rewrite sql for you! * Call similar to db_query. */ function db_fetch_all_as_objects($query) { $results = array(); $args = func_get_args(); array_shift($args); $res = db_query($query, $args); while ($row = db_fetch_object($res)) { $results[] = $row; } return $results; } /** * Returns an array with all arrays from a query * Does not do a rewrite sql for you! * Call similar to db_query. */ function db_fetch_all_as_arrays($query) { $results = array(); $args = func_get_args(); array_shift($args); $res = db_query($query, $args); while ($row = db_fetch_array($res)) { $results[] = $row; } return $results; } /** * Core functions. */ /** * Implementation of hook_help(). */ function helpers_database_help($path, $args) { $output = null; switch ($path) { case 'admin/help#helpers_database': $output .= t('A development library for the database. Contains useful functions and methods for database queries.'); } return $output; }