Skip to content

Commit

Permalink
Merge pull request #6146 from iRedds/fix/qb-increment-decrement
Browse files Browse the repository at this point in the history
Fixed: BaseBuilder increment/decrement do not reset state after a query
  • Loading branch information
kenjis committed Jun 19, 2022
2 parents 24d23c1 + 11f57e5 commit 44cbd89
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 6 deletions.
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.

0 comments on commit 44cbd89

Please sign in to comment.