3 namespace Drupal\Core\Database\Driver\pgsql;
5 use Drupal\Core\Database\Query\Upsert as QueryUpsert;
8 * PostgreSQL implementation of native \Drupal\Core\Database\Query\Upsert.
10 * @see http://www.postgresql.org/docs/9.5/static/sql-insert.html#SQL-ON-CONFLICT
12 class NativeUpsert extends QueryUpsert {
17 public function execute() {
18 if (!$this->preExecute()) {
22 $stmt = $this->connection->prepareQuery((string) $this);
24 // Fetch the list of blobs and sequences used on that table.
25 $table_information = $this->connection->schema()->queryTableInformation($this->table);
30 foreach ($this->insertValues as $insert_values) {
31 foreach ($this->insertFields as $idx => $field) {
32 if (isset($table_information->blob_fields[$field])) {
33 $blobs[$blob_count] = fopen('php://memory', 'a');
34 fwrite($blobs[$blob_count], $insert_values[$idx]);
35 rewind($blobs[$blob_count]);
37 $stmt->bindParam(':db_insert_placeholder_' . $max_placeholder++, $blobs[$blob_count], \PDO::PARAM_LOB);
39 // Pre-increment is faster in PHP than increment.
43 $stmt->bindParam(':db_insert_placeholder_' . $max_placeholder++, $insert_values[$idx]);
46 // Check if values for a serial field has been passed.
47 if (!empty($table_information->serial_fields)) {
48 foreach ($table_information->serial_fields as $index => $serial_field) {
49 $serial_key = array_search($serial_field, $this->insertFields);
50 if ($serial_key !== FALSE) {
51 $serial_value = $insert_values[$serial_key];
53 // Sequences must be greater than or equal to 1.
54 if ($serial_value === NULL || !$serial_value) {
57 // Set the sequence to the bigger value of either the passed
58 // value or the max value of the column. It can happen that another
59 // thread calls nextval() which could lead to a serial number being
60 // used twice. However, trying to insert a value into a serial
61 // column should only be done in very rare cases and is not thread
62 // safe by definition.
63 $this->connection->query("SELECT setval('" . $table_information->sequences[$index] . "', GREATEST(MAX(" . $serial_field . "), :serial_value)) FROM {" . $this->table . "}", [':serial_value' => (int) $serial_value]);
69 $options = $this->queryOptions;
70 if (!empty($table_information->sequences)) {
71 $options['sequence_name'] = $table_information->sequences[0];
74 // Re-initialize the values array so that we can re-use this query.
75 $this->insertValues = [];
77 // Create a savepoint so we can rollback a failed query. This is so we can
78 // mimic MySQL and SQLite transactions which don't fail if a single query
79 // fails. This is important for tables that are created on demand. For
80 // example, \Drupal\Core\Cache\DatabaseBackend.
81 $this->connection->addSavepoint();
83 $this->connection->query($stmt, [], $options);
84 $this->connection->releaseSavepoint();
86 catch (\Exception $e) {
87 $this->connection->rollbackSavepoint();
97 public function __toString() {
98 // Create a sanitized comment string to prepend to the query.
99 $comments = $this->connection->makeComment($this->comments);
101 // Default fields are always placed first for consistency.
102 $insert_fields = array_merge($this->defaultFields, $this->insertFields);
103 $insert_fields = array_map(function ($f) {
104 return $this->connection->escapeField($f);
107 $query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES ';
109 $values = $this->getInsertPlaceholderFragment($this->insertValues, $this->defaultFields);
110 $query .= implode(', ', $values);
112 // Updating the unique / primary key is not necessary.
113 unset($insert_fields[$this->key]);
116 foreach ($insert_fields as $field) {
117 $update[] = "$field = EXCLUDED.$field";
120 $query .= ' ON CONFLICT (' . $this->connection->escapeField($this->key) . ') DO UPDATE SET ' . implode(', ', $update);