-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: Add incrementMany() and decrementMany() methods to BaseBuilder and other driver specific builders
#10140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 4.8
Are you sure you want to change the base?
Changes from all commits
07b08d1
37d6527
232283b
a6fbba7
219e7e8
825f4b3
3784d4b
3e1e460
2496ba7
ccfbacc
b4d49c6
3127b88
8bad4ea
274a118
57ddc07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| use CodeIgniter\Database\Exceptions\DatabaseException; | ||
| use CodeIgniter\Database\RawSql; | ||
| use CodeIgniter\Exceptions\InvalidArgumentException; | ||
| use TypeError; | ||
|
|
||
| /** | ||
| * Builder for Postgre | ||
|
|
@@ -87,17 +88,37 @@ public function orderBy(string $orderBy, string $direction = '', ?bool $escape = | |
| } | ||
|
|
||
| /** | ||
| * Increments a numeric column by the specified value. | ||
| * Increments multiple numeric columns by the specified value(s). | ||
| * | ||
| * @return mixed | ||
| * | ||
| * @throws DatabaseException | ||
| * @param array<string, int>|list<string> $columns A list of columns or array of column => value pairs to increment. | ||
| * @param int $value The value to increment by if $columns is a list of column names. | ||
| */ | ||
| public function increment(string $column, int $value = 1) | ||
| public function incrementMany(array $columns, int $value = 1): bool | ||
| { | ||
| $column = $this->db->protectIdentifiers($column); | ||
| if ($columns === []) { | ||
| throw new InvalidArgumentException('Argument #1 ($columns) cannot be empty.'); | ||
| } | ||
|
|
||
| if (array_is_list($columns)) { | ||
| $columns = array_fill_keys($columns, $value); | ||
| } | ||
|
|
||
| $fields = []; | ||
|
|
||
| foreach ($columns as $col => $val) { | ||
| if (! is_int($val)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You forgot to test negative values. The The comment applies to all new methods
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, negative values were allowed in earlier behaviour.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see no problem with allowing negative values. This may be an actively used feature. |
||
| throw new TypeError(sprintf( | ||
| 'Argument #1 ($columns) must contain only int values, %s given for "%s".', | ||
| get_debug_type($val), | ||
| $col, | ||
| )); | ||
| } | ||
|
|
||
| $col = $this->db->protectIdentifiers($col); | ||
| $fields[$col] = "CAST({$col} AS numeric) + {$val}"; | ||
| } | ||
|
|
||
| $sql = $this->_update($this->QBFrom[0], [$column => "to_number({$column}, '9999999') + {$value}"]); | ||
| $sql = $this->_update($this->QBFrom[0], $fields); | ||
|
|
||
| if (! $this->testMode) { | ||
| $this->resetWrite(); | ||
|
|
@@ -109,17 +130,37 @@ public function increment(string $column, int $value = 1) | |
| } | ||
|
|
||
| /** | ||
| * Decrements a numeric column by the specified value. | ||
| * Decrements multiple numeric columns by the specified value(s). | ||
| * | ||
| * @return mixed | ||
| * | ||
| * @throws DatabaseException | ||
| * @param array<string, int>|list<string> $columns A list of columns or array of column => value pairs to decrement. | ||
| * @param int $value The value to decrement by if $columns is a list of column names. | ||
| */ | ||
| public function decrement(string $column, int $value = 1) | ||
| public function decrementMany(array $columns, int $value = 1): bool | ||
| { | ||
| $column = $this->db->protectIdentifiers($column); | ||
| if ($columns === []) { | ||
| throw new InvalidArgumentException('Argument #1 ($columns) cannot be empty.'); | ||
| } | ||
|
|
||
| if (array_is_list($columns)) { | ||
| $columns = array_fill_keys($columns, $value); | ||
| } | ||
|
|
||
| $fields = []; | ||
|
|
||
| foreach ($columns as $col => $val) { | ||
| if (! is_int($val)) { | ||
| throw new TypeError(sprintf( | ||
| 'Argument #1 ($columns) must contain only int values, %s given for "%s".', | ||
| get_debug_type($val), | ||
| $col, | ||
| )); | ||
| } | ||
|
|
||
| $col = $this->db->protectIdentifiers($col); | ||
| $fields[$col] = "CAST({$col} AS numeric) - {$val}"; | ||
| } | ||
|
|
||
| $sql = $this->_update($this->QBFrom[0], [$column => "to_number({$column}, '9999999') - {$value}"]); | ||
| $sql = $this->_update($this->QBFrom[0], $fields); | ||
|
|
||
| if (! $this->testMode) { | ||
| $this->resetWrite(); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.