Skip to content

Commit bbb53bc

Browse files
patel-vanshCopilot
andcommitted
feat: Added new methods instead of BC
Co-authored-by: Copilot <copilot@github.com>
1 parent 25a702c commit bbb53bc

6 files changed

Lines changed: 75 additions & 73 deletions

File tree

‎system/Database/BaseBuilder.php‎

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3017,20 +3017,23 @@ 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-
*
30233020
* @return bool
30243021
*/
3025-
public function increment(array|string $column, int $value = 1)
3022+
public function increment(string $column, int $value = 1)
30263023
{
3027-
if (is_string($column)) {
3028-
$column = [$column => $value];
3029-
}
3024+
return $this->incrementAll([$column => $value]);
3025+
}
30303026

3027+
/**
3028+
* Increments multiple numeric columns by the specified values.
3029+
*
3030+
* @param array<string, int> $columns An array of column => value pairs to increment.
3031+
*/
3032+
public function incrementAll(array $columns): bool
3033+
{
30313034
$fields = [];
30323035

3033-
foreach ($column as $col => $val) {
3036+
foreach ($columns as $col => $val) {
30343037
$col = $this->db->protectIdentifiers($col);
30353038
$fields[$col] = "{$col} + {$val}";
30363039
}
@@ -3049,20 +3052,23 @@ public function increment(array|string $column, int $value = 1)
30493052
/**
30503053
* Decrements a numeric column by the specified value.
30513054
*
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-
*
30553055
* @return bool
30563056
*/
3057-
public function decrement(array|string $column, int $value = 1)
3057+
public function decrement(string $column, int $value = 1)
30583058
{
3059-
if (is_string($column)) {
3060-
$column = [$column => $value];
3061-
}
3059+
return $this->decrementAll([$column => $value]);
3060+
}
30623061

3062+
/**
3063+
* Decrements multiple numeric columns by the specified values.
3064+
*
3065+
* @param array<string, int> $columns An array of column => value pairs to decrement.
3066+
*/
3067+
public function decrementAll(array $columns): bool
3068+
{
30633069
$fields = [];
30643070

3065-
foreach ($column as $col => $val) {
3071+
foreach ($columns as $col => $val) {
30663072
$col = $this->db->protectIdentifiers($col);
30673073
$fields[$col] = "{$col} - {$val}";
30683074
}

‎system/Database/Postgre/Builder.php‎

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,17 @@ public function orderBy(string $orderBy, string $direction = '', ?bool $escape =
8787
}
8888

8989
/**
90-
* Increments a numeric column by the specified value.
90+
* Increments multiple numeric columns by the specified values.
9191
*
92-
* @return mixed
93-
*
94-
* @throws DatabaseException
92+
* @param array<string, int> $columns An array of column => value pairs to increment.
9593
*/
96-
public function increment(array|string $column, int $value = 1)
94+
public function incrementAll(array $columns): bool
9795
{
98-
if (is_string($column)) {
99-
$column = [$column => $value];
100-
}
101-
10296
$fields = [];
10397

104-
foreach ($column as $col => $val) {
105-
$col = $this->db->protectIdentifiers($col);
106-
$fields[$col] = "to_number({$col}, '9999999') + {$val}";
98+
foreach ($columns as $column => $value) {
99+
$column = $this->db->protectIdentifiers($column);
100+
$fields[$column] = "to_number({$column}, '9999999') + {$value}";
107101
}
108102

109103
$sql = $this->_update($this->QBFrom[0], $fields);
@@ -118,23 +112,17 @@ public function increment(array|string $column, int $value = 1)
118112
}
119113

120114
/**
121-
* Decrements a numeric column by the specified value.
115+
* Decrements multiple numeric columns by the specified values.
122116
*
123-
* @return mixed
124-
*
125-
* @throws DatabaseException
117+
* @param array<string, int> $columns An array of column => value pairs to decrement.
126118
*/
127-
public function decrement(array|string $column, int $value = 1)
119+
public function decrementAll(array $columns): bool
128120
{
129-
if (is_string($column)) {
130-
$column = [$column => $value];
131-
}
132-
133121
$fields = [];
134122

135-
foreach ($column as $col => $val) {
136-
$col = $this->db->protectIdentifiers($col);
137-
$fields[$col] = "to_number({$col}, '9999999') - {$val}";
123+
foreach ($columns as $column => $value) {
124+
$column = $this->db->protectIdentifiers($column);
125+
$fields[$column] = "to_number({$column}, '9999999') - {$value}";
138126
}
139127

140128
$sql = $this->_update($this->QBFrom[0], $fields);

‎system/Database/SQLSRV/Builder.php‎

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -232,24 +232,20 @@ protected function _update(string $table, array $values): string
232232
}
233233

234234
/**
235-
* Increments a numeric column by the specified value.
235+
* Increments multiple numeric columns by the specified values.
236236
*
237-
* @return bool
237+
* @param array<string, int> $columns An array of column => value pairs to increment.
238238
*/
239-
public function increment(array|string $column, int $value = 1)
239+
public function incrementAll(array $columns): bool
240240
{
241-
if (is_string($column)) {
242-
$column = [$column => $value];
243-
}
244-
245241
$fields = [];
246242

247-
foreach ($column as $col => $val) {
248-
$col = $this->db->protectIdentifiers($col);
243+
foreach ($columns as $column => $value) {
244+
$column = $this->db->protectIdentifiers($column);
249245
if ($this->castTextToInt) {
250-
$fields[$col] = "CONVERT(VARCHAR(MAX),CONVERT(INT,CONVERT(VARCHAR(MAX), {$col})) + {$val})";
246+
$fields[$column] = "CONVERT(VARCHAR(MAX),CONVERT(INT,CONVERT(VARCHAR(MAX), {$column})) + {$value})";
251247
} else {
252-
$fields[$col] = "{$col} + {$val}";
248+
$fields[$column] = "{$column} + {$value}";
253249
}
254250
}
255251

@@ -265,24 +261,20 @@ public function increment(array|string $column, int $value = 1)
265261
}
266262

267263
/**
268-
* Decrements a numeric column by the specified value.
264+
* Decrements multiple numeric columns by the specified values.
269265
*
270-
* @return bool
266+
* @param array<string, int> $columns An array of column => value pairs to decrement.
271267
*/
272-
public function decrement(array|string $column, int $value = 1)
268+
public function decrementAll(array $columns): bool
273269
{
274-
if (is_string($column)) {
275-
$column = [$column => $value];
276-
}
277-
278270
$fields = [];
279271

280-
foreach ($column as $col => $val) {
281-
$col = $this->db->protectIdentifiers($col);
272+
foreach ($columns as $column => $value) {
273+
$column = $this->db->protectIdentifiers($column);
282274
if ($this->castTextToInt) {
283-
$fields[$col] = "CONVERT(VARCHAR(MAX),CONVERT(INT,CONVERT(VARCHAR(MAX), {$col})) - {$val})";
275+
$fields[$column] = "CONVERT(VARCHAR(MAX),CONVERT(INT,CONVERT(VARCHAR(MAX), {$column})) - {$value})";
284276
} else {
285-
$fields[$col] = "{$col} - {$val}";
277+
$fields[$column] = "{$column} - {$value}";
286278
}
287279
}
288280

‎tests/system/Database/Live/IncrementTest.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ public function testIncrementWithValue(): void
5151
$this->seeInDatabase('job', ['name' => 'incremental', 'description' => '8']);
5252
}
5353

54-
public function testIncrementWithMultipleColumns(): void
54+
public function testIncrementAll(): void
5555
{
5656
$this->hasInDatabase('task', ['name' => 'task1', 'description' => '6', 'priority' => '1']);
5757

5858
$this->db->table('task')
5959
->where('name', 'task1')
60-
->increment(['description' => 2, 'priority' => 3]);
60+
->incrementAll(['description' => 2, 'priority' => 3]);
6161

6262
$this->seeInDatabase('task', ['name' => 'task1', 'description' => '8', 'priority' => '4']);
6363
}
@@ -98,13 +98,13 @@ public function testDecrementWithValue(): void
9898
$this->seeInDatabase('job', ['name' => 'incremental', 'description' => '4']);
9999
}
100100

101-
public function testDecrementWithMultipleColumns(): void
101+
public function testDecrementAll(): void
102102
{
103103
$this->hasInDatabase('task', ['name' => 'task2', 'description' => '6', 'priority' => '5']);
104104

105105
$this->db->table('task')
106106
->where('name', 'task2')
107-
->decrement(['description' => 2, 'priority' => 3]);
107+
->decrementAll(['description' => 2, 'priority' => 3]);
108108

109109
$this->seeInDatabase('task', ['name' => 'task2', 'description' => '4', 'priority' => '2']);
110110
}

‎user_guide_src/source/changelogs/v4.8.0.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ Database
208208
Query Builder
209209
-------------
210210

211-
- Added support for incrementing/decrementing multiple columns in a single call to ``BaseBuilder::increment()`` and ``decrement()`` as well as driver-specific builders.
211+
- Added new ``incrementAll()`` and ``decrementAll()`` methods to ``CodeIgniter\Database\BaseBuilder`` for performing bulk increment/decrement operations.
212212

213213
Forge
214214
-----

‎user_guide_src/source/database/query_builder.rst‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,25 +2087,41 @@ Class Reference
20872087

20882088
.. php:method:: increment($column[, $value = 1])
20892089
2090-
:param array|string $column: The name of the column(s) to increment. If multiple, then should be an array of column names with their values.
2091-
:param int $value: The amount to increment in the column (will be ignored if $column is an array)
2090+
:param string $column: The name of the column to increment
2091+
:param int $value: The amount to increment in the column
20922092

20932093
Increments the value of a field by the specified amount. If the field
20942094
is not a numeric field, like a ``VARCHAR``, it will likely be replaced
20952095
with ``$value``.
20962096

2097-
.. note:: Prior to v4.8.0, only single columns could be incremented.
2097+
.. php:method:: incrementAll(array $columns)
2098+
2099+
.. versionadded:: 4.8.0
2100+
2101+
:param array $columns: An array of column names with their amounts to increment by
2102+
2103+
Increments the value of multiple fields by the specified amounts. If a field
2104+
is not a numeric field, like a ``VARCHAR``, it will likely be replaced
2105+
with the amount specified for that field.
20982106

20992107
.. php:method:: decrement($column[, $value = 1])
21002108
2101-
:param array|string $column: The name of the column(s) to decrement. If multiple, then should be an array of column names with their values.
2102-
:param int $value: The amount to decrement in the column (will be ignored if $column is an array)
2109+
:param string $column: The name of the column to decrement
2110+
:param int $value: The amount to decrement in the column
21032111

21042112
Decrements the value of a field by the specified amount. If the field
21052113
is not a numeric field, like a ``VARCHAR``, it will likely be replaced
21062114
with ``$value``.
21072115

2108-
.. note:: Prior to v4.8.0, only single columns could be decremented.
2116+
.. php:method:: decrementAll(array $columns)
2117+
2118+
.. versionadded:: 4.8.0
2119+
2120+
:param array $columns: An array of column names with their amounts to decrement by
2121+
2122+
Decrements the value of multiple fields by the specified amounts. If a field
2123+
is not a numeric field, like a ``VARCHAR``, it will likely be replaced
2124+
with the amount specified for that field.
21092125

21102126
.. php:method:: truncate()
21112127

0 commit comments

Comments
 (0)