Same filename in this branch
- 10 update.php
- 10 core/assets/scaffold/files/update.php
- 10 core/lib/Drupal/Core/Database/Query/Update.php
- 10 core/lib/Drupal/Core/Database/Driver/pgsql/Update.php
- 10 core/modules/mysql/src/Driver/Database/mysql/Update.php
- 10 core/modules/pgsql/src/Driver/Database/pgsql/Update.php
- 10 core/modules/sqlite/src/Driver/Database/sqlite/Update.php
- 10 core/tests/Drupal/Tests/Composer/Plugin/Scaffold/fixtures/drupal-assets-fixture/assets/update.php
Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Core/Database/Query/Update.php
- 9 core/lib/Drupal/Core/Database/Query/Update.php
Namespace
Drupal\Core\Database\Query
File
core/lib/Drupal/Core/Database/Query/Update.php
View source
<?php
namespace Drupal\Core\Database\Query;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Connection;
class Update extends Query implements ConditionInterface {
use QueryConditionTrait;
protected $table;
protected $fields = [];
protected $arguments = [];
protected $expressionFields = [];
public function __construct(Connection $connection, $table, array $options = []) {
$options['return'] = Database::RETURN_AFFECTED;
parent::__construct($connection, $options);
$this->table = $table;
$this->condition = $this->connection
->condition('AND');
}
public function fields(array $fields) {
$this->fields = $fields;
return $this;
}
public function expression($field, $expression, array $arguments = NULL) {
$this->expressionFields[$field] = [
'expression' => $expression,
'arguments' => $arguments,
];
return $this;
}
public function execute() {
$fields = $this->fields;
$update_values = [];
foreach ($this->expressionFields as $field => $data) {
if (!empty($data['arguments'])) {
$update_values += $data['arguments'];
}
if ($data['expression'] instanceof SelectInterface) {
$data['expression']
->compile($this->connection, $this);
$update_values += $data['expression']
->arguments();
}
unset($fields[$field]);
}
$max_placeholder = 0;
foreach ($fields as $value) {
$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());
}
$stmt = $this->connection
->prepareStatement((string) $this, $this->queryOptions, TRUE);
try {
$stmt
->execute($update_values, $this->queryOptions);
return $stmt
->rowCount();
} catch (\Exception $e) {
$this->connection
->exceptionHandler()
->handleExecutionException($e, $stmt, $update_values, $this->queryOptions);
}
}
public function __toString() {
$comments = $this->connection
->makeComment($this->comments);
$fields = $this->fields;
$update_fields = [];
foreach ($this->expressionFields as $field => $data) {
if ($data['expression'] instanceof SelectInterface) {
$data['expression']
->compile($this->connection, $this);
$data['expression'] = ' (' . $data['expression'] . ')';
}
$update_fields[] = $this->connection
->escapeField($field) . '=' . $data['expression'];
unset($fields[$field]);
}
$max_placeholder = 0;
foreach ($fields as $field => $value) {
$update_fields[] = $this->connection
->escapeField($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);
$query .= "\nWHERE " . $this->condition;
}
return $query;
}
}
Classes
Name |
Description |
Update |
General class for an abstracted UPDATE operation. |