-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Andrey Pyzhikov <[email protected]>
- Loading branch information
Showing
7 changed files
with
273 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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 <[email protected]> | ||
* | ||
* 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 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 <[email protected]> | ||
* | ||
* 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 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 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 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 | ||
*/ |