1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\SqlParser\Components; |
6
|
|
|
|
7
|
|
|
use PhpMyAdmin\SqlParser\Component; |
8
|
|
|
use PhpMyAdmin\SqlParser\Context; |
9
|
|
|
use PhpMyAdmin\SqlParser\Parsers\Conditions; |
10
|
|
|
|
11
|
|
|
use function count; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Parses a reference to a CASE expression. |
15
|
|
|
*/ |
16
|
|
|
final class CaseExpression implements Component |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The value to be compared. |
20
|
|
|
* |
21
|
|
|
* @var Expression|null |
22
|
|
|
*/ |
23
|
|
|
public $value; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The conditions in WHEN clauses. |
27
|
|
|
* |
28
|
|
|
* @var Condition[][] |
29
|
|
|
*/ |
30
|
|
|
public $conditions = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The results matching with the WHEN clauses. |
34
|
|
|
* |
35
|
|
|
* @var Expression[] |
36
|
|
|
*/ |
37
|
|
|
public $results = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The values to be compared against. |
41
|
|
|
* |
42
|
|
|
* @var Expression[] |
43
|
|
|
*/ |
44
|
|
|
public $compareValues = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The result in ELSE section of expr. |
48
|
|
|
* |
49
|
|
|
* @var Expression|null |
50
|
|
|
*/ |
51
|
|
|
public $elseResult; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The alias of this CASE statement. |
55
|
|
|
* |
56
|
|
|
* @var string|null |
57
|
|
|
*/ |
58
|
|
|
public $alias; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The sub-expression. |
62
|
|
|
* |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
public $expr = ''; |
66
|
|
|
|
67
|
56 |
|
public function build(): string |
68
|
|
|
{ |
69
|
56 |
|
$ret = 'CASE '; |
70
|
56 |
|
if (isset($this->value)) { |
71
|
|
|
// Syntax type 0 |
72
|
26 |
|
$ret .= $this->value . ' '; |
73
|
26 |
|
$valuesCount = count($this->compareValues); |
74
|
26 |
|
$resultsCount = count($this->results); |
75
|
26 |
|
for ($i = 0; $i < $valuesCount && $i < $resultsCount; ++$i) { |
76
|
26 |
|
$ret .= 'WHEN ' . $this->compareValues[$i] . ' '; |
77
|
26 |
|
$ret .= 'THEN ' . $this->results[$i] . ' '; |
78
|
|
|
} |
79
|
|
|
} else { |
80
|
|
|
// Syntax type 1 |
81
|
30 |
|
$valuesCount = count($this->conditions); |
82
|
30 |
|
$resultsCount = count($this->results); |
83
|
30 |
|
for ($i = 0; $i < $valuesCount && $i < $resultsCount; ++$i) { |
84
|
28 |
|
$ret .= 'WHEN ' . Conditions::buildAll($this->conditions[$i]) . ' '; |
85
|
28 |
|
$ret .= 'THEN ' . $this->results[$i] . ' '; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
56 |
|
if (isset($this->elseResult)) { |
90
|
32 |
|
$ret .= 'ELSE ' . $this->elseResult . ' '; |
91
|
|
|
} |
92
|
|
|
|
93
|
56 |
|
$ret .= 'END'; |
94
|
|
|
|
95
|
56 |
|
if ($this->alias) { |
96
|
20 |
|
$ret .= ' AS ' . Context::escape($this->alias); |
97
|
|
|
} |
98
|
|
|
|
99
|
56 |
|
return $ret; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function __toString(): string |
103
|
|
|
{ |
104
|
|
|
return $this->build(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|