public Connection::query($query, array $args = array(), $options = array())
Executes a query string against the database.
This method provides a central handler for the actual execution of every query. All queries executed by Drupal are executed as PDO prepared statements.
string|\Drupal\Core\Database\StatementInterface $query: The query to execute. In most cases this will be a string containing an SQL query with placeholders. An already-prepared instance of StatementInterface may also be passed in order to allow calling code to manually bind variables to a query. If a StatementInterface is passed, the $args array will be ignored. It is extremely rare that module code will need to pass a statement object to this method. It is used primarily for database drivers for databases that require special LOB field handling.
array $args: An array of arguments for the prepared statement. If the prepared statement uses ? placeholders, this array must be an indexed array. If it contains named placeholders, it must be an associative array.
array $options: An associative array of options to control how the query is run. The given options will be merged with self::defaultOptions(). See the documentation for self::defaultOptions() for details. Typically, $options['return'] will be set by a default or by a query builder, and should not be set by a user.
\Drupal\Core\Database\StatementInterface|int|null This method will return one of the following:
\Drupal\Core\Database\DatabaseExceptionWrapper
\Drupal\Core\Database\IntegrityConstraintViolationException
\InvalidArgumentException
Overrides Connection::query
\Drupal\Core\Database\Connection::defaultOptions()
public function query($query, array $args = array(), $options = array()) { $options += $this->defaultOptions(); // The PDO PostgreSQL driver has a bug which doesn't type cast booleans // correctly when parameters are bound using associative arrays. // @see http://bugs.php.net/bug.php?id=48383 foreach ($args as &$value) { if (is_bool($value)) { $value = (int) $value; } } // We need to wrap queries with a savepoint if: // - Currently in a transaction. // - A 'mimic_implicit_commit' does not exist already. // - The query is not a savepoint query. $wrap_with_savepoint = $this->inTransaction() && !isset($this->transactionLayers['mimic_implicit_commit']) && !(is_string($query) && ( stripos($query, 'ROLLBACK TO SAVEPOINT ') === 0 || stripos($query, 'RELEASE SAVEPOINT ') === 0 || stripos($query, 'SAVEPOINT ') === 0 ) ); if ($wrap_with_savepoint) { // Create a savepoint so we can rollback a failed query. This is so we can // mimic MySQL and SQLite transactions which don't fail if a single query // fails. This is important for tables that are created on demand. For // example, \Drupal\Core\Cache\DatabaseBackend. $this->addSavepoint(); try { $return = parent::query($query, $args, $options); $this->releaseSavepoint(); } catch (\Exception $e) { $this->rollbackSavepoint(); throw $e; } } else { $return = parent::query($query, $args, $options); } return $return; }
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Database!Driver!pgsql!Connection.php/function/Connection::query/8.1.x