You are here

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

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

Hierarchy

Expanded class hierarchy of DatabaseStatement_sqlsrv

File

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

View source
class DatabaseStatement_sqlsrv extends DatabaseStatementPrefetch implements Iterator, 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;
  }
  protected function getStatement($query, &$args = array()) {
    $pdo_options = array();

    // Set insecure options if requested so.
    if ($this->insecure) {

      // We have to log this, prepared statements are a security RISK.
      watchdog('SQL Server Driver', 'An insecure query has been executed against the database. This is not critical, but worth looking into.');
      $options = $this->dbh
        ->getConnectionOptions();

      // These are defined in class Connection.
      $pdo_options = $options['pdo'];
    }
    return $this->dbh
      ->PDOPrepare($query, $pdo_options);
  }
  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);
    }

    // Prepare the query.
    $statement = $this
      ->getStatement($this->queryString, $args);
    if (!$statement) {
      $this
        ->throwPDOException();
    }
    $return = $statement
      ->execute($args);
    if (!$return) {
      $this
        ->throwPDOException();
    }

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

    // Bind the binary columns properly.
    $null = array();
    for ($i = 0; $i < $statement
      ->columnCount(); $i++) {
      $meta = $statement
        ->getColumnMeta($i);
      if ($meta['sqlsrv:decl_type'] == 'varbinary') {
        $null[$i] = NULL;
        $statement
          ->bindColumn($i + 1, $null[$i], PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
      }
    }
    try {
      $this->data = $statement
        ->fetchAll(PDO::FETCH_ASSOC);
    } catch (Exception $e) {
      $this->data = array();
    }
    $this->resultRowCount = count($this->data);
    if ($this->resultRowCount) {
      $this->columnNames = array_keys($this->data[0]);
    }
    else {
      $this->columnNames = array();
    }
    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]);
      }
    }
    if ($dropped_columns) {

      // Renumber columns.
      $this->columnNames = array_values($this->columnNames);
      foreach ($this->data as $k => $row) {
        foreach ($dropped_columns as $column) {
          unset($this->data[$k][$column]);
        }
      }
    }

    // Destroy the statement as soon as possible.
    unset($statement);

    // Initialize the first row in $this->currentRow.
    $this
      ->next();
    return $return;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DatabaseStatementPrefetch::$columnNames protected property The list of column names in this result set.
DatabaseStatementPrefetch::$currentKey protected property The key of the current row.
DatabaseStatementPrefetch::$currentRow protected property The current row, retrieved in PDO::FETCH_ASSOC format.
DatabaseStatementPrefetch::$data protected property Main data store.
DatabaseStatementPrefetch::$dbh public property Reference to the database connection object for this statement.
DatabaseStatementPrefetch::$defaultFetchOptions protected property Holds supplementary default fetch options.
DatabaseStatementPrefetch::$defaultFetchStyle protected property Holds the default fetch style.
DatabaseStatementPrefetch::$driverOptions protected property Driver-specific options. Can be used by child classes.
DatabaseStatementPrefetch::$fetchOptions protected property Holds supplementary current fetch options (which will be used by the next fetch).
DatabaseStatementPrefetch::$fetchStyle protected property Holds the current fetch style (which will be used by the next fetch).
DatabaseStatementPrefetch::$queryString protected property The query string.
DatabaseStatementPrefetch::$resultRowCount protected property The number of rows in this result set.
DatabaseStatementPrefetch::$rowCount protected property The number of rows affected by the last query.
DatabaseStatementPrefetch::current public function Return the current row formatted according to the current fetch style.
DatabaseStatementPrefetch::fetch public function
DatabaseStatementPrefetch::fetchAll public function
DatabaseStatementPrefetch::fetchAllAssoc public function Returns the result set as an associative array keyed by the given field. Overrides DatabaseStatementInterface::fetchAllAssoc
DatabaseStatementPrefetch::fetchAllKeyed public function Returns the entire result set as a single associative array. Overrides DatabaseStatementInterface::fetchAllKeyed
DatabaseStatementPrefetch::fetchAssoc public function Fetches the next row and returns it as an associative array. Overrides DatabaseStatementInterface::fetchAssoc
DatabaseStatementPrefetch::fetchCol public function Returns an entire single column of a result set as an indexed array. Overrides DatabaseStatementInterface::fetchCol
DatabaseStatementPrefetch::fetchColumn public function
DatabaseStatementPrefetch::fetchField public function Returns a single field from the next record of a result set. Overrides DatabaseStatementInterface::fetchField
DatabaseStatementPrefetch::fetchObject public function
DatabaseStatementPrefetch::getQueryString public function Return the object's SQL query string. Overrides DatabaseStatementInterface::getQueryString
DatabaseStatementPrefetch::key public function
DatabaseStatementPrefetch::next public function
DatabaseStatementPrefetch::rewind public function
DatabaseStatementPrefetch::rowCount public function Returns the number of rows affected by the last SQL statement. Overrides DatabaseStatementInterface::rowCount
DatabaseStatementPrefetch::setFetchMode public function
DatabaseStatementPrefetch::throwPDOException protected function Throw a PDO Exception based on the last PDO error.
DatabaseStatementPrefetch::valid public function
DatabaseStatementPrefetch::__construct public function
DatabaseStatement_sqlsrv::$insecure private property
DatabaseStatement_sqlsrv::execute public function Executes a prepared statement. Overrides DatabaseStatementPrefetch::execute
DatabaseStatement_sqlsrv::getStatement protected function Grab a PDOStatement object from a given query and its arguments. Overrides DatabaseStatementPrefetch::getStatement
DatabaseStatement_sqlsrv::RequireInsecure public function