Skip to content
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

Fixed: BaseBuilder increment/decrement do not reset state after a query #6146

Merged
merged 2 commits into from
Jun 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2369,7 +2369,13 @@ public function increment(string $column, int $value = 1)

$sql = $this->_update($this->QBFrom[0], [$column => "{$column} + {$value}"]);

return $this->db->query($sql, $this->binds, false);
if (! $this->testMode) {
$this->resetWrite();

return $this->db->query($sql, $this->binds, false);
}

return true;
}

/**
Expand All @@ -2383,7 +2389,13 @@ public function decrement(string $column, int $value = 1)

$sql = $this->_update($this->QBFrom[0], [$column => "{$column}-{$value}"]);

return $this->db->query($sql, $this->binds, false);
if (! $this->testMode) {
$this->resetWrite();

return $this->db->query($sql, $this->binds, false);
}

return true;
}

/**
Expand Down
16 changes: 14 additions & 2 deletions system/Database/Postgre/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,13 @@ public function increment(string $column, int $value = 1)

$sql = $this->_update($this->QBFrom[0], [$column => "to_number({$column}, '9999999') + {$value}"]);

return $this->db->query($sql, $this->binds, false);
if (! $this->testMode) {
$this->resetWrite();

return $this->db->query($sql, $this->binds, false);
}

return true;
}

/**
Expand All @@ -112,7 +118,13 @@ public function decrement(string $column, int $value = 1)

$sql = $this->_update($this->QBFrom[0], [$column => "to_number({$column}, '9999999') - {$value}"]);

return $this->db->query($sql, $this->binds, false);
if (! $this->testMode) {
$this->resetWrite();

return $this->db->query($sql, $this->binds, false);
}

return true;
}

/**
Expand Down
16 changes: 14 additions & 2 deletions system/Database/SQLSRV/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,13 @@ public function increment(string $column, int $value = 1)

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

return $this->db->query($sql, $this->binds, false);
if (! $this->testMode) {
$this->resetWrite();

return $this->db->query($sql, $this->binds, false);
}

return true;
}

/**
Expand All @@ -275,7 +281,13 @@ public function decrement(string $column, int $value = 1)

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

return $this->db->query($sql, $this->binds, false);
if (! $this->testMode) {
$this->resetWrite();

return $this->db->query($sql, $this->binds, false);
}

return true;
}

/**
Expand Down
28 changes: 28 additions & 0 deletions tests/system/Database/Live/IncrementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ public function testIncrementWithValue()
$this->seeInDatabase('job', ['name' => 'incremental', 'description' => '8']);
}

public function testResetStateAfterIncrement()
{
$this->hasInDatabase('job', ['name' => 'account1', 'description' => '10']);
$this->hasInDatabase('job', ['name' => 'account2', 'description' => '10']);

$builder = $this->db->table('job');

$builder->where('name', 'account1')->increment('description');
$builder->where('name', 'account2')->increment('description');

$this->seeInDatabase('job', ['name' => 'account1', 'description' => '11']);
$this->seeInDatabase('job', ['name' => 'account2', 'description' => '11']);
}

public function testDecrement()
{
$this->hasInDatabase('job', ['name' => 'incremental', 'description' => '6']);
Expand All @@ -70,4 +84,18 @@ public function testDecrementWithValue()

$this->seeInDatabase('job', ['name' => 'incremental', 'description' => '4']);
}

public function testResetStateAfterDecrement()
{
$this->hasInDatabase('job', ['name' => 'account1', 'description' => '10']);
$this->hasInDatabase('job', ['name' => 'account2', 'description' => '10']);

$builder = $this->db->table('job');

$builder->where('name', 'account1')->decrement('description');
$builder->where('name', 'account2')->decrement('description');

$this->seeInDatabase('job', ['name' => 'account1', 'description' => '9']);
$this->seeInDatabase('job', ['name' => 'account2', 'description' => '9']);
}
}
35 changes: 35 additions & 0 deletions user_guide_src/source/changelogs/v4.2.2.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Version 4.2.2
#############

Release Date: Unreleased

**4.2.2 release of CodeIgniter4**

.. contents::
:local:
:depth: 2

BREAKING
********

none.

Enhancements
************

none.

Changes
*******

- Fixed: ``BaseBuilder::increment()`` and ``BaseBuilder::decrement()`` do not reset the ``BaseBuilder`` state after a query.

Deprecations
************

none.

Bugs Fixed
**********

See the repo's `CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_ for a complete list of bugs fixed.