You are here

class DatabaseStatement_sqlsrv in Drupal driver for SQL Server and SQL Azure 7.2

Same name and namespace in other branches
  1. 7.3 sqlsrv/database.inc \DatabaseStatement_sqlsrv
  2. 7 sqlsrv/database.inc \DatabaseStatement_sqlsrv

Hierarchy

Expanded class hierarchy of DatabaseStatement_sqlsrv

1 string reference to 'DatabaseStatement_sqlsrv'
DatabaseConnection_sqlsrv::__construct in sqlsrv/database.inc
Override of DatabaseConnection::databaseType().

File

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

View source
class DatabaseStatement_sqlsrv extends DatabaseStatementBase implements DatabaseStatementInterface {

  // Flag to tell if statement should be run insecure.
  private $insecure = FALSE;

  // Tells the statement to set insecure parameters
  // such as SQLSRV_ATTR_DIRECT_QUERY and ATTR_EMULATE_PREPARES.
  public function RequireInsecure() {
    $this->insecure = TRUE;
  }
  public function execute($args = array(), $options = array()) {
    if (isset($options['fetch'])) {
      if (is_string($options['fetch'])) {

        // Default to an object. Note: db fields will be added to the object
        // before the constructor is run. If you need to assign fields after
        // the constructor is run, see http://drupal.org/node/315092.
        $this
          ->setFetchMode(PDO::FETCH_CLASS, $options['fetch']);
      }
      else {
        $this
          ->setFetchMode($options['fetch']);
      }
    }
    $logger = $this->dbh
      ->getLogger();
    if (!empty($logger)) {
      $query_start = microtime(TRUE);
    }

    // If parameteres have already been binded
    // to the statement and we pass an empty array here
    // we will get a PDO Exception.
    if (empty($args)) {
      $args = NULL;
    }

    // Execute the query. Bypass parent override
    // and directly call PDOStatement implementation.
    $return = PDOStatement::execute($args);
    if (!$return) {
      $this
        ->throwPDOException($statement);
    }

    // Fetch all the data from the reply, in order to release any lock
    // as soon as possible.
    $this->rowCount = $this
      ->rowCount();

    // Bind column types properly.
    $null = array();
    $this->columnNames = array();
    for ($i = 0; $i < $this
      ->columnCount(); $i++) {
      $meta = $this
        ->getColumnMeta($i);
      $this->columnNames[] = $meta['name'];
      $sqlsrv_type = $meta['sqlsrv:decl_type'];
      $parts = explode(' ', $sqlsrv_type);
      $type = reset($parts);
      switch ($type) {
        case 'varbinary':
          $null[$i] = NULL;
          $this
            ->bindColumn($i + 1, $null[$i], PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
          break;
      }
    }
    if (!empty($logger)) {
      $query_end = microtime(TRUE);
      $logger
        ->log($this, $args, $query_end - $query_start);
    }

    // Remove technical columns from the final result set.
    $droppable_columns = array_flip(isset($options['sqlsrv_drop_columns']) ? $options['sqlsrv_drop_columns'] : array());
    $dropped_columns = array();
    foreach ($this->columnNames as $k => $column) {
      if (substr($column, 0, 2) == '__' || isset($droppable_columns[$column])) {
        $dropped_columns[] = $column;
        unset($this->columnNames[$k]);
      }
    }
    return $return;
  }

  /**
   * Throw a PDO Exception based on the last PDO error.
   *
   * @status: Unfinished.
   */
  protected function throwPDOException(&$statement = NULL) {

    // This is what a SQL Server PDO "no error" looks like.
    $null_error = array(
      0 => '00000',
      1 => NULL,
      2 => NULL,
    );

    // The implementation in Drupal's Core StatementPrefetch Class
    // takes for granted that the error information is in the PDOConnection
    // but it is regularly held in the PDOStatement.
    $error_info_connection = $this->dbh
      ->errorInfo();
    $error_info_statement = !empty($statement) ? $statement
      ->errorInfo() : $null_error;

    // TODO: Concatenate error information when both connection
    // and statement error info are valid.
    // We rebuild a message formatted in the same way as PDO.
    $error_info = $error_info_connection === $null_error ? $error_info_statement : $error_info_connection;
    $exception = new PDOException("SQLSTATE[" . $error_info[0] . "]: General error " . $error_info[1] . ": " . $error_info[2]);
    $exception->errorInfo = $error_info;
    unset($statement);
    throw $exception;
  }

  /**
   * Experimental, do not iterate if not needed.
   *
   * @param mixed $key_index
   * @param mixed $value_index
   * @return array|DatabaseStatement_sqlsrv
   */
  public function fetchAllKeyed($key_index = 0, $value_index = 1) {

    // If we are asked for the default behaviour, rely
    // on the PDO as being faster.
    if ($key_index == 0 && $value_index == 1 && $this
      ->columnCount() == 2) {
      $this
        ->setFetchMode(PDO::FETCH_KEY_PAIR);
      return $this
        ->fetchAll();
    }

    // We need to do this manually.
    $return = array();
    $this
      ->setFetchMode(PDO::FETCH_NUM);
    foreach ($this as $record) {
      $return[$record[$key_index]] = $record[$value_index];
    }
    return $return;
  }

  /**
   * Get parameters bound to this statement.
   *
   * @return string[]
   */
  public function &GetBoundParameters() {
    return $this->boundParams;
  }

  /**
   * @var string[] $boundParams - array of arrays containing values that have been bound to the query as parameters
   */
  protected $boundParams = array();

  /**
   * Overrides the default \PDOStatement method to add the named parameter and it's reference to the array of bound
   * parameters - then accesses and returns parent::bindParam method
   *
   * @param string $param
   * @param mixed $value
   * @param int $datatype
   * @param int $length
   * @param mixed $driverOptions
   * @return bool - default of \PDOStatement::bindParam()
   */
  public function bindParam($param, &$value, $datatype = PDO::PARAM_STR, $length = 0, $driverOptions = FALSE) {
    $this->boundParams[$param] = array(
      "value" => &$value,
      "datatype" => $datatype,
    );
    if (empty($driverOptions)) {
      return parent::bindParam($param, $value, $datatype, $length);
    }
    else {
      return parent::bindParam($param, $value, $datatype, $length, $driverOptions);
    }
  }

  /**
   * Overrides the default \PDOStatement method to add the named parameter and it's value to the array of bound values
   * - then accesses and returns parent::bindValue method
   *
   * @param string $param
   * @param string $value
   * @param int $datatype
   * @return bool - default of \PDOStatement::bindValue()
   */
  public function bindValue($param, $value, $datatype = PDO::PARAM_STR) {
    $this->boundParams[$param] = array(
      "value" => $value,
      "datatype" => $datatype,
    );
    return parent::bindValue($param, $value, $datatype);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DatabaseStatementBase::$dbh public property Reference to the database connection object for this statement.
DatabaseStatementBase::fetchAllAssoc public function Returns the result set as an associative array keyed by the given field. Overrides DatabaseStatementInterface::fetchAllAssoc
DatabaseStatementBase::fetchAssoc public function Fetches the next row and returns it as an associative array. Overrides DatabaseStatementInterface::fetchAssoc
DatabaseStatementBase::fetchCol public function Returns an entire single column of a result set as an indexed array. Overrides DatabaseStatementInterface::fetchCol
DatabaseStatementBase::fetchField public function Returns a single field from the next record of a result set. Overrides DatabaseStatementInterface::fetchField
DatabaseStatementBase::getQueryString public function Gets the query string of this statement. Overrides DatabaseStatementInterface::getQueryString
DatabaseStatementBase::__construct protected function
DatabaseStatementInterface::rowCount public function Returns the number of rows affected by the last SQL statement. 2
DatabaseStatement_sqlsrv::$boundParams protected property
DatabaseStatement_sqlsrv::$insecure private property
DatabaseStatement_sqlsrv::bindParam public function Overrides the default \PDOStatement method to add the named parameter and it's reference to the array of bound parameters - then accesses and returns parent::bindParam method
DatabaseStatement_sqlsrv::bindValue public function Overrides the default \PDOStatement method to add the named parameter and it's value to the array of bound values
DatabaseStatement_sqlsrv::execute public function Executes a prepared statement Overrides DatabaseStatementBase::execute
DatabaseStatement_sqlsrv::fetchAllKeyed public function Experimental, do not iterate if not needed. Overrides DatabaseStatementBase::fetchAllKeyed
DatabaseStatement_sqlsrv::GetBoundParameters public function Get parameters bound to this statement.
DatabaseStatement_sqlsrv::RequireInsecure public function
DatabaseStatement_sqlsrv::throwPDOException protected function Throw a PDO Exception based on the last PDO error.