8eda775c0a7469da1cd9084b79fa6b3fa02da056
[yaffs-website] / web / core / lib / Drupal / Core / Database / Driver / mysql / Upsert.php
1 <?php
2
3 namespace Drupal\Core\Database\Driver\mysql;
4
5 use Drupal\Core\Database\Query\Upsert as QueryUpsert;
6
7 /**
8  * MySQL implementation of \Drupal\Core\Database\Query\Upsert.
9  */
10 class Upsert extends QueryUpsert {
11
12   /**
13    * {@inheritdoc}
14    */
15   public function __toString() {
16     // Create a sanitized comment string to prepend to the query.
17     $comments = $this->connection->makeComment($this->comments);
18
19     // Default fields are always placed first for consistency.
20     $insert_fields = array_merge($this->defaultFields, $this->insertFields);
21     $insert_fields = array_map(function ($field) {
22       return $this->connection->escapeField($field);
23     }, $insert_fields);
24
25     $query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES ';
26
27     $values = $this->getInsertPlaceholderFragment($this->insertValues, $this->defaultFields);
28     $query .= implode(', ', $values);
29
30     // Updating the unique / primary key is not necessary.
31     unset($insert_fields[$this->key]);
32
33     $update = [];
34     foreach ($insert_fields as $field) {
35       $update[] = "$field = VALUES($field)";
36     }
37
38     $query .= ' ON DUPLICATE KEY UPDATE ' . implode(', ', $update);
39
40     return $query;
41   }
42
43 }