Skip to content

Commit

Permalink
Merge pull request #9010 from kenjis/fix-qb-select-null-escape
Browse files Browse the repository at this point in the history
fix: [QueryBuilder] `select()` does not escape after `NULL`
  • Loading branch information
kenjis authored Jul 1, 2024
2 parents e7cb689 + 227f53a commit 90cff96
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
6 changes: 4 additions & 2 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,10 @@ public function select($select = '*', ?bool $escape = null)
* This prevents NULL being escaped
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1169
*/
if (mb_stripos(trim($val), 'NULL') === 0) {
$escape = false;
if (mb_stripos($val, 'NULL') === 0) {
$this->QBNoEscape[] = false;

continue;
}

$this->QBNoEscape[] = $escape;
Expand Down
22 changes: 22 additions & 0 deletions tests/system/Database/Builder/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,28 @@ public function testSelectWorksWithComplexSelects(): void
$this->assertSame($expected, str_replace("\n", ' ', $builder->getCompiledSelect()));
}

public function testSelectNullAsInString(): void
{
$builder = new BaseBuilder('users', $this->db);

$builder->select('NULL as field_alias, name');

$expected = 'SELECT NULL as field_alias, "name" FROM "users"';

$this->assertSame($expected, str_replace("\n", ' ', $builder->getCompiledSelect()));
}

public function testSelectNullAsInArray(): void
{
$builder = new BaseBuilder('users', $this->db);

$builder->select(['NULL as field_alias', 'name']);

$expected = 'SELECT NULL as field_alias, "name" FROM "users"';

$this->assertSame($expected, str_replace("\n", ' ', $builder->getCompiledSelect()));
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/4355
*/
Expand Down

0 comments on commit 90cff96

Please sign in to comment.