You are here

public function Merge::__toString in Drupal driver for SQL Server and SQL Azure 8.2

Implements PHP magic __toString method to convert the query to a string.

In the degenerate case, there is no string-able query as this operation is potentially two queries.

Return value

string The prepared query statement.

Overrides Merge::__toString

1 call to Merge::__toString()
Merge::execute in drivers/lib/Drupal/Driver/Database/sqlsrv/Merge.php
Runs the query against the database.

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Merge.php, line 94

Class

Merge

Namespace

Drupal\Driver\Database\sqlsrv

Code

public function __toString() {

  // Initialize placeholder count.
  $max_placeholder = 0;
  $max_placeholder_conditions = 0;
  $query = [];

  // Enable direct insertion to identity columns if necessary.
  if (!empty($this->setIdentity)) {
    $query[] = 'SET IDENTITY_INSERT {' . $this->table . '} ON;';
  }
  $query[] = 'MERGE INTO {' . $this->table . '} _target';

  // 1. Condition part.
  $this->condition
    ->compile($this->connection, $this);
  $key_conditions = [];
  $template_item = [];
  $conditions = $this
    ->conditions();
  unset($conditions['#conjunction']);
  foreach ($conditions as $condition) {
    $key_conditions[] = '_target.' . $this->connection
      ->escapeField($condition['field']) . ' = ' . '_source.' . $this->connection
      ->escapeField($condition['field']);
    $template_item[] = ':db_condition_placeholder_' . $max_placeholder_conditions++ . ' AS ' . $this->connection
      ->escapeField($condition['field']);
  }
  $query[] = 'USING (SELECT ' . implode(', ', $template_item) . ') _source ' . PHP_EOL . 'ON ' . implode(' AND ', $key_conditions);

  // 2. "When matched" part.
  // Expressions take priority over literal fields, so we process those first
  // and remove any literal fields that conflict.
  $fields = $this->updateFields;
  $update_fields = [];
  foreach ($this->expressionFields as $field => $data) {
    $update_fields[] = $field . '=' . $data['expression'];
    unset($fields[$field]);
  }
  foreach ($fields as $field => $value) {
    $update_fields[] = $this->connection
      ->quoteIdentifier($field) . '=:db_merge_placeholder_' . $max_placeholder++;
  }
  if (!empty($update_fields)) {
    $query[] = 'WHEN MATCHED THEN UPDATE SET ' . implode(', ', $update_fields);
  }

  // 3. "When not matched" part.
  if ($this->insertFields) {

    // Build the list of placeholders.
    $placeholders = [];
    $insertFieldsCount = count($this->insertFields);
    for ($i = 0; $i < $insertFieldsCount; ++$i) {
      $placeholders[] = ':db_merge_placeholder_' . $max_placeholder++;
    }
    $query[] = 'WHEN NOT MATCHED THEN INSERT (' . implode(', ', $this->connection
      ->quoteIdentifiers(array_keys($this->insertFields))) . ') VALUES (' . implode(', ', $placeholders) . ')';
  }
  else {
    $query[] = 'WHEN NOT MATCHED THEN INSERT DEFAULT VALUES';
  }

  // Return information about the query.
  $query[] = 'OUTPUT $action;';
  $this->totalPlaceholders = $max_placeholder + $max_placeholder_conditions;
  return implode(PHP_EOL, $query);
}