-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQQueryExpression.cpp
More file actions
266 lines (209 loc) · 6.92 KB
/
QQueryExpression.cpp
File metadata and controls
266 lines (209 loc) · 6.92 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <assert.h>
#include <iostream>
#include "FileLexer.h"
#include "Type.h"
#include "Expression.h"
#include "CompilerHelpers.h"
#include "logging.h"
SET_LOG_CAT( LOG_CAT_ALL );
SET_LOG_LEVEL( LOG_LVL_NOISE );
using namespace QLang;
using namespace std;
// Parse a query pipeline step body: { expression }
// Used for where, order_by, join conditions
static Expression *parseStepBody( Lexer &l, Scope *scope )
{
int sym = l.getSymbol();
if ( sym != '{' )
COMPILE_ERROR( l, "Expected '{' in query pipeline step" );
Expression *expr = Expression::ParseExpr( l, scope, 0 );
if ( expr == nullptr )
COMPILE_ERROR( l, "Expected expression in query pipeline step body" );
sym = l.getSymbol();
if ( sym != '}' )
COMPILE_ERROR( l, "Expected '}' in query pipeline step" );
return expr;
}
// Parse set step assignments: { .field = value, .field = value, ... }
static QueryPipelineStep parseSetStep( Lexer &l, Scope *scope )
{
QueryPipelineStep step;
step.mType = QueryPipelineStep::SET;
int sym = l.getSymbol();
if ( sym != '{' )
COMPILE_ERROR( l, "Expected '{' after 'set'" );
do {
// Expect .field
sym = l.getSymbol();
if ( sym != '.' )
COMPILE_ERROR( l, "Expected '.field' in set clause" );
sym = l.getSymbol();
if ( sym != Lexer::SYMBOL )
COMPILE_ERROR( l, "Expected field name after '.' in set clause" );
string fieldName = l.getSymbolText();
// Expect =
sym = l.getSymbol();
if ( sym != '=' )
COMPILE_ERROR( l, "Expected '=' after field name in set clause" );
// Parse value expression
Expression *value = Expression::ParseExpr( l, scope, 0 );
if ( value == nullptr )
COMPILE_ERROR( l, "Expected expression after '=' in set clause" );
step.mSetFields.push_back( make_pair( fieldName, SmartPtr<Expression>( value ) ) );
// Check for ',' or '}'
if ( l.peekSymbol() == ',' )
l.getSymbol(); // consume ','
} while ( l.peekSymbol() != '}' );
sym = l.getSymbol();
assert( sym == '}' );
return step;
}
// Parse pipeline steps after a query/update/delete keyword and table name.
// Steps are: |> where { ... }, |> order_by { ... }, |> limit(n), |> first,
// |> join Table on { ... }, |> set { ... }
static void parsePipelineSteps( Lexer &l, Scope *scope, vector<QueryPipelineStep> &steps )
{
while ( l.peekSymbol() == Lexer::PIPE_ARROW )
{
l.getSymbol(); // consume |>
int sym = l.getSymbol();
if ( sym != Lexer::SYMBOL )
COMPILE_ERROR( l, "Expected pipeline step name after '|>'" );
string stepName = l.getSymbolText();
QueryPipelineStep step;
if ( stepName == "where" )
{
step.mType = QueryPipelineStep::WHERE;
step.mExpression = parseStepBody( l, scope );
}
else if ( stepName == "order_by" )
{
step.mType = QueryPipelineStep::ORDER_BY;
step.mExpression = parseStepBody( l, scope );
}
else if ( stepName == "limit" )
{
step.mType = QueryPipelineStep::LIMIT;
sym = l.getSymbol();
if ( sym != '(' )
COMPILE_ERROR( l, "Expected '(' after 'limit'" );
step.mExpression = Expression::ParseExpr( l, scope, 0 );
if ( step.mExpression == nullptr )
COMPILE_ERROR( l, "Expected expression in limit()" );
sym = l.getSymbol();
if ( sym != ')' )
COMPILE_ERROR( l, "Expected ')' after limit expression" );
}
else if ( stepName == "first" )
{
step.mType = QueryPipelineStep::FIRST;
// No arguments
}
else if ( stepName == "join" )
{
step.mType = QueryPipelineStep::JOIN;
sym = l.getSymbol();
if ( sym != Lexer::SYMBOL )
COMPILE_ERROR( l, "Expected table name after 'join'" );
step.mJoinTable = l.getSymbolText();
// Expect 'on' keyword
sym = l.getSymbol();
if ( sym != Lexer::KEYWORD_ON )
COMPILE_ERROR( l, "Expected 'on' after join table name" );
step.mExpression = parseStepBody( l, scope );
}
else if ( stepName == "set" )
{
step = parseSetStep( l, scope );
}
else
{
COMPILE_ERROR( l, "Unknown query pipeline step '" + stepName + "'" );
}
steps.push_back( step );
}
}
QueryExpression *QueryExpression::Parse( Lexer &l, Scope *scope )
{
TRACE_BEGIN( LOG_LVL_INFO );
int sym = l.getSymbol(); // consume 'query'
assert( sym == Lexer::KEYWORD_QUERY );
// Parse table name
sym = l.getSymbol();
if ( sym != Lexer::SYMBOL )
COMPILE_ERROR( l, "Expected table name after 'query'" );
QueryExpression *expr = new QueryExpression( l.getSymbolText() );
// Parse pipeline steps
parsePipelineSteps( l, scope, expr->mSteps );
cout << "Completed query expression on " << expr->mTableName << endl;
return expr;
}
InsertExpression *InsertExpression::Parse( Lexer &l, Scope *scope )
{
TRACE_BEGIN( LOG_LVL_INFO );
int sym = l.getSymbol(); // consume 'insert'
assert( sym == Lexer::KEYWORD_INSERT );
// Parse table name
sym = l.getSymbol();
if ( sym != Lexer::SYMBOL )
COMPILE_ERROR( l, "Expected table name after 'insert'" );
InsertExpression *expr = new InsertExpression( l.getSymbolText() );
// Expect '{' for field assignments
sym = l.getSymbol();
if ( sym != '{' )
COMPILE_ERROR( l, "Expected '{' after table name in insert expression" );
// Parse field: value pairs
if ( l.peekSymbol() != '}' )
{
do {
sym = l.getSymbol();
if ( sym != Lexer::SYMBOL )
COMPILE_ERROR( l, "Expected field name in insert expression" );
string fieldName = l.getSymbolText();
sym = l.getSymbol();
if ( sym != ':' )
COMPILE_ERROR( l, "Expected ':' after field name in insert expression" );
Expression *value = Expression::ParseExpr( l, scope, 0 );
if ( value == nullptr )
COMPILE_ERROR( l, "Expected expression for field value in insert" );
expr->addField( fieldName, value );
if ( l.peekSymbol() == ',' )
l.getSymbol(); // consume ','
} while ( l.peekSymbol() != '}' );
}
sym = l.getSymbol();
if ( sym != '}' )
COMPILE_ERROR( l, "Expected '}' in insert expression" );
cout << "Completed insert expression on " << expr->mTableName << endl;
return expr;
}
UpdateExpression *UpdateExpression::Parse( Lexer &l, Scope *scope )
{
TRACE_BEGIN( LOG_LVL_INFO );
int sym = l.getSymbol(); // consume 'update'
assert( sym == Lexer::KEYWORD_UPDATE );
// Parse table name
sym = l.getSymbol();
if ( sym != Lexer::SYMBOL )
COMPILE_ERROR( l, "Expected table name after 'update'" );
UpdateExpression *expr = new UpdateExpression( l.getSymbolText() );
// Parse pipeline steps (where, set)
parsePipelineSteps( l, scope, expr->mSteps );
cout << "Completed update expression on " << expr->mTableName << endl;
return expr;
}
DeleteExpression *DeleteExpression::Parse( Lexer &l, Scope *scope )
{
TRACE_BEGIN( LOG_LVL_INFO );
int sym = l.getSymbol(); // consume 'delete'
assert( sym == Lexer::KEYWORD_DELETE );
// Parse table name
sym = l.getSymbol();
if ( sym != Lexer::SYMBOL )
COMPILE_ERROR( l, "Expected table name after 'delete'" );
DeleteExpression *expr = new DeleteExpression( l.getSymbolText() );
// Parse pipeline steps (where)
parsePipelineSteps( l, scope, expr->mSteps );
cout << "Completed delete expression on " << expr->mTableName << endl;
return expr;
}