Skip to content

Commit 493e7ab

Browse files
committed
Applying suggested changes.
1 parent c1aab9a commit 493e7ab

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
@@ -3021,19 +3022,36 @@ protected function _deleteBatch(string $table, array $keys, array $values): stri
30213022
*/
30223023
public function increment(string $column, int $value = 1)
30233024
{
3024-
return $this->incrementAll([$column => $value]);
3025+
return $this->incrementMany([$column], $value);
30253026
}
30263027

30273028
/**
3028-
* Increments multiple numeric columns by the specified values.
3029+
* Increments multiple numeric columns by the specified value(s).
30293030
*
3030-
* @param array<string, int> $columns An array of column => value pairs to increment.
3031+
* @param array<string, int>|list<string> $columns A list of columns or array of column => value pairs to increment.
3032+
* @param int $value The value to increment by if $columns is a list of column names.
30313033
*/
3032-
public function incrementAll(array $columns): bool
3034+
public function incrementMany(array $columns, int $value = 1): bool
30333035
{
3036+
if ($columns === []) {
3037+
throw new InvalidArgumentException('Argument #1 ($columns) cannot be empty.');
3038+
}
3039+
3040+
if (array_is_list($columns)) {
3041+
$columns = array_fill_keys($columns, $value);
3042+
}
3043+
30343044
$fields = [];
30353045

30363046
foreach ($columns as $col => $val) {
3047+
if (! is_int($val)) {
3048+
throw new TypeError(sprintf(
3049+
'Argument #1 ($columns) must contain only int values, %s given for "%s".',
3050+
get_debug_type($val),
3051+
$col,
3052+
));
3053+
}
3054+
30373055
$col = $this->db->protectIdentifiers($col);
30383056
$fields[$col] = "{$col} + {$val}";
30393057
}
@@ -3056,19 +3074,36 @@ public function incrementAll(array $columns): bool
30563074
*/
30573075
public function decrement(string $column, int $value = 1)
30583076
{
3059-
return $this->decrementAll([$column => $value]);
3077+
return $this->decrementMany([$column], $value);
30603078
}
30613079

30623080
/**
3063-
* Decrements multiple numeric columns by the specified values.
3081+
* Decrements multiple numeric columns by the specified value(s).
30643082
*
3065-
* @param array<string, int> $columns An array of column => value pairs to decrement.
3083+
* @param array<string, int>|list<string> $columns A list of columns or array of column => value pairs to decrement.
3084+
* @param int $value The value to decrement by if $columns is a list of column names.
30663085
*/
3067-
public function decrementAll(array $columns): bool
3086+
public function decrementMany(array $columns, int $value = 1): bool
30683087
{
3088+
if ($columns === []) {
3089+
throw new InvalidArgumentException('Argument #1 ($columns) cannot be empty.');
3090+
}
3091+
3092+
if (array_is_list($columns)) {
3093+
$columns = array_fill_keys($columns, $value);
3094+
}
3095+
30693096
$fields = [];
30703097

30713098
foreach ($columns as $col => $val) {
3099+
if (! is_int($val)) {
3100+
throw new TypeError(sprintf(
3101+
'Argument #1 ($columns) must contain only int values, %s given for "%s".',
3102+
get_debug_type($val),
3103+
$col,
3104+
));
3105+
}
3106+
30723107
$col = $this->db->protectIdentifiers($col);
30733108
$fields[$col] = "{$col} - {$val}";
30743109
}

‎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)