public function Connection::prepareStatement in Drupal driver for SQL Server and SQL Azure 4.1.x
Same name and namespace in other branches
- 4.2.x src/Driver/Database/sqlsrv/Connection.php \Drupal\sqlsrv\Driver\Database\sqlsrv\Connection::prepareStatement()
- 4.0.x src/Driver/Database/sqlsrv/Connection.php \Drupal\sqlsrv\Driver\Database\sqlsrv\Connection::prepareStatement()
Return value
\Drupal\Core\Database\Statement A PDO prepared statement ready for its execute() method.
Overrides Connection::prepareStatement
2 calls to Connection::prepareStatement()
- Connection::query in src/
Driver/ Database/ sqlsrv/ Connection.php - Executes a query string against the database.
- Connection::queryDirect in src/
Driver/ Database/ sqlsrv/ Connection.php - Like query but with no query preprocessing.
File
- src/
Driver/ Database/ sqlsrv/ Connection.php, line 372
Class
- Connection
- Sqlsvr implementation of \Drupal\Core\Database\Connection.
Namespace
Drupal\sqlsrv\Driver\Database\sqlsrvCode
public function prepareStatement(string $query, array $options) : StatementInterface {
$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;
// 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'][\PDO::ATTR_EMULATE_PREPARES] = TRUE;
$driver_options['pdo'][\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'][\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'][\PDO::ATTR_CURSOR] = \PDO::CURSOR_SCROLL;
// Lets you access rows in any order. Creates a client-side cursor query.
$driver_options['pdo'][\PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE] = \PDO::SQLSRV_CURSOR_BUFFERED;
/** @var \Drupal\Core\Database\Statement $stmt */
$stmt = parent::prepareStatement($query, $driver_options);
return $stmt;
}