You are here

public function Connection::preprocessQuery 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::preprocessQuery()
  2. 3.0.x drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php \Drupal\Driver\Database\sqlsrv\Connection::preprocessQuery()

Internal function: massage a query to make it compliant with SQL Server.

1 call to Connection::preprocessQuery()
Connection::PDOPrepare in drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php
Internal function: prepare a query by calling PDO directly.

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php, line 637
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 preprocessQuery($query) {

  // Generate a cache signature for this query.
  $query_signature = 'query_cache_' . md5($query);

  // Drill through everything...
  $success = FALSE;
  $cache = '';
  if (extension_loaded('wincache')) {
    $cache = wincache_ucache_get($query_signature, $success);
  }
  elseif (extension_loaded('apcu') && (PHP_SAPI !== 'cli' || (bool) ini_get('apc.enable_cli'))) {
    $cache = apcu_fetch($query_signature, $success);
  }
  if ($success) {
    return $cache;
  }

  // Force quotes around some SQL Server reserved keywords.
  if (preg_match('/^SELECT/i', $query)) {
    $query = preg_replace_callback(self::RESERVED_REGEXP, array(
      $this,
      'replaceReservedCallback',
    ), $query);
  }

  // Last chance to modify some SQL Server-specific syntax.
  $replacements = array();

  // Add prefixes to Drupal-specific functions.
  $defaultSchema = $this
    ->schema()
    ->GetDefaultSchema();
  foreach ($this
    ->schema()
    ->DrupalSpecificFunctions() as $function) {
    $replacements['/\\b(?<![:.])(' . preg_quote($function) . ')\\(/i'] = "{$defaultSchema}.\$1(";
  }

  // Rename some functions.
  $funcs = array(
    'LENGTH' => 'LEN',
    'POW' => 'POWER',
  );
  foreach ($funcs as $function => $replacement) {
    $replacements['/\\b(?<![:.])(' . preg_quote($function) . ')\\(/i'] = $replacement . '(';
  }

  // Replace the ANSI concatenation operator with SQL Server poor one.
  $replacements['/\\|\\|/'] = '+';

  // Now do all the replacements at once.
  $query = preg_replace(array_keys($replacements), array_values($replacements), $query);

  // Store the processed query, and make sure we expire it some time
  // so that scarcely used queries don't stay in the cache forever.
  if (extension_loaded('wincache')) {
    wincache_ucache_set($query_signature, $query, rand(600, 3600));
  }
  elseif (extension_loaded('apcu') && (PHP_SAPI !== 'cli' || (bool) ini_get('apc.enable_cli'))) {
    apcu_store($query_signature, $query, rand(600, 3600));
  }
  return $query;
}