Index: drupal-6.x-dev/includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.704 diff -u -p -r1.704 common.inc --- drupal-6.x-dev/includes/common.inc 19 Oct 2007 10:30:54 -0000 1.704 +++ drupal-6.x-dev/includes/common.inc 19 Oct 2007 15:50:34 -0000 @@ -3092,7 +3092,7 @@ function drupal_schema_fields_sql($table * 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). Both array an object types may be passed. - * @param update + * @param primary_keys * 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. @@ -3101,19 +3101,15 @@ function drupal_schema_fields_sql($table * 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); +function drupal_write_record($table, &$object, $primary_keys = array()) { + // Standardize $primary_keys to an array. + if (!is_array($primary_keys)) { + $primary_keys = array($primary_keys); } // Convert to an object if needed. - if (is_array($object)) { + if ($array = is_array($object)) { $object = (object) $object; - $array = TRUE; - } - else { - $array = FALSE; } $schema = drupal_get_schema($table); @@ -3121,18 +3117,19 @@ function drupal_write_record($table, &$o return FALSE; } - $fields = $defs = $values = $serials = array(); - + $fields = array(); + $values = array(); + $serials = array(); // Go through our schema, build SQL, 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)) { + if ($info['type'] == 'serial' && count($primary_keys)) { continue; } // For inserts, populate defaults from Schema if not already provided - if (!isset($object->$field) && !count($update) && isset($info['default'])) { + if (!isset($object->$field) && !count($primary_keys) && isset($info['default'])) { $object->$field = $info['default']; } @@ -3159,7 +3156,7 @@ function drupal_write_record($table, &$o // Build the SQL. $query = ''; - if (!count($update)) { + if (!count($primary_keys)) { $query = "INSERT INTO {". $table ."} (". implode(', ', $fields) .') VALUES ('. implode(', ', $placeholders) .')'; $return = SAVED_NEW; } @@ -3172,7 +3169,7 @@ function drupal_write_record($table, &$o $query .= $field .' = '. $placeholders[$id]; } - foreach ($update as $key){ + foreach ($primary_keys as $key){ $conditions[] = "$key = ". db_type_placeholder($schema['fields'][$key]['type']); $values[] = $object->$key; } @@ -3191,13 +3188,56 @@ function drupal_write_record($table, &$o // If we began with an array, convert back so we don't surprise the caller. if ($array) { - $object = (array)$object; + $object = (array) $object; } return $return; } /** + * Drop a record from the database based upon the schema. + * + * @param $table + * The name of the table; this must exist in schema API. + * @param $object + * The object to drop. 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). Both array an object types may be passed. + * @param primary_keys + * Specify the primary keys' field names. 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. + */ +function drupal_drop_record($table, $object, $primary_keys = array()) { + // Standardize $primary_keys to an array. + if (!is_array($primary_keys)) { + $primary_keys = array($primary_keys); + } + + // Convert to an object if needed. + if ($array = is_array($object)) { + $object = (object) $object; + } + + $schema = drupal_get_schema($table); + if (empty($schema)) { + return FALSE; + } + + // Execute the request. + foreach ($primary_keys as $key){ + $conditions[] = "$key = ". db_type_placeholder($schema['fields'][$key]['type']); + $args[] = $object->$key; + } + + $query = "DELETE FROM {". $table ."} WHERE ". implode(' AND ', $conditions); + db_query($query, $args); + + return SAVED_DELETED; +} + +/** * @} End of "ingroup schemaapi". */ Index: drupal-6.x-dev/modules/node/node.module =================================================================== RCS file: /cvs/drupal/drupal/modules/node/node.module,v retrieving revision 1.895 diff -u -p -r1.895 node.module --- drupal-6.x-dev/modules/node/node.module 19 Oct 2007 10:19:02 -0000 1.895 +++ drupal-6.x-dev/modules/node/node.module 19 Oct 2007 15:51:24 -0000 @@ -845,8 +845,8 @@ function node_delete($nid) { $node = node_load($nid); if (node_access('delete', $node)) { - db_query('DELETE FROM {node} WHERE nid = %d', $node->nid); - db_query('DELETE FROM {node_revisions} WHERE nid = %d', $node->nid); + drupal_drop_record('{node}', $node, 'nid'); + drupal_drop_record('{node_revisions}', $node, 'nid'); // Call the node-specific callback (if any): node_invoke($node, 'delete');