/** * Executes the insert query. * * @return * The last insert ID of the query, if one exists. If the query was given * multiple sets of values to insert, the return value is undefined. If no * fields are specified, this method will do nothing and return NULL. That * That makes it safe to use in multi-insert loops. */ public function execute() { // If validation fails, simply return NULL. Note that validation routines // in preExecute() may throw exceptions instead. if (!$this->preExecute()) { return NULL; } // If we're selecting from a SelectQuery, finish building the query and // pass it back, as any remaining options are irrelevant. if (!empty($this->fromQuery)) { $sql = (string) $this; // The SelectQuery may contain arguments, load and pass them through. return $this->connection->query($sql, $this->fromQuery->getArguments(), $this->queryOptions); } $last_insert_id = 0; // Each insert happens in its own query in the degenerate case. However, // we wrap it in a transaction so that it is atomic where possible. On many // databases, such as SQLite, this is also a notable performance boost. $transaction = $this->connection->startTransaction(); try { $sql = (string) $this; foreach ($this->insertValues as $insert_values) { $last_insert_id = $this->connection->query($sql, $insert_values, $this->queryOptions); } } catch (\Exception $e) { // One of the INSERTs failed, rollback the whole batch. $transaction->rollback(); // Rethrow the exception for the calling code. throw $e; } // Re-initialize the values array so that we can re-use this query. $this->insertValues = array(); // Transaction commits here where $transaction looses scope. return $last_insert_id; }
/** * Generates a hash from a query object, to be used as part of the cache key. * * This smart caching strategy saves Drupal from querying and rendering to * HTML when the underlying query is unchanged. * * Expensive queries should use the query builder to create the query and then * call this function. Executing the query and formatting results should * happen in a #pre_render callback. * * @param \Drupal\Core\Database\Query\SelectInterface $query * A select query object. * * @return string * A hash of the query arguments. */ public static function keyFromQuery(SelectInterface $query) { $query->preExecute(); $keys = array((string) $query, $query->getArguments()); return hash('sha256', serialize($keys)); }
public function getArguments(PlaceholderInterface $queryPlaceholder = NULL) { return $this->query->getArguments($queryPlaceholder); }
/** * 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. * * @param \Drupal\Core\Database\Query\SelectInterface $db_query * The select query whose results should be stored in the temporary table. * * @return string|false * The name of the temporary table, or FALSE on failure. */ 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.', array('@fields' => implode(', ', array_keys($fields)))); return FALSE; } $expressions = &$db_query->getExpressions(); $expressions = array(); $db_query->distinct(); if (!$db_query->preExecute()) { return FALSE; } $args = $db_query->getArguments(); return $this->database->queryTemporary((string) $db_query, $args); }
/** * 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. * * @param \Drupal\Core\Database\Query\SelectInterface $db_query * The select query whose results should be stored in the temporary table. * * @return string|false * The name of the temporary table, or FALSE on failure. */ 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.', array('@fields' => implode(', ', array_keys($fields)))); 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)); $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) { watchdog_exception('search_api', $e, '%type while trying to create a temporary table: @message in %function (line %line of %file).'); return FALSE; } return $result; }