|
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
|
|
|
public Expression|null $value = null; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The conditions in WHEN clauses. |
|
25
|
|
|
* |
|
26
|
|
|
* @var Condition[][] |
|
27
|
|
|
*/ |
|
28
|
|
|
public array $conditions = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The results matching with the WHEN clauses. |
|
32
|
|
|
* |
|
33
|
|
|
* @var Expression[] |
|
34
|
|
|
*/ |
|
35
|
|
|
public array $results = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The values to be compared against. |
|
39
|
|
|
* |
|
40
|
|
|
* @var Expression[] |
|
41
|
|
|
*/ |
|
42
|
|
|
public array $compareValues = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The result in ELSE section of expr. |
|
46
|
|
|
*/ |
|
47
|
|
|
public Expression|null $elseResult = null; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* The alias of this CASE statement. |
|
51
|
|
|
*/ |
|
52
|
|
|
public string|null $alias = null; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* The sub-expression. |
|
56
|
|
|
*/ |
|
57
|
|
|
public string $expr = ''; |
|
58
|
|
|
|
|
59
|
56 |
|
public function build(): string |
|
60
|
|
|
{ |
|
61
|
56 |
|
$ret = 'CASE '; |
|
62
|
56 |
|
if (isset($this->value)) { |
|
63
|
|
|
// Syntax type 0 |
|
64
|
26 |
|
$ret .= $this->value . ' '; |
|
65
|
26 |
|
$valuesCount = count($this->compareValues); |
|
66
|
26 |
|
$resultsCount = count($this->results); |
|
67
|
26 |
|
for ($i = 0; $i < $valuesCount && $i < $resultsCount; ++$i) { |
|
68
|
26 |
|
$ret .= 'WHEN ' . $this->compareValues[$i] . ' '; |
|
69
|
26 |
|
$ret .= 'THEN ' . $this->results[$i] . ' '; |
|
70
|
|
|
} |
|
71
|
|
|
} else { |
|
72
|
|
|
// Syntax type 1 |
|
73
|
30 |
|
$valuesCount = count($this->conditions); |
|
74
|
30 |
|
$resultsCount = count($this->results); |
|
75
|
30 |
|
for ($i = 0; $i < $valuesCount && $i < $resultsCount; ++$i) { |
|
76
|
28 |
|
$ret .= 'WHEN ' . Conditions::buildAll($this->conditions[$i]) . ' '; |
|
77
|
28 |
|
$ret .= 'THEN ' . $this->results[$i] . ' '; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
56 |
|
if (isset($this->elseResult)) { |
|
82
|
32 |
|
$ret .= 'ELSE ' . $this->elseResult . ' '; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
56 |
|
$ret .= 'END'; |
|
86
|
|
|
|
|
87
|
56 |
|
if ($this->alias) { |
|
88
|
20 |
|
$ret .= ' AS ' . Context::escape($this->alias); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
56 |
|
return $ret; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function __toString(): string |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->build(); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|