diff options
author | John Molakvoæ <skjnldsv@users.noreply.github.com> | 2024-09-04 09:15:52 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-04 09:15:52 +0200 |
commit | 7efd244956a66835ba0faa42e4d4bfa3a03b3d0d (patch) | |
tree | d4e922db8de52294be9a3089b4e4d837ce08aa82 /lib | |
parent | 94d259e75048665718bef82ae4b94e521a78bbcb (diff) | |
parent | 8e6e7976d2ebc6a2f255ff3ff9ea27addd97c5fa (diff) | |
download | nextcloud-server-7efd244956a66835ba0faa42e4d4bfa3a03b3d0d.tar.gz nextcloud-server-7efd244956a66835ba0faa42e4d4bfa3a03b3d0d.zip |
Merge pull request #47675 from nextcloud/adapterQueryOpti
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/DB/Adapter.php | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/lib/private/DB/Adapter.php b/lib/private/DB/Adapter.php index ccbda897bb3..71824bda9e8 100644 --- a/lib/private/DB/Adapter.php +++ b/lib/private/DB/Adapter.php @@ -46,11 +46,10 @@ class Adapter { /** * 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'); } @@ -80,12 +79,14 @@ class Adapter { * @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); @@ -104,10 +105,9 @@ class Adapter { 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; } } |