Skip to content
Merged
Changes from all commits
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
24 changes: 12 additions & 12 deletions lib/private/DB/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@ public function fixupStatement($statement) {
/**
* Create an exclusive read+write lock on a table
*
* @param string $tableName
* @throws Exception
* @since 9.1.0
*/
public function lockTable($tableName) {
public function lockTable(string $tableName) {
$this->conn->beginTransaction();
$this->conn->executeUpdate('LOCK TABLE `' .$tableName . '` IN EXCLUSIVE MODE');
}
Expand Down Expand Up @@ -80,12 +79,14 @@ public function unlockTable() {
* @deprecated 15.0.0 - use unique index and "try { $db->insert() } catch (UniqueConstraintViolationException $e) {}" instead, because it is more reliable and does not have the risk for deadlocks - see https://github.com/nextcloud/server/pull/12371
*/
public function insertIfNotExist($table, $input, ?array $compare = null) {
if (empty($compare)) {
$compare = array_keys($input);
}
$query = 'INSERT INTO `' .$table . '` (`'
. implode('`,`', array_keys($input)) . '`) SELECT '
. str_repeat('?,', count($input) - 1).'? ' // Is there a prettier alternative?
$compare = $compare ?: array_keys($input);

// Prepare column names and generate placeholders
$columns = '`' . implode('`,`', array_keys($input)) . '`';
$placeholders = implode(', ', array_fill(0, count($input), '?'));

$query = 'INSERT INTO `' . $table . '` (' . $columns . ') '
. 'SELECT ' . $placeholders . ' '
. 'FROM `' . $table . '` WHERE ';

$inserts = array_values($input);
Expand All @@ -104,10 +105,9 @@ public function insertIfNotExist($table, $input, ?array $compare = null) {
try {
return $this->conn->executeUpdate($query, $inserts);
} catch (UniqueConstraintViolationException $e) {
// if this is thrown then a concurrent insert happened between the insert and the sub-select in the insert, that should have avoided it
// it's fine to ignore this then
//
// more discussions about this can be found at https://github.com/nextcloud/server/pull/12315
// This exception indicates a concurrent insert happened between
// the insert and the sub-select in the insert, which is safe to ignore.
// More details: https://github.com/nextcloud/server/pull/12315
return 0;
}
}
Expand Down