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\Pdo\MySql\Builders; |
19
|
|
|
|
20
|
|
|
use ArekX\PQL\Contracts\StructuredQuery; |
21
|
|
|
use ArekX\PQL\Drivers\Pdo\MySql\Builders\Traits\ConditionTrait; |
22
|
|
|
use ArekX\PQL\Drivers\Pdo\MySql\Builders\Traits\ValueColumnTrait; |
23
|
|
|
use ArekX\PQL\Drivers\Pdo\MySql\MySqlQueryBuilderState; |
24
|
|
|
use ArekX\PQL\Sql\Statement\CaseWhen; |
25
|
|
|
use Exception; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Represents a query builder for building a CASE statement |
29
|
|
|
* |
30
|
|
|
* @see CaseWhen |
31
|
|
|
*/ |
32
|
|
|
class CaseWhenBuilder extends QueryPartBuilder |
33
|
|
|
{ |
34
|
|
|
use ConditionTrait; |
35
|
|
|
use ValueColumnTrait; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritDoc |
39
|
|
|
*/ |
40
|
2 |
|
protected function getInitialParts(): array |
41
|
|
|
{ |
42
|
2 |
|
return ['CASE']; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritDoc |
47
|
|
|
*/ |
48
|
2 |
|
protected function getLastParts(): array |
49
|
|
|
{ |
50
|
2 |
|
return ['END']; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritDoc |
55
|
|
|
*/ |
56
|
2 |
|
protected function getRequiredParts(): array |
57
|
|
|
{ |
58
|
2 |
|
return ['when']; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritDoc |
63
|
|
|
*/ |
64
|
2 |
|
protected function getPartBuilders(): array |
65
|
|
|
{ |
66
|
2 |
|
return [ |
67
|
2 |
|
'when' => fn($name, MySqlQueryBuilderState $state) => $this->buildWhen($name, $state), |
68
|
2 |
|
'else' => fn($name, MySqlQueryBuilderState $state) => $this->buildElse($name, $state), |
69
|
2 |
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Build series of WHEN parts of the CASE. |
74
|
|
|
* |
75
|
|
|
* @param array $whenList List of [when, then] values to be built. |
76
|
|
|
* @param MySqlQueryBuilderState $state |
77
|
|
|
* @return string |
78
|
|
|
* @throws Exception |
79
|
|
|
*/ |
80
|
2 |
|
protected function buildWhen(array $whenList, MySqlQueryBuilderState $state): string |
81
|
|
|
{ |
82
|
2 |
|
$result = []; |
83
|
|
|
|
84
|
2 |
|
foreach ($whenList as $whenItem) { |
85
|
2 |
|
[$when, $then] = $whenItem; |
86
|
|
|
|
87
|
2 |
|
$result[] = 'WHEN ' . $this->buildCondition($when, $state) |
88
|
2 |
|
. ' THEN ' . $this->buildValueColumn($then, $state); |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
return implode(' ', $result); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Build ELSE part of the case |
96
|
|
|
* |
97
|
|
|
* @param array|StructuredQuery $else Value to be built |
98
|
|
|
* @param MySqlQueryBuilderState $state Current query builder state |
99
|
|
|
* @return string |
100
|
|
|
* @throws Exception |
101
|
|
|
*/ |
102
|
1 |
|
protected function buildElse(StructuredQuery|array $else, MySqlQueryBuilderState $state): string |
103
|
|
|
{ |
104
|
1 |
|
return 'ELSE ' . $this->buildValueColumn($else, $state); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|