db_rewrite_sql

includes/database.inc, line 236

Versions
4.6 – 7
db_rewrite_sql($query, $primary_table = 'n', $primary_field = 'nid', $args = array())

Rewrites node queries.

Parameters

$query Query to be rewritten.

$primary_table Name or alias of the table which has the primary key field for this query. Possible values are: comments, forum, node, term_data, vocabulary.

$primary_field Name of the primary field.

$args An array of arguments, passed to the implementations of hook_db_rewrite_sql.

Return value

The original query with JOIN and WHERE statements inserted from hook_db_rewrite_sql implementations. nid is rewritten if needed.

Related topics

▾ 37 functions call db_rewrite_sql()

archive_calendar in modules/archive.module
Generates a monthly calendar, for display in the archive block.
archive_page in modules/archive.module
Menu callback; lists all nodes posted on a given date.
blog_block in modules/blog.module
Implementation of hook_block().
blog_feed_last in modules/blog.module
Displays an RSS feed containing recent blog entries of all users.
blog_feed_user in modules/blog.module
Displays an RSS feed containing recent blog entries of a given user.
blog_page_last in modules/blog.module
Displays a Drupal page containing recent blog entries of all users.
blog_page_user in modules/blog.module
Displays a Drupal page containing recent blog entries of a given user.
book_admin_orphan in modules/book.module
Menu callback; displays a listing of all orphaned book pages.
book_admin_view_book in modules/book.module
book_block in modules/book.module
Implementation of hook_block().
book_location in modules/book.module
Return the path (call stack) to a certain book page.
book_location_down in modules/book.module
book_menu in modules/book.module
Implementation of hook_menu().
book_next in modules/book.module
Fetch the node object of the next page of the book.
book_prev in modules/book.module
Fetch the node object of the previous page of the book.
book_print in modules/book.module
Menu callback; generates printer-friendly book page with all descendants.
book_print_recurse in modules/book.module
book_render in modules/book.module
Menu callback; prints a listing of all books.
book_toc in modules/book.module
book_tree in modules/book.module
comment_block in modules/comment.module
Implementation of hook_block().
forum_block in modules/forum.module
Implementation of hook_block().
forum_get_forums in modules/forum.module
Returns a list of all forums for a given taxonomy id
forum_get_topics in modules/forum.module
forum_link in modules/forum.module
Implementation of hook_link().
node_feed in modules/node.module
A generic function for generating RSS feeds from a set of nodes.
node_load in modules/node.module
Load a node object from the database.
node_page_default in modules/node.module
Generate a listing of promoted nodes.
poll_block in modules/poll.module
Implementation of hook_block().
poll_page in modules/poll.module
queue_overview in modules/queue.module
Display a page listing the nodes in the submission queue.
statistics_title_list in modules/statistics.module
Returns all time or today top or last viewed node(s).
taxonomy_select_nodes in modules/taxonomy.module
Finds all nodes that match selected taxonomy conditions.
taxonomy_term_count_nodes in modules/taxonomy.module
Given a term id, count the number of published nodes in it.
tracker_page in modules/tracker.module
Menu callback. Prints a listing of active nodes on the site.
_forum_new in modules/forum.module
Finds the first unread node for a given forum.
_forum_topics_read in modules/forum.module

Code

<?php
function db_rewrite_sql($query, $primary_table = 'n', $primary_field = 'nid',  $args = array()) {
  list($join, $where, $distinct) = _db_rewrite_sql($query, $primary_table, $primary_field, $args);

  if ($distinct) {
    $field_to_select = 'DISTINCT('. $primary_table .'.'. $primary_field .')';
    // (?<!text) is a negative look-behind (no need to rewrite queries that already use DISTINCT).
    $query = preg_replace('/(SELECT.*)('. $primary_table .'\.)?(?<!DISTINCT\()(?<!DISTINCT\('. $primary_table .'\.)'. $primary_field .'(.*FROM)/AUsi', '\1'. $field_to_select .'\3', $query);
  }

  if (!empty($join)) {
    $query = preg_replace('|FROM[^[:upper:]/,]+|','\0 '. $join .' ', $query);
  }

  if (!empty($where)) {
    if (strpos($query, 'WHERE')) {
      $replace = 'WHERE';
      $add = 'AND';
    }
    elseif (strpos($query, 'GROUP')) {
      $replace = 'GROUP';
      $add = 'GROUP';
    }
    elseif (strpos($query, 'ORDER')) {
      $replace = 'ORDER';
      $add = 'ORDER';
    }
    elseif (strpos($query, 'LIMIT')) {
      $replace = 'LIMIT';
      $add = 'LIMIT';
    }
    else {
      $query .= ' WHERE '. $where;
    }
    if (isset($replace)) {
      $query = str_replace($replace, 'WHERE  '. $where .' '. $add .' ', $query);
    }
  }

  return $query;
}
?>
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.