You are here

protected function Database::getTemporaryResultsTable in Search API 8

Creates a temporary table from a select query.

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

Parameters

\Drupal\Core\Database\Query\SelectInterface $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 Database::getTemporaryResultsTable()
Database::getAutocompleteSuggestions in modules/search_api_db/src/Plugin/search_api/backend/Database.php
Retrieves autocompletion suggestions for some user input.
Database::getFacets in modules/search_api_db/src/Plugin/search_api/backend/Database.php
Computes facets for a search query.

File

modules/search_api_db/src/Plugin/search_api/backend/Database.php, line 2598

Class

Database
Indexes and searches items using the database.

Namespace

Drupal\search_api_db\Plugin\search_api\backend

Code

protected function getTemporaryResultsTable(SelectInterface $db_query) {

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

  // Remove the ORDER BY clause, as it may refer to expressions that are
  // unset above.
  $orderBy =& $db_query
    ->getOrderBy();
  $orderBy = [];

  // 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, [
    't.item_id' => TRUE,
  ]);
  $db_query
    ->distinct();
  if (!$db_query
    ->preExecute()) {
    return FALSE;
  }
  $args = $db_query
    ->getArguments();
  try {
    $result = $this->database
      ->queryTemporary((string) $db_query, $args);
  } catch (\PDOException $e) {
    $this
      ->logException($e, '%type while trying to create a temporary table: @message in %function (line %line of %file).');
    return FALSE;
  } catch (DatabaseException $e) {
    $this
      ->logException($e, '%type while trying to create a temporary table: @message in %function (line %line of %file).');
    return FALSE;
  }
  return $result;
}