You are here

public function UpdateQuery_sqlsrv::__toString 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::__toString()
  2. 7.2 sqlsrv/query.inc \UpdateQuery_sqlsrv::__toString()

Implements PHP magic __toString method to convert the query to a string.

Return value

string The prepared statement.

Overrides UpdateQuery::__toString

File

sqlsrv/query.inc, line 276

Class

UpdateQuery_sqlsrv
SQL Server-specific implementation of UPDATE.

Code

public function __toString() {

  // Create a sanitized comment string to prepend to the query.
  $prefix = $this->connection
    ->makeComment($this->comments);

  // Expressions take priority over literal fields, so we process those first
  // and remove any literal fields that conflict.
  $fields = $this->fields;
  $update_fields = array();
  foreach ($this->expressionFields as $field => $data) {
    $update_fields[] = $this->connection
      ->quoteIdentifier($field) . '=' . $data['expression'];
    unset($fields[$field]);
  }
  $max_placeholder = 0;
  foreach ($fields as $field => $value) {
    $update_fields[] = $this->connection
      ->quoteIdentifier($field) . '=:db_update_placeholder_' . $max_placeholder++;
  }
  $query = $prefix . 'UPDATE {' . $this->connection
    ->escapeTable($this->table) . '} SET ' . implode(', ', $update_fields);
  if (count($this->condition)) {
    $this->condition
      ->compile($this->connection, $this);

    // There is an implicit string cast on $this->condition.
    $query .= "\nWHERE " . $this->condition;
  }
  return $query;
}