You are here

public function Connection::queryTemporary in Drupal driver for SQL Server and SQL Azure 4.0.x

Same name and namespace in other branches
  1. 4.2.x src/Driver/Database/sqlsrv/Connection.php \Drupal\sqlsrv\Driver\Database\sqlsrv\Connection::queryTemporary()
  2. 3.1.x src/Driver/Database/sqlsrv/Connection.php \Drupal\sqlsrv\Driver\Database\sqlsrv\Connection::queryTemporary()
  3. 4.1.x src/Driver/Database/sqlsrv/Connection.php \Drupal\sqlsrv\Driver\Database\sqlsrv\Connection::queryTemporary()

Runs a SELECT query and stores its results in a temporary table.

Use this as a substitute for ->query() when the results need to stored in a temporary table. Temporary tables exist for the duration of the page request. User-supplied arguments to the query should be passed in as separate parameters so that they can be properly escaped to avoid SQL injection attacks.

Note that if you need to know how many results were returned, you should do a SELECT COUNT(*) on the temporary table afterwards.

Parameters

string $query: A string containing a normal SELECT SQL query.

array $args: (optional) An array of values to substitute into the query at placeholder markers.

array $options: (optional) An associative array of options to control how the query is run. See the documentation for DatabaseConnection::defaultOptions() for details.

Return value

string The name of the temporary table.

Overrides Connection::queryTemporary

Deprecated

in drupal:9.3.0 and is removed from drupal:10.0.0. There is no replacement.

See also

https://www.drupal.org/node/3211781

File

src/Driver/Database/sqlsrv/Connection.php, line 116

Class

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

Namespace

Drupal\sqlsrv\Driver\Database\sqlsrv

Code

public function queryTemporary($query, array $args = [], array $options = []) {
  $tablename = $this
    ->generateTemporaryTableName();

  // Having comments in the query can be tricky and break the
  // SELECT FROM  -> SELECT INTO conversion.

  /** @var \Drupal\sqlsrv\Driver\Database\sqlsrv\Schema $schema */
  $schema = $this
    ->schema();
  $query = $schema
    ->removeSQLComments($query);

  // Replace SELECT xxx FROM table by SELECT xxx INTO #table FROM table.
  $query = preg_replace('/^SELECT(.*?)FROM/is', 'SELECT$1 INTO {' . $tablename . '} FROM', $query);
  $this
    ->query($query, $args, $options);
  return $tablename;
}