You are here

public function Connection::queryTemporary in Drupal driver for SQL Server and SQL Azure 8

Same name and namespace in other branches
  1. 8.2 drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php \Drupal\Driver\Database\sqlsrv\Connection::queryTemporary()
  2. 3.0.x drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php \Drupal\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

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php, line 452
Definition of Drupal\Driver\Database\sqlsrv\Connection

Class

Connection
Temporary tables: temporary table support is done by means of global temporary tables (#) to avoid the use of DIRECT QUERIES. You can enable and disable the use of direct queries with $this->driver_settings->defaultDirectQuery =…

Namespace

Drupal\Driver\Database\sqlsrv

Code

public function queryTemporary($query, array $args = array(), array $options = array()) {

  // Generate a new GLOBAL temporary table name and protect it from prefixing.
  // SQL Server requires that temporary tables to be non-qualified.
  $tablename = '##' . $this
    ->generateTemporaryTableName();

  // Temporary tables cannot be introspected so using them is limited on some scenarios.
  if ($options['real_table'] === TRUE) {
    $tablename = trim($tablename, "#");
  }
  $prefixes = $this->prefixes;
  $prefixes[$tablename] = '';
  $this
    ->setPrefix($prefixes);

  // Having comments in the query can be tricky and break the SELECT FROM  -> SELECT INTO conversion
  $query = $this
    ->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;
}