Skip to content

Commit ca4fdf7

Browse files
patel-vanshCopilot
andcommitted
feat: Added new methods instead of BC
Co-authored-by: Copilot <[email protected]>
1 parent a845cf4 commit ca4fdf7

6 files changed

Lines changed: 75 additions & 74 deletions

File tree

system/Database/BaseBuilder.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2925,20 +2925,23 @@ 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-
*
29312928
* @return bool
29322929
*/
2933-
public function increment(array|string $column, int $value = 1)
2930+
public function increment(string $column, int $value = 1)
29342931
{
2935-
if (is_string($column)) {
2936-
$column = [$column => $value];
2937-
}
2932+
return $this->incrementAll([$column => $value]);
2933+
}
29382934

2935+
/**
2936+
* Increments multiple numeric columns by the specified values.
2937+
*
2938+
* @param array<string, int> $columns An array of column => value pairs to increment.
2939+
*/
2940+
public function incrementAll(array $columns): bool
2941+
{
29392942
$fields = [];
29402943

2941-
foreach ($column as $col => $val) {
2944+
foreach ($columns as $col => $val) {
29422945
$col = $this->db->protectIdentifiers($col);
29432946
$fields[$col] = "{$col} + {$val}";
29442947
}
@@ -2957,20 +2960,23 @@ public function increment(array|string $column, int $value = 1)
29572960
/**
29582961
* Decrements a numeric column by the specified value.
29592962
*
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-
*
29632963
* @return bool
29642964
*/
2965-
public function decrement(array|string $column, int $value = 1)
2965+
public function decrement(string $column, int $value = 1)
29662966
{
2967-
if (is_string($column)) {
2968-
$column = [$column => $value];
2969-
}
2967+
return $this->decrementAll([$column => $value]);
2968+
}
29702969

2970+
/**
2971+
* Decrements multiple numeric columns by the specified values.
2972+
*
2973+
* @param array<string, int> $columns An array of column => value pairs to decrement.
2974+
*/
2975+
public function decrementAll(array $columns): bool
2976+
{
29712977
$fields = [];
29722978

2973-
foreach ($column as $col => $val) {
2979+
foreach ($columns as $col => $val) {
29742980
$col = $this->db->protectIdentifiers($col);
29752981
$fields[$col] = "{$col} - {$val}";
29762982
}

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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ Method Signature Changes
6060
- **Config:** ``CodeIgniter\Config\Services::request()`` no longer accepts any parameter.
6161
- **Database:** The following methods have had their signatures updated to remove deprecated parameters:
6262
- ``CodeIgniter\Database\Forge::_createTable()`` no longer accepts the deprecated ``$ifNotExists`` parameter. The method signature is now ``_createTable(string $table, array $attributes)``.
63-
- ``CodeIgniter\Database\BaseBuilder::increment()`` and ``decrement()`` as well as driver-specific builders now accept a string or array for the first parameter, allowing multiple columns to be incremented/decremented in a single call. The method signatures are now ``increment(array|string $column, int $value = 1)`` and ``decrement(array|string $column, int $value = 1)``.
6463

6564
Property Scope Changes
6665
======================
@@ -201,7 +200,7 @@ Database
201200
Query Builder
202201
-------------
203202

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

206205
Forge
207206
-----

user_guide_src/source/database/query_builder.rst

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

20342034
.. php:method:: increment($column[, $value = 1])
20352035
2036-
: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.
2037-
:param int $value: The amount to increment in the column (will be ignored if $column is an array)
2036+
:param string $column: The name of the column to increment
2037+
:param int $value: The amount to increment in the column
20382038

20392039
Increments the value of a field by the specified amount. If the field
20402040
is not a numeric field, like a ``VARCHAR``, it will likely be replaced
20412041
with ``$value``.
20422042

2043-
.. note:: Prior to v4.8.0, only single columns could be incremented.
2043+
.. php:method:: incrementAll(array $columns)
2044+
2045+
.. versionadded:: 4.8.0
2046+
2047+
:param array $columns: An array of column names with their amounts to increment by
2048+
2049+
Increments the value of multiple fields by the specified amounts. If a field
2050+
is not a numeric field, like a ``VARCHAR``, it will likely be replaced
2051+
with the amount specified for that field.
20442052

20452053
.. php:method:: decrement($column[, $value = 1])
20462054
2047-
: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.
2048-
:param int $value: The amount to decrement in the column (will be ignored if $column is an array)
2055+
:param string $column: The name of the column to decrement
2056+
:param int $value: The amount to decrement in the column
20492057

20502058
Decrements the value of a field by the specified amount. If the field
20512059
is not a numeric field, like a ``VARCHAR``, it will likely be replaced
20522060
with ``$value``.
20532061

2054-
.. note:: Prior to v4.8.0, only single columns could be decremented.
2062+
.. php:method:: decrementAll(array $columns)
2063+
2064+
.. versionadded:: 4.8.0
2065+
2066+
:param array $columns: An array of column names with their amounts to decrement by
2067+
2068+
Decrements the value of multiple fields by the specified amounts. If a field
2069+
is not a numeric field, like a ``VARCHAR``, it will likely be replaced
2070+
with the amount specified for that field.
20552071

20562072
.. php:method:: truncate()
20572073

0 commit comments

Comments
 (0)