Problem/Motivation

If database / table prefixes are used, you get an error like

user warning: Table 'vfst_trunk.uuid_term_data' doesn't exist query: INSERT INTO uuid_term_data (tid, uuid) VALUES (72, 'a5d1fed9-331f-11e2-b127-00ffd5e12856') in C:\webdev\projekte\wsr.dev\www\sites\all\modules\contrib\uuid\uuid.module on line 289.

user warning: Table 'vfst_trunk.uuid_vocabulary' doesn't exist query: SELECT uuid FROM uuid_vocabulary WHERE vid = 6 in C:\webdev\projekte\wsr.dev\www\sites\all\modules\contrib\uuid\uuid.module on line 292.

Here, "vfst_trunk" is the prefix used. However, this prefix is missing in the issued SQL statements (see above).

Proposed resolution

uuid.module does not use the {}-syntax correctly on lines 284ff:

    case 'insert':
      if (empty($array['uuid']) || !uuid_is_valid($array['uuid'])) {
        $array['uuid'] = uuid_uuid();
      }
      db_query("INSERT INTO {$table} ($keyfield, uuid) VALUES (%d, '%s')",
        $key, $array['uuid']);
      break;
    case 'update':
      $existing_uuid = db_result(db_query("SELECT uuid FROM {$table} WHERE $keyfield = %d", $key));
      if ($existing_uuid) {
        // If there is an existing uuid, but no new one, remove the existing one.
        if (!isset($array['uuid']) || empty($array['uuid'])) {
          uuid_taxonomy('delete', $type, $array);
        }
        // If the new uuid is not the same, update it.
        elseif ($array['uuid'] != $existing_uuid) {
          db_query("UPDATE {$table} SET uuid = '%s' WHERE $keyfield = %d", $array['uuid'], $key);
        }
        // Otherwise, don't do anything. No update necessary.
      }

The braces used are interpreted as PHP expressions and db_query does not "see" any of them.
For example, the last db_query should be written as

db_query("UPDATE {%s} SET uuid = '%s' WHERE $keyfield = %d", $table, $array['uuid'], $key);

Remaining tasks

(reviews needed, tests to be written or run, documentation to be written, etc.)

User interface changes

(new or changed features/functionality in the user interface, modules added or removed, changes to URL paths, changes to user interface text)

API changes

(API changes/additions that would affect module, install profile, and theme developers, including examples of before/after code if appropriate)

Original report by [username]

// Text of original report here.

Reason:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

apotek’s picture

Version: 6.x-1.0-rc2 » 6.x-1.x-dev
FileSize
3.96 KB

Bug still exists in dev, so updated to 6.x-1.x-dev.

Attaching patch, so setting status to "needs review."

I started re-writing this as suggested (adding proper %s placeholders and using variable substitution to get the tablenames into the queries properly), but as I was doing this, I realized that the code was becoming pretty illegible, since all the parts of the queries were essentially variables: tables, keys, values, all of it.

So I looked at the whole function again and realized that the switch($type), while reducing the number of queries that would have to be written (since it sets variables for the queries), was contributing to the illegibility in the end, and also assumed that the table structure for vocabulary items and term items would be the same indefinitely.

So I decided to remove that, and write a bunch more if/else statements. While this means the source code is longer, it actually represents *less* execution time since there are fewer variables being set, and the if/else is only run once per function call regardless, so it's not like we are testing the $type more than once in any one iteration of this function. Also, since terms tend to outnumber vocabularies, I test for term first, and falling back to the rarer case vocabulary type, which should further improve speed.

While I was re-engineering this function, I took the opportunity to move away from the hook_taxonomy() poorly named $array variable, and call it $item instead, and made the recursive parts of the function (where we call uuid_taxonomy() from within uuid_taxonomy()) a little more future proofed by putting return statements in front of them, in case someone adds further processing at the end of uuid_taxonomy().

apotek’s picture

Status: Active » Needs review
apotek’s picture

Status: Needs review » Active
apotek’s picture

