=== modified file 'includes/database.mysql-common.inc'
--- includes/database.mysql-common.inc	2007-05-29 11:36:33 +0000
+++ includes/database.mysql-common.inc	2007-05-29 23:52:22 +0000
@@ -411,6 +411,10 @@ function db_update_field(&$ret, $table, 
   db_change_field($ret, $table, $field, $field, $spec['fields'][$field]);
 }
 
+function db_last_insert_id() {
+  return db_result(db_query('SELECT LAST_INSERT_ID()'));
+}
+
 /**
  * @} End of "ingroup schemaapi".
  */

=== modified file 'includes/database.mysql.inc'
--- includes/database.mysql.inc	2007-05-25 21:01:29 +0000
+++ includes/database.mysql.inc	2007-05-29 23:52:29 +0000
@@ -239,25 +239,6 @@ function db_error() {
 }
 
 /**
- * Return a new unique ID in the given sequence.
- *
- * For compatibility reasons, Drupal does not use auto-numbered fields in its
- * database tables. Instead, this function is used to return a new unique ID
- * of the type requested. If necessary, a new sequence with the given name
- * will be created.
- *
- * Note that the table name should be in curly brackets to preserve compatibility
- * with table prefixes. For example, db_next_id('{node}_nid');
- */
-function db_next_id($name) {
-  global $active_db;
-  $name = db_prefix_tables($name);
-  db_query('INSERT INTO {sequences} VALUES ("%s", LAST_INSERT_ID(1)) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id + 1)', $name);
-
-  return mysql_insert_id($active_db);
-}
-
-/**
  * Determine the number of rows changed by the preceding query.
  */
 function db_affected_rows() {

=== modified file 'includes/database.mysqli.inc'
--- includes/database.mysqli.inc	2007-05-25 21:01:29 +0000
+++ includes/database.mysqli.inc	2007-05-29 23:51:57 +0000
@@ -231,25 +231,6 @@ function db_error() {
 }
 
 /**
- * Return a new unique ID in the given sequence.
- *
- * For compatibility reasons, Drupal does not use auto-numbered fields in its
- * database tables. Instead, this function is used to return a new unique ID
- * of the type requested. If necessary, a new sequence with the given name
- * will be created.
- *
- * Note that the table name should be in curly brackets to preserve compatibility
- * with table prefixes. For example, db_next_id('{node}_nid');
- */
-function db_next_id($name) {
-  global $active_db;
-  $name = db_prefix_tables($name);
-  db_query('INSERT INTO {sequences} VALUES ("%s", LAST_INSERT_ID(1)) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id + 1)', $name);
-
-  return mysqli_insert_id($active_db);
-}
-
-/**
  * Determine the number of rows changed by the preceding query.
  */
 function db_affected_rows() {

=== modified file 'includes/database.pgsql.inc'
--- includes/database.pgsql.inc	2007-05-29 11:36:33 +0000
+++ includes/database.pgsql.inc	2007-05-29 23:28:13 +0000
@@ -221,20 +221,8 @@ function db_error() {
   return pg_last_error($active_db);
 }
 
-/**
- * Return a new unique ID in the given sequence.
- *
- * For compatibility reasons, Drupal does not use auto-numbered fields in its
- * database tables. Instead, this function is used to return a new unique ID
- * of the type requested. If necessary, a new sequence with the given name
- * will be created.
- *
- * Note that the table name should be in curly brackets to preserve compatibility
- * with table prefixes. For example, db_next_id('{node}_nid');
- */
-function db_next_id($name) {
-  $id = db_result(db_query("SELECT nextval('%s_seq')", db_prefix_tables($name)));
-  return $id;
+function db_last_insert_id($name) {
+  return db_result(db_query("SELECT currval('%s_seq')", db_prefix_tables($name)));
 }
 
 /**

=== modified file 'includes/form.inc'
--- includes/form.inc	2007-05-28 18:53:29 +0000
+++ includes/form.inc	2007-05-30 00:16:30 +0000
@@ -1852,7 +1852,6 @@ function batch_set($batch_definition) {
     // Initialize the batch
     if (empty($batch)) {
       $batch = array(
-        'id' => db_next_id('{batch}_bid'),
         'sets' => array(),
       );
     }
@@ -1933,7 +1932,9 @@ function batch_process($redirect = NULL,
         $batch['destination'] = $_REQUEST['edit']['destination'];
         unset($_REQUEST['edit']['destination']);
       }
-      db_query("INSERT INTO {batch} (bid, token, timestamp, batch) VALUES (%d, %d, %d, '%s')", $batch['id'], drupal_get_token($batch['id']), time(), serialize($batch));
+      db_query('INSERT INTO {batch} (timestamp) VALUES (%d)', time());
+      $batch['id'] = db_next_id('{batch}_bid');
+      db_query("UPDATE {batch} SET token = '%s', batch = '%s' WHERE bid = %d", drupal_get_token($batch['id']), serialize($batch), $batch['id']);
       drupal_goto($batch['url'], 'op=start&id='. $batch['id']);
     }
     else {

=== modified file 'includes/menu.inc'
--- includes/menu.inc	2007-05-28 06:24:41 +0000
+++ includes/menu.inc	2007-05-30 00:18:15 +0000
@@ -1311,10 +1311,7 @@ function menu_link_save(&$item) {
     $existing_item = db_fetch_array(db_query("SELECT * FROM {menu_links} WHERE menu_name = '%s' AND link_path = '%s'", $menu_name, $item['link_path']));
   }
 
-  if (!$existing_item) {
-    $item['mlid'] = db_next_id('{menu_links}_mlid');
-  }
-  else {
+  if ($existing_item) {
     $item['mlid'] = $existing_item['mlid'];
   }
 
@@ -1395,21 +1392,22 @@ function menu_link_save(&$item) {
   }
   else {
     db_query("INSERT INTO {menu_links} (
-      menu_name, mlid, plid, link_path,
+      menu_name, plid, link_path,
       router_path, hidden, external, has_children,
       expanded, weight, depth,
       p1, p2, p3, p4, p5, p6,
       module, link_title, options) VALUES (
-      '%s', %d, %d, '%s',
+      '%s', %d, '%s',
       '%s', %d, %d, %d,
       %d, %d, %d,
       %d, %d, %d, %d, %d, %d,
       '%s', '%s', '%s')",
-      $item['menu_name'], $item['mlid'], $item['plid'], $item['link_path'],
+      $item['menu_name'], $item['plid'], $item['link_path'],
       $item['router_path'], $item['hidden'], $item['_external'], $item['has_children'],
       $item['expanded'], $item['weight'],  $item['depth'],
       $item['p1'], $item['p2'], $item['p3'], $item['p4'], $item['p5'], $item['p6'],
       $item['module'],  $item['link_title'], serialize($item['options']));
+      $item['mlid'] = db_last_insert_id('{menu_links}_mlid');
   }
   // Check the has_children status of the parent.
   if ($item['plid']) {

=== modified file 'modules/aggregator/aggregator.module'
--- modules/aggregator/aggregator.module	2007-05-29 14:37:49 +0000
+++ modules/aggregator/aggregator.module	2007-05-29 23:29:56 +0000
@@ -427,8 +427,7 @@ function aggregator_save_category($edit)
   }
   else if (!empty($edit['title'])) {
     // A single unique id for bundles and feeds, to use in blocks
-    $next_id = db_next_id('{aggregator_category}_cid');
-    db_query("INSERT INTO {aggregator_category} (cid, title, description, block) VALUES (%d, '%s', '%s', 5)", $next_id, $edit['title'], $edit['description']);
+    db_query("INSERT INTO {aggregator_category} (title, description, block) VALUES ('%s', '%s', 5)", $edit['title'], $edit['description']);
   }
 }
 
@@ -578,9 +577,9 @@ function aggregator_save_feed($edit) {
     db_query('DELETE FROM {aggregator_item} WHERE fid = %d', $edit['fid']);
   }
   else if ($edit['title']) {
+    db_query("INSERT INTO {aggregator_feed} (title, url, refresh, block) VALUES ('%s', '%s', %d, 5)", $edit['title'], $edit['url'], $edit['refresh']);
     // A single unique id for bundles and feeds, to use in blocks.
-    $edit['fid'] = db_next_id('{aggregator_feed}_fid');
-    db_query("INSERT INTO {aggregator_feed} (fid, title, url, refresh, block) VALUES (%d, '%s', '%s', %d, 5)", $edit['fid'], $edit['title'], $edit['url'], $edit['refresh']);
+    $edit['fid'] = db_last_insert_id('{aggregator_feed}_fid');
   }
   if ($edit['title']) {
     // The feed is being saved, save the categories as well.
@@ -985,8 +984,8 @@ function aggregator_save_item($edit) {
     db_query('DELETE FROM {aggregator_category_item} WHERE iid = %d', $edit['iid']);
   }
   else if ($edit['title'] && $edit['link']) {
-    $edit['iid'] = db_next_id('{aggregator_item}_iid');
-    db_query("INSERT INTO {aggregator_item} (iid, fid, title, link, author, description, timestamp, guid) VALUES (%d, %d, '%s', '%s', '%s', '%s', %d, '%s')", $edit['iid'], $edit['fid'], $edit['title'], $edit['link'], $edit['author'], $edit['description'], $edit['timestamp'], $edit['guid']);
+    db_query("INSERT INTO {aggregator_item} (fid, title, link, author, description, timestamp, guid) VALUES (%d, '%s', '%s', '%s', '%s', %d, '%s')", $edit['fid'], $edit['title'], $edit['link'], $edit['author'], $edit['description'], $edit['timestamp'], $edit['guid']);
+    $edit['iid'] = db_last_insert_id('{aggregator_item}_iid');
     // file the items in the categories indicated by the feed
     $categories = db_query('SELECT cid FROM {aggregator_category_feed} WHERE fid = %d', $edit['fid']);
     while ($category = db_fetch_object($categories)) {

=== modified file 'modules/block/block.module'
--- modules/block/block.module	2007-05-28 06:08:37 +0000
+++ modules/block/block.module	2007-05-29 23:31:50 +0000
@@ -538,7 +538,8 @@ function block_add_block_form_validate($
  * Save the new custom block.
  */
 function block_add_block_form_submit($form, &$form_state, $form_values) {
-  $delta = db_next_id('{boxes}_bid');
+  db_query("INSERT INTO {boxes} (body, info, format) VALUES  ('%s', '%s', %d)", $form_values['body'], $form_values['info'], $form_values['format']);
+  $delta = db_last_insert_id('{boxes}_bid');
 
   foreach (list_themes() as $key => $theme) {
     if ($theme->status) {
@@ -550,8 +551,6 @@ function block_add_block_form_submit($fo
     db_query("INSERT INTO {blocks_roles} (rid, module, delta) VALUES (%d, '%s', '%s')", $rid, $form_values['module'], $delta);
   }
 
-  db_query("INSERT INTO {boxes} (bid, body, info, format) VALUES  (%d, '%s', '%s', %d)", $delta, $form_values['body'], $form_values['info'], $form_values['format']);
-
   drupal_set_message(t('The block has been created.'));
   cache_clear_all();
 

=== modified file 'modules/comment/comment.module'
--- modules/comment/comment.module	2007-05-29 06:27:37 +0000
+++ modules/comment/comment.module	2007-05-29 23:32:30 +0000
@@ -812,7 +812,6 @@ function comment_save($edit) {
           }
         }
 
-        $edit['cid'] = db_next_id('{comments}_cid');
         $edit['timestamp'] = time();
 
         if ($edit['uid'] === $user->uid) { // '===' because we want to modify anonymous users too
@@ -820,7 +819,8 @@ function comment_save($edit) {
         }
 
         $edit += array('mail' => '', 'homepage' => '');
-        db_query("INSERT INTO {comments} (cid, nid, pid, uid, subject, comment, format, hostname, timestamp, status, score, users, thread, name, mail, homepage) VALUES (%d, %d, %d, %d, '%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s', '%s', '%s', '%s')", $edit['cid'], $edit['nid'], $edit['pid'], $edit['uid'], $edit['subject'], $edit['comment'], $edit['format'], ip_address(), $edit['timestamp'], $status, $score, $users, $thread, $edit['name'], $edit['mail'], $edit['homepage']);
+        db_query("INSERT INTO {comments} (nid, pid, uid, subject, comment, format, hostname, timestamp, status, score, users, thread, name, mail, homepage) VALUES %d, %d, %d, '%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s', '%s', '%s', '%s')", $edit['nid'], $edit['pid'], $edit['uid'], $edit['subject'], $edit['comment'], $edit['format'], ip_address(), $edit['timestamp'], $status, $score, $users, $thread, $edit['name'], $edit['mail'], $edit['homepage']);
+        $edit['cid'] = db_last_insert_id('{comments}_cid');
 
         _comment_update_node_statistics($edit['nid']);
 

=== modified file 'modules/drupal/drupal.module'
--- modules/drupal/drupal.module	2007-04-30 17:03:22 +0000
+++ modules/drupal/drupal.module	2007-05-29 23:33:16 +0000
@@ -196,8 +196,8 @@ function drupal_client_ping($client, $sy
       db_query("UPDATE {client} SET link = '%s', name = '%s', mail = '%s', slogan = '%s', mission = '%s', users = %d, nodes = %d, version = '%s', changed = '%s' WHERE cid = %d", $client['uid'], $client['link'], $client['name'], $client['mail'], $client['slogan'], $client['mission'], $client['users'], $client['nodes'], $client['version'], time(), $client['cid']);
     }
     else {
-      $client['cid'] = db_next_id('{client}_cid');
-      db_query("INSERT INTO {client} (cid, link, name, mail, slogan, mission, users, nodes, version, created, changed) VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", $client['cid'], $client['link'], $client['name'], $client['mail'], $client['slogan'], $client['mission'], $client['users'], $client['nodes'], $client['version'], time(), time());
+      db_query("INSERT INTO {client} (link, name, mail, slogan, mission, users, nodes, version, created, changed) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", $client['link'], $client['name'], $client['mail'], $client['slogan'], $client['mission'], $client['users'], $client['nodes'], $client['version'], time(), time());
+      $client['cid'] = db_last_insert_id('{client}_cid');
     }
     if (is_array($system)) {
       db_query("DELETE FROM {client_system} WHERE cid = %d", $client['cid']);

=== modified file 'modules/node/node.module'
--- modules/node/node.module	2007-05-28 06:08:37 +0000
+++ modules/node/node.module	2007-05-30 00:24:17 +0000
@@ -621,9 +621,6 @@ function node_save(&$node) {
   if (empty($node->nid)) {
     // Insert a new node.
     $node->is_new = TRUE;
-
-    $node->nid = db_next_id('{node}_nid');
-    $node->vid = db_next_id('{node_revisions}_vid');
   }
   else {
     // We need to ensure that all node fields are filled.
@@ -635,7 +632,6 @@ function node_save(&$node) {
 
     if (!empty($node->revision)) {
       $node->old_vid = $node->vid;
-      $node->vid = db_next_id('{node_revisions}_vid');
     }
   }
 
@@ -647,12 +643,10 @@ function node_save(&$node) {
   $node->changed = time();
 
   // Split off revisions data to another structure
-  $revisions_table_values = array('nid' => $node->nid, 'vid' => $node->vid,
-                     'title' => $node->title, 'body' => $node->body,
+  $revisions_table_values = array('title' => $node->title, 'body' => $node->body,
                      'teaser' => $node->teaser, 'timestamp' => $node->changed,
                      'uid' => $user->uid, 'format' => $node->format);
-  $revisions_table_types = array('nid' => '%d', 'vid' => '%d',
-                     'title' => "'%s'", 'body' => "'%s'",
+  $revisions_table_types = array('title' => "'%s'", 'body' => "'%s'",
                      'teaser' => "'%s'", 'timestamp' => '%d',
                      'uid' => '%d', 'format' => '%d');
   if (!empty($node->log) || $node->is_new || $node->revision) {
@@ -662,12 +656,12 @@ function node_save(&$node) {
     $revisions_table_values['log'] = $node->log;
     $revisions_table_types['log'] = "'%s'";
   }
-  $node_table_values = array('nid' => $node->nid, 'vid' => $node->vid,
+  $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('nid' => '%d', 'vid' => '%d',
+  $node_table_types = array(
                     'title' => "'%s'", 'type' => "'%s'", 'uid' => '%d',
                     'status' => '%d', 'language' => "'%s'",'created' => '%d',
                     'changed' => '%d', 'comment' => '%d',
@@ -676,8 +670,8 @@ function node_save(&$node) {
   //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) .')';
     $revisions_query = 'INSERT INTO {node_revisions} ('. implode(', ', array_keys($revisions_table_types)) .') VALUES ('. implode(', ', $revisions_table_types) .')';
+    $op = 'insert';
   }
   else {
     $arr = array();
@@ -697,22 +691,30 @@ function node_save(&$node) {
       $revisions_table_values[] = $node->vid;
       $revisions_query = 'UPDATE {node_revisions} SET '. implode(', ', $arr) .' WHERE vid = %d';
     }
+    $op = 'update';
   }
 
-  // Insert the node into the database:
-  db_query($node_query, $node_table_values);
   db_query($revisions_query, $revisions_table_values);
-
-  // Call the node specific callback (if any):
+  if ($node->is_new || !empty($node->revision)) {
+    $node->vid = db_last_insert_id('{node_revisions}_vid');
+  }
+  $node_table_values['vid'] = $node->vid;
+  $node_table_types['vid'] = '%d';
   if ($node->is_new) {
-    node_invoke($node, 'insert');
-    node_invoke_nodeapi($node, 'insert');
+    $node_query = 'INSERT INTO {node} ('. implode(', ', array_keys($node_table_types)) .') VALUES ('. implode(', ', $node_table_types) .')';
   }
-  else {
-    node_invoke($node, 'update');
-    node_invoke_nodeapi($node, 'update');
+  // Save the node into the database:
+  db_query($node_query, $node_table_values);
+  if ($node->is_new) {
+    $node->nid = db_last_insert_id('{node}_nid');
+    // We need to fix up node_revisions -- we just had the relevant nid
+    db_query('UPDATE {node_revisions} r SET nid = (SELECT n.nid FROM {node} n WHERE n.vid = r.vid) WHERE r.nid = 0');
   }
 
+  // Call the node specific callback (if any):
+  node_invoke($node, $op);
+  node_invoke_nodeapi($node, $op);
+
   // Update the node access table for this node.
   node_access_acquire_grants($node);
 

=== modified file 'modules/taxonomy/taxonomy.module'
--- modules/taxonomy/taxonomy.module	2007-05-28 06:08:37 +0000
+++ modules/taxonomy/taxonomy.module	2007-05-29 23:34:49 +0000
@@ -339,8 +339,8 @@ function taxonomy_save_vocabulary(&$edit
     $status = taxonomy_del_vocabulary($edit['vid']);
   }
   else {
-    $edit['vid'] = db_next_id('{vocabulary}_vid');
-    db_query("INSERT INTO {vocabulary} (vid, name, description, help, multiple, required, hierarchy, relations, tags, weight, module) VALUES (%d, '%s', '%s', '%s', %d, %d, %d, %d, %d, %d, '%s')", $edit['vid'], $edit['name'], isset($edit['description']) ? $edit['description'] : NULL, isset($edit['help']) ? $edit['help'] : NULL, $edit['multiple'], $edit['required'], $edit['hierarchy'], $edit['relations'], isset($edit['tags']) ? $edit['tags'] : NULL, $edit['weight'], isset($edit['module']) ? $edit['module'] : 'taxonomy');
+    db_query("INSERT INTO {vocabulary} (name, description, help, multiple, required, hierarchy, relations, tags, weight, module) VALUES ('%s', '%s', '%s', %d, %d, %d, %d, %d, %d, '%s')", $edit['name'], isset($edit['description']) ? $edit['description'] : NULL, isset($edit['help']) ? $edit['help'] : NULL, $edit['multiple'], $edit['required'], $edit['hierarchy'], $edit['relations'], isset($edit['tags']) ? $edit['tags'] : NULL, $edit['weight'], isset($edit['module']) ? $edit['module'] : 'taxonomy');
+    $edit['vid'] = db_last_insert_id('{vocabulary}_vid');
     foreach ($edit['nodes'] as $type => $selected) {
       db_query("INSERT INTO {vocabulary_node_types} (vid, type) VALUES (%d, '%s')", $edit['vid'], $type);
     }
@@ -518,8 +518,8 @@ function taxonomy_save_term(&$form_value
     return taxonomy_del_term($form_values['tid']);
   }
   else {
-    $form_values['tid'] = db_next_id('{term_data}_tid');
-    db_query("INSERT INTO {term_data} (tid, name, description, vid, weight) VALUES (%d, '%s', '%s', %d, %d)", $form_values['tid'], $form_values['name'], $form_values['description'], $form_values['vid'], $form_values['weight']);
+    db_query("INSERT INTO {term_data} (name, description, vid, weight) VALUES ('%s', '%s', %d, %d)", $form_values['name'], $form_values['description'], $form_values['vid'], $form_values['weight']);
+    $form_values['tid'] = db_last_insert_id('{term_data}_tid');
     $hook = 'insert';
     $status = SAVED_NEW;
   }

=== modified file 'modules/upload/upload.module'
--- modules/upload/upload.module	2007-05-29 16:06:07 +0000
+++ modules/upload/upload.module	2007-05-29 23:35:21 +0000
@@ -721,8 +721,8 @@ function upload_save(&$node) {
     // New file upload
     elseif (strpos($file->fid, 'upload') !== FALSE) {
       if ($file = file_save_upload($file, $file->filename)) {
-        $file->fid = db_next_id('{files}_fid');
-        db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', %d)", $file->fid, $node->nid, $file->filename, $file->filepath, $file->filemime, $file->filesize);
+        db_query("INSERT INTO {files} (nid, filename, filepath, filemime, filesize) VALUES (%d, '%s', '%s', '%s', %d)", $node->nid, $file->filename, $file->filepath, $file->filemime, $file->filesize);
+        $file->fid = db_last_insert_id('{files}_fid');
         db_query("INSERT INTO {file_revisions} (fid, vid, list, description) VALUES (%d, %d, %d, '%s')", $file->fid, $node->vid, $file->list, $file->description);
         // Tell other modules where the file was stored.
         $node->files[$fid] = $file;

=== modified file 'modules/user/user.module'
--- modules/user/user.module	2007-05-28 06:08:37 +0000
+++ modules/user/user.module	2007-05-29 23:37:19 +0000
@@ -210,8 +210,6 @@ function user_save($account, $array = ar
     user_module_invoke('after_update', $array, $user, $category);
   }
   else {
-    $array['uid'] = db_next_id('{users}_uid');
-
     if (!isset($array['created'])) {    // Allow 'created' to be set by hook_auth
       $array['created'] = time();
     }
@@ -226,7 +224,7 @@ function user_save($account, $array = ar
           $values[] = md5($value);
           $s[] = "'%s'";
           break;
-        case 'uid':        case 'mode':     case 'sort':
+        case 'mode':     case 'sort':
         case 'threshold':  case 'created':  case 'access':
         case 'login':      case 'status':
           $fields[] = $key;
@@ -243,6 +241,7 @@ function user_save($account, $array = ar
       }
     }
     db_query('INSERT INTO {users} ('. implode(', ', $fields) .') VALUES ('. implode(', ', $s) .')', $values);
+    $array['uid'] = db_last_insert_id('{users}_uid');
 
     // Build the initial user object.
     $user = user_load(array('uid' => $array['uid']));

