1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2021 Aleksandar Panic |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace ArekX\PQL\Drivers\MySql\Builder\Builders; |
19
|
|
|
|
20
|
|
|
use ArekX\PQL\Drivers\MySql\Builder\Builders\Traits\SubQueryTrait; |
21
|
|
|
use ArekX\PQL\Drivers\MySql\Builder\MySqlQueryBuilderState; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Represents a query builder for building an UNION query |
25
|
|
|
*/ |
26
|
|
|
class UnionBuilder extends QueryPartBuilder |
27
|
|
|
{ |
28
|
|
|
use SubQueryTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @inheritDoc |
32
|
|
|
*/ |
33
|
4 |
|
protected function getInitialParts(): array |
34
|
|
|
{ |
35
|
4 |
|
return []; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritDoc |
40
|
|
|
*/ |
41
|
4 |
|
protected function getRequiredParts(): array |
42
|
|
|
{ |
43
|
4 |
|
return ['from']; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritDoc |
48
|
|
|
*/ |
49
|
4 |
|
protected function getPartBuilders(): array |
50
|
|
|
{ |
51
|
|
|
return [ |
52
|
4 |
|
'from' => fn($part, $state) => $this->buildQuery($part, $state), |
53
|
4 |
|
'union' => fn($part, $state) => $this->buildUnions($part, $state) |
54
|
4 |
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Build other unions added. |
59
|
|
|
* |
60
|
|
|
* @param array $unions Unions to be built |
61
|
|
|
* @param MySqlQueryBuilderState $state Query builder state |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
2 |
|
protected function buildUnions($unions, $state) |
65
|
|
|
{ |
66
|
2 |
|
$result = []; |
67
|
2 |
|
foreach ($unions as [$query, $type]) { |
68
|
2 |
|
$result[] = ($type ? strtoupper($type) . ' ' : '') . $this->buildQuery($query, $state); |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
return 'UNION ' . implode(' UNION ', $result); |
72
|
|
|
} |
73
|
|
|
} |