You are here

public function Connection::prepareQuery in Drupal driver for SQL Server and SQL Azure 3.0.x

Same name and namespace in other branches
  1. 8.2 drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php \Drupal\Driver\Database\sqlsrv\Connection::prepareQuery()
  2. 8 drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php \Drupal\Driver\Database\sqlsrv\Connection::prepareQuery()

Prepares a query string and returns the prepared statement.

This method caches prepared statements, reusing them when possible. It also prefixes tables names enclosed in curly-braces.

Parameters

string $query: The query string as SQL, with curly-braces surrounding the table names.

array $options: An array ooptions to determine which PDO Parameters should be used.

Return value

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

Overrides Connection::prepareQuery

2 calls to Connection::prepareQuery()
Connection::query in drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php
Executes a query string against the database.
Connection::queryDirect in drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php
Like query but with no query preprocessing.

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php, line 451

Class

Connection
Sqlsvr implementation of \Drupal\Core\Database\Connection.

Namespace

Drupal\Driver\Database\sqlsrv

Code

public function prepareQuery($query, array $options = []) {
  $default_options = [
    'emulate_prepares' => FALSE,
    'bypass_preprocess' => FALSE,
  ];

  // Merge default statement options. These options are
  // only specific for this preparation and will only override
  // the global configuration if set to different than NULL.
  $options += $default_options;
  $query = $this
    ->prefixTables($query);

  // Preprocess the query.
  if (!$options['bypass_preprocess']) {
    $query = $this
      ->preprocessQuery($query);
  }
  $driver_options = [];
  if ($options['emulate_prepares'] === TRUE) {

    // Never use this when you need special column binding.
    // Unlike other PDO drivers, sqlsrv requires this attribute be set
    // on the statement, not the connection.
    $driver_options[\PDO::ATTR_EMULATE_PREPARES] = TRUE;
    $driver_options[\PDO::SQLSRV_ATTR_ENCODING] = \PDO::SQLSRV_ENCODING_UTF8;
  }

  // We run the statements in "direct mode" because the way PDO prepares
  // statement in non-direct mode cause temporary tables to be destroyed
  // at the end of the statement.
  // If you are using the PDO_SQLSRV driver and you want to execute a query
  // that changes a database setting (e.g. SET NOCOUNT ON), use the PDO::query
  // method with the PDO::SQLSRV_ATTR_DIRECT_QUERY attribute.
  // http://blogs.iis.net/bswan/archive/2010/12/09/how-to-change-database-settings-with-the-pdo-sqlsrv-driver.aspx
  // If a query requires the context that was set in a previous query,
  // you should execute your queries with PDO::SQLSRV_ATTR_DIRECT_QUERY set to
  // True. For example, if you use temporary tables in your queries,
  // PDO::SQLSRV_ATTR_DrIRECT_QUERY must be set to True.
  $driver_options[\PDO::SQLSRV_ATTR_DIRECT_QUERY] = TRUE;

  // It creates a cursor for the query, which allows you to iterate over the
  // result set without fetching the whole result at once. A scrollable
  // cursor, specifically, is one that allows iterating backwards.
  // https://msdn.microsoft.com/en-us/library/hh487158%28v=sql.105%29.aspx
  $driver_options[\PDO::ATTR_CURSOR] = \PDO::CURSOR_SCROLL;

  // Lets you access rows in any order. Creates a client-side cursor query.
  $driver_options[\PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE] = \PDO::SQLSRV_CURSOR_BUFFERED;

  /** @var \Drupal\Core\Database\Statement $stmt */
  $stmt = $this->connection
    ->prepare($query, $driver_options);
  return $stmt;
}