-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path03-new-top-operator.php
214 lines (174 loc) · 4.87 KB
/
03-new-top-operator.php
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
namespace Graviton\RqlParserExample03;
use Graviton\RqlParser\Parser as BaseParser;
use Graviton\RqlParser\Query as BaseQuery;
use Graviton\RqlParser\QueryBuilder as BaseQueryBuilder;
use Graviton\RqlParser\Lexer;
use Graviton\RqlParser\Token;
use Graviton\RqlParser\NodeParserInterface;
use Graviton\RqlParser\TokenStream;
use Graviton\RqlParser\NodeParserChain;
use Graviton\RqlParser\AbstractNode;
use Graviton\RqlParser\Node\SelectNode;
use Graviton\RqlParser\NodeParser\Query\ComparisonOperator\Rql\EqNodeParser;
use Graviton\RqlParser\ValueParser\FieldParser;
use Graviton\RqlParser\ValueParser\ScalarParser;
require __DIR__ . '/../vendor/autoload.php';
/**
* aggregate(fieldName)
*/
class AggregateFunctionNode extends AbstractNode
{
private $function;
private $field;
public function __construct($function, $field)
{
$this->function = $function;
$this->field = $field;
}
public function getField()
{
return $this->field;
}
public function getFunction()
{
return $this->function;
}
public function getNodeName()
{
return $this->function;
}
public function __toString()
{
return sprintf('%s(%s)', $this->function, $this->field);
}
}
/**
* select(field1,count(field2),sum(field3),...)
*/
class SelectTokenParser implements NodeParserInterface
{
private $allowedFunctions;
public function __construct(array $allowedFunctions)
{
$this->allowedFunctions = $allowedFunctions;
}
public function parse(TokenStream $tokenStream)
{
$fields = [];
$tokenStream->expect(Token::T_OPERATOR, 'select');
$tokenStream->expect(Token::T_OPEN_PARENTHESIS);
do {
if (($agregate = $tokenStream->nextIf(Token::T_OPERATOR, $this->allowedFunctions)) !== null) {
$tokenStream->expect(Token::T_OPEN_PARENTHESIS);
$fields[] = new AggregateFunctionNode(
$agregate->getValue(),
$tokenStream->expect(Token::T_STRING)->getValue()
);
$tokenStream->expect(Token::T_CLOSE_PARENTHESIS);
} else {
$fields[] = $tokenStream->expect(Token::T_STRING)->getValue();
}
if (!$tokenStream->nextIf(Token::T_COMMA)) {
break;
}
} while (true);
$tokenStream->expect(Token::T_CLOSE_PARENTHESIS);
return new SelectNode($fields);
}
public function supports(TokenStream $tokenStream)
{
return $tokenStream->test(Token::T_OPERATOR, 'select');
}
}
/**
* groupby(field1,field2,...)
*/
class GroupbyNode extends AbstractNode
{
private $fields;
public function __construct(array $fields)
{
$this->fields = $fields;
}
public function getFields()
{
return $this->fields;
}
public function getNodeName()
{
return 'groupby';
}
}
class GroupbyTokenParser implements NodeParserInterface
{
public function parse(TokenStream $tokenStream)
{
$fields = [];
$tokenStream->expect(Token::T_OPERATOR, 'groupby');
$tokenStream->expect(Token::T_OPEN_PARENTHESIS);
do {
$fields[] = $tokenStream->expect(Token::T_STRING)->getValue();
if (!$tokenStream->nextIf(Token::T_COMMA)) {
break;
}
} while (true);
$tokenStream->expect(Token::T_CLOSE_PARENTHESIS);
return new GroupbyNode($fields);
}
public function supports(TokenStream $tokenStream)
{
return $tokenStream->test(Token::T_OPERATOR, 'groupby');
}
}
/**
* add supports for "groupby"
*/
class Query extends BaseQuery
{
private $groupby;
public function getGroupby()
{
return $this->groupby;
}
public function setGroupby(GroupbyNode $node)
{
$this->groupby = $node;
return $this;
}
}
class QueryBuilder extends BaseQueryBuilder
{
public function __construct()
{
parent::__construct();
$this->query = new Query();
}
public function addNode(AbstractNode $node)
{
if ($node instanceof GroupbyNode) {
return $this->query->setGroupby($node);
}
return parent::addNode($node);
}
}
class Parser extends BaseParser
{
protected function createQueryBuilder()
{
return new QueryBuilder();
}
}
$nodeParser = (new NodeParserChain())
->addNodeParser(new SelectTokenParser(['count', 'sum', 'avg', 'min', 'max']))
->addNodeParser(new GroupbyTokenParser())
->addNodeParser(new EqNodeParser(new FieldParser(), new ScalarParser()));
// parse
$lexer = new Lexer();
$parser = new Parser($nodeParser);
$tokenStream = $lexer->tokenize(implode('&', [
'select(type,avg(age),min(age),max(age))',
'eq(type,customer)',
'groupby(type)',
]));
var_dump($parser->parse($tokenStream));