db_rewrite_sql

  1. drupal
    1. 5
    2. 6
Versions
5 – 6 db_rewrite_sql($query, $primary_table = 'n', $primary_field = 'nid', $args = array())

Rewrites node, taxonomy and comment queries. Use it for listing queries. Do not use FROM table1, table2 syntax, use JOIN instead.

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}, {menu}, {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

▾ 48 functions call db_rewrite_sql()

blogapi_mt_validate_terms in modules/blogapi/blogapi.module
Blogging API helper - find allowed taxonomy terms for a node type.
blog_block in modules/blog/blog.module
Implementation of hook_block().
blog_feed_last in modules/blog/blog.module
Displays an RSS feed containing recent blog entries of all users.
blog_feed_user in modules/blog/blog.module
Displays an RSS feed containing recent blog entries of a given user.
blog_page_last in modules/blog/blog.module
Displays a Drupal page containing recent blog entries of all users.
blog_page_user in modules/blog/blog.module
Displays a Drupal page containing recent blog entries of a given user.
book_admin_orphan in modules/book/book.module
Menu callback; displays a listing of all orphaned book pages.
book_admin_overview in modules/book/book.module
Returns an administrative overview of all books.
book_block in modules/book/book.module
Implementation of hook_block().
book_export in modules/book/book.module
Menu callback; Generates various representation of a book page with all descendants and prints the requested representation to output.
book_location in modules/book/book.module
Given a node, this function returns an array of 'book node' objects representing the path in the book tree from the root to the parent of the given node.
book_location_down in modules/book/book.module
Given a node, this function returns an array of 'book node' objects representing the path in the book tree from the given node down to the last sibling of it.
book_menu in modules/book/book.module
Implementation of hook_menu().
book_next in modules/book/book.module
Fetches the node object of the next page of the book.
book_prev in modules/book/book.module
Fetches the node object of the previous page of the book.
book_recurse in modules/book/book.module
Traverses the book tree. Applies the $visit_pre() callback to each node, is called recursively for each child of the node (in weight, title order). Finally appends the output of the $visit_post() callback to the output before returning the generated…
book_render in modules/book/book.module
Menu callback; prints a listing of all books.
book_toc in modules/book/book.module
Returns an array of titles and nid entries of book pages in table of contents order.
book_tree in modules/book/book.module
Returns an HTML nested list (wrapped in a menu-class div) representing the book nodes as a tree.
comment_get_recent in modules/comment/comment.module
Find a number of recent comments. This is done in two steps. 1. Find the n (specified by $number) nodes that have the most recent comments. This is done by querying node_comment_statistics which has an index on last_comment_timestamp, and is thus a…
forum_block in modules/forum/forum.module
Implementation of hook_block().
forum_get_forums in modules/forum/forum.module
Returns a list of all forums for a given taxonomy id
forum_get_topics in modules/forum/forum.module
node_feed in modules/node/node.module
A generic function for generating RSS feeds from a set of nodes.
node_page_default in modules/node/node.module
Menu callback; Generate a listing of promoted nodes.
poll_block in modules/poll/poll.module
Implementation of hook_block().
poll_page in modules/poll/poll.module
statistics_title_list in modules/statistics/statistics.module
Returns all time or today top or last viewed node(s).
taxonomy_autocomplete in modules/taxonomy/taxonomy.module
Helper function for autocompletion
taxonomy_form_alter in modules/taxonomy/taxonomy.module
Implementation of hook_form_alter(). Generate a form for selecting terms to associate with a node.
taxonomy_get_children in modules/taxonomy/taxonomy.module
Find all children of a term ID.
taxonomy_get_parents in modules/taxonomy/taxonomy.module
Find all parents of a given term ID.
taxonomy_get_term_by_name in modules/taxonomy/taxonomy.module
Try to map a string to an existing term, as for glossary use.
taxonomy_get_tree in modules/taxonomy/taxonomy.module
Create a hierarchical representation of a vocabulary.
taxonomy_get_vocabularies in modules/taxonomy/taxonomy.module
Return an array of all vocabulary objects.
taxonomy_node_get_terms in modules/taxonomy/taxonomy.module
Find all terms associated with the given node, ordered by vocabulary and term weight.
taxonomy_node_get_terms_by_vocabulary in modules/taxonomy/taxonomy.module
Find all terms associated with the given node, within one vocabulary.
taxonomy_overview_terms in modules/taxonomy/taxonomy.module
Display a tree of all the terms in a vocabulary, with options to edit each one.
taxonomy_select_nodes in modules/taxonomy/taxonomy.module
Finds all nodes that match selected taxonomy conditions.
taxonomy_term_count_nodes in modules/taxonomy/taxonomy.module
Count the number of published nodes classified by a term.
taxonomy_term_page in modules/taxonomy/taxonomy.module
Menu callback; displays all nodes associated with a term.
theme_forum_topic_navigation in modules/forum/forum.module
Format the next/previous forum topic navigation links.
tracker_page in modules/tracker/tracker.module
Menu callback. Prints a listing of active nodes on the site.
_book_admin_table_tree in modules/book/book.module
_forum_new in modules/forum/forum.module
Finds the first unread node for a given forum.
_forum_topics_unread in modules/forum/forum.module
Calculate the number of nodes the user has not yet read and are newer than NODE_NEW_LIMIT.
_menu_build in includes/menu.inc
Build the menu by querying both modules and the database.
_node_types_build in modules/node/node.module
Builds and returns the list of available node types.

Code

includes/database.inc, line 263

<?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) {
    $query = db_distinct_field($primary_table, $primary_field, $query);
  }

  if (!empty($where) || !empty($join)) {
    if (!empty($where)) {
      $new = "WHERE $where ";
    }
    $new = " $join $new";
    if (strpos($query, 'WHERE')) {
      $query = str_replace('WHERE', $new . 'AND (', $query);
      $insert = ') ';
    }
    else {
      $insert = $new;
    }
    if (strpos($query, 'GROUP')) {
      $replace = 'GROUP';
    }
    elseif (strpos($query, 'HAVING')) {
      $replace = 'HAVING';
    }
    elseif (strpos($query, 'ORDER')) {
      $replace = 'ORDER';
    }
    elseif (strpos($query, 'LIMIT')) {
      $replace = 'LIMIT';
    }
    else {
      $query .= $insert;
    }
    if (isset($replace)) {
      $query = str_replace($replace, $insert . $replace, $query);
    }
  }

  return $query;
}
?>