From 3f24caff8f0695c6a5ac185f046e67682d2648b1 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 28 Jun 2024 09:33:32 +0900 Subject: [PATCH 1/3] test: add tests for `SELECT NULL as` --- tests/system/Database/Builder/SelectTest.php | 22 ++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/system/Database/Builder/SelectTest.php b/tests/system/Database/Builder/SelectTest.php index cd3f39029f6b..a761706dd3df 100644 --- a/tests/system/Database/Builder/SelectTest.php +++ b/tests/system/Database/Builder/SelectTest.php @@ -100,6 +100,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 */ From 6ba48cf061e28321986b0cc389e0bab41380082d Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 28 Jun 2024 09:34:14 +0900 Subject: [PATCH 2/3] fix: select() does not escape after `NULL as` --- system/Database/BaseBuilder.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/system/Database/BaseBuilder.php b/system/Database/BaseBuilder.php index 4e6e422b2a6f..fab19b60dab8 100644 --- a/system/Database/BaseBuilder.php +++ b/system/Database/BaseBuilder.php @@ -425,7 +425,9 @@ public function select($select = '*', ?bool $escape = null) * @see https://github.com/codeigniter4/CodeIgniter4/issues/1169 */ if (mb_stripos(trim($val), 'NULL') === 0) { - $escape = false; + $this->QBNoEscape[] = false; + + continue; } $this->QBNoEscape[] = $escape; From 227f53a6cdaf1f1a7156c0e588e70625214ce920 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 28 Jun 2024 09:39:06 +0900 Subject: [PATCH 3/3] refactor: remove duplicated `trim()` It is already trimmed in Line 415. --- system/Database/BaseBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Database/BaseBuilder.php b/system/Database/BaseBuilder.php index fab19b60dab8..acfaa9e9183c 100644 --- a/system/Database/BaseBuilder.php +++ b/system/Database/BaseBuilder.php @@ -424,7 +424,7 @@ 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) { + if (mb_stripos($val, 'NULL') === 0) { $this->QBNoEscape[] = false; continue;