aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/DB/Adapter.php
diff options
context:
space:
mode:
authorGit'Fellow <12234510+solracsf@users.noreply.github.com>2024-09-02 09:57:48 +0200
committerGitHub <noreply@github.com>2024-09-02 09:57:48 +0200
commitdfcf3132dda2c74ca96a3972ae8d78b4eba55b88 (patch)
tree7f41b2eacb0b894e88e3ec718ea1ba61e53dd9ba /lib/private/DB/Adapter.php
parent3973a8f722c4acf5da82a611ee50f04e19627727 (diff)
downloadnextcloud-server-dfcf3132dda2c74ca96a3972ae8d78b4eba55b88.tar.gz
nextcloud-server-dfcf3132dda2c74ca96a3972ae8d78b4eba55b88.zip
chore(db): Make the query more simpler to read
Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com>
Diffstat (limited to 'lib/private/DB/Adapter.php')
-rw-r--r--lib/private/DB/Adapter.php28
1 files changed, 14 insertions, 14 deletions
diff --git a/lib/private/DB/Adapter.php b/lib/private/DB/Adapter.php
index ccbda897bb3..ad853bf0cf1 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): void {
$this->conn->beginTransaction();
$this->conn->executeUpdate('LOCK TABLE `' .$tableName . '` IN EXCLUSIVE MODE');
}
@@ -61,7 +60,7 @@ class Adapter {
* @throws Exception
* @since 9.1.0
*/
- public function unlockTable() {
+ public function unlockTable(): void {
$this->conn->commit();
}
@@ -79,13 +78,15 @@ class Adapter {
* @throws Exception
* @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?
+ public function insertIfNotExist(string $table, array $input, ?array $compare = null): int {
+ $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;
}
}