You are here

protected function SearchApiDbService::getTemporaryResultsTable in Search API Database Search 7

Creates a temporary table from a SelectQuery.

Will return the name of a table containing the item IDs of all results, or FALSE on failure.

Parameters

SelectQueryInterface $db_query: The select query whose results should be stored in the temporary table.

Return value

string|false The name of the temporary table, or FALSE on failure.

2 calls to SearchApiDbService::getTemporaryResultsTable()
SearchApiDbService::getAutocompleteSuggestions in ./service.inc
Implements SearchApiAutocompleteInterface::getAutocompleteSuggestions().
SearchApiDbService::getFacets in ./service.inc
Computes facets for a search query.

File

./service.inc, line 2069
Contains SearchApiDbService.

Class

SearchApiDbService
Indexes and searches items using the database.

Code

protected function getTemporaryResultsTable(SelectQueryInterface $db_query) {

  // We only need the ID column, not the score.
  $fields =& $db_query
    ->getFields();
  unset($fields['score']);
  if (count($fields) != 1 || !isset($fields['item_id'])) {
    watchdog('search_api_db', 'Error while adding facets: only "item_id" field should be used, used are: @fields.', array(
      '@fields' => implode(', ', array_keys($fields)),
    ), WATCHDOG_WARNING);
    return FALSE;
  }
  $expressions =& $db_query
    ->getExpressions();
  $expressions = array();

  // If there's a GROUP BY for item_id, we leave that, all others need to be
  // discarded.
  $group_by =& $db_query
    ->getGroupBy();
  $group_by = array_intersect_key($group_by, array(
    't.item_id' => TRUE,
  ));

  // The order of results also doesn't matter here. Also, this might lead to
  // errors if the ORDER BY clause references any expressions we removed.
  $sort =& $db_query
    ->getOrderBy();
  $sort = array();
  $db_query
    ->distinct();
  if (!$db_query
    ->preExecute()) {
    return FALSE;
  }
  $args = $db_query
    ->getArguments();
  return $this->connection
    ->queryTemporary((string) $db_query, $args);
}