You are here

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

Executes a prepared statement.

Parameters

$args: An array of values with as many elements as there are bound parameters in the SQL statement being executed.

$options: An array of options for this query.

Return value

TRUE on success, or FALSE on failure.

Overrides DatabaseStatementPrefetch::execute

File

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

Class

DatabaseStatement_sqlsrv

Code

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;
}