Update.php 7.63 KB
Newer Older
1 2
<?php

Crell's avatar
Crell committed
3 4 5 6 7
/**
 * @file
 * Definition of Drupal\Core\Database\Query\Update
 */

8
namespace Drupal\Core\Database\Query;
9

10 11
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Connection;
12 13 14

/**
 * General class for an abstracted UPDATE operation.
15 16
 *
 * @ingroup database
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 */
class Update extends Query implements ConditionInterface {

  /**
   * The table to update.
   *
   * @var string
   */
  protected $table;

  /**
   * An array of fields that will be updated.
   *
   * @var array
   */
  protected $fields = array();

  /**
   * An array of values to update to.
   *
   * @var array
   */
  protected $arguments = array();

  /**
   * The condition object for this query.
   *
   * Condition handling is handled via composition.
   *
46
   * @var \Drupal\Core\Database\Query\Condition
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
   */
  protected $condition;

  /**
   * Array of fields to update to an expression in case of a duplicate record.
   *
   * This variable is a nested array in the following format:
   * @code
   * <some field> => array(
   *  'condition' => <condition to execute, as a string>,
   *  'arguments' => <array of arguments for condition, or NULL for none>,
   * );
   * @endcode
   *
   * @var array
   */
  protected $expressionFields = array();

  /**
66
   * Constructs an Update query object.
67
   *
68
   * @param \Drupal\Core\Database\Connection $connection
69
   *   A Connection object.
70 71 72 73 74 75 76 77 78 79
   * @param string $table
   *   Name of the table to associate with this query.
   * @param array $options
   *   Array of database options.
   */
  public function __construct(Connection $connection, $table, array $options = array()) {
    $options['return'] = Database::RETURN_AFFECTED;
    parent::__construct($connection, $options);
    $this->table = $table;

80
    $this->condition = new Condition('AND');
81 82 83
  }

  /**
84
   * Implements Drupal\Core\Database\Query\ConditionInterface::condition().
85 86 87 88 89 90 91
   */
  public function condition($field, $value = NULL, $operator = NULL) {
    $this->condition->condition($field, $value, $operator);
    return $this;
  }

  /**
92
   * Implements Drupal\Core\Database\Query\ConditionInterface::isNull().
93 94 95 96 97 98 99
   */
  public function isNull($field) {
    $this->condition->isNull($field);
    return $this;
  }

  /**
100
   * Implements Drupal\Core\Database\Query\ConditionInterface::isNotNull().
101 102 103 104 105 106 107
   */
  public function isNotNull($field) {
    $this->condition->isNotNull($field);
    return $this;
  }

  /**
108
   * Implements Drupal\Core\Database\Query\ConditionInterface::exists().
109 110 111 112 113 114 115
   */
  public function exists(SelectInterface $select) {
    $this->condition->exists($select);
    return $this;
  }

  /**
116
   * Implements Drupal\Core\Database\Query\ConditionInterface::notExists().
117 118 119 120 121 122 123
   */
  public function notExists(SelectInterface $select) {
    $this->condition->notExists($select);
    return $this;
  }

  /**
124
   * Implements Drupal\Core\Database\Query\ConditionInterface::conditions().
125 126 127 128 129 130
   */
  public function &conditions() {
    return $this->condition->conditions();
  }

  /**
131
   * Implements Drupal\Core\Database\Query\ConditionInterface::arguments().
132 133 134 135 136 137
   */
  public function arguments() {
    return $this->condition->arguments();
  }

  /**
138
   * Implements Drupal\Core\Database\Query\ConditionInterface::where().
139 140 141 142 143 144 145
   */
  public function where($snippet, $args = array()) {
    $this->condition->where($snippet, $args);
    return $this;
  }

  /**
146
   * Implements Drupal\Core\Database\Query\ConditionInterface::compile().
147 148 149 150 151 152
   */
  public function compile(Connection $connection, PlaceholderInterface $queryPlaceholder) {
    return $this->condition->compile($connection, $queryPlaceholder);
  }

  /**
153
   * Implements Drupal\Core\Database\Query\ConditionInterface::compiled().
154 155 156 157 158 159 160 161 162 163 164 165
   */
  public function compiled() {
    return $this->condition->compiled();
  }

  /**
   * Adds a set of field->value pairs to be updated.
   *
   * @param $fields
   *   An associative array of fields to write into the database. The array keys
   *   are the field names and the values are the values to which to set them.
   *
166
   * @return \Drupal\Core\Database\Query\Update
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
   *   The called object.
   */
  public function fields(array $fields) {
    $this->fields = $fields;
    return $this;
  }

  /**
   * Specifies fields to be updated as an expression.
   *
   * Expression fields are cases such as counter=counter+1. This method takes
   * precedence over fields().
   *
   * @param $field
   *   The field to set.
   * @param $expression
   *   The field will be set to the value of this expression. This parameter
   *   may include named placeholders.
   * @param $arguments
   *   If specified, this is an array of key/value pairs for named placeholders
   *   corresponding to the expression.
   *
189
   * @return \Drupal\Core\Database\Query\Update
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
   *   The called object.
   */
  public function expression($field, $expression, array $arguments = NULL) {
    $this->expressionFields[$field] = array(
      'expression' => $expression,
      'arguments' => $arguments,
    );

    return $this;
  }

  /**
   * Executes the UPDATE query.
   *
   * @return
205 206
   *   The number of rows matched by the update query. This includes rows that
   *   actually didn't have to be updated because the values didn't change.
207 208 209 210 211 212 213 214 215 216
   */
  public function execute() {
    // Expressions take priority over literal fields, so we process those first
    // and remove any literal fields that conflict.
    $fields = $this->fields;
    $update_values = array();
    foreach ($this->expressionFields as $field => $data) {
      if (!empty($data['arguments'])) {
        $update_values += $data['arguments'];
      }
217 218 219 220
      if ($data['expression'] instanceof SelectInterface) {
        $data['expression']->compile($this->connection, $this);
        $update_values += $data['expression']->arguments();
      }
221 222 223 224 225 226
      unset($fields[$field]);
    }

    // Because we filter $fields the same way here and in __toString(), the
    // placeholders will all match up properly.
    $max_placeholder = 0;
227
    foreach ($fields as $value) {
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
      $update_values[':db_update_placeholder_' . ($max_placeholder++)] = $value;
    }

    if (count($this->condition)) {
      $this->condition->compile($this->connection, $this);
      $update_values = array_merge($update_values, $this->condition->arguments());
    }

    return $this->connection->query((string) $this, $update_values, $this->queryOptions);
  }

  /**
   * Implements PHP magic __toString method to convert the query to a string.
   *
   * @return string
   *   The prepared statement.
   */
  public function __toString() {
    // Create a sanitized comment string to prepend to the query.
    $comments = $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) {
254 255 256 257 258
      if ($data['expression'] instanceof SelectInterface) {
        // Compile and cast expression subquery to a string.
        $data['expression']->compile($this->connection, $this);
        $data['expression'] = ' (' . $data['expression'] . ')';
      }
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
      $update_fields[] = $field . '=' . $data['expression'];
      unset($fields[$field]);
    }

    $max_placeholder = 0;
    foreach ($fields as $field => $value) {
      $update_fields[] = $field . '=:db_update_placeholder_' . ($max_placeholder++);
    }

    $query = $comments . '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;
  }

}