You are here

public function DatabaseConnection_sqlsrv::query 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::query()
  2. 7.2 sqlsrv/database.inc \DatabaseConnection_sqlsrv::query()

This method is overriden to manage the insecure (EMULATE_PREPARE) behaviour to prevent some compatibility issues with SQL Server.

Overrides DatabaseConnection::query

3 calls to DatabaseConnection_sqlsrv::query()
DatabaseConnection_sqlsrv::nextId in sqlsrv/database.inc
Override of DatabaseConnection::nextId().
DatabaseConnection_sqlsrv::queryRange in sqlsrv/database.inc
Override of DatabaseConnection::queryRange().
DatabaseConnection_sqlsrv::queryTemporary in sqlsrv/database.inc
Override of DatabaseConnection::queryTemporary().

File

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

Class

DatabaseConnection_sqlsrv

Code

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

  // Use default values if not already set.
  $options += $this
    ->defaultOptions();
  try {

    // We allow either a pre-bound statement object or a literal string.
    // In either case, we want to end up with an executed statement object,
    // which we pass to PDOStatement::execute.
    if ($query instanceof DatabaseStatementInterface) {
      $stmt = $query;
      $stmt
        ->execute(NULL, $options);
    }
    else {
      $this
        ->expandArguments($query, $args);
      $stmt = $this
        ->prepareQuery($query);
      $insecure = isset($options['insecure']) ? $options['insecure'] : FALSE;

      // Try to detect duplicate place holders, this check's performance
      // is not a good addition to the driver, but does a good job preventing
      // duplicate placeholder errors.
      $argcount = count($args);
      if ($insecure === TRUE || $argcount >= 2100 || $argcount != substr_count($query, ':')) {
        $stmt
          ->RequireInsecure();
      }
      $stmt
        ->execute($args, $options);
    }

    // Depending on the type of query we may need to return a different value.
    // See DatabaseConnection::defaultOptions() for a description of each
    // value.
    switch ($options['return']) {
      case Database::RETURN_STATEMENT:
        return $stmt;
      case Database::RETURN_AFFECTED:
        return $stmt
          ->rowCount();
      case Database::RETURN_INSERT_ID:
        return $this
          ->lastInsertId();
      case Database::RETURN_NULL:
        return;
      default:
        throw new PDOException('Invalid return directive: ' . $options['return']);
    }
  } catch (PDOException $e) {
    if ($options['throw_exception']) {

      // Add additional debug information.
      if ($query instanceof DatabaseStatementInterface) {
        $e->query_string = $stmt
          ->getQueryString();
      }
      else {
        $e->query_string = $query;
      }
      $e->args = $args;
      throw $e;
    }
    return NULL;
  }
}