You are here

function finder_query in Finder 6

3 calls to finder_query()
finder_load_objects in ./finder.module
Load objects from the database.
finder_node_finder_find in modules/finder_node/finder_node.module
Implementation of hook_finder_find().
finder_user_finder_find in modules/finder_user/finder_user.module
Implementation of hook_finder_find().

File

./finder.module, line 1046
The finder module.

Code

function finder_query($query) {

  // Allow modules to modify this query.
  drupal_alter('finder_query', $query);

  // Prepare 'selects'.
  // Expecting empty or array('field1', 'alias.field2', etc..).
  if (!empty($query['selects'])) {
    $selects = "SELECT " . implode(', ', $query['selects']);
  }
  else {
    $selects = "SELECT *";
  }

  // Prepare 'from'
  // expecting string like "{tablename} tablealias".
  if ($selects && $query['from']) {
    $from = " FROM " . $query['from'];
  }
  elseif ($selects) {
    drupal_set_message(t("No 'from' given in finder_query()"), "error");
    return FALSE;
  }
  else {
    $from = '';
  }

  // Prepare joins.
  // Expecting array("LEFT JOIN {table} alias ON alias.field1 = x.field2", "INNER JOIN {table} alias ON alias.field1 = x.field2", etc..).
  if (!empty($query['joins'])) {
    $joins = " " . implode(' ', $query['joins']);
  }
  else {
    $joins = '';
  }

  // Prepare wheres.
  // See finder_wheres() for expected values.
  if (!empty($query['wheres'])) {
    $wheres = " WHERE " . finder_wheres($query['wheres']);
  }
  else {
    $wheres = '';
  }

  // Prepare groups.
  // Expecting array('a field', 'another field', etc...).
  if (!empty($query['groups'])) {
    $groups = " GROUP BY " . implode(', ', $query['groups']);
  }
  else {
    $groups = '';
  }

  // Prepare orders.
  // Expecting array("field1 ASC", "alias.field2 DESC", etc..).
  if (!empty($query['orders'])) {
    $orders = " ORDER BY " . implode(', ', $query['orders']);
  }
  else {
    $orders = '';
  }

  // Build the query string.
  $query['sql'] = $selects . $from . $joins . $wheres . $groups . $orders;

  // Rewrite if required information is given.
  if (isset($query['primary_table']) && isset($query['primary_field'])) {
    $query['sql'] = db_rewrite_sql($query['sql'], $query['primary_table'], $query['primary_field'], $query['arguments']);
  }

  // Do a pager query
  if (isset($query['pager']) && !empty($query['pager'])) {
    $query['limit'] = $query['limit'] ? $query['limit'] : 10;
    $query['element'] = $query['element'] ? $query['element'] : 0;
    $query['count_sql'] = $query['count_sql'] ? $query['count_sql'] : "SELECT COUNT(*) " . $from . $joins . $wheres;
    $query['query_function'] = 'pager_query';
    $query['query_function_arguments'] = array(
      'query' => $query['sql'],
      'limit' => $query['limit'],
      'element' => $query['element'],
      'count_query' => $query['count_sql'],
      'arguments' => isset($query['arguments']) ? $query['arguments'] : array(),
    );
  }
  elseif (isset($query['range']) && !empty($query['range'])) {
    $query['from'] = $query['from'] ? $query['from'] : 0;
    $query['count'] = $query['count'] ? $query['count'] : 10;
    $query['query_function'] = 'db_query_range';
    $query['query_function_arguments'] = array(
      'query' => $query['sql'],
      'arguments' => isset($query['arguments']) ? $query['arguments'] : array(),
      'from' => $query['from'],
      'count' => $query['count'],
    );
  }
  else {
    $query['query_function'] = 'db_query';
    $query['query_function_arguments'] = array(
      'query' => $query['sql'],
      'arguments' => isset($query['arguments']) ? $query['arguments'] : NULL,
    );
  }

  // Allow modules to modify the built query.
  drupal_alter('finder_query_built', $query);

  //dpm($query);

  // If not executing a query just return the query object here.
  if (isset($query['execute']) && $query['execute'] === FALSE) {
    return $query;
  }
  if ($query['query_function']) {
    $result = call_user_func_array($query['query_function'], $query['query_function_arguments']);
  }

  // process results
  $db_function = isset($query['db_function']) ? $query['db_function'] : 'db_fetch_object';
  $results = array();
  while ($row = $db_function($result)) {
    $results[] = $row;
  }
  return $results;
}