Security update for Core, with self-updated composer
[yaffs-website] / web / core / lib / Drupal / Core / Database / Driver / pgsql / NativeUpsert.php
1 <?php
2
3 namespace Drupal\Core\Database\Driver\pgsql;
4
5 use Drupal\Core\Database\Query\Upsert as QueryUpsert;
6
7 /**
8  * PostgreSQL implementation of native \Drupal\Core\Database\Query\Upsert.
9  *
10  * @see http://www.postgresql.org/docs/9.5/static/sql-insert.html#SQL-ON-CONFLICT
11  */
12 class NativeUpsert extends QueryUpsert {
13
14   /**
15    * {@inheritdoc}
16    */
17   public function execute() {
18     if (!$this->preExecute()) {
19       return NULL;
20     }
21
22     $stmt = $this->connection->prepareQuery((string) $this);
23
24     // Fetch the list of blobs and sequences used on that table.
25     $table_information = $this->connection->schema()->queryTableInformation($this->table);
26
27     $max_placeholder = 0;
28     $blobs = [];
29     $blob_count = 0;
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]);
36
37           $stmt->bindParam(':db_insert_placeholder_' . $max_placeholder++, $blobs[$blob_count], \PDO::PARAM_LOB);
38
39           // Pre-increment is faster in PHP than increment.
40           ++$blob_count;
41         }
42         else {
43           $stmt->bindParam(':db_insert_placeholder_' . $max_placeholder++, $insert_values[$idx]);
44         }
45       }
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];
52
53             // Sequences must be greater than or equal to 1.
54             if ($serial_value === NULL || !$serial_value) {
55               $serial_value = 1;
56             }
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]);
64           }
65         }
66       }
67     }
68
69     $options = $this->queryOptions;
70     if (!empty($table_information->sequences)) {
71       $options['sequence_name'] = $table_information->sequences[0];
72     }
73
74     // Re-initialize the values array so that we can re-use this query.
75     $this->insertValues = [];
76
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();
82     try {
83       $this->connection->query($stmt, [], $options);
84       $this->connection->releaseSavepoint();
85     }
86     catch (\Exception $e) {
87       $this->connection->rollbackSavepoint();
88       throw $e;
89     }
90
91     return TRUE;
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function __toString() {
98     // Create a sanitized comment string to prepend to the query.
99     $comments = $this->connection->makeComment($this->comments);
100
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);
105     }, $insert_fields);
106
107     $query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES ';
108
109     $values = $this->getInsertPlaceholderFragment($this->insertValues, $this->defaultFields);
110     $query .= implode(', ', $values);
111
112     // Updating the unique / primary key is not necessary.
113     unset($insert_fields[$this->key]);
114
115     $update = [];
116     foreach ($insert_fields as $field) {
117       $update[] = "$field = EXCLUDED.$field";
118     }
119
120     $query .= ' ON CONFLICT (' . $this->connection->escapeField($this->key) . ') DO UPDATE SET ' . implode(', ', $update);
121
122     return $query;
123   }
124
125 }