Skip to content

Commit 6c4acef

Browse files
patel-vanshmichalsnpaulbalandan
authored
fix: SQLSRV driver's decrement() method (#10155)
Co-authored-by: Michal Sniatala <michal@sniatala.pl> Co-authored-by: John Paul E. Balandan, CPA <paulbalandan@gmail.com>
1 parent 3afb68a commit 6c4acef

3 files changed

Lines changed: 75 additions & 1 deletion

File tree

system/Database/SQLSRV/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public function decrement(string $column, int $value = 1)
269269
if ($this->castTextToInt) {
270270
$values = [$column => "CONVERT(VARCHAR(MAX),CONVERT(INT,CONVERT(VARCHAR(MAX), {$column})) - {$value})"];
271271
} else {
272-
$values = [$column => "{$column} + {$value}"];
272+
$values = [$column => "{$column} - {$value}"];
273273
}
274274

275275
$sql = $this->_update($this->QBFrom[0], $values);
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Database\Live\SQLSRV;
15+
16+
use CodeIgniter\Database\SQLSRV\Builder;
17+
use CodeIgniter\Test\CIUnitTestCase;
18+
use CodeIgniter\Test\DatabaseTestTrait;
19+
use PHPUnit\Framework\Attributes\Group;
20+
use Tests\Support\Database\Seeds\CITestSeeder;
21+
22+
/**
23+
* @internal
24+
*/
25+
#[Group('DatabaseLive')]
26+
final class IncrementTest extends CIUnitTestCase
27+
{
28+
use DatabaseTestTrait;
29+
30+
protected $refresh = true;
31+
protected $seed = CITestSeeder::class;
32+
33+
protected function setUp(): void
34+
{
35+
parent::setUp();
36+
37+
if ($this->db->DBDriver !== 'SQLSRV') {
38+
$this->markTestSkipped('This test is only for SQLSRV.');
39+
}
40+
}
41+
42+
public function testIncrementWhenCastTextToIntFalse(): void
43+
{
44+
$this->hasInDatabase('job', ['name' => 'incremental', 'created_at' => 6]);
45+
46+
$builder = $this->db->table('job');
47+
48+
$this->assertInstanceOf(Builder::class, $builder);
49+
50+
$builder->castTextToInt = false;
51+
52+
$builder->where('name', 'incremental')
53+
->increment('created_at');
54+
55+
$this->seeInDatabase('job', ['name' => 'incremental', 'created_at' => 7]);
56+
}
57+
58+
public function testDecrementWhenCastTextToIntFalse(): void
59+
{
60+
$this->hasInDatabase('job', ['name' => 'decremental', 'created_at' => 6]);
61+
62+
$builder = $this->db->table('job');
63+
64+
$this->assertInstanceOf(Builder::class, $builder);
65+
66+
$builder->castTextToInt = false;
67+
68+
$builder->where('name', 'decremental')
69+
->decrement('created_at');
70+
71+
$this->seeInDatabase('job', ['name' => 'decremental', 'created_at' => 5]);
72+
}
73+
}

user_guide_src/source/changelogs/v4.7.3.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Bugs Fixed
4040
- **CLI:** Fixed a bug where ``CLI::generateDimensions()`` leaked ``stty`` error output (e.g., ``stty: 'standard input': Inappropriate ioctl for device``) to stderr when stdin was not a TTY.
4141
- **Commands:** Fixed a bug in the ``env`` command where passing options only would cause the command to throw a ``TypeError`` instead of showing the current environment.
4242
- **Common:** Fixed a bug where the ``command()`` helper function did not properly clean up output buffers, which could lead to risky tests when exceptions were thrown.
43+
- **Database:** Fixed a bug where the SQLSRV driver's decrement method was adding instead of subtracting the decrement value when ``$castTextToInt`` was false.
4344
- **Kint:** Fixed a bug where stale Content Security Policy nonces were reused in worker mode, causing browser CSP violations for Debug Toolbar assets.
4445
- **Time:** Fixed a bug where ``Time::createFromTimestamp()`` could fail for microsecond timestamps when ``LC_NUMERIC`` used a comma decimal separator.
4546
- **Validation:** Fixed a bug where ``Validation::getValidated()`` dropped fields whose validated value was explicitly ``null``.

0 commit comments

Comments
 (0)