Replace db_rewrite_sql() with hook_query_alter().

Crell - August 23, 2008 - 22:43
Project:Drupal
Version:7.x-dev
Component:database system
Category:task
Priority:critical
Assigned:Crell
Status:closed
Issue tags:dc2009 code sprint, Needs Documentation, Quick fix
Description

Now that the new database layer supports dynamic runtime alteration of dynamic queries using hook_query_alter(), the time has finally come to exterminate db_rewrite_sql() and replace it with a proper structured approach using an alter hook.

1) Convert all db_rewrite_sql()-using queries to dynamic queries using the new query builder.

2) Tag those queries that should be manipulated by the node access system with "node_access".

3) Write the alter hook to add the necessary joins to do node access stuff.

4) Delete all hook_db_rewrite_sql() code.

5) Profit!!!!

#1

Susurrus - August 23, 2008 - 23:44
Title:Replace db_write_sql() with hook_query_alter().» Replace db_rewrite_sql() with hook_query_alter().

#2

catch - August 25, 2008 - 00:33

#3

drewish - August 30, 2008 - 11:41

subscribe

#4

Damien Tournoud - September 4, 2008 - 17:07

New plan, in the correct order:

1) Implement node_query_alter() (a DISABLED_node_query_alter() is in, but is marked as "don't work yet")
2) Convert all db_rewrite_sql() that rewrite node queries using the new query builder, and tag them with 'node_access'
3) Remove node_db_rewrite_sql() (yeah!!)
4) Think of a plan for other rewritten queries (taxonomy, ...)
5) Execute the plan defined in 4
6) Remove db_rewrite_sql() (re-yeah!!)

#5

webchick - September 4, 2008 - 17:25

7) Profit! ;)

I greatly look forward to committing this patch when it's ready. :D

#6

BioALIEN - September 15, 2008 - 23:25

Subscribing.

#7

Crell - October 6, 2008 - 04:06
Status:active» needs review

OK, I'm marking this needs review to get some attention. :-) I have what I think is a working node_query_alter() implementation, at least from reading the existing code. However, I really don't know how best to test it. I've taken a stab at starting the relevant unit tests, but this will absolutely require input from one of the 4-5 people who actually grok node access in the first place. If you are one of them, please jump in and see if you can confirm (with tests) that my code actually does something useful.

AttachmentSize
node_access.patch 5.03 KB
Testbed results
node_access.patchfailedFailed: Failed to apply patch. Detailed results

#8

moshe weitzman - October 9, 2008 - 20:27

I was just thinking about this. i will review and give some feedback.

In order to test this, it is best IMO to upgrade http://cvs.drupal.org/viewvc.py/drupal/contributions/docs/developer/exam... to use the new API.

#9

moshe weitzman - October 11, 2008 - 14:25

I committed an update for that node_access_example module so it can be used in HEAD to generate sensible node access records ... I hope to look at this patch soon.

#10

moshe weitzman - October 12, 2008 - 01:36

Is there an example out there of of a query that used to do pager_query() but now does dynamic select? If not, could you give some pointers or even just paste in a converted query from node_page_default() ... I am assuming that we must do dynamic in order to add a tag.

Also, I would like to remove the generic node_query_alter hook and instead go to the specific ones that form_alter does like hook_form_alter_[form_id]. we would do hook_query_alter_[tag].

#11

Crell - October 12, 2008 - 19:52

All pager queries will eventually end up dynamic, but I need to take care of #299267: Add "Extender" support to SELECT query builder first as that's how I plan to handle pager and tablesort queries. Yes, tagging only works on dynamic queries. However, paging and node_access should be independent of each other, shouldn't they?

I considered splitting out query_alter a la FAPI, but we would have to keep at least the common one. If not, then it would be impossible (or at least very very difficult) to operate on queries based on multiple tags (which are supported). I left the split version out of the original patch mostly for simplicity, but I'm fine with adding it, just not removing the base one.

#12

moshe weitzman - October 12, 2008 - 20:46
Status:needs review» needs work

