Same filename in this branch
- 10 core/lib/Drupal/Core/Database/Query/Upsert.php
- 10 core/lib/Drupal/Core/Database/Driver/mysql/Upsert.php
- 10 core/lib/Drupal/Core/Database/Driver/pgsql/Upsert.php
- 10 core/lib/Drupal/Core/Database/Driver/sqlite/Upsert.php
- 10 core/modules/mysql/src/Driver/Database/mysql/Upsert.php
- 10 core/modules/pgsql/src/Driver/Database/pgsql/Upsert.php
- 10 core/modules/sqlite/src/Driver/Database/sqlite/Upsert.php
Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Core/Database/Query/Upsert.php
- 9 core/lib/Drupal/Core/Database/Query/Upsert.php
Namespace
Drupal\Core\Database\Query
File
core/lib/Drupal/Core/Database/Query/Upsert.php
View source
<?php
namespace Drupal\Core\Database\Query;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Database;
abstract class Upsert extends Query implements \Countable {
use InsertTrait;
protected $key;
public function __construct(Connection $connection, $table, array $options = []) {
$options['return'] = Database::RETURN_AFFECTED;
parent::__construct($connection, $options);
$this->table = $table;
}
public function key($field) {
$this->key = $field;
return $this;
}
protected function preExecute() {
if (!$this->key) {
throw new NoUniqueFieldException('There is no unique field specified.');
}
if (array_intersect($this->insertFields, $this->defaultFields)) {
throw new FieldsOverlapException('You may not specify the same field to have a value and a schema-default value.');
}
if (count($this->insertFields) + count($this->defaultFields) == 0) {
throw new NoFieldsException('There are no fields available to insert with.');
}
return isset($this->insertValues[0]) || $this->insertFields;
}
public function execute() {
if (!$this
->preExecute()) {
return NULL;
}
$max_placeholder = 0;
$values = [];
foreach ($this->insertValues as $insert_values) {
foreach ($insert_values as $value) {
$values[':db_insert_placeholder_' . $max_placeholder++] = $value;
}
}
$stmt = $this->connection
->prepareStatement((string) $this, $this->queryOptions, TRUE);
try {
$stmt
->execute($values, $this->queryOptions);
$affected_rows = $stmt
->rowCount();
} catch (\Exception $e) {
$this->connection
->exceptionHandler()
->handleExecutionException($e, $stmt, $values, $this->queryOptions);
}
$this->insertValues = [];
return $affected_rows;
}
}
Classes
Name |
Description |
Upsert |
General class for an abstracted "Upsert" (UPDATE or INSERT) query operation. |