### Eclipse Workspace Patch 1.0 #P Test Drupal 6 Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.678 diff -u -r1.678 common.inc --- includes/common.inc 23 Aug 2007 16:41:19 -0000 1.678 +++ includes/common.inc 24 Aug 2007 01:27:14 -0000 @@ -2953,6 +2953,130 @@ } } + +/** + * Save a record to the database based upon the schema. Default values are + * filled in for missing items, and 'serial' (auto increment) types are + * filled in with IDs. + * + * @param $table + * The name of the table; this must exist in schema API. + * @param $object + * The object to write. This is a reference, as defaults according to + * the schema may be filled in on the object, as well as ID on the serial + * type(s). + * @param $update + * TRUE if this is an update, FALSE if this is an insert. It is the + * caller's responsibility to know if a record for this object already + * exists in the database. + */ +function drupal_write_schema_record($table, &$object, $update = NULL) { + $schema = drupal_get_schema($table); + if (empty($schema)) { + return FALSE; + } + + $fields = $defs = $values = $serials = array(); + + // Go through our schema, build correlations, and fill in defaults for + // fields that are not set. + foreach ($schema['fields'] as $field => $info) { + if (!isset($object->$field)) { + $object->$field = isset($info['default']) ? $info['default'] : ''; + } + // special case -- skip serial types if we are updating. + if ($info['type'] == 'serial' && $update) { + continue; + } + + // Based on the field type, figure out what db_query substitution to use. + $fields[] = $field; + switch ($info['type']) { + case 'serial': + $defs[] = '%s'; + $object->$field = 'NULL'; + $serials[] = $field; + break; + case 'int': + $defs[] = '%d'; + break; + case 'float': + case 'numeric': + $defs[] = '%f'; + break; + case 'blob': + $defs[] = '%b'; + break; + default: + $defs[] = "'%s'"; + } + if (empty($info['serialize'])) { + $values[] = $object->$field; + } + else { + $values[] = serialize($object->$field); + } + } + + // Build the query string. + $query = ''; + if (!$update) { + $query = "INSERT INTO {$table} (" . implode(', ', $fields) . ') VALUES (' . implode(', ', $defs) . ')'; + } + else { + $query = ''; + foreach ($fields as $id => $field) { + if ($query) { + $query .= ', '; + } + $query .= $field . ' = ' . $defs[$id]; + } + $query = "UPDATE {$table} SET " . $query . " WHERE $update = " . $object->$update; + } + db_query($query, $values); + + if ($serials) { + // get last insert ids and fill them in. + foreach ($serials as $field) { + $object->$field = db_last_insert_id($table, $field); + } + } + return TRUE; +} + +/** + * Unpack items loaded from the database onto an object based upon data + * from the schema API. + * + * @param $table + * The name of the table; this must exist in schema API. + * @param $data + * Data loaded from the a db_query via db_fetch_object. + * @param $object + * The object load items onto. + */ +function drupal_unpack_schema_record($table, $data, $object = NULL) { + if (!isset($object)) { + $object = new stdClass(); + } + + $schema = drupal_get_schema($table); + // Go through our schema and build correlations. + + foreach ($schema['fields'] as $field => $info) { + if (!isset($data->$field)) { + $object->$field = isset($info['default']) ? $info['default'] : ''; + } + else { + $object->$field = $data->$field; + } + if (!empty($info['serialize'])) { + $object->$field = unserialize($object->field); + } + } + return $object; +} + /** * @} End of "ingroup schemaapi". */