diff -urpN drupal-6.x-dev-drupal_drop_record-0.3/includes/common.inc drupal-6.x-dev-drupal_drop_record-0.3+/includes/common.inc --- drupal-6.x-dev-drupal_drop_record-0.3/includes/common.inc 2007-10-17 00:12:07.000000000 +0800 +++ drupal-6.x-dev-drupal_drop_record-0.3+/includes/common.inc 2007-10-17 00:10:25.000000000 +0800 @@ -3114,7 +3114,6 @@ function drupal_write_record($table, &$o return FALSE; } - $fields = array(); $values = array(); $serials = array(); // Go through our schema, build SQL, and when inserting, fill in defaults for @@ -3139,42 +3138,27 @@ 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($primary_keys)) { - $query = "INSERT INTO {". $table ."} (" . implode(', ', $fields) . ') VALUES (' . implode(', ', $placeholders) . ')'; + db_query_insert("{". $table ."}", $values); $return = SAVED_NEW; } else { - $query = ''; - foreach ($fields as $id => $field) { - if ($query) { - $query .= ', '; - } - $query .= $field . ' = ' . $placeholders[$id]; - } - 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. @@ -3221,8 +3205,7 @@ function drupal_drop_record($table, $obj $conditions[] = "$key = ". db_type_placeholder($schema['fields'][$key]['type']); $args[] = $object->$key; } - $query = "DELETE FROM {". $table ."} WHERE ". implode(' AND ', $conditions); - db_query($query, $args); + db_query_delete("{". $table ."}", implode(' AND ', $conditions), $args); return SAVED_DELETED; } diff -urpN drupal-6.x-dev-drupal_drop_record-0.3/includes/database.mysql-common.inc drupal-6.x-dev-drupal_drop_record-0.3+/includes/database.mysql-common.inc --- drupal-6.x-dev-drupal_drop_record-0.3/includes/database.mysql-common.inc 2007-10-03 00:15:56.000000000 +0800 +++ drupal-6.x-dev-drupal_drop_record-0.3+/includes/database.mysql-common.inc 2007-10-17 00:07:46.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-6.x-dev-drupal_drop_record-0.3/includes/database.pgsql.inc drupal-6.x-dev-drupal_drop_record-0.3+/includes/database.pgsql.inc --- drupal-6.x-dev-drupal_drop_record-0.3/includes/database.pgsql.inc 2007-10-03 00:15:56.000000000 +0800 +++ drupal-6.x-dev-drupal_drop_record-0.3+/includes/database.pgsql.inc 2007-10-17 00:07:46.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. *