Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(dbal): add proper insert ignore conflict method for MySQL
Signed-off-by: Benjamin Gaussorgues <[email protected]>
  • Loading branch information
Altahrim committed Jun 25, 2024
commit 1e19566aa4fa6f08f01ebad8a7d21ebb0974ae01
28 changes: 28 additions & 0 deletions lib/private/DB/AdapterMySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,32 @@ 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));
}

/*
* We can't use ON DUPLICATE KEY UPDATE here because Nextcloud use the CLIENT_FOUND_ROWS flag
* With this flag the MySQL returns the number of selected rows
* instead of the number of affected/modified rows
* It's impossible to change this behaviour at runtime or for a single query
* Then, the result is 1 if a row is inserted and also 1 if a row is updated with same or different values
*
* With INSERT IGNORE, the result is 1 when a row is inserted, 0 otherwise
*
* Risk: it can also ignore other errors like type mismatch or truncated data…
*/
$res = $this->conn->executeStatement(
preg_replace('/^INSERT/i', 'INSERT IGNORE', $builder->getSQL()),
$builder->getParameters(),
$builder->getParameterTypes()
);

return $res;
}
}