d7: to be inserted ID, is there a cleaner way? I need to know the 'to be inserted ID'  

I wrote the following function, it works, however, I'm sure this is not the way to do it.

 function insert_node_style($style) {
  // get latest id
  $id = db_query("SELECT MAX(nid) FROM {node}")-&gt;fetchCol('nid');
  $id = array_shift(array_values($id));
  $new_nid = $id + '1';
  // insert nid, vid and style
  db_insert('nodestyle_node') -&gt;fields(array(
    'nid' =&gt; $new_nid,
    'vid' =&gt; '1',
    'style' =&gt; $style,
  ))
  -&gt;execute();
}


Is there are cleaner/drupal way to achieve the same result?
I don't think there's a 100% reliable way to get the ID that will be used next. Even the method you've got won't work completely. Take this example:


Insert a node into the database (it has the new nid of 1 for arguments sake)
Insert another node into the database (it has the new nid of 2)
Then delete that second node
Add a new node to the database again. This time it will have a nid of 3, not 2 (as would be expected from your code).


The problem is that the MySQL AUTO_INCREMENT value exists completely independently of the primary key ids that are currently in the database; so unless you reset the auto increment counter each time there's no way to reliably determine the next id.

Also in a system with multiple simultaneous accessess to the database using code like that introduces potential race conditions.

It would be far simpler to perform this task when the node has been saved, perhaps in hook_node_save().

Oh and one other little thing, the vid you're inserting should not be 1, it'll be determined for you by the system. Using 1 for every node will link every single node to one revision in the node_revision table...probably not what you're looking for.