Skip to content

Commit 6f1e5be

Browse files
committed
- Added union and unionAll to Kir\MySQL\Builder\Select
1 parent d78e491 commit 6f1e5be

File tree

3 files changed

+69
-10
lines changed

3 files changed

+69
-10
lines changed

src/Builder/Select.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Kir\MySQL\Builder\Traits\JoinBuilder;
1010
use Kir\MySQL\Builder\Traits\LimitBuilder;
1111
use Kir\MySQL\Builder\Traits\TableNameBuilder;
12+
use Kir\MySQL\Builder\Traits\UnionBuilder;
1213
use Kir\MySQL\Builder\Traits\WhereBuilder;
1314

1415
class Select extends Statement {
@@ -21,18 +22,13 @@ class Select extends Statement {
2122
use OrderByBuilder;
2223
use LimitBuilder;
2324
use OffsetBuilder;
25+
use UnionBuilder;
2426

25-
/**
26-
* @var string[]
27-
*/
27+
/** @var string[] */
2828
private $fields = array();
29-
/**
30-
* @var bool
31-
*/
29+
/** @var bool */
3230
private $calcFoundRows = false;
33-
/**
34-
* @var bool
35-
*/
31+
/** @var bool */
3632
private $forUpdate = false;
3733

3834
/**
@@ -137,6 +133,7 @@ public function __toString() {
137133
$query = $this->buildOrder($query);
138134
$query = $this->buildLimit($query);
139135
$query = $this->buildOffset($query);
136+
$query = $this->buildUnions($query);
140137
$query = $this->buildForUpdate($query);
141138
return $query;
142139
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
namespace Kir\MySQL\Builder\Traits;
3+
4+
trait UnionBuilder {
5+
use AbstractDB;
6+
7+
/** @var array */
8+
private $unions = [];
9+
10+
/**
11+
* @param string $query
12+
* @return $this
13+
*/
14+
public function union($query) {
15+
$this->unions[] = array('', $query);
16+
return $this;
17+
}
18+
19+
/**
20+
* @param string $query
21+
* @return $this
22+
*/
23+
public function unionAll($query) {
24+
$this->unions[] = array('ALL', $query);
25+
return $this;
26+
}
27+
28+
/**
29+
* @param string $query
30+
* @return string
31+
*/
32+
protected function buildUnions($query) {
33+
$qaueries = [$query];
34+
foreach($this->unions as $unionQuery) {
35+
if($unionQuery[0] === 'ALL') {
36+
$qaueries[] = 'UNION ALL';
37+
} else {
38+
$qaueries[] = 'UNION';
39+
}
40+
$qaueries[] = $unionQuery[1];
41+
}
42+
return join("\n", $qaueries);
43+
}
44+
}

tests/Builder/SelectTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,22 @@ public function testSubselectAsField() {
174174

175175
$this->assertEquals("SELECT\n\t(\n\t\tSELECT\n\t\t\tCOUNT(*)\n\t\tFROM\n\t\t\ttest1 t1\n\t\tINNER JOIN\n\t\t\ttest2 t2 ON t1.id=t2.id\n\t\tWHERE\n\t\t\t(t1.id > 10)\n\t) AS `testfield`\nFROM\n\ttest1 t1\nINNER JOIN\n\ttest2 t2 ON t1.id=t2.id\nWHERE\n\t(t1.id > 10)\n", $query);
176176
}
177-
}
177+
178+
public function testSubselectUnion() {
179+
$query = TestSelect::create()
180+
->field('t1.field')
181+
->from('t1', 'test1')
182+
->joinInner('t2', 'test2', 't1.id=t2.id')
183+
->where('t1.id > 10');
184+
185+
$query = TestSelect::create()
186+
->union($query)
187+
->field('t1.field')
188+
->from('t1', 'test1')
189+
->joinInner('t2', 'test2', 't1.id=t2.id')
190+
->where('t1.id > 10')
191+
->asString();
192+
193+
$this->assertEquals("SELECT\n\tt1.field\nFROM\n\ttest1 t1\nINNER JOIN\n\ttest2 t2 ON t1.id=t2.id\nWHERE\n\t(t1.id > 10)\n\nUNION\nSELECT\n\tt1.field\nFROM\n\ttest1 t1\nINNER JOIN\n\ttest2 t2 ON t1.id=t2.id\nWHERE\n\t(t1.id > 10)\n", $query);
194+
}
195+
}

0 commit comments

Comments
 (0)