function Merge::__toString
Same name in this branch
- main core/lib/Drupal/Core/Database/Query/Merge.php \Drupal\Core\Database\Query\Merge::__toString()
Same name and namespace in other branches
- 11.x core/lib/Drupal/Core/Database/Query/Merge.php \Drupal\Core\Database\Query\Merge::__toString()
- 10 core/lib/Drupal/Core/Database/Query/Merge.php \Drupal\Core\Database\Query\Merge::__toString()
- 9 core/lib/Drupal/Core/Database/Query/Merge.php \Drupal\Core\Database\Query\Merge::__toString()
- 8.9.x core/lib/Drupal/Core/Database/Query/Merge.php \Drupal\Core\Database\Query\Merge::__toString()
Overrides Merge::__toString
File
-
core/
modules/ pgsql/ src/ Driver/ Database/ pgsql/ Merge.php, line 175
Class
- Merge
- PostgreSQL implementation of \Drupal\Core\Database\Query\Merge.
Namespace
Drupal\pgsql\Driver\Database\pgsqlCode
public function __toString() {
// A query using the generic emulation is potentially two queries, so it
// has no string representation. Let the parent implementation throw.
if (!$this->isNativeMerge()) {
return parent::__toString();
}
// Create a sanitized comment string to prepend to the query.
$comments = $this->connection
->makeComment($this->comments);
$this->condition
->compile($this->connection, $this);
// MERGE requires a source relation even when merging a single row of
// literal values, so use a constant single-row subquery. The ON clause
// then only matches against the merge table.
$query = $comments . 'MERGE INTO {' . $this->table . '} USING (SELECT 1) AS drupal_merge_source ON (' . $this->condition . ')';
if ($this->needsUpdate) {
$update_fields = [];
// Expressions take priority over literal fields, so we process those
// first and remove any literal fields that conflict.
foreach ($this->expressionFields as $field => $data) {
if ($data['expression'] instanceof SelectInterface) {
$data['expression']->compile($this->connection, $this);
$update_fields[] = $this->connection
->escapeField($field) . ' = (' . $data['expression'] . ')';
}
else {
$update_fields[] = $this->connection
->escapeField($field) . ' = ' . $data['expression'];
}
}
$max_placeholder = 0;
foreach (array_diff_key($this->updateFields, $this->expressionFields) as $field => $value) {
$update_fields[] = $this->connection
->escapeField($field) . ' = :db_update_placeholder_' . $max_placeholder++;
}
$query .= ' WHEN MATCHED THEN UPDATE SET ' . implode(', ', $update_fields);
}
$insert_fields = [];
$values = [];
$max_placeholder = 0;
foreach (array_keys($this->insertFields) as $field) {
$insert_fields[] = $this->connection
->escapeField($field);
$values[] = ':db_insert_placeholder_' . $max_placeholder++;
}
foreach ($this->defaultFields as $field) {
$insert_fields[] = $this->connection
->escapeField($field);
$values[] = 'DEFAULT';
}
$query .= ' WHEN NOT MATCHED THEN INSERT (' . implode(', ', $insert_fields) . ') VALUES (' . implode(', ', $values) . ')';
// The merge_action() function reports whether the row was inserted or
// updated, which execute() maps to the STATUS_INSERT and STATUS_UPDATE
// return values.
return $query . ' RETURNING merge_action()';
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.