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