? db_last_insert_id.patch
? db_last_insert_id_10.patch
? db_last_insert_id_7.patch
? db_last_insert_id_9.patch
? menu_module.patch
? optimize_drupal_lookup.patch
? rollback.patch
Index: includes/database.mysql-common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database.mysql-common.inc,v
retrieving revision 1.4
diff -u -p -r1.4 database.mysql-common.inc
--- includes/database.mysql-common.inc	30 May 2007 09:11:21 -0000	1.4
+++ includes/database.mysql-common.inc	4 Jun 2007 03:33:40 -0000
@@ -448,6 +448,18 @@ function db_update_field(&$ret, $table, 
 }
 
 /**
+ * Returns the last insert id.
+ *
+ * @param $table
+ *   The name of the table you inserted into.
+ * @param $field
+ *   The name of the autoincrement field.
+ */
+function db_last_insert_id($table, $field) {
+  return db_result(db_query('SELECT LAST_INSERT_ID()'));
+}
+
+/**
  * @} End of "ingroup schemaapi".
  */
 
Index: includes/database.mysql.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database.mysql.inc,v
retrieving revision 1.73
diff -u -p -r1.73 database.mysql.inc
--- includes/database.mysql.inc	25 May 2007 21:01:30 -0000	1.73
+++ includes/database.mysql.inc	4 Jun 2007 03:33:40 -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() {
Index: includes/database.mysqli.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database.mysqli.inc,v
retrieving revision 1.37
diff -u -p -r1.37 database.mysqli.inc
--- includes/database.mysqli.inc	25 May 2007 21:01:30 -0000	1.37
+++ includes/database.mysqli.inc	4 Jun 2007 03:33:40 -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() {
Index: includes/database.pgsql.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database.pgsql.inc,v
retrieving revision 1.48
diff -u -p -r1.48 database.pgsql.inc
--- includes/database.pgsql.inc	30 May 2007 09:11:21 -0000	1.48
+++ includes/database.pgsql.inc	4 Jun 2007 03:33:40 -0000
@@ -258,19 +258,15 @@ function db_error() {
 }
 
 /**
- * Return a new unique ID in the given sequence.
+ * Returns the last insert id. This function is thread safe.
  *
- * 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;
+ * @param $table
+ *   The name of the table you inserted into.
+ * @param $field
+ *   The name of the autoincrement field.
+ */
+function db_last_insert_id($table, $field) {
+  return db_result(db_query("SELECT currval('%s_seq')", db_prefix_tables('{'. $table .'}') . '_'. $field));
 }
 
 /**
Index: includes/file.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/file.inc,v
retrieving revision 1.99
diff -u -p -r1.99 file.inc
--- includes/file.inc	30 May 2007 08:08:57 -0000	1.99
+++ includes/file.inc	4 Jun 2007 03:33:41 -0000
@@ -574,8 +574,8 @@ function file_save_upload($source, $vali
     }
 
     // If we made it this far it's safe to record this file in the database.
-    $file->fid = db_next_id('fid');
-    db_query("INSERT INTO {files} (fid, uid, filename, filepath, filemime, filesize, status, timestamp) VALUES (%d, %d, '%s', '%s', '%s', %d, %d, %d)", $file->fid, $user->uid, $file->filename, $file->filepath, $file->filemime, $file->filesize, FILE_STATUS_TEMPORARY, time());
+    db_query("INSERT INTO {files} (uid, filename, filepath, filemime, filesize, status, timestamp) VALUES (%d, '%s', '%s', '%s', %d, %d, %d)", $user->uid, $file->filename, $file->filepath, $file->filemime, $file->filesize, FILE_STATUS_TEMPORARY, time());
+    $file->fid = db_last_insert_id('files', 'fid');
 
     // Add file to the cache.
     $upload_cache[$source] = $file;
Index: includes/form.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/form.inc,v
retrieving revision 1.201
diff -u -p -r1.201 form.inc
--- includes/form.inc	31 May 2007 12:36:18 -0000	1.201
+++ includes/form.inc	4 Jun 2007 03:33:42 -0000
@@ -1854,7 +1854,6 @@ function batch_set($batch_definition) {
     // Initialize the batch
     if (empty($batch)) {
       $batch = array(
-        'id' => db_next_id('{batch}_bid'),
         'sets' => array(),
       );
     }
@@ -1935,7 +1934,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_last_insert_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 {
Index: includes/menu.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/menu.inc,v
retrieving revision 1.174
diff -u -p -r1.174 menu.inc
--- includes/menu.inc	1 Jun 2007 09:40:40 -0000	1.174
+++ includes/menu.inc	4 Jun 2007 03:33:42 -0000
@@ -1313,10 +1313,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'];
   }
 
@@ -1339,6 +1336,21 @@ function menu_link_save(&$item) {
   else {
     $item['plid'] = $parent['mlid'];
   }
+  db_query("INSERT INTO {menu_links} (
+    menu_name, plid, link_path,
+    hidden, external, has_children,
+    expanded, weight,
+    module, link_title, options) VALUES (
+    '%s', %d, '%s',
+    %d, %d, %d,
+    %d, %d,
+    '%s', '%s', '%s')",
+    $item['menu_name'], $item['plid'], $item['link_path'],
+    $item['hidden'], $item['_external'], $item['has_children'],
+    $item['expanded'], $item['weight'],
+    $item['module'],  $item['link_title'], serialize($item['options']));
+  $item['mlid'] = db_last_insert_id('menu_links', 'mlid');
+
 
   if (!$item['plid']) {
     $item['p1'] = $item['mlid'];
@@ -1383,36 +1395,16 @@ function menu_link_save(&$item) {
       }
     }
   }
-  if ($existing_item) {
-    db_query("UPDATE {menu_links} SET menu_name = '%s', plid = %d, link_path = '%s',
-      router_path = '%s', hidden = %d, external = %d, has_children = %d,
-      expanded = %d, weight = %d,  depth = %d,
-      p1 = %d, p2 = %d, p3 = %d, p4 = %d, p5 = %d, p6 = %d,
-      module = '%s', link_title = '%s', options = '%s' WHERE mlid = %d",
-      $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']);
-  }
-  else {
-    db_query("INSERT INTO {menu_links} (
-      menu_name, mlid, 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, %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['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']));
-  }
+  db_query("UPDATE {menu_links} SET menu_name = '%s', plid = %d, link_path = '%s',
+    router_path = '%s', hidden = %d, external = %d, has_children = %d,
+    expanded = %d, weight = %d,  depth = %d,
+    p1 = %d, p2 = %d, p3 = %d, p4 = %d, p5 = %d, p6 = %d,
+    module = '%s', link_title = '%s', options = '%s' WHERE mlid = %d",
+    $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']);
   // Check the has_children status of the parent.
   if ($item['plid']) {
     $parent_has_children = (bool)db_result(db_query("SELECT COUNT(*) FROM {menu_links} WHERE plid = %d AND hidden = 0", $item['plid']));
Index: modules/aggregator/aggregator.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/aggregator/aggregator.module,v
retrieving revision 1.343
diff -u -p -r1.343 aggregator.module
--- modules/aggregator/aggregator.module	31 May 2007 12:14:04 -0000	1.343
+++ modules/aggregator/aggregator.module	4 Jun 2007 03:33:43 -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)) {
Index: modules/block/block.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/block/block.module,v
retrieving revision 1.262
diff -u -p -r1.262 block.module
--- modules/block/block.module	28 May 2007 06:08:39 -0000	1.262
+++ modules/block/block.module	4 Jun 2007 03:33:44 -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();
 
Index: modules/comment/comment.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/comment/comment.module,v
retrieving revision 1.547
diff -u -p -r1.547 comment.module
--- modules/comment/comment.module	29 May 2007 06:27:37 -0000	1.547
+++ modules/comment/comment.module	4 Jun 2007 03:33:45 -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']);
 
Index: modules/drupal/drupal.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/drupal/drupal.module,v
retrieving revision 1.144
diff -u -p -r1.144 drupal.module
--- modules/drupal/drupal.module	30 Apr 2007 17:03:24 -0000	1.144
+++ modules/drupal/drupal.module	4 Jun 2007 03:33:45 -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']);
Index: modules/node/node.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/node/node.module,v
retrieving revision 1.823
diff -u -p -r1.823 node.module
--- modules/node/node.module	1 Jun 2007 11:17:44 -0000	1.823
+++ modules/node/node.module	4 Jun 2007 03:33:47 -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,11 +643,11 @@ function node_save(&$node) {
   $node->changed = time();
 
   // Split off revisions data to another structure
-  $revisions_table_values = array('nid' => $node->nid, 'vid' => $node->vid,
+  $revisions_table_values = array('nid' => &$node->nid
                      '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',
+  $revisions_table_types = array('nid' => '%d',
                      'title' => "'%s'", 'body' => "'%s'",
                      'teaser' => "'%s'", 'timestamp' => '%d',
                      'uid' => '%d', 'format' => '%d');
@@ -662,22 +658,25 @@ 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',
                     'promote' => '%d', 'sticky' => '%d');
-
+  $update_node = TRUE;
   //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) .')';
+    $node->nid = db_last_insert_id();
     $revisions_query = 'INSERT INTO {node_revisions} ('. implode(', ', array_keys($revisions_table_types)) .') VALUES ('. implode(', ', $revisions_table_types) .')';
+    $node->vid = db_last_insert_id();
+    $op = 'insert';
   }
   else {
     $arr = array();
@@ -688,6 +687,7 @@ function node_save(&$node) {
     $node_query = 'UPDATE {node} SET '. implode(', ', $arr) .' WHERE nid = %d';
     if (!empty($node->revision)) {
       $revisions_query = 'INSERT INTO {node_revisions} ('. implode(', ', array_keys($revisions_table_types)) .') VALUES ('. implode(', ', $revisions_table_types) .')';
+      $node->vid = db_last_insert_id();
     }
     else {
       $arr = array();
@@ -696,7 +696,12 @@ function node_save(&$node) {
       }
       $revisions_table_values[] = $node->vid;
       $revisions_query = 'UPDATE {node_revisions} SET '. implode(', ', $arr) .' WHERE vid = %d';
+      $update_node = FALSE;
     }
+    $op = 'update';
+  }
+  if ($update_node) {
+    db_query('UPDATE node SET vid = %d WHERE nid = %d', $node->vid, $node->nid);
   }
 
   // Insert the node into the database:
@@ -704,14 +709,8 @@ function node_save(&$node) {
   db_query($revisions_query, $revisions_table_values);
 
   // Call the node specific callback (if any):
-  if ($node->is_new) {
-    node_invoke($node, 'insert');
-    node_invoke_nodeapi($node, 'insert');
-  }
-  else {
-    node_invoke($node, 'update');
-    node_invoke_nodeapi($node, 'update');
-  }
+  node_invoke($node, $op);
+  node_invoke_nodeapi($node, $op);
 
   // Update the node access table for this node.
   node_access_acquire_grants($node);
Index: modules/node/node.schema
===================================================================
RCS file: /cvs/drupal/drupal/modules/node/node.schema,v
retrieving revision 1.1
diff -u -p -r1.1 node.schema
--- modules/node/node.schema	25 May 2007 12:46:45 -0000	1.1
+++ modules/node/node.schema	4 Jun 2007 03:33:47 -0000
@@ -5,7 +5,7 @@ function node_schema() {
   $schema['node'] = array(
     'fields' => array(
       'nid'      => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
-      'vid'      => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
+      'vid'      => array('type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE, 'default' => 0),
       'type'     => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => ''),
       'language' => array('type' => 'varchar', 'length' => 12, 'not null' => TRUE, 'default' => ''),
       'title'    => array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'default' => ''),
@@ -65,7 +65,7 @@ function node_schema() {
 
   $schema['node_revisions'] = array(
     'fields' => array(
-      'nid'       => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE),
+      'nid'       => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
       'vid'       => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
       'uid'       => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
       'title'     => array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'default' => ''),
Index: modules/taxonomy/taxonomy.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/taxonomy/taxonomy.module,v
retrieving revision 1.356
diff -u -p -r1.356 taxonomy.module
--- modules/taxonomy/taxonomy.module	28 May 2007 06:08:46 -0000	1.356
+++ modules/taxonomy/taxonomy.module	4 Jun 2007 03:33:48 -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;
   }
Index: modules/user/user.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/user/user.module,v
retrieving revision 1.789
diff -u -p -r1.789 user.module
--- modules/user/user.module	3 Jun 2007 06:17:58 -0000	1.789
+++ modules/user/user.module	4 Jun 2007 03:33:50 -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']));
@@ -1788,8 +1787,8 @@ function user_admin_access_add($mask = N
       form_set_error('mask', t('You must enter a mask.'));
     }
     else {
-      $aid = db_next_id('{access}_aid');
       db_query("INSERT INTO {access} (aid, mask, type, status) VALUES ('%s', '%s', '%s', %d)", $aid, $edit['mask'], $edit['type'], $edit['status']);
+      $aid = db_last_insert_id('access', 'aid');
       drupal_set_message(t('The access rule has been added.'));
       drupal_goto('admin/user/rules');
     }
