You are here

public function Connection::prepareStatement in Drupal 9

Same name in this branch
  1. 9 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::prepareStatement()
  2. 9 core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php \Drupal\Core\Database\Driver\sqlite\Connection::prepareStatement()
  3. 9 core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php \Drupal\Core\Database\Driver\pgsql\Connection::prepareStatement()

Returns a prepared statement given a SQL string.

This method caches prepared statements, reusing them when possible. It also prefixes tables names enclosed in curly braces and, optionally, quotes identifiers enclosed in square brackets.

Parameters

string $query: The query string as SQL, with curly braces surrounding the table names, and square brackets surrounding identifiers.

array $options: An associative array of options to control how the query is run. See the documentation for self::defaultOptions() for details. The content of the 'pdo' key will be passed to the prepared statement.

bool $allow_row_count: (optional) A flag indicating if row count is allowed on the statement object. Defaults to FALSE.

Return value

\Drupal\Core\Database\StatementInterface A PDO prepared statement ready for its execute() method.

Throws

\InvalidArgumentException If multiple statements are included in the string, and delimiters are not allowed in the query.

\Drupal\Core\Database\DatabaseExceptionWrapper

3 calls to Connection::prepareStatement()
Connection::prepareQuery in core/lib/Drupal/Core/Database/Connection.php
Prepares a query string and returns the prepared statement.
Connection::prepareStatement in core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php
Returns a prepared statement given a SQL string.
Connection::query in core/lib/Drupal/Core/Database/Connection.php
Executes a query string against the database.
2 methods override Connection::prepareStatement()
Connection::prepareStatement in core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php
Returns a prepared statement given a SQL string.
Connection::prepareStatement in core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php
Returns a prepared statement given a SQL string.

File

core/lib/Drupal/Core/Database/Connection.php, line 596

Class

Connection
Base Database API class.

Namespace

Drupal\Core\Database

Code

public function prepareStatement(string $query, array $options, bool $allow_row_count = FALSE) : StatementInterface {
  try {
    $query = $this
      ->preprocessStatement($query, $options);

    // @todo in Drupal 10, only return the StatementWrapper.
    // @see https://www.drupal.org/node/3177490
    $statement = $this->statementWrapperClass ? new $this->statementWrapperClass($this, $this->connection, $query, $options['pdo'] ?? [], $allow_row_count) : $this->connection
      ->prepare($query, $options['pdo'] ?? []);
  } catch (\Exception $e) {
    $this
      ->exceptionHandler()
      ->handleStatementException($e, $query, $options);
  }

  // BC layer: $options['throw_exception'] = FALSE or a \PDO::prepare() call
  // returning false would lead to returning a value that fails the return
  // typehint. Throw an exception in that case.
  // @todo in Drupal 10, remove the check.
  if (!isset($statement) || !$statement instanceof StatementInterface) {
    throw new DatabaseExceptionWrapper("Statement preparation failure for query: {$query}");
  }
  return $statement;
}