Skip to content

Commit 775c977

Browse files
patel-vanshCopilot
andcommitted
feat: update increment and decrement methods to accept multiple columns as input
Co-authored-by: Copilot <copilot@github.com>
1 parent f300ca0 commit 775c977

3 files changed

Lines changed: 82 additions & 26 deletions

File tree

‎system/Database/BaseBuilder.php‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3017,13 +3017,25 @@ protected function _deleteBatch(string $table, array $keys, array $values): stri
30173017
/**
30183018
* Increments a numeric column by the specified value.
30193019
*
3020+
* @param array<string, int>|string $column The column(s) to increment. Can be a string or an associative array of column => value pairs.
3021+
* @param int $value The value to increment by (default is 1). Ignored if $column is an associative array.
3022+
*
30203023
* @return bool
30213024
*/
3022-
public function increment(string $column, int $value = 1)
3025+
public function increment(array|string $column, int $value = 1)
30233026
{
3024-
$column = $this->db->protectIdentifiers($column);
3027+
if (is_string($column)) {
3028+
$column = [$column => $value];
3029+
}
3030+
3031+
$fields = [];
3032+
3033+
foreach ($column as $col => $val) {
3034+
$col = $this->db->protectIdentifiers($col);
3035+
$fields[$col] = "{$col} + {$val}";
3036+
}
30253037

3026-
$sql = $this->_update($this->QBFrom[0], [$column => "{$column} + {$value}"]);
3038+
$sql = $this->_update($this->QBFrom[0], $fields);
30273039

30283040
if (! $this->testMode) {
30293041
$this->resetWrite();
@@ -3037,13 +3049,25 @@ public function increment(string $column, int $value = 1)
30373049
/**
30383050
* Decrements a numeric column by the specified value.
30393051
*
3052+
* @param array<string, int>|string $column The column(s) to decrement. Can be a string or an associative array of column => value pairs.
3053+
* @param int $value The value to decrement by (default is 1). Ignored if $column is an associative array.
3054+
*
30403055
* @return bool
30413056
*/
3042-
public function decrement(string $column, int $value = 1)
3057+
public function decrement(array|string $column, int $value = 1)
30433058
{
3044-
$column = $this->db->protectIdentifiers($column);
3059+
if (is_string($column)) {
3060+
$column = [$column => $value];
3061+
}
3062+
3063+
$fields = [];
3064+
3065+
foreach ($column as $col => $val) {
3066+
$col = $this->db->protectIdentifiers($col);
3067+
$fields[$col] = "{$col} - {$val}";
3068+
}
30453069

3046-
$sql = $this->_update($this->QBFrom[0], [$column => "{$column}-{$value}"]);
3070+
$sql = $this->_update($this->QBFrom[0], $fields);
30473071

30483072
if (! $this->testMode) {
30493073
$this->resetWrite();

‎system/Database/Postgre/Builder.php‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,20 @@ public function orderBy(string $orderBy, string $direction = '', ?bool $escape =
9393
*
9494
* @throws DatabaseException
9595
*/
96-
public function increment(string $column, int $value = 1)
96+
public function increment(array|string $column, int $value = 1)
9797
{
98-
$column = $this->db->protectIdentifiers($column);
98+
if (is_string($column)) {
99+
$column = [$column => $value];
100+
}
101+
102+
$fields = [];
103+
104+
foreach ($column as $col => $val) {
105+
$col = $this->db->protectIdentifiers($col);
106+
$fields[$col] = "to_number({$col}, '9999999') + {$val}";
107+
}
99108

100-
$sql = $this->_update($this->QBFrom[0], [$column => "to_number({$column}, '9999999') + {$value}"]);
109+
$sql = $this->_update($this->QBFrom[0], $fields);
101110

102111
if (! $this->testMode) {
103112
$this->resetWrite();
@@ -115,11 +124,20 @@ public function increment(string $column, int $value = 1)
115124
*
116125
* @throws DatabaseException
117126
*/
118-
public function decrement(string $column, int $value = 1)
127+
public function decrement(array|string $column, int $value = 1)
119128
{
120-
$column = $this->db->protectIdentifiers($column);
129+
if (is_string($column)) {
130+
$column = [$column => $value];
131+
}
132+
133+
$fields = [];
134+
135+
foreach ($column as $col => $val) {
136+
$col = $this->db->protectIdentifiers($col);
137+
$fields[$col] = "to_number({$col}, '9999999') - {$val}";
138+
}
121139

122-
$sql = $this->_update($this->QBFrom[0], [$column => "to_number({$column}, '9999999') - {$value}"]);
140+
$sql = $this->_update($this->QBFrom[0], $fields);
123141

124142
if (! $this->testMode) {
125143
$this->resetWrite();

‎system/Database/SQLSRV/Builder.php‎

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -236,17 +236,24 @@ protected function _update(string $table, array $values): string
236236
*
237237
* @return bool
238238
*/
239-
public function increment(string $column, int $value = 1)
239+
public function increment(array|string $column, int $value = 1)
240240
{
241-
$column = $this->db->protectIdentifiers($column);
241+
if (is_string($column)) {
242+
$column = [$column => $value];
243+
}
242244

243-
if ($this->castTextToInt) {
244-
$values = [$column => "CONVERT(VARCHAR(MAX),CONVERT(INT,CONVERT(VARCHAR(MAX), {$column})) + {$value})"];
245-
} else {
246-
$values = [$column => "{$column} + {$value}"];
245+
$fields = [];
246+
247+
foreach ($column as $col => $val) {
248+
$col = $this->db->protectIdentifiers($col);
249+
if ($this->castTextToInt) {
250+
$fields[$col] = "CONVERT(VARCHAR(MAX),CONVERT(INT,CONVERT(VARCHAR(MAX), {$col})) + {$val})";
251+
} else {
252+
$fields[$col] = "{$col} + {$val}";
253+
}
247254
}
248255

249-
$sql = $this->_update($this->QBFrom[0], $values);
256+
$sql = $this->_update($this->QBFrom[0], $fields);
250257

251258
if (! $this->testMode) {
252259
$this->resetWrite();
@@ -262,17 +269,24 @@ public function increment(string $column, int $value = 1)
262269
*
263270
* @return bool
264271
*/
265-
public function decrement(string $column, int $value = 1)
272+
public function decrement(array|string $column, int $value = 1)
266273
{
267-
$column = $this->db->protectIdentifiers($column);
274+
if (is_string($column)) {
275+
$column = [$column => $value];
276+
}
268277

269-
if ($this->castTextToInt) {
270-
$values = [$column => "CONVERT(VARCHAR(MAX),CONVERT(INT,CONVERT(VARCHAR(MAX), {$column})) - {$value})"];
271-
} else {
272-
$values = [$column => "{$column} + {$value}"];
278+
$fields = [];
279+
280+
foreach ($column as $col => $val) {
281+
$col = $this->db->protectIdentifiers($col);
282+
if ($this->castTextToInt) {
283+
$fields[$col] = "CONVERT(VARCHAR(MAX),CONVERT(INT,CONVERT(VARCHAR(MAX), {$col})) - {$val})";
284+
} else {
285+
$fields[$col] = "{$col} - {$val}";
286+
}
273287
}
274288

275-
$sql = $this->_update($this->QBFrom[0], $values);
289+
$sql = $this->_update($this->QBFrom[0], $fields);
276290

277291
if (! $this->testMode) {
278292
$this->resetWrite();

0 commit comments

Comments
 (0)