The hook_query_alter() and hook_query_TAG_alter() mechanism only works on Select queries, and only on the outer query. Subqueries are not altered.

This should either be fixed so they can be tagged/altered, or documented. I'm not sure which.

If the answer is documentation, it should be documented in the hook doc headers (for api.drupal.org) as well as on http://drupal.org/node/310077

If the answer is to alter the queries, this would be done presumably in SelectQuery::__toString() in the section right here:

     if ($table['table'] instanceof SelectQueryInterface) {
        $table_string = '(' . (string)$table['table'] . ')';
      }

You would need to call the alter stuff there instead of just doing (string) cast.

Presumably, that would be by taking this out of SelectQuery::preExecute() and making a separate function:

    if (isset($this->alterTags)) {
      drupal_alter('query', $query);
      foreach ($this->alterTags as $tag => $value) {
        drupal_alter("query_$tag", $query);
      }
    }
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

moshe weitzman’s picture

If a subquery has a tag, it should get altered separately IMO. So, should be fixed IMO.

jhodgdon’s picture

I tend to agree. Patch wouldn't be difficult, but we'd also need a test...

Crell’s picture

Title: Subqueries are not altered. Fix or document. » Subqueries are not altered. Fix.

After thinking about it, I would agree. Let's do. jhodgdon, can you take a stab at a patch?

jhodgdon’s picture

Assigned: Unassigned » jhodgdon

OK.

jhodgdon’s picture

Assigned: jhodgdon » Unassigned
Status: Active » Needs review
FileSize
2.75 KB

Here's a patch, with a test and everything. I am running all the Database group tests and they seem to be fine so far. Let's see if the test bot is any faster.

Berdir’s picture

There is already a Query alter test class, any reason why not to extend that one?

jhodgdon’s picture

Ummm...

I am not sure what good extending it would do, since all it has that we could use is a setup function? There is already

class DatabaseAlterTestCase extends DatabaseTestCase {
class DatabaseAlter2TestCase extends DatabaseTestCase {

This adds

class DatabaseAlterSubqueryTestCase extends DatabaseTestCase {

I don't see why this is bad or any different?

Berdir’s picture

I don't even know why we have two different classes already, can't be the size of them as there are much bigger test classes in core than those :)

jhodgdon’s picture

Yes, all three of them could be combined into one class. But I'm afraid of getting a "killing kittens" message if I do that in this patch to combine the first two into one class. So can we do that later on a separate issue?

Crell’s picture

Status: Needs review » Needs work

I discussed test structure of that sort with Jimmy in SF a bit. We really should not have a separate test class for this patch; let's incorporate the test into an existing alter test class. Let's do that and get this patch in ASAP.

There may be other classes we can combine later, but we'll do that later.

jhodgdon’s picture

Status: Needs work » Needs review
FileSize
3 KB

OK. Here's a patch with all of the query alter classes combined into one.

Status: Needs review » Needs work

The last submitted patch, 701888-onetestclass.patch, failed testing.

jhodgdon’s picture

Status: Needs work » Needs review
FileSize
3.13 KB

That's strange. I am not seeing a PHP error, and the database query alter test is running fine on my machine. I'm meanwhile running all the database tests, and I'll also reroll the patch just in case. ??

Crell’s picture

Status: Needs review » Reviewed & tested by the community

Bot looks happy. I look happy. Everyone's happy. :-)

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD. Thanks.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.