Yes paging and node access are independant but node access queries are always node listings by definition. Each one that I can find is either a pager_query or a db_query_range(). I don't know how to add a tag to those.

#13

moshe weitzman - October 13, 2008 - 02:36
Assigned to:Anonymous» moshe weitzman
Status:needs work» needs review

With Crell's help, I have worked though the bugs in the previous patch and converted one forum block to use the new node access system. It is going to be a biggish job to convert all the other node listing queries to be dynamic queries so I'd prefer to have that happen in a follow up patch. Also, there are some patches in the queue that will make those queries simpler (e.g. addFields method).

The forum block does not use node_title_list() since I could not see how to use it now that the query builder can alias our fields, willy nilly.

There is a small bug fix to the DB API - added missing parens.

AttachmentSize
mw.patch 5.48 KB
Testbed results
mw.patchfailedFailed: Failed to apply patch. Detailed results

#14

moshe weitzman - October 13, 2008 - 12:01

Actually, we can use node_title_list() as is. Simpler patch attached.

AttachmentSize
mw.patch 5.01 KB
Testbed results
mw.patchfailedFailed: Failed to apply patch. Detailed results

#15

moshe weitzman - October 13, 2008 - 14:04

This patch has no dependencies so feel free to commit it once it is RTBC :)

In order to easily convert the rest of the node listing queries, we ought to get these in:

#16

nigel - October 20, 2008 - 02:47

subscribe

#17

Crell - November 8, 2008 - 07:44
Status:needs review» needs work

