From 5c6f468f03f817f1cd63b045a5690e231cced2e9 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 19 Feb 2023 14:06:55 +0900 Subject: [PATCH 1/2] fix: RawSql causes error when using like() and countAllResults() --- system/Database/BaseBuilder.php | 4 ++++ tests/system/Database/Live/LikeTest.php | 29 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/system/Database/BaseBuilder.php b/system/Database/BaseBuilder.php index 653f43013f15..31234f9edf1a 100644 --- a/system/Database/BaseBuilder.php +++ b/system/Database/BaseBuilder.php @@ -3082,6 +3082,10 @@ protected function compileWhereHaving(string $qbKey): string continue; } + if ($qbkey instanceof RawSql) { + continue; + } + if ($qbkey['condition'] instanceof RawSql) { $qbkey = $qbkey['condition']; diff --git a/tests/system/Database/Live/LikeTest.php b/tests/system/Database/Live/LikeTest.php index f192d4a0f224..379de0cf1820 100644 --- a/tests/system/Database/Live/LikeTest.php +++ b/tests/system/Database/Live/LikeTest.php @@ -11,6 +11,7 @@ namespace CodeIgniter\Database\Live; +use CodeIgniter\Database\RawSql; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\DatabaseTestTrait; use Tests\Support\Database\Seeds\CITestSeeder; @@ -121,4 +122,32 @@ public function testLikeSpacesOrTabs() $this->assertCount(1, $spaces); $this->assertCount(1, $tabs); } + + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/7268 + */ + public function testLikeRawSqlAndCountAllResultsAndGet() + { + $builder = $this->db->table('job'); + $builder->like(new RawSql('name'), 'Developer'); + $count = $builder->countAllResults(false); + $results = $builder->get()->getResult(); + + $this->assertSame(1, $count); + $this->assertSame('Developer', $results[0]->name); + } + + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/7268 + */ + public function testLikeRawSqlAndGetAndCountAllResults() + { + $builder = $this->db->table('job'); + $builder->like(new RawSql('name'), 'Developer'); + $results = $builder->get(null, 0, false)->getResult(); + $count = $builder->countAllResults(); + + $this->assertSame(1, $count); + $this->assertSame('Developer', $results[0]->name); + } } From 0d8c914ced2c4197e7c7d6ea098904ee51eee4f9 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 19 Feb 2023 14:39:03 +0900 Subject: [PATCH 2/2] test: fix for OCI8 --- tests/system/Database/Live/LikeTest.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tests/system/Database/Live/LikeTest.php b/tests/system/Database/Live/LikeTest.php index 379de0cf1820..fe616105a466 100644 --- a/tests/system/Database/Live/LikeTest.php +++ b/tests/system/Database/Live/LikeTest.php @@ -129,7 +129,14 @@ public function testLikeSpacesOrTabs() public function testLikeRawSqlAndCountAllResultsAndGet() { $builder = $this->db->table('job'); - $builder->like(new RawSql('name'), 'Developer'); + + if ($this->db->DBDriver === 'OCI8') { + $key = new RawSql('"name"'); + } else { + $key = new RawSql('name'); + } + + $builder->like($key, 'Developer'); $count = $builder->countAllResults(false); $results = $builder->get()->getResult(); @@ -143,7 +150,14 @@ public function testLikeRawSqlAndCountAllResultsAndGet() public function testLikeRawSqlAndGetAndCountAllResults() { $builder = $this->db->table('job'); - $builder->like(new RawSql('name'), 'Developer'); + + if ($this->db->DBDriver === 'OCI8') { + $key = new RawSql('"name"'); + } else { + $key = new RawSql('name'); + } + + $builder->like($key, 'Developer'); $results = $builder->get(null, 0, false)->getResult(); $count = $builder->countAllResults();