Skip to content

Commit afc7f0c

Browse files
patel-vanshCopilot
andcommitted
feat: update increment and decrement methods to accept multiple columns as input
Co-authored-by: Copilot <[email protected]>
1 parent 2db0ed7 commit afc7f0c

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
@@ -2925,13 +2925,25 @@ protected function _deleteBatch(string $table, array $keys, array $values): stri
29252925
/**
29262926
* Increments a numeric column by the specified value.
29272927
*
2928+
* @param array<string, int>|string $column The column(s) to increment. Can be a string or an associative array of column => value pairs.
2929+
* @param int $value The value to increment by (default is 1). Ignored if $column is an associative array.
2930+
*
29282931
* @return bool
29292932
*/
2930-
public function increment(string $column, int $value = 1)
2933+
public function increment(array|string $column, int $value = 1)
29312934
{
2932-
$column = $this->db->protectIdentifiers($column);
2935+
if (is_string($column)) {
2936+
$column = [$column => $value];
2937+
}
2938+
2939+
$fields = [];
2940+
2941+
foreach ($column as $col => $val) {
2942+
$col = $this->db->protectIdentifiers($col);
2943+
$fields[$col] = "{$col} + {$val}";
2944+
}
29332945

2934-
$sql = $this->_update($this->QBFrom[0], [$column => "{$column} + {$value}"]);
2946+
$sql = $this->_update($this->QBFrom[0], $fields);
29352947

29362948
if (! $this->testMode) {
29372949
$this->resetWrite();
@@ -2945,13 +2957,25 @@ public function increment(string $column, int $value = 1)
29452957
/**
29462958
* Decrements a numeric column by the specified value.
29472959
*
2960+
* @param array<string, int>|string $column The column(s) to decrement. Can be a string or an associative array of column => value pairs.
2961+
* @param int $value The value to decrement by (default is 1). Ignored if $column is an associative array.
2962+
*
29482963
* @return bool
29492964
*/
2950-
public function decrement(string $column, int $value = 1)
2965+
public function decrement(array|string $column, int $value = 1)
29512966
{
2952-
$column = $this->db->protectIdentifiers($column);
2967+
if (is_string($column)) {
2968+
$column = [$column => $value];
2969+
}
2970+
2971+
$fields = [];
2972+
2973+
foreach ($column as $col => $val) {
2974+
$col = $this->db->protectIdentifiers($col);
2975+
$fields[$col] = "{$col} - {$val}";
2976+
}
29532977

2954-
$sql = $this->_update($this->QBFrom[0], [$column => "{$column}-{$value}"]);
2978+
$sql = $this->_update($this->QBFrom[0], $fields);
29552979

29562980
if (! $this->testMode) {
29572981
$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)