[development] db_rewrite_sql

Rob Barreca rob at electronicinsight.com
Wed Apr 18 01:36:24 UTC 2007


> Looking at this function which raps db_query calls and allows modules 
> to alter the query, I was wondering how a hook_db_rewrite_sql is 
> supposed to distinguish the queries that it wants to alter from the 
> queries that it wants to leave alone? 
This is something that still needs some work IMO. Right now you can 
access the primary table (i.e. 'n' for node, but I always hated how it 
didn't use the full pre-prefixed table name) and field (i.e. 'nid'). 
This is pretty scary considering we now have such clean implementations 
for other drupal_alter()isms.

I know I have participated in a discussion like this before, but 
couldn't find the issue. So here it is again: What about passing a 
unique identifier along with every call of db_rewrite_sql? Instead of 
just wrapping db_rewrite_sql() we could also pass $module and $key so we 
have accurate namespaces for each query. (Or, we could just do it like 
hook_mail_alter(), but I think there could be namespace conflicts if a 
module named foo_module's foo_module_key screws with a module named 
foo's foo_module_key :P)

I know we have $args, but that is already being used for actual args IIRC.

What about replacing

db_rewrite_sql('SELECT * FROM ...', 'node_query_select_foo', 'n', 'nid', 
$args)

with some form of drupal_alter() call

function db_rewrite_sql(...) {
  $query = 'SELECT * FROM ...';
  drupal_alter('query', $query, 'node_query_select_foo', 'node', 'nid', 
$args)
  return $query;
}

I feel like this is much cleaner so you know exactly which query you are 
rewriting as opposed to just analyzing the query, primary table/field. 
If we made this really clean and precise maybe we could open to door to 
writing even better access modules. But then again...code is gold.

Rob Roy Barreca
Founder and COO
Electronic Insight Corporation
http://www.electronicinsight.com
rob at electronicinsight.com



Ashraf Amayreh wrote:
> Looking at this function which raps db_query calls and allows modules 
> to alter the query, I was wondering how a hook_db_rewrite_sql is 
> supposed to distinguish the queries that it wants to alter from the 
> queries that it wants to leave alone?
>
> Only way I can think of is to do a string match on large parts of the 
> original query, but isn't this quite messy, or am I missing something? 
> Could someone please shed the light on this issue?


More information about the development mailing list