You are here

public function UpdateQuery_sqlsrv::execute in Drupal driver for SQL Server and SQL Azure 7

Same name and namespace in other branches
  1. 7.3 sqlsrv/query.inc \UpdateQuery_sqlsrv::execute()
  2. 7.2 sqlsrv/query.inc \UpdateQuery_sqlsrv::execute()

Executes the UPDATE query.

Return value

The number of rows affected by the update.

Overrides UpdateQuery::execute

File

sqlsrv/query.inc, line 210

Class

UpdateQuery_sqlsrv
SQL Server-specific implementation of UPDATE.

Code

public function execute() {

  // Rewrite the query to exclude the non-affected rows.
  $this
    ->excludeNonAffectedRows();

  // Now perform the special handling for BLOB fields.
  $max_placeholder = 0;

  // Because we filter $fields the same way here and in __toString(), the
  // placeholders will all match up properly.
  $stmt = $this->connection
    ->PDOPrepare($this->connection
    ->prefixTables((string) $this));

  // Fetch the list of blobs and sequences used on that table.
  $columnInformation = $this->connection
    ->schema()
    ->queryColumnInformation($this->table);

  // Expressions take priority over literal fields, so we process those first
  // and remove any literal fields that conflict.
  $fields = $this->fields;
  $expression_fields = array();
  foreach ($this->expressionFields as $field => $data) {
    if (!empty($data['arguments'])) {
      foreach ($data['arguments'] as $placeholder => $argument) {

        // We assume that an expression will never happen on a BLOB field,
        // which is a fairly safe assumption to make since in most cases
        // it would be an invalid query anyway.
        $stmt
          ->bindParam($placeholder, $data['arguments'][$placeholder]);
      }
    }
    unset($fields[$field]);
  }

  // We use this array to store references to the blob handles.
  // This is necessary because the PDO will otherwise messes up with references.
  $blobs = array();
  $blob_count = 0;
  foreach ($fields as $field => $value) {
    $placeholder = ':db_update_placeholder_' . $max_placeholder++;
    if (isset($columnInformation['blobs'][$field])) {
      $blobs[$blob_count] = fopen('php://memory', 'a');
      fwrite($blobs[$blob_count], $value);
      rewind($blobs[$blob_count]);
      $stmt
        ->bindParam($placeholder, $blobs[$blob_count], PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
      $blob_count++;
    }
    else {
      $stmt
        ->bindParam($placeholder, $fields[$field]);
    }
  }
  if (count($this->condition)) {
    $this->condition
      ->compile($this->connection, $this);
    $arguments = $this->condition
      ->arguments();
    foreach ($arguments as $placeholder => $value) {
      $stmt
        ->bindParam($placeholder, $arguments[$placeholder]);
    }
  }
  $options = $this->queryOptions;
  $options['already_prepared'] = TRUE;
  $stmt
    ->execute();
  return $stmt
    ->rowCount();
}