[drupal-devel] db_rewrite_sql handbook update
Negyesi Karoly
karoly at negyesi.net
Mon Feb 21 13:36:52 UTC 2005
If you have a module which queries the node table or implements a node type and holds the information in its own table with a nid field, then the following applies.
The functions node_access_join_sql() and node_access_where_sql() should not be used any more but the SELECT-queries should be wrapped in a db_rewrite_sql() call.
If you have used DISTINCT(nid) -- because of node_access_join_sql() -- you no longer need it, replace it simply with n.nid. If you have SELECT *, please replace it with SELECT n.nid, n.* -- this way the db_rewrite_sql() function can rewrite the query to use DISTINCT(nid) should there be a need for it.
Always use table name before the field names, especially before nid because other tables may be JOINed during the rewrite process.
Example:
<?php
// Drupal 4.5:
$nodes = db_query_range('SELECT DISTINCT(n.nid) FROM {node} n '. node_access_join_sql() .' WHERE '. node_access_where_sql() .' AND n.promote = 1 AND n.status = 1 ORDER BY n.created DESC', 0, 15);
// Drupal 4.6:
$nodes = db_query_range(db_rewrite_sql('SELECT n.nid FROM {node} n WHERE n.promote = 1 AND n.status = 1 ORDER BY n.created DESC'), 0, 15);
?>
If you are not using the node table, then you shall pass the table name from which you SELECTing the nodes. For example
<?php
$result = db_query(db_rewrite_sql("SELECT f.nid, f.* from {files} f WHERE filepath = '%s'", 'f'), $file);
?>
note the 'f' parameter of db_rewrite_sql().
More information about the drupal-devel
mailing list