You are here

public function Connection::preprocessQuery in Drupal driver for SQL Server and SQL Azure 3.0.x

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. 8 drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php \Drupal\Driver\Database\sqlsrv\Connection::preprocessQuery()

Massage a query to make it compliant with SQL Server.

Parameters

mixed $query: Query string.

Return value

string Query string in MS SQL format.

1 call to Connection::preprocessQuery()
Connection::prepareQuery in drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php
Prepares a query string and returns the prepared statement.

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php, line 914

Class

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

Namespace

Drupal\Driver\Database\sqlsrv

Code

public function preprocessQuery($query) {

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

  // Last chance to modify some SQL Server-specific syntax.
  $replacements = [];

  // Add prefixes to Drupal-specific functions.

  /** @var \Drupal\Driver\Database\sqlsrv\Schema $schema */
  $schema = $this
    ->schema();
  $defaultSchema = $schema
    ->GetDefaultSchema();
  foreach ($schema
    ->DrupalSpecificFunctions() as $function) {
    $replacements['/\\b(?<![:.])(' . preg_quote($function) . ')\\(/i'] = "{$defaultSchema}.\$1(";
  }

  // Rename some functions.
  $funcs = [
    '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);
  return $query;
}