Skip to content

Commit 7aa7fcf

Browse files
authored
Merge pull request #5277 from kenjis/fix-escape-negative-integers
Fix db escape negative integers
2 parents 03f5195 + 127b9cf commit 7aa7fcf

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

system/Database/BaseConnection.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ abstract protected function execute(string $sql);
565565
*
566566
* @param mixed ...$binds
567567
*
568-
* @return BaseResult|bool|Query
568+
* @return BaseResult|bool|Query BaseResult when “read” type query, bool when “write” type query, Query when prepared query
569569
*
570570
* @todo BC set $queryClass default as null in 4.1
571571
*/
@@ -955,6 +955,8 @@ public function getConnectDuration(int $decimals = 6): string
955955
* the correct identifiers.
956956
*
957957
* @param array|string $item
958+
* @param bool $prefixSingle Prefix an item with no segments?
959+
* @param bool $fieldExists Supplied $item contains a field name?
958960
*
959961
* @return array|string
960962
*/
@@ -1200,10 +1202,6 @@ public function escape($str)
12001202
return ($str === false) ? 0 : 1;
12011203
}
12021204

1203-
if (is_numeric($str) && $str < 0) {
1204-
return "'{$str}'";
1205-
}
1206-
12071205
return $str ?? 'NULL';
12081206
}
12091207

tests/system/Database/BaseQueryTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,38 @@ public function testSetQueryBindsWithSetEscapeFalse()
345345
$this->assertSame($expected, $query->getQuery());
346346
}
347347

348+
/**
349+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/4973
350+
*/
351+
public function testSetQueryBindsWithSetEscapeNegativeIntegers()
352+
{
353+
$query = new Query($this->db);
354+
355+
$query->setQuery(
356+
'SELECT * FROM product WHERE date_pickup < DateAdd(month, ?, Convert(date, GetDate())',
357+
[-6],
358+
true
359+
);
360+
361+
$expected = 'SELECT * FROM product WHERE date_pickup < DateAdd(month, -6, Convert(date, GetDate())';
362+
363+
$this->assertSame($expected, $query->getQuery());
364+
}
365+
366+
public function testSetQueryNamedBindsWithNegativeIntegers()
367+
{
368+
$query = new Query($this->db);
369+
370+
$query->setQuery(
371+
'SELECT * FROM product WHERE date_pickup < DateAdd(month, :num:, Convert(date, GetDate())',
372+
['num' => -6]
373+
);
374+
375+
$expected = 'SELECT * FROM product WHERE date_pickup < DateAdd(month, -6, Convert(date, GetDate())';
376+
377+
$this->assertSame($expected, $query->getQuery());
378+
}
379+
348380
/**
349381
* @see https://github.com/codeigniter4/CodeIgniter4/issues/2762
350382
*/

tests/system/Database/Live/EscapeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ protected function setUp(): void
3838
*
3939
* @see https://github.com/codeigniter4/CodeIgniter4/issues/606
4040
*/
41-
public function testEscapeProtectsNegativeNumbers()
41+
public function testDoesNotEscapeNegativeNumbers()
4242
{
43-
$this->assertSame("'-100'", $this->db->escape(-100));
43+
$this->assertSame(-100, $this->db->escape(-100));
4444
}
4545

4646
public function testEscape()

0 commit comments

Comments
 (0)