diff -urpN drupal/includes/common.inc drupal-6.x-dev-drupal_drop_record-0.3/includes/common.inc --- drupal/includes/common.inc 2007-10-12 22:10:17.000000000 +0800 +++ drupal-6.x-dev-drupal_drop_record-0.3/includes/common.inc 2007-10-17 00:12:07.000000000 +0800 @@ -3093,7 +3093,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. @@ -3102,38 +3102,31 @@ 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. + $primary_keys = (is_array($primary_keys)) ? $primary_keys : array($primary_keys); // Convert to an object if needed. - if (is_array($object)) { - $object = (object) $object; - $array = TRUE; - } - else { - $array = FALSE; - } + $object = ($array = is_array($object)) ? (object) $object : $object; $schema = drupal_get_schema($table); if (empty($schema)) { 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']; } @@ -3160,7 +3153,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; } @@ -3173,7 +3166,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,14 +3184,49 @@ 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) ? (array) $object : $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. + $primary_keys = (is_array($primary_keys)) ? $primary_keys : array($primary_keys); + + // Convert to an object if needed. + $object = (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". */ diff -urpN drupal/modules/node/node.module drupal-6.x-dev-drupal_drop_record-0.3/modules/node/node.module --- drupal/modules/node/node.module 2007-10-12 22:10:18.000000000 +0800 +++ drupal-6.x-dev-drupal_drop_record-0.3/modules/node/node.module 2007-10-16 23:43:43.000000000 +0800 @@ -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');