Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.681 diff -u -F^f -r1.681 common.inc --- includes/common.inc 29 Aug 2007 18:38:55 -0000 1.681 +++ includes/common.inc 30 Aug 2007 00:10:11 -0000 @@ -2973,6 +2973,130 @@ function _drupal_initialize_schema($modu } } + +/** + * 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 + * Set this to the primary key's field name 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_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_read_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". */ Index: modules/node/node.module =================================================================== RCS file: /cvs/drupal/drupal/modules/node/node.module,v retrieving revision 1.875 diff -u -F^f -r1.875 node.module --- modules/node/node.module 26 Aug 2007 14:15:51 -0000 1.875 +++ modules/node/node.module 30 Aug 2007 00:10:11 -0000 @@ -790,17 +790,8 @@ function node_save(&$node) { // Insert a new node. $node->is_new = TRUE; } - else { - // We need to ensure that all node fields are filled. - $node_current = node_load($node->nid); - foreach ($node as $field => $data) { - $node_current->$field = $data; - } - $node = $node_current; - - if (!empty($node->revision)) { - $node->old_vid = $node->vid; - } + elseif (!empty($node->revision)) { + $node->old_vid = $node->vid; } // Set some required fields: @@ -809,16 +800,11 @@ function node_save(&$node) { } // The changed timestamp is always updated for bookkeeping purposes (revisions, searching, ...) $node->changed = time(); - - // Split off revisions data to another structure - $revisions_table_values = array('nid' => &$node->nid, - 'title' => $node->title, 'body' => isset($node->body) ? $node->body : '', - 'teaser' => $node->teaser, 'timestamp' => $node->changed, - 'uid' => $user->uid, 'format' => isset($node->format) ? $node->format : FILTER_FORMAT_DEFAULT); - $revisions_table_types = array('nid' => '%d', - 'title' => "'%s'", 'body' => "'%s'", - 'teaser' => "'%s'", 'timestamp' => '%d', - 'uid' => '%d', 'format' => '%d'); + $node->timestamp = time(); + + $node->format = isset($node->format) ? $node->format : FILTER_FORMAT_DEFAULT; + + // TODO: problem if (!empty($node->log) || $node->is_new || (isset($node->revision) && $node->revision)) { // Only store the log message if there's something to store; this prevents // existing log messages from being unintentionally overwritten by a blank @@ -826,49 +812,24 @@ function node_save(&$node) { $revisions_table_values['log'] = $node->log; $revisions_table_types['log'] = "'%s'"; } - $node_table_values = array( - 'title' => $node->title, 'type' => $node->type, 'uid' => $node->uid, - 'status' => $node->status, 'language' => $node->language, 'created' => $node->created, - 'changed' => $node->changed, 'comment' => $node->comment, - 'promote' => $node->promote, 'sticky' => $node->sticky); - $node_table_types = array( - 'title' => "'%s'", 'type' => "'%s'", 'uid' => '%d', - 'status' => '%d', 'language' => "'%s'",'created' => '%d', - 'changed' => '%d', 'comment' => '%d', - 'promote' => '%d', 'sticky' => '%d'); + $update_node = TRUE; - //Generate the node table query and the - //the node_revisions table query + if ($node->is_new) { - $node_query = 'INSERT INTO {node} ('. implode(', ', array_keys($node_table_types)) .') VALUES ('. implode(', ', $node_table_types) .')'; - db_query($node_query, $node_table_values); + drupal_write_record('node', $node, FALSE); $node->nid = db_last_insert_id('node', 'nid'); - $revisions_query = 'INSERT INTO {node_revisions} ('. implode(', ', array_keys($revisions_table_types)) .') VALUES ('. implode(', ', $revisions_table_types) .')'; - db_query($revisions_query, $revisions_table_values); + drupal_write_record('node_revisions', $node, FALSE); $node->vid = db_last_insert_id('node_revisions', 'vid'); $op = 'insert'; } else { - $arr = array(); - foreach ($node_table_types as $key => $value) { - $arr[] = $key .' = '. $value; - } - $node_table_values[] = $node->nid; - $node_query = 'UPDATE {node} SET '. implode(', ', $arr) .' WHERE nid = %d'; - db_query($node_query, $node_table_values); + drupal_write_record('node', $node, 'nid'); if (!empty($node->revision)) { - $revisions_query = 'INSERT INTO {node_revisions} ('. implode(', ', array_keys($revisions_table_types)) .') VALUES ('. implode(', ', $revisions_table_types) .')'; - db_query($revisions_query, $revisions_table_values); + drupal_write_record('node_revisions', $node, FALSE); $node->vid = db_last_insert_id('node_revisions', 'vid'); } else { - $arr = array(); - foreach ($revisions_table_types as $key => $value) { - $arr[] = $key .' = '. $value; - } - $revisions_table_values[] = $node->vid; - $revisions_query = 'UPDATE {node_revisions} SET '. implode(', ', $arr) .' WHERE vid = %d'; - db_query($revisions_query, $revisions_table_values); + drupal_write_record('node_revisions', $node, 'vid'); $update_node = FALSE; } $op = 'update';