You are here

public function DatabaseConnection_sqlsrv::preprocessQuery in Drupal driver for SQL Server and SQL Azure 7

Same name and namespace in other branches
  1. 7.3 sqlsrv/database.inc \DatabaseConnection_sqlsrv::preprocessQuery()
  2. 7.2 sqlsrv/database.inc \DatabaseConnection_sqlsrv::preprocessQuery()

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

1 call to DatabaseConnection_sqlsrv::preprocessQuery()
DatabaseConnection_sqlsrv::PDOPrepare in sqlsrv/database.inc
Internal function: prepare a query by calling PDO directly.

File

sqlsrv/database.inc, line 354
Database interface code for Microsoft SQL Server.

Class

DatabaseConnection_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, array(
      $this,
      'replaceReservedCallback',
    ), $query);
  }

  // Last chance to modify some SQL Server-specific syntax.
  $replacements = array(
    // Normalize SAVEPOINT syntax to the SQL Server one.
    '/^SAVEPOINT (.*)$/' => 'SAVE TRANSACTION $1',
    '/^ROLLBACK TO SAVEPOINT (.*)$/' => 'ROLLBACK TRANSACTION $1',
    // SQL Server doesn't need an explicit RELEASE SAVEPOINT.
    // Run a non-operaiton query to avoid a fatal error
    // when no query is runned.
    '/^RELEASE SAVEPOINT (.*)$/' => 'SELECT 1 /* $0 */',
  );
  $query = preg_replace(array_keys($replacements), $replacements, $query);

  // Add prefixes to Drupal-specific functions.
  $functions = $this
    ->schema()
    ->DrupalSpecificFunctions();
  foreach ($functions as $function) {
    $query = preg_replace('/\\b(?<![:.])(' . preg_quote($function) . ')\\(/i', $this
      ->schema()->defaultSchema . '.$1(', $query);
  }
  $replacements = array(
    'LENGTH' => 'LEN',
    'POW' => 'POWER',
  );
  foreach ($replacements as $function => $replacement) {
    $query = preg_replace('/\\b(?<![:.])(' . preg_quote($function) . ')\\(/i', $replacement . '(', $query);
  }

  // Replace the ANSI concatenation operator with SQL Server poor one.
  $query = preg_replace('/\\|\\|/', '+', $query);
  return $query;
}