Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.683 diff -u -F^f -r1.683 common.inc --- includes/common.inc 6 Sep 2007 12:47:20 -0000 1.683 +++ includes/common.inc 10 Sep 2007 12:24:54 -0000 @@ -2980,6 +2980,126 @@ function _drupal_initialize_schema($modu } /** + * Retrieve a list of fields from a table schema. The list is suitable for use in a SQL query. + * + * @param $table + * The name of the table from which to retrieve fields. + * @param + * An optional prefix to to all fields. + * + * @return An array of fields. + **/ +function drupal_schema_fields_sql($table, $prefix = NULL) { + $schema = drupal_get_schema($table); + $fields = array_keys($schema['fields']); + if ($prefix) { + $columns = array(); + foreach ($fields as $field) { + $columns[] = "$prefix.$field"; + } + return $columns; + } + else { + return $fields; + } +} + +/** + * 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 + * If this is an update, specify the primary keys' field names. It is the + * caller's responsibility to know if a record for this object already + * exists in the database. If there is only 1 key, you may pass a simple string. + * @return (boolean) Failure to write a record will return FALSE. Otherwise, + * TRUE is returned. The $object parameter contains values for any serial + * fields defined by the $table. For example, $object->nid will be populated + * after inserting a new node. + */ +function drupal_write_record($table, &$object, $update = array()) { + // Standardize $update to an array. + if (is_string($update)) { + $update = array($update); + } + + $schema = drupal_get_schema($table); + if (empty($schema)) { + return FALSE; + } + + $fields = $defs = $values = $serials = array(); + + // Go through our schema, build correlations, and when inserting, fill in defaults for + // fields that are not set. + foreach ($schema['fields'] as $field => $info) { + // Special case -- skip serial types if we are updating. + if ($info['type'] == 'serial' && count($update)) { + continue; + } + + if (!isset($object->$field) && !count($update)) { + $object->$field = isset($info['default']) ? $info['default'] : ''; + } + + // Build arrays for the fields, placeholders, and values in our query. + if (isset($object->$field)) { + $fields[] = $field; + $placeholders[] = _db_type_placeholder($info['type']); + if ($info['type'] == 'serial') { + $object->$field = 'NULL'; + $serials[] = $field; + } + + if (empty($info['serialize'])) { + $values[] = $object->$field; + } + else { + $values[] = serialize($object->$field); + } + } + } + + // Build the SQL. + $query = ''; + if (!count($update)) { + $query = "INSERT INTO {$table} (" . implode(', ', $fields) . ') VALUES (' . implode(', ', $placeholders) . ')'; + } + else { + $query = ''; + foreach ($fields as $id => $field) { + if ($query) { + $query .= ', '; + } + $query .= $field . ' = ' . $placeholders[$id]; + } + + foreach ($update as $key){ + $cond[] = "$key = ". _db_type_placeholder($schema['fields'][$key]['type']); + $values[] = $object->$key; + } + + $query = "UPDATE {$table} SET $query WHERE ". implode(' AND ', $cond); + } + 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; +} + +/** * @} End of "ingroup schemaapi". */ Index: modules/node/node.module =================================================================== RCS file: /cvs/drupal/drupal/modules/node/node.module,v retrieving revision 1.879 diff -u -F^f -r1.879 node.module --- modules/node/node.module 5 Sep 2007 08:42:01 -0000 1.879 +++ modules/node/node.module 10 Sep 2007 12:24:55 -0000 @@ -652,14 +652,20 @@ function node_load($param = array(), $re return FALSE; } + // Retrieve a field list based on the site's schema. + $fields = drupal_schema_fields_sql('node', 'n'); + $fields = array_merge($fields, drupal_schema_fields_sql('node_revisions', 'r')); + $fields = array_merge($fields, array('u.name', 'u.data')); + $fields = implode(', ', $fields); + // Retrieve the node. // No db_rewrite_sql is applied so as to get complete indexing for search. if ($revision) { array_unshift($arguments, $revision); - $node = db_fetch_object(db_query('SELECT n.nid, r.vid, n.type, n.status, n.language, n.created, n.changed, n.comment, n.promote, n.sticky, n.tnid, n.translate, r.timestamp AS revision_timestamp, r.title, r.body, r.teaser, r.log, r.format, u.uid, u.name, u.picture, u.data FROM {node} n INNER JOIN {users} u ON u.uid = n.uid INNER JOIN {node_revisions} r ON r.nid = n.nid AND r.vid = %d WHERE '. $cond, $arguments)); + $node = db_fetch_object(db_query('SELECT '. $fields. ' FROM {node} n INNER JOIN {users} u ON u.uid = n.uid INNER JOIN {node_revisions} r ON r.nid = n.nid AND r.vid = %d WHERE '. $cond, $arguments)); } else { - $node = db_fetch_object(db_query('SELECT n.nid, n.vid, n.type, n.status, n.language, n.created, n.changed, n.comment, n.promote, n.sticky, n.tnid, n.translate, r.timestamp AS revision_timestamp, r.title, r.body, r.teaser, r.log, r.format, u.uid, u.name, u.picture, u.data FROM {node} n INNER JOIN {users} u ON u.uid = n.uid INNER JOIN {node_revisions} r ON r.vid = n.vid WHERE '. $cond, $arguments)); + $node = db_fetch_object(db_query('SELECT '. $fields. ' FROM {node} n INNER JOIN {users} u ON u.uid = n.uid INNER JOIN {node_revisions} r ON r.vid = n.vid WHERE '. $cond, $arguments)); } if ($node && $node->nid) { @@ -757,7 +763,7 @@ function node_submit($node) { $node->created = $node->date ? strtotime($node->date) : NULL; } - + $node->validated = TRUE; return $node; @@ -778,17 +784,12 @@ function node_save(&$node) { // Insert a new node. $node->is_new = TRUE; } + elseif (!empty($node->revision)) { + $node->old_vid = $node->vid; + } 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; - } + // When updating a node, avoid overwriting an existing log entry when we don't save a new revision. + unset($node->log); } // Set some required fields: @@ -798,65 +799,22 @@ 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'); - 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 - // message. A new revision will have an empty log message (or $node->log). - $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'); + $node->timestamp = time(); + $node->format = isset($node->format) ? $node->format : FILTER_FORMAT_DEFAULT; $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); - $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); - $node->vid = db_last_insert_id('node_revisions', 'vid'); + drupal_write_record('node', $node); + drupal_write_record('node_revisions', $node); $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, array('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); - $node->vid = db_last_insert_id('node_revisions', 'vid'); + drupal_write_record('node_revisions', $node); } 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, array('vid')); $update_node = FALSE; } $op = 'update';