-
Notifications
You must be signed in to change notification settings - Fork 2k
Feature. QueryBuilder. Query union. #6015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kenjis
merged 3 commits into
codeigniter4:develop
from
iRedds:feature/query-builder-union
May 28, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * This file is part of CodeIgniter 4 framework. | ||
| * | ||
| * (c) CodeIgniter Foundation <admin@codeigniter.com> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace CodeIgniter\Database\Builder; | ||
|
|
||
| use CodeIgniter\Database\BaseBuilder; | ||
| use CodeIgniter\Database\SQLSRV\Connection as SQLSRVConnection; | ||
| use CodeIgniter\Test\CIUnitTestCase; | ||
| use CodeIgniter\Test\Mock\MockConnection; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| final class UnionTest extends CIUnitTestCase | ||
| { | ||
| /** | ||
| * @var MockConnection | ||
| */ | ||
| protected $db; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $this->db = new MockConnection([]); | ||
| } | ||
|
|
||
| public function testUnion(): void | ||
| { | ||
| $expected = 'SELECT * FROM (SELECT * FROM "test") "uwrp0" UNION SELECT * FROM (SELECT * FROM "test") "uwrp1"'; | ||
| $builder = $this->db->table('test'); | ||
|
|
||
| $builder->union($this->db->table('test')); | ||
| $this->assertSame($expected, $this->buildSelect($builder)); | ||
|
|
||
| $builder = $this->db->table('test'); | ||
|
|
||
| $builder->union(static fn ($builder) => $builder->from('test')); | ||
| $this->assertSame($expected, $this->buildSelect($builder)); | ||
| } | ||
|
|
||
| public function testUnionAll(): void | ||
| { | ||
| $expected = 'SELECT * FROM (SELECT * FROM "test") "uwrp0"' | ||
| . ' UNION ALL SELECT * FROM (SELECT * FROM "test") "uwrp1"'; | ||
| $builder = $this->db->table('test'); | ||
|
|
||
| $builder->unionAll($this->db->table('test')); | ||
| $this->assertSame($expected, $this->buildSelect($builder)); | ||
| } | ||
|
|
||
| public function testOrderLimit(): void | ||
| { | ||
| $expected = 'SELECT * FROM (SELECT * FROM "test" ORDER BY "id" DESC LIMIT 10) "uwrp0"' | ||
| . ' UNION SELECT * FROM (SELECT * FROM "test") "uwrp1"'; | ||
| $builder = $this->db->table('test'); | ||
|
|
||
| $builder->union($this->db->table('test'))->limit(10)->orderBy('id', 'DESC'); | ||
| $this->assertSame($expected, $this->buildSelect($builder)); | ||
| } | ||
|
|
||
| public function testUnionSQLSRV(): void | ||
| { | ||
| $expected = 'SELECT * FROM (SELECT * FROM "test"."dbo"."users") "uwrp0"' | ||
| . ' UNION SELECT * FROM (SELECT * FROM "test"."dbo"."users") "uwrp1"'; | ||
|
|
||
| $db = new SQLSRVConnection(['DBDriver' => 'SQLSRV', 'database' => 'test', 'schema' => 'dbo']); | ||
|
|
||
| $builder = $db->table('users'); | ||
|
|
||
| $builder->union($db->table('users')); | ||
| $this->assertSame($expected, $this->buildSelect($builder)); | ||
|
|
||
| $builder = $db->table('users'); | ||
|
|
||
| $builder->union(static fn ($builder) => $builder->from('users')); | ||
| $this->assertSame($expected, $this->buildSelect($builder)); | ||
| } | ||
|
|
||
| protected function buildSelect(BaseBuilder $builder): string | ||
| { | ||
| return str_replace("\n", ' ', $builder->getCompiledSelect()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * This file is part of CodeIgniter 4 framework. | ||
| * | ||
| * (c) CodeIgniter Foundation <admin@codeigniter.com> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace CodeIgniter\Database\Live; | ||
|
|
||
| use CodeIgniter\Test\CIUnitTestCase; | ||
| use CodeIgniter\Test\DatabaseTestTrait; | ||
| use Tests\Support\Database\Seeds\CITestSeeder; | ||
|
|
||
| /** | ||
| * @group DatabaseLive | ||
| * | ||
| * @internal | ||
| */ | ||
| final class UnionTest extends CIUnitTestCase | ||
| { | ||
| use DatabaseTestTrait; | ||
|
|
||
| protected $refresh = true; | ||
| protected $seed = CITestSeeder::class; | ||
|
|
||
| public function testUnion(): void | ||
| { | ||
| $union = $this->db->table('user') | ||
| ->limit(1) | ||
| ->orderBy('id', 'ASC'); | ||
| $builder = $this->db->table('user'); | ||
|
|
||
| $builder->union($union) | ||
| ->limit(1) | ||
| ->orderBy('id', 'DESC'); | ||
|
|
||
| $result = $this->db->newQuery() | ||
| ->fromSubquery($builder, 'q') | ||
| ->orderBy('id', 'DESC') | ||
| ->get(); | ||
|
|
||
| $this->assertSame(2, $result->getNumRows()); | ||
|
|
||
| $rows = $result->getResult(); | ||
| $this->assertSame(4, (int) $rows[0]->id); | ||
| $this->assertSame(1, (int) $rows[1]->id); | ||
| } | ||
|
|
||
| public function testUnionAll(): void | ||
| { | ||
| $union = $this->db->table('user'); | ||
| $builder = $this->db->table('user'); | ||
|
|
||
| $result = $builder->unionAll($union)->get(); | ||
|
|
||
| $this->assertSame(8, $result->getNumRows()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?php | ||
|
|
||
| $union = $this->db->table('users')->select('id', 'name'); | ||
| $builder = $this->db->table('users')->select('id', 'name'); | ||
|
|
||
| $builder->union($union)->limit(10)->get(); | ||
| /* | ||
| * Produces: | ||
| * SELECT * FROM (SELECT `id`, `name` FROM `users` LIMIT 10) uwrp0 | ||
| * UNION SELECT * FROM (SELECT `id`, `name` FROM `users`) uwrp1 | ||
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?php | ||
|
|
||
| $union = $this->db->table('users')->select('id', 'name')->orderBy('id', 'DESC')->limit(5); | ||
| $builder = $this->db->table('users')->select('id', 'name')->orderBy('id', 'ASC')->limit(5)->union($union); | ||
|
|
||
| $this->db->newQuery()->fromSubquery($builder, 'q')->orderBy('id', 'DESC')->get(); | ||
| /* | ||
| * Produces: | ||
| * SELECT * FROM ( | ||
| * SELECT * FROM (SELECT `id`, `name` FROM `users` ORDER BY `id` ASC LIMIT 5) uwrp0 | ||
| * UNION | ||
| * SELECT * FROM (SELECT `id`, `name` FROM `users` ORDER BY `id` DESC LIMIT 5) uwrp1 | ||
| * ) q ORDER BY `id` DESC | ||
| */ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.