_db_rewrite_sql

Definition

_db_rewrite_sql($query = '', $primary_table = 'n', $primary_field = 'nid', $args = array())
includes/database.inc, line 194

Description

Helper function for db_rewrite_sql.

Collects JOIN and WHERE statements via hook_db_rewrite_sql() Decides whether to select primary_key or DISTINCT(primary_key)

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 Array of additional arguments.

Return value

An array: join statements, where statements, field or DISTINCT(field).

Related topics

Namesort iconDescription
Database abstraction layerAllow the use of different database servers using the same code base.

Code

<?php
function _db_rewrite_sql($query = '', $primary_table = 'n', $primary_field = 'nid', $args = array()) {
  $where = array();
  $join = array();
  $distinct = FALSE;
  foreach (module_implements('db_rewrite_sql') as $module) {
    $result = module_invoke($module, 'db_rewrite_sql', $query, $primary_table, $primary_field, $args);
    if (is_array($result)) {
      if (isset($result['where'])) {
        $where[] .= $result['where'];
      }
      if (isset($result['join'])) {
        $join[] .= $result['join'];
      }
      if (isset($result['distinct']) && $result['distinct']) {
        $distinct = TRUE;
      }
    }
    elseif (isset($result)) {
      $where[] .= $result;
    }
  }

  $where = empty($where) ? '' : '('. implode(') AND (', $where) .')';
  $join = empty($join) ? '' : implode(' ', $join);

  return array($join, $where, $distinct);
}
?>
 
 

Drupal is a registered trademark of Dries Buytaert.