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