From 084fe164cbe518e3b6f30058ed37018fccdc0c46 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Mon, 1 Apr 2019 22:57:07 +0530 Subject: [PATCH 1/6] Subquery related changes --- 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 e881148a5fa7..8a26ac3f2c9f 100644 --- a/system/Database/BaseBuilder.php +++ b/system/Database/BaseBuilder.php @@ -2951,7 +2951,7 @@ protected function getOperator($str, bool $list = false) ]; } - return preg_match_all('/' . implode('|', $_operators) . '/i', $str, $match) ? ($list ? $match[0] : $match[0][count($match[0]) - 1]) : false; + return preg_match_all('/(?=(((?!\)).)*\()|[^\(\)]*$)(' . implode('|', $_operators) . ')/i', $str, $match) ? ($list ? $match[0] : $match[0][count($match[0]) - 1]) : false; } // -------------------------------------------------------------------- From 69d987a8c571dbe8042781cc9d83fceca0123551 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Mon, 1 Apr 2019 23:35:48 +0530 Subject: [PATCH 2/6] Subquery related changes --- tests/system/Database/Live/WhereTest.php | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/system/Database/Live/WhereTest.php b/tests/system/Database/Live/WhereTest.php index af015d686858..8853ed6330e2 100644 --- a/tests/system/Database/Live/WhereTest.php +++ b/tests/system/Database/Live/WhereTest.php @@ -121,4 +121,30 @@ public function testWhereNotIn() //-------------------------------------------------------------------- + public function testSubQuery() + { + $jobs = $this->db->table('job') + ->where('id not in (select id from job where name = "Developer")', null, false) + ->get() + ->getResult(); + + $this->assertCount(3, $jobs); + $this->assertEquals('Politician', $jobs[0]->name); + $this->assertEquals('Accountant', $jobs[1]->name); + $this->assertEquals('Musician', $jobs[2]->name); + } + + public function testSubQueryAnotherType() + { + $jobs = $this->db->table('job') + ->where('id = (select id from job where name = "Developer")', null, false) + ->get() + ->getResult(); + + $this->assertCount(1, $jobs); + $this->assertEquals('Developer', $jobs[0]->name); + } + + //-------------------------------------------------------------------- + } From c7c8ac0efe7672c37af9c731df6c3de3244ae31b Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Tue, 2 Apr 2019 00:22:28 +0530 Subject: [PATCH 3/6] Subquery related changes --- tests/system/Database/Live/WhereTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/system/Database/Live/WhereTest.php b/tests/system/Database/Live/WhereTest.php index 8853ed6330e2..334423f7ddb0 100644 --- a/tests/system/Database/Live/WhereTest.php +++ b/tests/system/Database/Live/WhereTest.php @@ -123,8 +123,10 @@ public function testWhereNotIn() public function testSubQuery() { + $subQuery = $this->db->table('job')->where('name', 'Developer')->getCompiledSelect(); + $jobs = $this->db->table('job') - ->where('id not in (select id from job where name = "Developer")', null, false) + ->where('id not in (' . $subQuery . ')', null, false) ->get() ->getResult(); From 28ba4b2e196a7875c0a29674a7e62f5f05c7f3fd Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Tue, 2 Apr 2019 00:24:08 +0530 Subject: [PATCH 4/6] Subquery related changes --- tests/system/Database/Live/WhereTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/system/Database/Live/WhereTest.php b/tests/system/Database/Live/WhereTest.php index 334423f7ddb0..fa8a2221774f 100644 --- a/tests/system/Database/Live/WhereTest.php +++ b/tests/system/Database/Live/WhereTest.php @@ -138,8 +138,10 @@ public function testSubQuery() public function testSubQueryAnotherType() { + $subQuery = $this->db->table('job')->where('name', 'Developer')->getCompiledSelect(); + $jobs = $this->db->table('job') - ->where('id = (select id from job where name = "Developer")', null, false) + ->where('id = (' . $subQuery . ')', null, false) ->get() ->getResult(); From f9b3cf4b74f2880030ed967da278ae33dbd7d7e7 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Tue, 2 Apr 2019 00:27:53 +0530 Subject: [PATCH 5/6] reverting getOperator method --- 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 8a26ac3f2c9f..e881148a5fa7 100644 --- a/system/Database/BaseBuilder.php +++ b/system/Database/BaseBuilder.php @@ -2951,7 +2951,7 @@ protected function getOperator($str, bool $list = false) ]; } - return preg_match_all('/(?=(((?!\)).)*\()|[^\(\)]*$)(' . implode('|', $_operators) . ')/i', $str, $match) ? ($list ? $match[0] : $match[0][count($match[0]) - 1]) : false; + return preg_match_all('/' . implode('|', $_operators) . '/i', $str, $match) ? ($list ? $match[0] : $match[0][count($match[0]) - 1]) : false; } // -------------------------------------------------------------------- From 3e6e06e272aa727a71286bd32fa209541b64153a Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Tue, 2 Apr 2019 00:36:17 +0530 Subject: [PATCH 6/6] Correcting subquery --- tests/system/Database/Live/WhereTest.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/tests/system/Database/Live/WhereTest.php b/tests/system/Database/Live/WhereTest.php index fa8a2221774f..82208d924e6d 100644 --- a/tests/system/Database/Live/WhereTest.php +++ b/tests/system/Database/Live/WhereTest.php @@ -123,10 +123,13 @@ public function testWhereNotIn() public function testSubQuery() { - $subQuery = $this->db->table('job')->where('name', 'Developer')->getCompiledSelect(); - + $subQuery = $this->db->table('job') + ->select('id') + ->where('name', 'Developer') + ->getCompiledSelect(); + $jobs = $this->db->table('job') - ->where('id not in (' . $subQuery . ')', null, false) + ->where('id not in (' . $subQuery . ')', null, false) ->get() ->getResult(); @@ -136,12 +139,17 @@ public function testSubQuery() $this->assertEquals('Musician', $jobs[2]->name); } + //-------------------------------------------------------------------- + public function testSubQueryAnotherType() { - $subQuery = $this->db->table('job')->where('name', 'Developer')->getCompiledSelect(); + $subQuery = $this->db->table('job') + ->select('id') + ->where('name', 'Developer') + ->getCompiledSelect(); $jobs = $this->db->table('job') - ->where('id = (' . $subQuery . ')', null, false) + ->where('id = (' . $subQuery . ')', null, false) ->get() ->getResult();