1 query.inc public MergeQuery::execute()

Runs the query against the database.

Overrides Query::execute


core/includes/database/query.inc, line 1617
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'));

  // If doing an update, try that first and see if it matches any rows.
  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']);
    $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);
    $row_exists = (bool) $select->execute()->fetchField();

  if (!$row_exists) {
    try {
      $insert = $this->connection->insert($this->table)->fields($this->insertFields);
      if ($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);
      if (!$select->execute()->fetchField()) {
        throw $e;
  return NULL;