Skip to content

Commit

Permalink
feat(dbal): add proper insert ignore conflict method for MySQL
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
  • Loading branch information
Altahrim committed Jun 4, 2024
1 parent a28a970 commit 5894248
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/private/DB/AdapterMySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,20 @@ protected function getCollation(): string {

return $this->collation;
}

public function insertIgnoreConflict(string $table, array $values) : int {
$builder = $this->conn->getQueryBuilder();
$builder->insert($table);
$updates = [];
foreach ($values as $key => $value) {
$builder->setValue($key, $builder->createNamedParameter($value));
$updates[] = "`$key`=VALUES(`$key`)";
}

return $this->conn->executeStatement(
$builder->getSQL() . ' ON DUPLICATE KEY UPDATE '.implode(',', $updates),
$builder->getParameters(),
$builder->getParameterTypes()
);
}
}

0 comments on commit 5894248

Please sign in to comment.