7 query.inc public MergeQuery::execute()

Runs the query against the database.

Overrides Query::execute


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


General class for an abstracted MERGE query operation.


public function execute() {
  if (!count($this->condition)) {
    throw new InvalidMergeQueryException(t('Invalid merge query: no conditions'));
  $select = $this->connection->select($this->conditionTable)
  if (!$select->execute()->fetchField()) {
    try {
      $insert = $this->connection->insert($this->table)->fields($this->insertFields);
      if ($this->defaultFields) {
      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 (!$select->execute()->fetchField()) {
        throw $e;
  if ($this->needsUpdate) {
    $update = $this->connection->update($this->table)
    if ($this->expressionFields) {
      foreach ($this->expressionFields as $field => $data) {
        $update->expression($field, $data['expression'], $data['arguments']);
    return self::STATUS_UPDATE;