--- C:/Documents and Settings/poda/Desktop/uuid.module	Wed Sep 08 17:07:14 2010
+++ C:/Documents and Settings/poda/workspace/ehealth/build/dist/modules/contrib/uuid/uuid.module	Tue Dec 14 13:29:29 2010
@@ -33,10 +33,12 @@
 
   switch ($op) {
     case 'load':
-      return array(
-        'uuid' => db_result(db_query('SELECT uuid FROM {uuid_node} WHERE nid = %d', $node->nid)),
-        'revision_uuid' => db_result(db_query('SELECT uuid FROM {uuid_node_revisions} WHERE vid = %d', $node->vid))
-      );
+      if ($arr = db_fetch_array(db_query('SELECT un.uuid AS uuid, unr.uuid AS revision_uuid FROM {uuid_node} un LEFT JOIN {uuid_node_revisions} unr ON unr.vid=%d WHERE un.nid=%d', $node->vid, $node->nid))) {
+        return $arr;
+      }
+      else {
+        return array('uuid' => FALSE, 'revision_uuid' => FALSE, 'loaded_revision_uuid' => FALSE);
+      }
     case 'insert':
       // Handle the case where automatic UUID generation is turned OFF but a
       // node is created, node_save(), intentionally with the $node->uuid
@@ -57,13 +59,14 @@
       }
       break;
     case 'update':
-      if (isset($node->revision)) {
-        if (isset($node->revision_uuid) && uuid_is_valid($node->revision_uuid)) {
+      if (isset($node->revision) && in_array($node->type, $automatic_types)) {
+        $exists = db_result(db_query('SELECT 1 FROM {uuid_node_revisions} WHERE vid = %d', $node->vid));
+        if (!$exists) {
+          if (!isset($node->revision_uuid) || !uuid_is_valid($node->revision_uuid)) {
+            $node->revision_uuid = uuid_uuid();
+          }
           db_query("INSERT INTO {uuid_node_revisions} (vid, uuid) VALUES (%d, '%s')", $node->vid, $node->revision_uuid);
         }
-        else {
-          db_query("INSERT INTO {uuid_node_revisions} (vid, uuid) VALUES (%d, '%s')", $node->vid, uuid_uuid());
-        }
       }
       break;
     case 'delete':
@@ -79,6 +82,9 @@
  * Returns the node associated with a UUID. Uses db_rewrite_sql() to ensure
  * node_access rules are preserved.
  *
+ * @param string $revision_uuid
+ *   The uuid to use for the node lookup.
+ *
  * @return
  *   Either the $node object, or FALSE on failure.
  */
@@ -184,7 +190,7 @@
       'field' => 'vid',
     ),
   );
-  
+
   if (array_key_exists($type, $types) && in_array($array['vid'], variable_get('uuid_automatic_for_taxonomy', array()))) {
     $field = $types[$type]['field'];
     switch ($op) {
@@ -217,7 +223,7 @@
         db_query("INSERT INTO {uuid_comments} (cid, uuid) VALUES (%d, '%s')", $a1['cid'], uuid_uuid());
       }
       break;
-      
+
     case 'update':
       if (isset($a1['uuid']) && uuid_is_valid($a1['uuid'])) {
         $exists = db_result(db_query('SELECT 1 FROM {uuid_comments} WHERE cid = %d', $a1['cid']));
