Uh oh!
There was an error while loading. Please reload this page.
- 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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
775c9770e25da91bf013db2ddb9625a702cbbb53bcc1aab9a493e7ab7ca102cb8a4dff4d63297ce9afb3747465270416b9155e206839b6cfFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -19,6 +19,7 @@ | ||
| use CodeIgniter\Exceptions\InvalidArgumentException; | ||
| use CodeIgniter\Traits\ConditionalTrait; | ||
| use Config\Feature; | ||
| use TypeError; | ||
| /** | ||
| * Class BaseBuilder | ||
| @@ -3021,9 +3022,41 @@ 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->incrementMany([$column], $value); | ||
| } | ||
| /** | ||
| * Increments multiple numeric columns by the specified value(s). | ||
| * | ||
| * @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 incrementMany(array $columns, int $value = 1): bool | ||
| { | ||
| 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] = "{$col} + {$val}"; | ||
| } | ||
| $sql = $this->_update($this->QBFrom[0], [$column => "{$column} + {$value}"]); | ||
| $sql = $this->_update($this->QBFrom[0], $fields); | ||
patel-vansh marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (! $this->testMode) { | ||
| $this->resetWrite(); | ||
| @@ -3041,9 +3074,41 @@ public function increment(string $column, int $value = 1) | ||
| */ | ||
| public function decrement(string $column, int $value = 1) | ||
| { | ||
| $column = $this->db->protectIdentifiers($column); | ||
| return $this->decrementMany([$column], $value); | ||
| } | ||
| /** | ||
| * Decrements multiple numeric columns by the specified value(s). | ||
| * | ||
| * @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 decrementMany(array $columns, int $value = 1): bool | ||
| { | ||
| 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] = "{$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 |
|---|---|---|
| @@ -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)) { | ||
| ||
| 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(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -18,7 +18,9 @@ | ||
| use CodeIgniter\Database\Exceptions\DataException; | ||
| use CodeIgniter\Database\RawSql; | ||
| use CodeIgniter\Database\ResultInterface; | ||
| use CodeIgniter\Exceptions\InvalidArgumentException; | ||
| use Config\Feature; | ||
| use TypeError; | ||
| /** | ||
| * Builder for SQLSRV | ||
| @@ -232,21 +234,41 @@ protected function _update(string $table, array $values): string | ||
| } | ||
| /** | ||
| * Increments a numeric column by the specified value. | ||
| * Increments multiple numeric columns by the specified value(s). | ||
| * | ||
| * @return bool | ||
| * @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 ($this->castTextToInt) { | ||
| $values = [$column => "CONVERT(VARCHAR(MAX),CONVERT(INT,CONVERT(VARCHAR(MAX), {$column})) + {$value})"]; | ||
| } else { | ||
| $values = [$column => "{$column} + {$value}"]; | ||
| 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); | ||
| if ($this->castTextToInt) { | ||
| $fields[$col] = "CONVERT(VARCHAR(MAX),CONVERT(INT,CONVERT(VARCHAR(MAX), {$col})) + {$val})"; | ||
| } else { | ||
| $fields[$col] = "{$col} + {$val}"; | ||
| } | ||
| } | ||
| $sql = $this->_update($this->QBFrom[0], $values); | ||
| $sql = $this->_update($this->QBFrom[0], $fields); | ||
| if (! $this->testMode) { | ||
| $this->resetWrite(); | ||
| @@ -258,21 +280,41 @@ 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 bool | ||
| * @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 ($this->castTextToInt) { | ||
| $values = [$column => "CONVERT(VARCHAR(MAX),CONVERT(INT,CONVERT(VARCHAR(MAX), {$column})) - {$value})"]; | ||
| } else { | ||
| $values = [$column => "{$column} + {$value}"]; | ||
patel-vansh marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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); | ||
| if ($this->castTextToInt) { | ||
| $fields[$col] = "CONVERT(VARCHAR(MAX),CONVERT(INT,CONVERT(VARCHAR(MAX), {$col})) - {$val})"; | ||
| } else { | ||
| $fields[$col] = "{$col} - {$val}"; | ||
| } | ||
| } | ||
| $sql = $this->_update($this->QBFrom[0], $values); | ||
| $sql = $this->_update($this->QBFrom[0], $fields); | ||
| if (! $this->testMode) { | ||
| $this->resetWrite(); | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.