I'm working on making the community-managed taxonomy module so I'm looking at taxonomy.module more closely than is probably healthy for either of us...

This is either a request for an explanation of a finer point of SQL that I don't understand, or a suggestion that 25 characters, namely "ORDER BY v.weight, v.name", can be struck from taxonomy.module. I'll make a patch if the latter.

db_query('SELECT v.*, n.type FROM {vocabulary} v LEFT JOIN {vocabulary_node_types} n ON v.vid = n.vid WHERE v.vid = %d ORDER BY v.weight, v.name', $vid);

But v.vid is a primary key so all v (vocabulary) components are the same for each row of the result set. In fact, this is relied on later in the code where the entire object is repeatedly created ( while ($voc = db_fetch_object($result)) with only the (vocabulary node) type aggregated and added on each time, until done.

So is there any purpose to the two-part ORDER BY clause?

... now that I've expended the most discussion for least importance in the history of Drupal...

The whole function, for reference:

/**
 * Return the vocabulary object matching a vocabulary ID.
 *
 * @param $vid
 *   The vocabulary's ID
 *
 * @return Object
 *   The vocabulary object with all of its metadata.
 *   Results are statically cached.
 */
function taxonomy_get_vocabulary($vid) {
  static $vocabularies = array();

  if (!array_key_exists($vid, $vocabularies)) {
    $result = db_query('SELECT v.*, n.type FROM {vocabulary} v LEFT JOIN {vocabulary_node_types} n ON v.vid = n.vid WHERE v.vid = %d ORDER BY v.weight, v.name', $vid);
    $node_types = array();
    while ($voc = db_fetch_object($result)) {
      $node_types[] = $voc->type;
      unset($voc->type);
      $voc->nodes = $node_types;
      $vocabularies[$vid] = $voc;
    }
  }

  return $vocabularies[$vid];
}

ben, Agaric Design Collective

CommentFileSizeAuthor
#3 taxonomy_vocabulary_load.patch.patch935 byteswim leers

Comments

wim leers’s picture

We're selecting only one vocabulary, so I think you're right. A vid is unique, so only one result can be found, no need to order it by v.weight or v.name then.

wim leers’s picture

Version: 5.x-dev » 6.x-dev
wim leers’s picture

Assigned: Unassigned » wim leers
Status: Active » Needs review
StatusFileSize
new935 bytes

Patch attached.

wim leers’s picture

Title: Unnecessary code in taxonomy_get_vocabulary()? "ORDER BY" on fields that will be identical for an entire result » Unnecessary "ORDER BY" in taxonomy_vocabulary_load

Better title.

dmitrig01’s picture

Status: Needs review » Reviewed & tested by the community

RTBC

gerhard killesreiter’s picture

ack, the order by doesn't make any sense in this case.

dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed! Thanks a bundle. Good catch too. :)

Anonymous’s picture

Status: Fixed » Closed (fixed)