You are here

protected function UpdateQuery_sqlsrv::excludeNonAffectedRows in Drupal driver for SQL Server and SQL Azure 7

Rewrite the query not to select non-affected rows.

A query like this one: UPDATE test SET col1 = 'newcol1', col2 = 'newcol2' WHERE tid = 1 will become: UPDATE test SET col1 = 'newcol1', col2 = 'newcol2' WHERE tid = 1 AND (col1 <> 'newcol1' OR col2 <> 'newcol2')

1 call to UpdateQuery_sqlsrv::excludeNonAffectedRows()
UpdateQuery_sqlsrv::execute in sqlsrv/query.inc
Executes the UPDATE query.

File

sqlsrv/query.inc, line 166

Class

UpdateQuery_sqlsrv
SQL Server-specific implementation of UPDATE.

Code

protected function excludeNonAffectedRows() {
  if (!empty($this->queryOptions['sqlsrv_return_matched_rows'])) {
    return;
  }

  // Get the fields used in the update query.
  $fields = $this->expressionFields + $this->fields;

  // Add the inverse of the fields to the condition.
  $condition = new DatabaseCondition('OR');
  foreach ($fields as $field => $data) {
    if (is_array($data)) {

      // The field is an expression.
      // Re-bind the placeholders.
      $expression = $data['expression'];
      $arguments = array();
      if (!empty($data['arguments'])) {
        foreach ($data['arguments'] as $placeholder => $value) {
          $new_placeholder = ':db_exclude_placeholder_' . $this
            ->nextPlaceholder();
          $expression = str_replace($placeholder, $new_placeholder, $expression);
          $arguments[$new_placeholder] = $value;
        }
      }
      $condition
        ->where($field . ' <> ' . $expression, $arguments);
      $condition
        ->isNull($field);
    }
    elseif (!isset($data)) {

      // The field will be set to NULL.
      $condition
        ->isNotNull($field);
    }
    else {
      $condition
        ->condition($field, $data, '<>');
      $condition
        ->isNull($field);
    }
  }
  if (count($condition)) {

    // Workaround for a bug in the base MergeQuery implementation:
    // a DatabaseCondition object is reused without being re-compiled,
    // leading to duplicate placeholders.
    $this->nextPlaceholder = 1000000;
    $this
      ->condition($condition);
  }
}