Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.864 diff -u -p -r1.864 common.inc --- includes/common.inc 5 Feb 2009 01:21:16 -0000 1.864 +++ includes/common.inc 5 Feb 2009 02:48:39 -0000 @@ -3965,6 +3965,139 @@ function drupal_write_record($table, &$o } /** + * Load a record from the database, consulting the schema if necessary. + * + * @param $table + * The name of the table; this must exist in schema API. + * @param $alias + * An alias for the table. + * @param $conditions + * An ID or array of conditions to apply to the query. + * @param $fields + * Array: the names of the fields to be returned. If empty, all fields will be + * returned. + * @param $alter + * Boolean, whether to pass the results through drupal_alter(). + * @return + * An array of all matching records, or false on failure. + */ +function drupal_load_record($table, $alias = NULL, $conditions = array(), $fields = NULL, $alter = FALSE, $unserialize = FALSE) { + $records = drupal_load_records($table, $alias, $conditions, $fields, $alter, $unserialize); + if (!empty($records)) { + return $records[0]; + } + else { + return FALSE; + } +} + +/** + * Load one or more records from the database, consulting the schema if necessary. + * + * @param $table + * The name of the table; this must exist in schema API. + * @param $alias + * An alias for the table. + * @param $conditions + * An array of conditions to apply to the query. + * @param $fields + * Array: the names of the fields to be returned. If empty, all fields will be + * returned. + * @param $alter + * Boolean, whether to pass the results through drupal_alter(). + * @return + * An array of all matching records, or false on failure. + */ +function drupal_load_records($table, $alias = NULL, $conditions = array(), $fields = array(), $alter = FALSE, $unserialize = FALSE) { + + // If no alias was specified, use the full table name. + if (empty($alias)) { + $alias = $table; + } + + // We need schema information if loading by numerical ID or if unserializing + // unserializing without a list of fields to unserialize. + if ((!empty($unserialize) && !is_array($unserialize)) || is_numeric($conditions) || is_numeric(key($conditions))) { + $schema = drupal_get_schema($table); + if (empty($schema)) { + return FALSE; + } + // Accept a numeric ID key or an array of IDs. + if (is_numeric($conditions) || is_numeric(key($conditions))) { + $primary_keys = $schema['primary key']; + if (count($primary_keys) > 1) { + return FALSE; + } + $conditions = array($primary_keys[0], $conditions); + } + } + $query = db_select($table, $alias); + $query->fields($alias, $fields); + foreach ($conditions as $field => $value) { + $query->condition($field, $value, is_array($value) ? 'IN' : '='); + } + + $result = $query->execute()->fetchAll(); + if (empty($result)) { + return FALSE; + } + foreach (array_keys($result) as $key) { + // Send the results for altering if requested. + if ($alter) { + drupal_alter('drupal_read_records', $result[$key], $table); + } + if (!empty($unserialize)) { + // Iterate through result records. + foreach ($result[$key] as $field => $value) { + // If required, unserialize results. + if (in_array($field, $unserialize) || ($unserialize == TRUE && isset($schema['fields'][$field]) && !empty($schema['fields'][$field]['serialize']))) { + $result[$key]->$field = unserialize($value); + } + } + } + } + + return $result; +} + +/** + * Delete one or more records from the database, consulting the schema if + * necessary. + * + * @param $table + * The name of the table. + * @param $conditions + * The conditions to match for deletion. If an integer or array of integers is + * given, these are treated as primary key values with the primary key being + * determined from the schema. Matching criteria may also be fed as an array + * of key-value pairs keyed by field name, in which case the schema is not + * consulted. + * + * @return + * Failure to delete based on missing schema information will return FALSE. + * Otherwise SAVED_DELETED. + */ +function drupal_delete_records($table, $conditions) { + if (is_numeric($conditions) || is_numeric(key($conditions))) { + $schema = drupal_get_schema($table); + if (empty($schema)) { + return FALSE; + } + if (count($schema['primary keys']) > 1) { + return FALSE; + } + $primary_key = current($schema['primary keys']); + $conditions = array($primary_key => $value); + } + $query = db_delete($table); + foreach ($conditions as $field => $value) { + $query->condition($field, $value, is_array($value) ? 'IN' : '='); + } + + $query->execute(); +} + +/** * @} End of "ingroup schemaapi". */