1 query.inc public MergeQuery::execute()

Runs the query against the database.

Overrides Query::execute

File

core/includes/database/query.inc, line 1603
Non-specific Database query code. Used by all engines.

Class

MergeQuery
General class for an abstracted MERGE query operation.

Code

public function execute() {
  if (!count($this->condition)) {
    throw new InvalidMergeQueryException(t('Invalid merge query: no conditions'));
  }

  // If doing an update, try that first and see if it matches any rows.
  if ($this->needsUpdate) {
    $update = $this->connection->update($this->table)
      ->fields($this->updateFields)
      ->condition($this->condition);
    if ($this->expressionFields) {
      foreach ($this->expressionFields as $field => $data) {
        $update->expression($field, $data['expression'], $data['arguments']);
      }
    }
    $this->result = $update->execute();
    $row_exists = (bool) $this->result;
    // If updated, we have no more work to be done.
    if ($row_exists) {
      return self::STATUS_UPDATE;
    }
  }
  // If only doing inserts, select first to see if the row exists.
  else {
    $select = $this->connection->select($this->table);
    $select->condition($this->condition);
    $select->addExpression('1');
    $row_exists = (bool) $select->execute()->fetchField();
  }

  if (!$row_exists) {
    try {
      $insert = $this->connection->insert($this->table)->fields($this->insertFields);
      if ($this->defaultFields) {
        $insert->useDefaults($this->defaultFields);
      }
      $this->result = $insert->execute();
      return self::STATUS_INSERT;
    }
    catch (Exception $e) {
      // The insert query failed, maybe it's because a racing insert query
      // beat us in inserting the same row. Retry the select query, if it
      // returns a row, ignore the error and continue with the update
      // query below.
      if (!isset($select)) {
        $select = $this->connection->select($this->table);
        $select->condition($this->condition);
        $select->addExpression('1');
      }
      if (!$select->execute()->fetchField()) {
        throw $e;
      }
    }
  }
  return NULL;
}