Status: Active » Needs review
apotek’s picture

Looks like this got stuck in the queue. Any ideas how to get this going again?

apotek’s picture

SomeStupid’s picture

Thanks for providing a patch so quickly!
I do not have the time to test your patch currently, but I reviewed your changes: this looks good to me. :-)
Will try out the patch as soon as possible.

ryan_courtnage’s picture

I just stumbled into this problem when trying to run some simpletests on code that creates uuids for taxonomy terms.

An alternative way to fix this problem is to just use double curly braces. Patch to uuid.module would look like

@@ -285,11 +285,11 @@
       if (empty($array['uuid']) || !uuid_is_valid($array['uuid'])) {
         $array['uuid'] = uuid_uuid();
       }
-      db_query("INSERT INTO {$table} ($keyfield, uuid) VALUES (%d, '%s')",
+      db_query("INSERT INTO {{$table}} ($keyfield, uuid) VALUES (%d, '%s')",
         $key, $array['uuid']);
       break;
     case 'update':
-      $existing_uuid = db_result(db_query("SELECT uuid FROM {$table} WHERE $keyfield = %d", $key));
+      $existing_uuid = db_result(db_query("SELECT uuid FROM {{$table}} WHERE $keyfield = %d", $key));
       if ($existing_uuid) {
         // If there is an existing uuid, but no new one, remove the existing one.
         if (!isset($array['uuid']) || empty($array['uuid'])) {
@@ -297,7 +297,7 @@
         }
         // If the new uuid is not the same, update it.
         elseif ($array['uuid'] != $existing_uuid) {
-          db_query("UPDATE {$table} SET uuid = '%s' WHERE $keyfield = %d", $array['uuid'], $key);
+          db_query("UPDATE {{$table}} SET uuid = '%s' WHERE $keyfield = %d", $array['uuid'], $key);
         }
         // Otherwise, don't do anything. No update necessary.
       }
@@ -306,7 +306,7 @@
       }
       break;
     case 'delete':
-      db_query("DELETE FROM {$table} WHERE $keyfield = %d", $keyfield, $key);
+      db_query("DELETE FROM {{$table}} WHERE $keyfield = %d", $keyfield, $key);
       break;
   }
 }
apotek’s picture

An alternative way to fix this problem is to just use double curly braces

@ryan_courtnage: Yes, that will make it work, but since we're using Drupal we need to do more than make it work, we should use the Drupal DB API correctly, as the patch above does.

@SomeStupid: Any idea how to get this patch into the testing queue. Drupal.org is not obeying. Thanks.

Oh, just found out that issue testing is postponted if a branch is deemed unstable. What do we need to do to get the dev branch marked stable?

ryan_courtnage’s picture

@apotek

we should use the Drupal DB API correctly

Using a variable to hold the table name is not incorrect AFAIK. It's quite common, and high-quality modules such as Views, CCK and Features do it.

apotek’s picture

Yes, using a variable to hold the table name is normal, but inserting a variable straight into a query is not good practice. That's what the Drupal db api is for, to handle sql-injection and other filtering.

Don't argue with me, argue with the docs! :) http://api.drupal.org/api/drupal/includes%21database.inc/group/database/6

apotek’s picture

@SomeStupid So it looks like the dev branch here is still marked unstable, which is blocking all related patches from testing. What can we help with in order to get this whole project moving? thanks.

SomeStupid’s picture

Status: Needs review » Reviewed & tested by the community

Sorry, but I don't know much about the Drupal procedures. All I found were the following instructions:

https://drupal.org/empty-git-master
https://drupal.org/node/324268/git-instructions/6.x-1.x

Maybe we should forward this to the maintainer?

BTW: I reviewed your changes. They look good to me, much more readable than before. :-)

skwashd’s picture

Issue summary: View changes

Drupal 6 core is no longer supported. We are no longer supporting 6.x-1.x versions of this module. I am closing this issue as won't fix.

skwashd’s picture

Status: Reviewed & tested by the community » Closed (won't fix)