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

Fix db escape negative integers #5277

Merged
merged 4 commits into from
Nov 20, 2021
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
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