db_rewrite_sql

  1. drupal
    1. drupal6 database.inc
    2. drupal7
Versionen
drupal6 – drupal7 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.

@todo Remove this function when all code has been converted to query_alter.

Übergabeparameter

$query Query to be rewritten.

$primary_table Name or alias of the table which has the primary key field for this query. Typical table names would be: {block}, {comment}, {forum}, {node}, {menu}, {taxonomy_term_data} or {taxonomy_vocabulary}. However, it is more common to use the the usual table aliases: b, c, f, n, m, t or v.

$primary_field Name of the primary field.

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

Rückgabewert

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

Verwandte Themen

▾ 9 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.
forum_block_view in modules/forum/forum.module
Implementation of hook_block_view().
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
forum_term_load in modules/forum/forum.module
Fetch a forum term.
node_admin_nodes in modules/node/node.admin.inc
Form builder: Builds the node administration overview.
template_preprocess_forum_topic_navigation in modules/forum/forum.module
Preprocess variables to format the next/previous forum topic navigation links.
_block_load_blocks in modules/block/block.module
Load blocks information from the database.
_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.

Code

includes/database/database.inc, line 2640

<?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)) {
    $pattern = '{
      # Beginning of the string
      ^
      ((?P<anonymous_view>
        # Everything within this set of parentheses is named "anonymous view"
        (?:
          [^()]++                   # anything not parentheses
        |
          \( (?P>anonymous_view) \)          # an open parenthesis, more "anonymous view" and finally a close parenthesis.
        )*
      )[^()]+WHERE)
    }x';
    preg_match($pattern, $query, $matches);
    if ($where) {
      $n = strlen($matches[1]);
      $second_part = substr($query, $n);
      $first_part = substr($matches[1], 0, $n - 5) . " $join WHERE $where AND ( ";
      foreach (array('GROUP', 'ORDER', 'LIMIT') as $needle) {
        $pos = strrpos($second_part, $needle);
        if ($pos !== FALSE) {
          // All needles are five characters long.
          $pos += 5;
          break;
        }
      }
      if ($pos === FALSE) {
        $query = $first_part . $second_part . ')';
      }
      else {
        $query = $first_part . substr($second_part, 0, -$pos) . ')' . substr($second_part, -$pos);
      }
    }
    else {
      $query = $matches[1] . " $join " . substr($query, strlen($matches[1]));
    }
  }

  return $query;
}
?>

Kommentare

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Zeilen und Absätze werden automatisch erzeugt.

Weitere Informationen über Formatierungsoptionen

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Zeilen und Absätze werden automatisch erzeugt.

Weitere Informationen über Formatierungsoptionen