-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: Add incrementAll() and decrementAll() 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
afc7f0c
bff9dc8
9d69905
77f431a
a845cf4
ca4fdf7
acdc974
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 |
|---|---|---|
|
|
@@ -2929,9 +2929,24 @@ protected function _deleteBatch(string $table, array $keys, array $values): stri | |
| */ | ||
| public function increment(string $column, int $value = 1) | ||
| { | ||
| $column = $this->db->protectIdentifiers($column); | ||
| return $this->incrementAll([$column => $value]); | ||
| } | ||
|
|
||
| /** | ||
| * Increments multiple numeric columns by the specified values. | ||
| * | ||
| * @param array<string, int> $columns An array of column => value pairs to increment. | ||
| */ | ||
| public function incrementAll(array $columns): bool | ||
| { | ||
| $fields = []; | ||
|
|
||
| foreach ($columns as $col => $val) { | ||
| $col = $this->db->protectIdentifiers($col); | ||
| $fields[$col] = "{$col} + {$val}"; | ||
| } | ||
|
|
||
| $sql = $this->_update($this->QBFrom[0], [$column => "{$column} + {$value}"]); | ||
| $sql = $this->_update($this->QBFrom[0], $fields); | ||
|
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. What if |
||
|
|
||
| if (! $this->testMode) { | ||
| $this->resetWrite(); | ||
|
|
@@ -2949,9 +2964,24 @@ public function increment(string $column, int $value = 1) | |
| */ | ||
| public function decrement(string $column, int $value = 1) | ||
| { | ||
| $column = $this->db->protectIdentifiers($column); | ||
| return $this->decrementAll([$column => $value]); | ||
| } | ||
|
|
||
| /** | ||
| * Decrements multiple numeric columns by the specified values. | ||
| * | ||
| * @param array<string, int> $columns An array of column => value pairs to decrement. | ||
| */ | ||
| public function decrementAll(array $columns): bool | ||
| { | ||
| $fields = []; | ||
|
|
||
| foreach ($columns as $col => $val) { | ||
| $col = $this->db->protectIdentifiers($col); | ||
| $fields[$col] = "{$col} - {$val}"; | ||
| } | ||
|
|
||
| $sql = $this->_update($this->QBFrom[0], [$column => "{$column}-{$value}"]); | ||
| $sql = $this->_update($this->QBFrom[0], $fields); | ||
|
|
||
| if (! $this->testMode) { | ||
| $this->resetWrite(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -232,21 +232,24 @@ protected function _update(string $table, array $values): string | |
| } | ||
|
|
||
| /** | ||
| * Increments a numeric column by the specified value. | ||
| * Increments multiple numeric columns by the specified values. | ||
| * | ||
| * @return bool | ||
| * @param array<string, int> $columns An array of column => value pairs to increment. | ||
| */ | ||
| public function increment(string $column, int $value = 1) | ||
| public function incrementAll(array $columns): bool | ||
|
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'm rethinking now my suggestion for
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.
|
||
| { | ||
| $column = $this->db->protectIdentifiers($column); | ||
| $fields = []; | ||
|
|
||
| if ($this->castTextToInt) { | ||
| $values = [$column => "CONVERT(VARCHAR(MAX),CONVERT(INT,CONVERT(VARCHAR(MAX), {$column})) + {$value})"]; | ||
| } else { | ||
| $values = [$column => "{$column} + {$value}"]; | ||
| foreach ($columns as $column => $value) { | ||
| $column = $this->db->protectIdentifiers($column); | ||
| if ($this->castTextToInt) { | ||
| $fields[$column] = "CONVERT(VARCHAR(MAX),CONVERT(INT,CONVERT(VARCHAR(MAX), {$column})) + {$value})"; | ||
| } else { | ||
| $fields[$column] = "{$column} + {$value}"; | ||
| } | ||
| } | ||
|
|
||
| $sql = $this->_update($this->QBFrom[0], $values); | ||
| $sql = $this->_update($this->QBFrom[0], $fields); | ||
|
|
||
| if (! $this->testMode) { | ||
| $this->resetWrite(); | ||
|
|
@@ -258,21 +261,24 @@ public function increment(string $column, int $value = 1) | |
| } | ||
|
|
||
| /** | ||
| * Decrements a numeric column by the specified value. | ||
| * Decrements multiple numeric columns by the specified values. | ||
| * | ||
| * @return bool | ||
| * @param array<string, int> $columns An array of column => value pairs to decrement. | ||
| */ | ||
| public function decrement(string $column, int $value = 1) | ||
| public function decrementAll(array $columns): bool | ||
| { | ||
| $column = $this->db->protectIdentifiers($column); | ||
| $fields = []; | ||
|
|
||
| if ($this->castTextToInt) { | ||
| $values = [$column => "CONVERT(VARCHAR(MAX),CONVERT(INT,CONVERT(VARCHAR(MAX), {$column})) - {$value})"]; | ||
| } else { | ||
| $values = [$column => "{$column} + {$value}"]; | ||
|
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. Hmm. Comparing this removed L272 against the new L277. It seems an unnoticed bug in |
||
| foreach ($columns as $column => $value) { | ||
| $column = $this->db->protectIdentifiers($column); | ||
| if ($this->castTextToInt) { | ||
| $fields[$column] = "CONVERT(VARCHAR(MAX),CONVERT(INT,CONVERT(VARCHAR(MAX), {$column})) - {$value})"; | ||
| } else { | ||
| $fields[$column] = "{$column} - {$value}"; | ||
| } | ||
| } | ||
|
|
||
| $sql = $this->_update($this->QBFrom[0], $values); | ||
| $sql = $this->_update($this->QBFrom[0], $fields); | ||
|
|
||
| if (! $this->testMode) { | ||
| $this->resetWrite(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,6 +51,17 @@ public function testIncrementWithValue(): void | |
| $this->seeInDatabase('job', ['name' => 'incremental', 'description' => '8']); | ||
| } | ||
|
|
||
| public function testIncrementAll(): void | ||
|
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. Can you move this test after all increment tests, then replicate the 3 testing done ( base increment test, with value, and reset state). Same goes for decrement. |
||
| { | ||
| $this->hasInDatabase('task', ['name' => 'task1', 'description' => '6', 'priority' => '1']); | ||
|
|
||
| $this->db->table('task') | ||
| ->where('name', 'task1') | ||
| ->incrementAll(['description' => 2, 'priority' => 3]); | ||
|
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. Does this accept negative values? If yes, maybe add some to the tests? |
||
|
|
||
| $this->seeInDatabase('task', ['name' => 'task1', 'description' => '8', 'priority' => '4']); | ||
| } | ||
|
|
||
| public function testResetStateAfterIncrement(): void | ||
| { | ||
| $this->hasInDatabase('job', ['name' => 'account1', 'description' => '10']); | ||
|
|
@@ -87,6 +98,17 @@ public function testDecrementWithValue(): void | |
| $this->seeInDatabase('job', ['name' => 'incremental', 'description' => '4']); | ||
| } | ||
|
|
||
| public function testDecrementAll(): void | ||
| { | ||
| $this->hasInDatabase('task', ['name' => 'task2', 'description' => '6', 'priority' => '5']); | ||
|
|
||
| $this->db->table('task') | ||
| ->where('name', 'task2') | ||
| ->decrementAll(['description' => 2, 'priority' => 3]); | ||
|
|
||
| $this->seeInDatabase('task', ['name' => 'task2', 'description' => '4', 'priority' => '2']); | ||
| } | ||
|
|
||
| public function testResetStateAfterDecrement(): void | ||
| { | ||
| $this->hasInDatabase('job', ['name' => 'account1', 'description' => '10']); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2034,12 +2034,22 @@ Class Reference | |
| .. php:method:: increment($column[, $value = 1]) | ||
|
|
||
| :param string $column: The name of the column to increment | ||
| :param int $value: The amount to increment in the column | ||
| :param int $value: The amount to increment in the column | ||
|
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. Do you mean to align this with above param? |
||
|
|
||
| Increments the value of a field by the specified amount. If the field | ||
| is not a numeric field, like a ``VARCHAR``, it will likely be replaced | ||
| with ``$value``. | ||
|
|
||
| .. php:method:: incrementAll(array $columns) | ||
|
|
||
| .. versionadded:: 4.8.0 | ||
|
|
||
| :param array $columns: An array of column names with their amounts to increment by | ||
|
|
||
| Increments the value of multiple fields by the specified amounts. If a field | ||
| is not a numeric field, like a ``VARCHAR``, it will likely be replaced | ||
| with the amount specified for that field. | ||
|
|
||
| .. php:method:: decrement($column[, $value = 1]) | ||
|
|
||
| :param string $column: The name of the column to decrement | ||
|
|
@@ -2049,6 +2059,16 @@ Class Reference | |
| is not a numeric field, like a ``VARCHAR``, it will likely be replaced | ||
| with ``$value``. | ||
|
|
||
| .. php:method:: decrementAll(array $columns) | ||
|
|
||
| .. versionadded:: 4.8.0 | ||
|
|
||
| :param array $columns: An array of column names with their amounts to decrement by | ||
|
|
||
| Decrements the value of multiple fields by the specified amounts. If a field | ||
| is not a numeric field, like a ``VARCHAR``, it will likely be replaced | ||
| with the amount specified for that field. | ||
|
|
||
| .. php:method:: truncate() | ||
|
|
||
| :returns: ``true`` on success, ``false`` on failure, string on test mode | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| # total 2036 errors | ||
| # total 2032 errors | ||
|
|
||
| includes: | ||
| - argument.type.neon | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| # total 1228 errors | ||
| # total 1226 errors | ||
|
|
||
| parameters: | ||
| ignoreErrors: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we no longer have runtime type enforcement for each value, we should add a check manually. Something like: