Skip to content

Commit

Permalink
Merge pull request #5277 from kenjis/fix-escape-negative-integers
Browse files Browse the repository at this point in the history
Fix db escape negative integers
  • Loading branch information
kenjis authored Nov 20, 2021
2 parents 03f5195 + 127b9cf commit 7aa7fcf
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
8 changes: 3 additions & 5 deletions system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ abstract protected function execute(string $sql);
*
* @param mixed ...$binds
*
* @return BaseResult|bool|Query
* @return BaseResult|bool|Query BaseResult when “read” type query, bool when “write” type query, Query when prepared query
*
* @todo BC set $queryClass default as null in 4.1
*/
Expand Down Expand Up @@ -955,6 +955,8 @@ public function getConnectDuration(int $decimals = 6): string
* the correct identifiers.
*
* @param array|string $item
* @param bool $prefixSingle Prefix an item with no segments?
* @param bool $fieldExists Supplied $item contains a field name?
*
* @return array|string
*/
Expand Down Expand Up @@ -1200,10 +1202,6 @@ public function escape($str)
return ($str === false) ? 0 : 1;
}

if (is_numeric($str) && $str < 0) {
return "'{$str}'";
}

return $str ?? 'NULL';
}

Expand Down
32 changes: 32 additions & 0 deletions tests/system/Database/BaseQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,38 @@ public function testSetQueryBindsWithSetEscapeFalse()
$this->assertSame($expected, $query->getQuery());
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/4973
*/
public function testSetQueryBindsWithSetEscapeNegativeIntegers()
{
$query = new Query($this->db);

$query->setQuery(
'SELECT * FROM product WHERE date_pickup < DateAdd(month, ?, Convert(date, GetDate())',
[-6],
true
);

$expected = 'SELECT * FROM product WHERE date_pickup < DateAdd(month, -6, Convert(date, GetDate())';

$this->assertSame($expected, $query->getQuery());
}

public function testSetQueryNamedBindsWithNegativeIntegers()
{
$query = new Query($this->db);

$query->setQuery(
'SELECT * FROM product WHERE date_pickup < DateAdd(month, :num:, Convert(date, GetDate())',
['num' => -6]
);

$expected = 'SELECT * FROM product WHERE date_pickup < DateAdd(month, -6, Convert(date, GetDate())';

$this->assertSame($expected, $query->getQuery());
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/2762
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/system/Database/Live/EscapeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ protected function setUp(): void
*
* @see https://github.com/codeigniter4/CodeIgniter4/issues/606
*/
public function testEscapeProtectsNegativeNumbers()
public function testDoesNotEscapeNegativeNumbers()
{
$this->assertSame("'-100'", $this->db->escape(-100));
$this->assertSame(-100, $this->db->escape(-100));
}

public function testEscape()
Expand Down

0 comments on commit 7aa7fcf

Please sign in to comment.