diff -urpN drupal/includes/common.inc drupal-6.x-dev-drupal_drop_record-0.2/includes/common.inc --- drupal/includes/common.inc 2007-10-12 22:10:17.000000000 +0800 +++ drupal-6.x-dev-drupal_drop_record-0.2/includes/common.inc 2007-10-13 13:07:16.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,30 @@ 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(); - - // Go through our schema, build SQL, and when inserting, fill in defaults for - // fields that are not set. + $values = array(); + $serials = array(); + // Go through our schema, 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']; } @@ -3146,59 +3138,78 @@ function drupal_write_record($table, &$o // Build arrays for the fields, placeholders, and values in our query. if (isset($object->$field)) { - $fields[] = $field; - $placeholders[] = db_type_placeholder($info['type']); - - if (empty($info['serialize'])) { - $values[] = $object->$field; - } - else { - $values[] = serialize($object->$field); - } + $values[] = array( + 'field' => $field, + 'placeholder' => db_type_placeholder($info['type']), + 'data' => (empty($info['serialize'])) ? $object->$field : serialize($object->$field), + ); } } - // Build the SQL. - $query = ''; - if (!count($update)) { - $query = "INSERT INTO {". $table ."} (" . implode(', ', $fields) . ') VALUES (' . implode(', ', $placeholders) . ')'; + // Execute the request. + if (!count($primary_keys)) { + db_query_insert("{". $table ."}", $values); $return = SAVED_NEW; } else { - $query = ''; - foreach ($fields as $id => $field) { - if ($query) { - $query .= ', '; - } - $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; + $args[] = $object->$key; } - - $query = "UPDATE {". $table ."} SET $query WHERE ". implode(' AND ', $conditions); + db_query_update("{". $table ."}", $values, implode(' AND ', $conditions), $args); $return = SAVED_UPDATED; } - db_query($query, $values); if ($serials) { - // Get last insert ids and fill them in. foreach ($serials as $field) { + // Get last insert ids and fill them in. $object->$field = db_last_insert_id($table, $field); } } // 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; + } + db_query_delete("{". $table ."}", implode(' AND ', $conditions), $args); + return SAVED_DELETED; +} + +/** * @} End of "ingroup schemaapi". */ diff -urpN drupal/includes/database.mysql-common.inc drupal-6.x-dev-drupal_drop_record-0.2/includes/database.mysql-common.inc --- drupal/includes/database.mysql-common.inc 2007-10-03 00:15:56.000000000 +0800 +++ drupal-6.x-dev-drupal_drop_record-0.2/includes/database.mysql-common.inc 2007-10-13 13:08:09.000000000 +0800 @@ -43,6 +43,130 @@ function db_query($query) { } /** + * Insert a row of record into database. + * + * @param $table + * Table to insert. + * @param $values + * An array containing the insert values. Each element of the array + * should be an associatie array with the following keys: + * - "field": The database field represented in the table column. + * - "placeholder": The placeholder of the table column, using printf() + * syntax. Valid %-modifiers are: %d, %f, %s and %b. + * - "data": The data to insert into the table column. + * @return + * A database query result resource, or FALSE if the query was not + * executed correctly. + */ +function db_query_insert($table, $values) { + $fields = array(); + $placeholders = array(); + $data = array(); + foreach ($values as $value) { + $fields[] = $value['field']; + $placeholders[] = $value['placeholder']; + $data[] = $value['data']; + } + + if (count($fields)) { + $query = "INSERT INTO ". $table ." (". implode(', ', $fields) .") VALUES (". implode(', ', $placeholders) .")"; + return db_query($query, $data); + } +} + +/** + * Update a row of record in database. + * + * @param $table + * Table to update. + * @param $values + * An array containing the update values. Each element of the array + * should be an associatie array with the following keys: + * - "field": The database field represented in the table column. + * - "placeholder": The placeholder of the table column, using printf() + * syntax. Valid %-modifiers are: %d, %f, %s and %b. + * - "data": The data to insert into the table column. + * @param $where_clause + * A string containing an update condition query (where clause). + * @param ... + * A variable number of arguments which are substituted into the query + * WHERE condition, using printf() syntax. Instead of a variable number + * of query arguments, you may also pass a single array containing the + * query arguments. + * + * Valid %-modifiers are: %d, %f and %s. + * + * NOTE: using this syntax will cast NULL and FALSE values to decimal 0, + * and TRUE values to decimal 1. + * + * @return + * A database query result resource, or FALSE if the query was not + * executed correctly. + */ +function db_query_update($table, $values, $where_clause = NULL) { + $args = func_get_args(); + $args = array_slice($args, 3); + if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax + $args = $args[0]; + } + + $fields = array(); + $data = array(); + foreach ($values as $value) { + $fields[] = $value['field'] .'='. $value['placeholder']; + $data[] = $value['data']; + } + + if (count($fields)) { + $query = "UPDATE ". $table ." SET ". implode(', ', $fields); + if ($where_clause) { + $query .= " WHERE ". $where_clause; + $data = array_merge($data, $args); + } + return db_query($query, $data); + } +} + +/** + * Delete a row of record from database. + * + * @param $table + * Table to delete. + * @param $where_clause + * A string containing an update condition query (where clause). + * @param ... + * A variable number of arguments which are substituted into the query + * WHERE condition, using printf() syntax. Instead of a variable number + * of query arguments, you may also pass a single array containing the + * query arguments. + * + * Valid %-modifiers are: %d, %f and %s. + * + * NOTE: using this syntax will cast NULL and FALSE values to decimal 0, + * and TRUE values to decimal 1. + * + * @return + * A database query result resource, or FALSE if the query was not + * executed correctly. + */ +function db_query_delete($table, $where_clause = NULL) { + $args = func_get_args(); + $args = array_slice($args, 2); + if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax + $args = $args[0]; + } + + $data = array(); + + $query = "DELETE FROM ". $table; + if ($where_clause) { + $query .= " WHERE ". $where_clause; + $data = $args; + } + return db_query($query, $data); +} + +/** * @ingroup schemaapi * @{ */ diff -urpN drupal/includes/database.pgsql.inc drupal-6.x-dev-drupal_drop_record-0.2/includes/database.pgsql.inc --- drupal/includes/database.pgsql.inc 2007-10-03 00:15:56.000000000 +0800 +++ drupal-6.x-dev-drupal_drop_record-0.2/includes/database.pgsql.inc 2007-10-13 13:09:00.000000000 +0800 @@ -348,6 +348,130 @@ function db_query_temporary($query) { } /** + * Insert a row of record into database. + * + * @param $table + * Table to insert. + * @param $values + * An array containing the insert values. Each element of the array + * should be an associatie array with the following keys: + * - "field": The database field represented in the table column. + * - "placeholder": The placeholder of the table column, using printf() + * syntax. Valid %-modifiers are: %d, %f, %s and %b. + * - "data": The data to insert into the table column. + * @return + * A database query result resource, or FALSE if the query was not + * executed correctly. + */ +function db_query_insert($table, $values) { + $fields = array(); + $placeholders = array(); + $data = array(); + foreach ($values as $value) { + $fields[] = $value['field']; + $placeholders[] = $value['placeholder']; + $data[] = $value['data']; + } + + if (count($fields)) { + $query = "INSERT INTO ". $table ." (". implode(', ', $fields) .") VALUES (". implode(', ', $placeholders) .")"; + return db_query($query, $data); + } +} + +/** + * Update a row of record in database. + * + * @param $table + * Table to update. + * @param $values + * An array containing the update values. Each element of the array + * should be an associatie array with the following keys: + * - "field": The database field represented in the table column. + * - "placeholder": The placeholder of the table column, using printf() + * syntax. Valid %-modifiers are: %d, %f, %s and %b. + * - "data": The data to insert into the table column. + * @param $where_clause + * A string containing an update condition query (where clause). + * @param ... + * A variable number of arguments which are substituted into the query + * WHERE condition, using printf() syntax. Instead of a variable number + * of query arguments, you may also pass a single array containing the + * query arguments. + * + * Valid %-modifiers are: %d, %f and %s. + * + * NOTE: using this syntax will cast NULL and FALSE values to decimal 0, + * and TRUE values to decimal 1. + * + * @return + * A database query result resource, or FALSE if the query was not + * executed correctly. + */ +function db_query_update($table, $values, $where_clause = NULL) { + $args = func_get_args(); + $args = array_slice($args, 3); + if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax + $args = $args[0]; + } + + $fields = array(); + $data = array(); + foreach ($values as $value) { + $fields[] = $value['field'] .'='. $value['placeholder']; + $data[] = $value['data']; + } + + if (count($fields)) { + $query = "UPDATE ". $table ." SET ". implode(', ', $fields); + if ($where_clause) { + $query .= " WHERE ". $where_clause; + $data = array_merge($data, $args); + } + return db_query($query, $data); + } +} + +/** + * Delete a row of record from database. + * + * @param $table + * Table to delete. + * @param $where_clause + * A string containing an update condition query (where clause). + * @param ... + * A variable number of arguments which are substituted into the query + * WHERE condition, using printf() syntax. Instead of a variable number + * of query arguments, you may also pass a single array containing the + * query arguments. + * + * Valid %-modifiers are: %d, %f and %s. + * + * NOTE: using this syntax will cast NULL and FALSE values to decimal 0, + * and TRUE values to decimal 1. + * + * @return + * A database query result resource, or FALSE if the query was not + * executed correctly. + */ +function db_query_delete($table, $where_clause = NULL) { + $args = func_get_args(); + $args = array_slice($args, 2); + if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax + $args = $args[0]; + } + + $data = array(); + + $query = "DELETE FROM ". $table; + if ($where_clause) { + $query .= " WHERE ". $where_clause; + $data = $args; + } + return db_query($query, $data); +} + +/** * Returns a properly formatted Binary Large OBject value. * In case of PostgreSQL encodes data for insert into bytea field. * diff -urpN drupal/modules/node/node.module drupal-6.x-dev-drupal_drop_record-0.2/modules/node/node.module --- drupal/modules/node/node.module 2007-10-13 12:16:48.000000000 +0800 +++ drupal-6.x-dev-drupal_drop_record-0.2/modules/node/node.module 2007-10-13 13:06:03.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');