Skip to content

Commit 3f8d887

Browse files
committed
Applying suggested changes.
1 parent acdc974 commit 3f8d887

6 files changed

Lines changed: 292 additions & 62 deletions

File tree

system/Database/BaseBuilder.php

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use CodeIgniter\Exceptions\InvalidArgumentException;
2020
use CodeIgniter\Traits\ConditionalTrait;
2121
use Config\Feature;
22+
use TypeError;
2223

2324
/**
2425
* Class BaseBuilder
@@ -2929,19 +2930,36 @@ protected function _deleteBatch(string $table, array $keys, array $values): stri
29292930
*/
29302931
public function increment(string $column, int $value = 1)
29312932
{
2932-
return $this->incrementAll([$column => $value]);
2933+
return $this->incrementMany([$column], $value);
29332934
}
29342935

29352936
/**
2936-
* Increments multiple numeric columns by the specified values.
2937+
* Increments multiple numeric columns by the specified value(s).
29372938
*
2938-
* @param array<string, int> $columns An array of column => value pairs to increment.
2939+
* @param array<string, int>|list<string> $columns A list of columns or array of column => value pairs to increment.
2940+
* @param int $value The value to increment by if $columns is a list of column names.
29392941
*/
2940-
public function incrementAll(array $columns): bool
2942+
public function incrementMany(array $columns, int $value = 1): bool
29412943
{
2944+
if ($columns === []) {
2945+
throw new InvalidArgumentException('Argument #1 ($columns) cannot be empty.');
2946+
}
2947+
2948+
if (array_is_list($columns)) {
2949+
$columns = array_fill_keys($columns, $value);
2950+
}
2951+
29422952
$fields = [];
29432953

29442954
foreach ($columns as $col => $val) {
2955+
if (! is_int($val)) {
2956+
throw new TypeError(sprintf(
2957+
'Argument #1 ($columns) must contain only int values, %s given for "%s".',
2958+
get_debug_type($val),
2959+
$col,
2960+
));
2961+
}
2962+
29452963
$col = $this->db->protectIdentifiers($col);
29462964
$fields[$col] = "{$col} + {$val}";
29472965
}
@@ -2964,19 +2982,36 @@ public function incrementAll(array $columns): bool
29642982
*/
29652983
public function decrement(string $column, int $value = 1)
29662984
{
2967-
return $this->decrementAll([$column => $value]);
2985+
return $this->decrementMany([$column], $value);
29682986
}
29692987

29702988
/**
2971-
* Decrements multiple numeric columns by the specified values.
2989+
* Decrements multiple numeric columns by the specified value(s).
29722990
*
2973-
* @param array<string, int> $columns An array of column => value pairs to decrement.
2991+
* @param array<string, int>|list<string> $columns A list of columns or array of column => value pairs to decrement.
2992+
* @param int $value The value to decrement by if $columns is a list of column names.
29742993
*/
2975-
public function decrementAll(array $columns): bool
2994+
public function decrementMany(array $columns, int $value = 1): bool
29762995
{
2996+
if ($columns === []) {
2997+
throw new InvalidArgumentException('Argument #1 ($columns) cannot be empty.');
2998+
}
2999+
3000+
if (array_is_list($columns)) {
3001+
$columns = array_fill_keys($columns, $value);
3002+
}
3003+
29773004
$fields = [];
29783005

29793006
foreach ($columns as $col => $val) {
3007+
if (! is_int($val)) {
3008+
throw new TypeError(sprintf(
3009+
'Argument #1 ($columns) must contain only int values, %s given for "%s".',
3010+
get_debug_type($val),
3011+
$col,
3012+
));
3013+
}
3014+
29803015
$col = $this->db->protectIdentifiers($col);
29813016
$fields[$col] = "{$col} - {$val}";
29823017
}

system/Database/Postgre/Builder.php

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\Database\Exceptions\DatabaseException;
1818
use CodeIgniter\Database\RawSql;
1919
use CodeIgniter\Exceptions\InvalidArgumentException;
20+
use TypeError;
2021

2122
/**
2223
* Builder for Postgre
@@ -87,17 +88,34 @@ public function orderBy(string $orderBy, string $direction = '', ?bool $escape =
8788
}
8889

8990
/**
90-
* Increments multiple numeric columns by the specified values.
91+
* Increments multiple numeric columns by the specified value(s).
9192
*
92-
* @param array<string, int> $columns An array of column => value pairs to increment.
93+
* @param array<string, int>|list<string> $columns A list of columns or array of column => value pairs to increment.
94+
* @param int $value The value to increment by if $columns is a list of column names.
9395
*/
94-
public function incrementAll(array $columns): bool
96+
public function incrementMany(array $columns, int $value = 1): bool
9597
{
98+
if ($columns === []) {
99+
throw new InvalidArgumentException('Argument #1 ($columns) cannot be empty.');
100+
}
101+
102+
if (array_is_list($columns)) {
103+
$columns = array_fill_keys($columns, $value);
104+
}
105+
96106
$fields = [];
97107

98-
foreach ($columns as $column => $value) {
99-
$column = $this->db->protectIdentifiers($column);
100-
$fields[$column] = "to_number({$column}, '9999999') + {$value}";
108+
foreach ($columns as $col => $val) {
109+
if (! is_int($val)) {
110+
throw new TypeError(sprintf(
111+
'Argument #1 ($columns) must contain only int values, %s given for "%s".',
112+
get_debug_type($val),
113+
$col,
114+
));
115+
}
116+
117+
$col = $this->db->protectIdentifiers($col);
118+
$fields[$col] = "to_number({$col}, '9999999') + {$val}";
101119
}
102120

103121
$sql = $this->_update($this->QBFrom[0], $fields);
@@ -112,17 +130,34 @@ public function incrementAll(array $columns): bool
112130
}
113131

114132
/**
115-
* Decrements multiple numeric columns by the specified values.
133+
* Decrements multiple numeric columns by the specified value(s).
116134
*
117-
* @param array<string, int> $columns An array of column => value pairs to decrement.
135+
* @param array<string, int>|list<string> $columns A list of columns or array of column => value pairs to decrement.
136+
* @param int $value The value to decrement by if $columns is a list of column names.
118137
*/
119-
public function decrementAll(array $columns): bool
138+
public function decrementMany(array $columns, int $value = 1): bool
120139
{
140+
if ($columns === []) {
141+
throw new InvalidArgumentException('Argument #1 ($columns) cannot be empty.');
142+
}
143+
144+
if (array_is_list($columns)) {
145+
$columns = array_fill_keys($columns, $value);
146+
}
147+
121148
$fields = [];
122149

123-
foreach ($columns as $column => $value) {
124-
$column = $this->db->protectIdentifiers($column);
125-
$fields[$column] = "to_number({$column}, '9999999') - {$value}";
150+
foreach ($columns as $col => $val) {
151+
if (! is_int($val)) {
152+
throw new TypeError(sprintf(
153+
'Argument #1 ($columns) must contain only int values, %s given for "%s".',
154+
get_debug_type($val),
155+
$col,
156+
));
157+
}
158+
159+
$col = $this->db->protectIdentifiers($col);
160+
$fields[$col] = "to_number({$col}, '9999999') - {$val}";
126161
}
127162

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

system/Database/SQLSRV/Builder.php

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
use CodeIgniter\Database\Exceptions\DataException;
1919
use CodeIgniter\Database\RawSql;
2020
use CodeIgniter\Database\ResultInterface;
21+
use CodeIgniter\Exceptions\InvalidArgumentException;
2122
use Config\Feature;
23+
use TypeError;
2224

2325
/**
2426
* Builder for SQLSRV
@@ -232,20 +234,37 @@ protected function _update(string $table, array $values): string
232234
}
233235

234236
/**
235-
* Increments multiple numeric columns by the specified values.
237+
* Increments multiple numeric columns by the specified value(s).
236238
*
237-
* @param array<string, int> $columns An array of column => value pairs to increment.
239+
* @param array<string, int>|list<string> $columns A list of columns or array of column => value pairs to increment.
240+
* @param int $value The value to increment by if $columns is a list of column names.
238241
*/
239-
public function incrementAll(array $columns): bool
242+
public function incrementMany(array $columns, int $value = 1): bool
240243
{
244+
if ($columns === []) {
245+
throw new InvalidArgumentException('Argument #1 ($columns) cannot be empty.');
246+
}
247+
248+
if (array_is_list($columns)) {
249+
$columns = array_fill_keys($columns, $value);
250+
}
251+
241252
$fields = [];
242253

243-
foreach ($columns as $column => $value) {
244-
$column = $this->db->protectIdentifiers($column);
254+
foreach ($columns as $col => $val) {
255+
if (! is_int($val)) {
256+
throw new TypeError(sprintf(
257+
'Argument #1 ($columns) must contain only int values, %s given for "%s".',
258+
get_debug_type($val),
259+
$col,
260+
));
261+
}
262+
263+
$col = $this->db->protectIdentifiers($col);
245264
if ($this->castTextToInt) {
246-
$fields[$column] = "CONVERT(VARCHAR(MAX),CONVERT(INT,CONVERT(VARCHAR(MAX), {$column})) + {$value})";
265+
$fields[$col] = "CONVERT(VARCHAR(MAX),CONVERT(INT,CONVERT(VARCHAR(MAX), {$col})) + {$val})";
247266
} else {
248-
$fields[$column] = "{$column} + {$value}";
267+
$fields[$col] = "{$col} + {$val}";
249268
}
250269
}
251270

@@ -261,20 +280,37 @@ public function incrementAll(array $columns): bool
261280
}
262281

263282
/**
264-
* Decrements multiple numeric columns by the specified values.
283+
* Decrements multiple numeric columns by the specified value(s).
265284
*
266-
* @param array<string, int> $columns An array of column => value pairs to decrement.
285+
* @param array<string, int>|list<string> $columns A list of columns or array of column => value pairs to decrement.
286+
* @param int $value The value to decrement by if $columns is a list of column names.
267287
*/
268-
public function decrementAll(array $columns): bool
288+
public function decrementMany(array $columns, int $value = 1): bool
269289
{
290+
if ($columns === []) {
291+
throw new InvalidArgumentException('Argument #1 ($columns) cannot be empty.');
292+
}
293+
294+
if (array_is_list($columns)) {
295+
$columns = array_fill_keys($columns, $value);
296+
}
297+
270298
$fields = [];
271299

272-
foreach ($columns as $column => $value) {
273-
$column = $this->db->protectIdentifiers($column);
300+
foreach ($columns as $col => $val) {
301+
if (! is_int($val)) {
302+
throw new TypeError(sprintf(
303+
'Argument #1 ($columns) must contain only int values, %s given for "%s".',
304+
get_debug_type($val),
305+
$col,
306+
));
307+
}
308+
309+
$col = $this->db->protectIdentifiers($col);
274310
if ($this->castTextToInt) {
275-
$fields[$column] = "CONVERT(VARCHAR(MAX),CONVERT(INT,CONVERT(VARCHAR(MAX), {$column})) - {$value})";
311+
$fields[$col] = "CONVERT(VARCHAR(MAX),CONVERT(INT,CONVERT(VARCHAR(MAX), {$col})) - {$val})";
276312
} else {
277-
$fields[$column] = "{$column} - {$value}";
313+
$fields[$col] = "{$col} - {$val}";
278314
}
279315
}
280316

0 commit comments

Comments
 (0)