public function Connection::query in Drupal driver for SQL Server and SQL Azure 4.0.x
Same name and namespace in other branches
- 4.2.x src/Driver/Database/sqlsrv/Connection.php \Drupal\sqlsrv\Driver\Database\sqlsrv\Connection::query()
- 3.1.x src/Driver/Database/sqlsrv/Connection.php \Drupal\sqlsrv\Driver\Database\sqlsrv\Connection::query()
- 4.1.x src/Driver/Database/sqlsrv/Connection.php \Drupal\sqlsrv\Driver\Database\sqlsrv\Connection::query()
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.
This method is overriden to manage EMULATE_PREPARE behaviour to prevent some compatibility issues with SQL Server.
Parameters
string|\Drupal\Core\Database\Statement $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.
mixed $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.
Return value
\Drupal\Core\Database\Statement|int|string|null This method will return one of the following:
- If either $options['return'] === self::RETURN_STATEMENT, or $options['return'] is not set (due to self::defaultOptions()), returns the executed statement.
- If $options['return'] === self::RETURN_AFFECTED, returns the number of rows affected by the query (not the number matched).
- If $options['return'] === self::RETURN_INSERT_ID, returns the generated insert ID of the last query.
- If either $options['return'] === self::RETURN_NULL, or an exception occurs and $options['throw_exception'] evaluates to FALSE, returns NULL.
Throws
\Drupal\Core\Database\DatabaseExceptionWrapper
\Drupal\Core\Database\IntegrityConstraintViolationException
\InvalidArgumentException
Overrides Connection::query
See also
\Drupal\Core\Database\Connection::defaultOptions()
3 calls to Connection::query()
- Connection::queryRange in src/
Driver/ Database/ sqlsrv/ Connection.php - Runs a limited-range query on this database object.
- Connection::queryTemporary in src/
Driver/ Database/ sqlsrv/ Connection.php - Runs a SELECT query and stores its results in a temporary table.
- Connection::rollBack in src/
Driver/ Database/ sqlsrv/ Connection.php - Using SQL Server query syntax.
File
- src/
Driver/ Database/ sqlsrv/ Connection.php, line 541
Class
- Connection
- Sqlsvr implementation of \Drupal\Core\Database\Connection.
Namespace
Drupal\sqlsrv\Driver\Database\sqlsrvCode
public function query($query, array $args = [], $options = []) {
// Use default values if not already set.
$options += $this
->defaultOptions();
if (isset($options['target'])) {
@trigger_error('Passing a \'target\' key to \\Drupal\\Core\\Database\\Connection::query $options argument is deprecated in drupal:8.8.0 and will be removed before drupal:9.0.0. Instead, use \\Drupal\\Core\\Database\\Database::getConnection($target)->query(). See https://www.drupal.org/node/2993033', E_USER_DEPRECATED);
}
$stmt = NULL;
try {
// We allow either a pre-bound statement object or a literal string.
// In either case, we want to end up with an executed statement object,
// which we pass to PDOStatement::execute.
if ($query instanceof StatementInterface) {
$stmt = $query;
$stmt
->execute(NULL, $options);
}
else {
$this
->expandArguments($query, $args);
$query = rtrim($query, "; \t\n\r\0\v");
if (strpos($query, ';') !== FALSE && empty($options['allow_delimiter_in_query'])) {
throw new \InvalidArgumentException('; is not supported in SQL strings. Use only one statement at a time.');
}
$emulate = isset($options['emulate_prepares']) ? $options['emulate_prepares'] : FALSE;
// Try to detect duplicate place holders, this check's performance
// is not a good addition to the driver, but does a good job preventing
// duplicate placeholder errors.
$argcount = count($args);
if ($emulate === TRUE || $argcount >= 2100 || $argcount != substr_count($query, ':')) {
$emulate = TRUE;
}
// Replace CONCAT_WS to ensure SQL Server 2016 compatibility.
while (($pos1 = strpos($query, 'CONCAT_WS')) !== FALSE) {
// We assume the the separator does not contain any single-quotes
// and none of the arguments contain commas.
$pos2 = $this
->findParenMatch($query, $pos1 + 9);
$argument_list = substr($query, $pos1 + 10, $pos2 - 10 - $pos1);
$arguments = explode(', ', $argument_list);
$closing_quote_pos = stripos($argument_list, '\'', 1);
$separator = substr($argument_list, 1, $closing_quote_pos - 1);
$strings_list = substr($argument_list, $closing_quote_pos + 3);
$arguments = explode(', ', $strings_list);
$replace = "STUFF(";
$coalesce = [];
foreach ($arguments as $argument) {
if (substr($argument, 0, 1) == ':') {
$args[$argument . '_sqlsrv_concat'] = $args[$argument];
$coalesce[] = "CASE WHEN {$argument} IS NULL THEN '' ELSE CONCAT('{$separator}', {$argument}_sqlsrv_concat) END";
}
else {
$coalesce[] = "CASE WHEN {$argument} IS NULL THEN '' ELSE CONCAT('{$separator}', {$argument}) END";
}
}
$coalesce_string = implode(' + ', $coalesce);
$sep_len = strlen($separator);
$replace = "STUFF({$coalesce_string}, 1, {$sep_len}, '')";
$query = substr($query, 0, $pos1) . $replace . substr($query, $pos2 + 1);
}
$stmt = $this
->prepareStatement($query, [
'emulate_prepares' => $emulate,
]);
$stmt
->execute($args, $options);
}
// Depending on the type of query we may need to return a different value.
// See DatabaseConnection::defaultOptions() for a description of each
// value.
switch ($options['return']) {
case Database::RETURN_STATEMENT:
return $stmt;
case Database::RETURN_AFFECTED:
$stmt->allowRowCount = TRUE;
return $stmt
->rowCount();
case Database::RETURN_INSERT_ID:
return $this->connection
->lastInsertId();
case Database::RETURN_NULL:
return NULL;
default:
throw new \PDOException('Invalid return directive: ' . $options['return']);
}
} catch (\PDOException $e) {
// Most database drivers will return NULL here, but some of them
// (e.g. the SQLite driver) may need to re-run the query, so the return
// value will be the same as for static::query().
return $this
->handleQueryException($e, $query, $args, $options);
}
}