Skip to content

💡 Add support for MySQL: ON DUPLICATE KEY UPDATE, PostgreSQL: ON CONFLICT #50

@insane-dev

Description

@insane-dev

I have an idea!

Hello, Cycle team!

I've been looking for an ability to write more complex insert queries.
And so far I can't find a way to build a query like this one:

INSERT INTO some_table (`key`, `value`) VALUES (...) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`)

As I understand PostgreSQL also has this functionality:
https://www.postgresql.org/docs/current/sql-insert.html#SQL-ON-CONFLICT

So I believe It would be really great to be able to do something like that:

$this->database()
  ->insert('some_table')
  ->columns('key', 'value')
  ->values([['key_1', 'value_1'], ['key_2', 'value_2']])
  ->onConflict(new Fragment('value = VALUES(value)')) // or ->onDuplicate(...)
  ->run();

Because now, I have to write it like:

$rows = [...];
$query = <<<SQL
INSERT INTO `some_table` (`key`, `value`) VALUES $rows ON DUPLICATE KEY UPDATE `value` = VALUES(`value`)
SQL;

$this->database()->getDriver()->execute($query);

Any feedback on this would be appreciated.

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions