Skip to content

Add onDuplicateKeyUpdate() support to Insert class #7

@jerryxh

Description

@jerryxh

Feature Request
MySQL/MariaDB supports INSERT ... ON DUPLICATE KEY UPDATE for upsert operations, but Atlas doesn't currently provide a way to generate this syntax.

Proposed API
Add an onDuplicateKeyUpdate() method to the Insert class:

  /**
   * Add ON DUPLICATE KEY UPDATE clause to INSERT statement
   *
   * @param array $columns Column names and values to update on duplicate key
   * @return static
   */
  public function onDuplicateKeyUpdate(array $columns): static

Example Usage

  $insert = Insert::new('mysql')
      ->into('users')
      ->columns(['id' => 1, 'name' => 'John'])
      ->onDuplicateKeyUpdate(['name' => 'Jane']);

  echo $insert->getQueryString();
  // INSERT INTO users (id, name) VALUES (?, ?) ON DUPLICATE KEY UPDATE name = ?

  print_r($insert->getBindValueArrays());
  // [1, 'John', 'Jane']

Use Case
This prevents race condition errors when multiple processes might insert the same record simultaneously -- the first succeeds with INSERT, subsequent ones UPDATE instead of throwing duplicate key errors.

Is this something you'd accept?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions