|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\SqlParser\Components; |
|
6
|
|
|
|
|
7
|
|
|
use PhpMyAdmin\SqlParser\Component; |
|
8
|
|
|
use PhpMyAdmin\SqlParser\Parser; |
|
9
|
|
|
use PhpMyAdmin\SqlParser\TokensList; |
|
10
|
|
|
use PhpMyAdmin\SqlParser\Translator; |
|
11
|
|
|
use RuntimeException; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* `WITH` keyword builder. |
|
15
|
|
|
*/ |
|
16
|
|
|
final class WithKeyword implements Component |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var string */ |
|
19
|
|
|
public $name; |
|
20
|
|
|
|
|
21
|
|
|
/** @var ArrayObj[] */ |
|
22
|
|
|
public $columns = []; |
|
23
|
|
|
|
|
24
|
|
|
/** @var Parser|null */ |
|
25
|
|
|
public $statement; |
|
26
|
|
|
|
|
27
|
48 |
|
public function __construct(string $name) |
|
28
|
|
|
{ |
|
29
|
48 |
|
$this->name = $name; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Parses the tokens contained in the given list in the context of the given parser. |
|
34
|
|
|
* |
|
35
|
|
|
* @param Parser $parser the parser that serves as context |
|
36
|
|
|
* @param TokensList $list the list of tokens that are being parsed |
|
37
|
|
|
* @param array<string, mixed> $options parameters for parsing |
|
38
|
|
|
* |
|
39
|
|
|
* @return mixed |
|
40
|
|
|
* |
|
41
|
|
|
* @throws RuntimeException not implemented yet. |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function parse(Parser $parser, TokensList $list, array $options = []) |
|
44
|
|
|
{ |
|
45
|
|
|
throw new RuntimeException(Translator::gettext('Not implemented yet.')); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param WithKeyword $component |
|
50
|
|
|
* @param array<string, mixed> $options |
|
51
|
|
|
*/ |
|
52
|
14 |
|
public static function build($component, array $options = []): string |
|
53
|
|
|
{ |
|
54
|
14 |
|
if (! $component instanceof WithKeyword) { |
|
|
|
|
|
|
55
|
2 |
|
throw new RuntimeException('Can not build a component that is not a WithKeyword'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
12 |
|
if (! isset($component->statement)) { |
|
59
|
2 |
|
throw new RuntimeException('No statement inside WITH'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
10 |
|
$str = $component->name; |
|
63
|
|
|
|
|
64
|
10 |
|
if ($component->columns) { |
|
|
|
|
|
|
65
|
6 |
|
$str .= ArrayObj::build($component->columns); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
10 |
|
$str .= ' AS ('; |
|
69
|
|
|
|
|
70
|
10 |
|
foreach ($component->statement->statements as $statement) { |
|
71
|
10 |
|
$str .= $statement->build(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
10 |
|
$str .= ')'; |
|
75
|
|
|
|
|
76
|
10 |
|
return $str; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function __toString(): string |
|
80
|
|
|
{ |
|
81
|
|
|
return static::build($this); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|