3 namespace Drupal\Core\Database\Driver\mysql;
5 use Drupal\Core\Database\Query\Upsert as QueryUpsert;
8 * MySQL implementation of \Drupal\Core\Database\Query\Upsert.
10 class Upsert extends QueryUpsert {
15 public function __toString() {
16 // Create a sanitized comment string to prepend to the query.
17 $comments = $this->connection->makeComment($this->comments);
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);
25 $query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES ';
27 $values = $this->getInsertPlaceholderFragment($this->insertValues, $this->defaultFields);
28 $query .= implode(', ', $values);
30 // Updating the unique / primary key is not necessary.
31 unset($insert_fields[$this->key]);
34 foreach ($insert_fields as $field) {
35 $update[] = "$field = VALUES($field)";
38 $query .= ' ON DUPLICATE KEY UPDATE ' . implode(', ', $update);