The bug fix for nested conditions was committed separately (#331737: Nested conditions don't work), so this won't apply now.

#18

redndahead - December 22, 2008 - 02:50
Status:needs work» needs review

Reroll

AttachmentSize
db_rewrite.patch 3.98 KB
Testbed results
db_rewrite.patchfailedFailed: Failed to apply patch. Detailed results

#19

chx - December 28, 2008 - 10:39
Status:needs review» reviewed & tested by the community

I think we are good to go, the patch is rather simple. Yay db_rewrite_sql dies.

#20

Dries - December 29, 2008 - 06:43

While this patch is relatively easy indeed, I'd still like to get a better handle on how other queries would get converted. Therefore, I'm going to investigate #320591: Tag specific alter hook and #299267: Add "Extender" support to SELECT query builder some more first to see if that helps me wrap my head around that question.

#21

Crell - December 29, 2008 - 07:04

@Dries: Here's the short version, if I can manage that... :-)

hook_query_alter() is intended, primarily, to replace db_rewrite_sql() and hook_views_query_alter(). Anywhere we previously would use db_rewrite_sql() you'd now use a dynamic query and add a node_access tag to it. node_query_alter() does almost exactly the same thing as node_rewrite_sql(), but in a structured fashion rather than a hacky string parsing. We can then potentially do other things elsewhere if people come up with them, but that's the main use case. (And I will honestly defer to Moshe on the exact implementation, as I still don't fully grok node_access. :-) )

The tag-specific alter hook makes no change to that. It's just a minor performance/usability improvement with the exact same concept as hook_form_*_alter(). All arguments for that apply here as well.

Extenders are an entirely different animal. They're for cases where you need additional functionality on a select query that is controlled at the point you're creating the query, whereas hook_query_alter() is for modifying a set of queries in some standard way from elsewhere in Drupal. They're really unrelated sorts of functionality.

#22

Dries - December 29, 2008 - 22:29
Status:reviewed & tested by the community» fixed

Committed to CVS HEAD. Thanks.

#23

Crell - December 29, 2008 - 22:44

Woohoo! Thanks, Dries!

Moshe, can you update the handbook pages if necessary? I know we had discussed what the tag structure would be, and as I said you know the implementation better than I.

http://drupal.org/node/310077

#24

moshe weitzman - December 30, 2008 - 01:20

Those handbook docs need no further updates IMO. The upgrade docs need to mention this but we really need to commit #320591: Tag specific alter hook and #299267: Add "Extender" support to SELECT query builder to do a good job explaining the conversion process.

#25

Crell - December 30, 2008 - 02:21

I've added an item to the upgrade docs. Once we have all of the various subystems in place we can add a dedicated handbook page for how to update the most complex case (db_rewrite_sql(), paged, with tablesort) to Drupal 7. Let's continue those threads in their own threads.

#26

System Message - January 13, 2009 - 02:30
Status:fixed» closed

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

#27

webchick - March 4, 2009 - 19:39
Status:closed» active

hook_query_alter() needs an example in the docs.

#28

Crell - March 7, 2009 - 17:39
Assigned to:moshe weitzman» Crell
Status:active» needs review
Issue tags:+dc2009 code sprint, +Quick fix

OK, fine, here's your docs. :-)

AttachmentSize
query_alter_docs.patch 1.91 KB
Testbed results
query_alter_docs.patchfailedFailed: Invalid PHP syntax in modules/system/system.api.php. Detailed results

#29

System Message - March 7, 2009 - 17:50
Status:needs review» needs work

The last submitted patch failed testing.

#30

Crell - March 7, 2009 - 21:30
Status:needs work» needs review

What syntax error? Stupid bot...

AttachmentSize
query_alter_docs.patch 1.93 KB
Testbed results
query_alter_docs.patchfailedFailed: Invalid PHP syntax in modules/system/system.api.php. Detailed results

#31

System Message - March 7, 2009 - 21:40
Status:needs review» needs work

The last submitted patch failed testing.

#32

Crell - March 8, 2009 - 03:09
Status:needs work» needs review

Third time's the charm.

AttachmentSize
query_alter_docs.patch 1.08 KB
Testbed results
query_alter_docs.patchfailedFailed: Failed to apply patch. Detailed results

#33

chx - April 19, 2009 - 18:43

Reposting for bot's sake.

AttachmentSize
query_alter_docs_1.patch 1.08 KB
Testbed results
query_alter_docs_1.patchfailedFailed: Failed to apply patch. Detailed results

#34

Dries - April 20, 2009 - 07:42
Status:needs review» fixed

Committed to CVS HEAD. Thanks.

#35

RandalK - April 20, 2009 - 09:16
Status:fixed» needs work

Don't know if you noticed but there's a typo in this patch

<?php
$td_alias
= $query->join('taxonomy_term_data', 'td', '***tn.tid*** = tn.tid');
?>

Seems like this should be td.tid.

#36

redndahead - April 20, 2009 - 18:04
Status:needs work» fixed

@RandalK The original patch that has the typo was committed quite a bit ago. Can you check core and make sure the typo still exists and open up a new issue maybe referencing this issue?

#37

Dave Reid - April 20, 2009 - 18:42
Status:fixed» needs review

It is still present in forum_block_view():

        $query = db_select('node', 'n');
        $tn_alias = $query->join('taxonomy_term_node', 'tn', 'tn.vid = n.vid');
        $td_alias = $query->join('taxonomy_term_data', 'td', 'tn.tid = tn.tid');
        $l_alias = $query->join('node_comment_statistics', 'l', 'n.nid = l.nid');

AttachmentSize
299176-forum-block-join-D7.patch 911 bytes
Testbed results
299176-forum-block-join-D7.patchpassedPassed: 10794 passes, 0 fails, 0 exceptions Detailed results

#38

Berdir - April 20, 2009 - 22:36

I'm assuming that there are no tests for that block, or it would have been caught. Probably not as part of this patch, but adding a test would be nice :)

#39

Dries - April 21, 2009 - 10:58
Status:needs review» needs work

Committed to CVS HEAD. I'm marking this 'code needs work' because we don't have a test yet for the forum block.

#40

Dave Reid - April 21, 2009 - 14:06
Status:needs work» fixed

Created a separate issue in forum.module for the followup and now we can mark this as fixed.
#440344: Tests needed for forum blocks

#41

System Message - May 5, 2009 - 14:10
Status:fixed» closed

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

 
 

Drupal is a registered trademark of Dries Buytaert.