From de42dc4b3023d554c04c426cecc14cdbc64ed329 Mon Sep 17 00:00:00 2001 From: partydragen Date: Thu, 30 Jan 2025 19:02:39 +0100 Subject: [PATCH 1/2] Increment array support --- core/classes/Database/DB.php | 62 ++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/core/classes/Database/DB.php b/core/classes/Database/DB.php index e639cb43fb..0c61208886 100644 --- a/core/classes/Database/DB.php +++ b/core/classes/Database/DB.php @@ -386,33 +386,67 @@ public function update(string $table, $where, array $fields): bool } /** - * Increment a numeric column value by 1. + * Increment a numeric column value by 1 or array of data in "column => value" format to increment. * - * @param string $table The table to use. - * @param int $id The id of the row to increment a column in. - * @param string $field The field to increment. - * @return bool Whether an error occurred or not. + * @param string $table The table to use. + * @param int $id The id of the row to increment a column in. + * @param string|array $fields Array of data to in "column => value" format to increment. + * @return bool Whether an error occurred or not. */ - public function increment(string $table, int $id, string $field): bool + public function increment(string $table, int $id, $fields): bool { $table = $this->_prefix . $table; - return !$this->query("UPDATE {$table} SET {$field} = {$field} + 1 WHERE id = ?", [$id])->error(); + if (!is_array($fields)) { + return !$this->query("UPDATE {$table} SET {$fields} = {$fields} + 1 WHERE id = ?", [$id])->error(); + } else { + $set = ''; + $x = 1; + + foreach (array_keys($fields) as $column) { + $set .= "`{$column}` = `{$column}` + ?"; + if ($x < count($fields)) { + $set .= ', '; + } + $x++; + } + } + + $sql = "UPDATE {$table} SET {$set} WHERE id = ?"; + + return !$this->query($sql, array_merge($fields, [$id]))->error(); } /** - * Decrement a numeric column value by 1. + * Decrement a numeric column value by 1 or array of data in "column => value" format to decrement. * - * @param string $table The table to use. - * @param int $id The id of the row to decrement a column in. - * @param string $field The field to increment. - * @return bool Whether an error occurred or not. + * @param string $table The table to use. + * @param int $id The id of the row to decrement a column in. + * @param string|array $fields Array of data to in "column => value" format to decrement. + * @return bool Whether an error occurred or not. */ - public function decrement(string $table, int $id, string $field): bool + public function decrement(string $table, int $id, string $fields): bool { $table = $this->_prefix . $table; - return !$this->query("UPDATE {$table} SET {$field} = {$field} - 1 WHERE id = ?", [$id])->error(); + if (!is_array($fields)) { + return !$this->query("UPDATE {$table} SET {$fields} = {$fields} - 1 WHERE id = ?", [$id])->error(); + } else { + $set = ''; + $x = 1; + + foreach (array_keys($fields) as $column) { + $set .= "`{$column}` = `{$column}` - ?"; + if ($x < count($fields)) { + $set .= ', '; + } + $x++; + } + } + + $sql = "UPDATE {$table} SET {$set} WHERE id = ?"; + + return !$this->query($sql, array_merge($fields, [$id]))->error(); } /** From 44422403c37970a9c2455a09469e29aee6243fcb Mon Sep 17 00:00:00 2001 From: partydragen Date: Thu, 30 Jan 2025 19:03:43 +0100 Subject: [PATCH 2/2] Remove decrement fields type --- core/classes/Database/DB.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/classes/Database/DB.php b/core/classes/Database/DB.php index 0c61208886..629b4b52a1 100644 --- a/core/classes/Database/DB.php +++ b/core/classes/Database/DB.php @@ -425,7 +425,7 @@ public function increment(string $table, int $id, $fields): bool * @param string|array $fields Array of data to in "column => value" format to decrement. * @return bool Whether an error occurred or not. */ - public function decrement(string $table, int $id, string $fields): bool + public function decrement(string $table, int $id, $fields): bool { $table = $this->_prefix . $table;