Skip to content

Commit b558db7

Browse files
committed
add unionSelectAll() method and tests
1 parent c1724a6 commit b558db7

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/QueryBuilder.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,22 @@ public function unionSelect($table, bool $unionAll = false): QueryBuilder
10281028
return $this;
10291029
}
10301030

1031+
/**
1032+
* @param string|array $table
1033+
* @return QueryBuilder
1034+
*/
1035+
public function unionSelectAll($table): QueryBuilder
1036+
{
1037+
if (empty($table)) {
1038+
$this->setError('Empty $table in ' . __METHOD__);
1039+
return $this;
1040+
}
1041+
1042+
$this->unionSelect($table, true);
1043+
1044+
return $this;
1045+
}
1046+
10311047
/**
10321048
* @return QueryBuilder
10331049
*/

tests/SqlSelectExtTest.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,22 +248,22 @@ public function testUnionSelectEmptyTable()
248248
$this->assertSame($result->getErrorMessage(), 'Empty $table in QueryBuilder::unionSelect');
249249
}
250250

251-
public function testUnionSelectIncorrectTable()
251+
public function testUnionSelectAllMethodEmptyTable()
252252
{
253-
$result = $this->qb->select('clients', ['name', 'age'])->unionSelect(2);
253+
$result = $this->qb->select('clients', ['name', 'age'])->unionSelectAll('');
254254

255255
$this->assertSame($this->qb, $result);
256256
$this->assertSame(true, $result->hasError());
257-
$this->assertSame($result->getErrorMessage(), 'Incorrect type of $table in QueryBuilder::unionSelect. $table must be a string or an array');
257+
$this->assertSame($result->getErrorMessage(), 'Empty $table in QueryBuilder::unionSelectAll');
258258
}
259259

260-
public function testUnionSelectDoubleUnion()
260+
public function testUnionSelectIncorrectTable()
261261
{
262-
$result = $this->qb->select('clients', ['name', 'age'])->union()->unionSelect('clients');
262+
$result = $this->qb->select('clients', ['name', 'age'])->unionSelect(2);
263263

264264
$this->assertSame($this->qb, $result);
265265
$this->assertSame(true, $result->hasError());
266-
$this->assertSame($result->getErrorMessage(), 'SQL has already UNION in QueryBuilder::unionSelect');
266+
$this->assertSame($result->getErrorMessage(), 'Incorrect type of $table in QueryBuilder::unionSelect. $table must be a string or an array');
267267
}
268268

269269
public function testUnionSelectWhere()
@@ -288,6 +288,17 @@ public function testUnionAllSelectWhere()
288288
$this->assertSame([10], $result->getParams());
289289
}
290290

291+
public function testUnionSelectAllMethodWhere()
292+
{
293+
$result = $this->qb->select('cabs', ['id', 'name'])
294+
->unionSelectAll('printer_models')->where([['id', '<', 10]]);
295+
296+
$this->assertSame($this->qb, $result);
297+
$this->assertSame(false, $result->hasError());
298+
$this->assertSame("SELECT `id`, `name` FROM `cabs` UNION ALL SELECT `id`, `name` FROM `printer_models` WHERE (`id` < 10)", $result->getSql());
299+
$this->assertSame([10], $result->getParams());
300+
}
301+
291302
public function testSelectExceptsWhere()
292303
{
293304
$result = $this->qb->select('contacts', ['contact_id', 'last_name', 'first_name'])

0 commit comments

Comments
 (0)