Skip to content

Commit dfa7418

Browse files
committed
add unionAll() method and tests
1 parent 18291fa commit dfa7418

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/QueryBuilder.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,16 @@ public function union(bool $unionAll = false): QueryBuilder
972972
return $this;
973973
}
974974

975+
/**
976+
* @return $this
977+
*/
978+
public function unionAll(): QueryBuilder
979+
{
980+
$this->concat = true;
981+
$this->sql .= ' UNION ALL ';
982+
return $this;
983+
}
984+
975985
/**
976986
* @param string|array $table
977987
* @param bool $unionAll

tests/SqlSelectExtTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,20 @@ public function testSelectUnionAllWhere()
183183
$this->assertSame([3000, 3000], $result->getParams());
184184
}
185185

186+
public function testSelectUnionAllMethodWhere()
187+
{
188+
$result = $this->qb->select('clients', ['name', 'age', 'total_sum' => 'account_sum + account_sum * 0.1'])
189+
->where([['account_sum', '<', 3000]])
190+
->unionAll()
191+
->select('clients', ['name', 'age', 'total_sum' => 'account_sum + account_sum * 0.3'])
192+
->where([['account_sum', '>=', 3000]]);
193+
194+
$this->assertSame($this->qb, $result);
195+
$this->assertSame(false, $result->hasError());
196+
$this->assertSame("SELECT `name`, `age`, account_sum + account_sum * 0.1 AS `total_sum` FROM `clients` WHERE (`account_sum` < 3000) UNION ALL SELECT `name`, `age`, account_sum + account_sum * 0.3 AS `total_sum` FROM `clients` WHERE (`account_sum` >= 3000)", $result->getSql());
197+
$this->assertSame([3000, 3000], $result->getParams());
198+
}
199+
186200
public function testSelectUnionWhereOrderBy()
187201
{
188202
$result = $this->qb->select('departments', ['department_id', 'department_name'])
@@ -211,6 +225,20 @@ public function testSelectUnionAllWhereOrderBy()
211225
$this->assertSame([10, 'Rassohin'], $result->getParams());
212226
}
213227

228+
public function testSelectUnionAllMethodWhereOrderBy()
229+
{
230+
$result = $this->qb->select('departments', ['department_id', 'department_name'])
231+
->where([['department_id', '>=', 10]])
232+
->unionAll()
233+
->select('employees', ['employee_id', 'last_name'])
234+
->where([['last_name', 'Rassohin']])->orderBy('2');
235+
236+
$this->assertSame($this->qb, $result);
237+
$this->assertSame(false, $result->hasError());
238+
$this->assertSame("SELECT `department_id`, `department_name` FROM `departments` WHERE (`department_id` >= 10) UNION ALL SELECT `employee_id`, `last_name` FROM `employees` WHERE (`last_name` = 'Rassohin') ORDER BY `2` ASC", $result->getSql());
239+
$this->assertSame([10, 'Rassohin'], $result->getParams());
240+
}
241+
214242
public function testUnionSelectEmptyTable()
215243
{
216244
$result = $this->qb->select('clients', ['name', 'age'])->unionSelect('');

0 commit comments

Comments
 (0)