-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_interp.cpp
More file actions
464 lines (413 loc) · 10.9 KB
/
Copy pathparser_interp.cpp
File metadata and controls
464 lines (413 loc) · 10.9 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "redbase.h"
#include "parser_node.h"
#include "parser_interp.h"
#include "sm_manager.h"
#include "ql_manager.h"
#include "pf_manager.h"
extern SMManager smManager;
extern QLManager qlManager;
extern PFManager pfManager;
extern bool stop;
#define E_OK 0
#define E_INCOMPATIBLE -1
#define E_TOOMANY -2
#define E_NOLENGTH -3
#define E_INVINTSIZE -4
#define E_INVREALSIZE -5
#define E_INVFORMATSTRING -6
#define E_INVSTRLEN -7
#define E_DUPLICATEATTR -8
#define E_TOOLONG -9
#define E_STRINGTOOLONG -10
#define ERRFP stderr
/*
* print_error: prints an error message corresponding to errval
*/
static void print_error(const char *errmsg, RC errval)
{
if (errmsg != NULL)
fprintf(stderr, "%s: ", errmsg);
switch (errval) {
case E_OK:
fprintf(ERRFP, "no error\n");
break;
case E_INCOMPATIBLE:
fprintf(ERRFP, "attributes must be from selected relation(s)\n");
break;
case E_TOOMANY:
fprintf(ERRFP, "too many elements\n");
break;
case E_NOLENGTH:
fprintf(ERRFP, "length must be specified for STRING attribute\n");
break;
case E_INVINTSIZE:
fprintf(ERRFP, "invalid size for INTEGER attribute (should be %d)\n",
(int)sizeof(int));
break;
case E_INVREALSIZE:
fprintf(ERRFP, "invalid size for REAL attribute (should be %d)\n",
(int)sizeof(float));
break;
case E_INVFORMATSTRING:
fprintf(ERRFP, "invalid format string\n");
break;
case E_INVSTRLEN:
fprintf(ERRFP, "invalid length for string attribute\n");
break;
case E_DUPLICATEATTR:
fprintf(ERRFP, "duplicated attribute name\n");
break;
case E_TOOLONG:
fprintf(stderr, "relation name or attribute name too long\n");
break;
case E_STRINGTOOLONG:
fprintf(stderr, "string attribute too long\n");
break;
default:
fprintf(ERRFP, "unrecognized errval: %d\n", errval);
}
}
/*
* parse_format_string: deciphers a format string of the form: xl
* where x is a type specification (one of `i' INTEGER, `r' REAL,
* `s' STRING, or `c' STRING (character)) and l is a length (l is
* optional for `i' and `r'), and stores the type in *type and the
* length in *len.
*
* Returns
* E_OK on success
* error code otherwise
*/
static int parse_format_string(char *format_string, AttrType *type, int *len)
{
int n;
char c;
/* extract the components of the format string */
n = sscanf(format_string, "%c%d", &c, len);
/* if no length given... */
if (n == 1) {
switch (c) {
case 'i': /* int */
*type = INT;
*len = sizeof(int);
break;
case 'f':
case 'r': /* float or real */
*type = FLOAT;
*len = sizeof(float);
break;
case 's': /* string */
case 'c': /* char */
return E_NOLENGTH;
default:
return E_INVFORMATSTRING;
}
}
/* if both are given, make sure the length is valid */
else if (n == 2) {
switch (c) {
case 'i':
*type = INT;
if (*len != sizeof(int))
return E_INVINTSIZE;
break;
case 'f':
case 'r':
*type = FLOAT;
if (*len != sizeof(float))
return E_INVREALSIZE;
break;
case 's':
case 'c':
*type = STRING;
if (*len < 1 || *len > MAXSTRINGLEN)
return E_INVSTRLEN;
break;
default:
return E_INVFORMATSTRING;
}
}
/* otherwise it's not a valid format string */
else
return E_INVFORMATSTRING;
return E_OK;
}
/*
* mk_attr_infos: converts a list of attribute descriptors (attribute names,
* types, and lengths) to an array of AttrInfo's so it can be sent to
* Create.
*
* Returns:
* length of the list on success ( >= 0 )
* error code otherwise
*/
static int mk_attr_infos(NODE *list, int max, AttrInfo infos[])
{
int i;
int len;
AttrType type;
NODE *n;
RC errval;
/* for each element of the list... */
for (i = 0; list != NULL; ++i, list = list->u.LIST.next) {
/* if the list is too long, then error */
if (i == max)
return E_TOOMANY;
n = list->u.LIST.curr;
/* Make sure the attribute name isn't too long */
if (strlen(n->u.ATTRTYPE.attrname) > MAXNAME)
return E_TOOLONG;
/* interpret the format string */
errval = parse_format_string(n->u.ATTRTYPE.type, &type, &len);
if (errval != E_OK)
return errval;
/* add it to the list */
infos[i].attrname = n->u.ATTRTYPE.attrname;
infos[i].type = type;
infos[i].len = len;
}
return i;
}
/*
* mk_values: converts a single value node into a Value
*/
static void mk_value(NODE *node, Value &value)
{
value.type = node->u.VALUE.type;
switch (value.type) {
case INT:
value.data = (void *)&node->u.VALUE.ival;
break;
case FLOAT:
value.data = (void *)&node->u.VALUE.rval;
break;
case STRING:
value.data = (void *)node->u.VALUE.sval;
break;
}
}
/*
* mk_values: converts a list of values into an array of values
*
* Returns:
* the lengh of the list on success ( >= 0 )
* error code otherwise
*/
static int mk_values(NODE *list, int max, Value values[])
{
int i;
/* for each element of the list... */
for (i = 0; list != NULL; ++i, list = list->u.LIST.next) {
/* If the list is too long then error */
if (i == max)
return E_TOOMANY;
mk_value(list->u.LIST.curr, values[i]);
}
return i;
}
/*
* mk_agg_rel_attr: converts a single relation-attribute (<relation,
* attribute> pair) into a AggRelAttr
* 将一个relation-attribute对转化为AggRelAttr
*/
static void mk_agg_rel_attr(NODE *node, AggRelAttr &relAttr)
{
relAttr.func = node->u.AGGRELATTR.func;
relAttr.relname = node->u.AGGRELATTR.relname; /* relation表示关系,对应数据库中的表,relname即表名 */
relAttr.attrname = node->u.AGGRELATTR.attrname; /* attribute表示属性,attrname即属性名称 */
}
/*
* mk_agg_rel_attrs: converts a list of relation-attributes (<relation,
* attribute> pairs) into an array of AggRelAttrs
*
* Returns:
* the lengh of the list on success ( >= 0 )
* error code otherwise
*/
static int mk_agg_rel_attrs(NODE *list, int max, AggRelAttr relAttrs[])
{
int i;
/* For each element of the list... */
for (i = 0; list != NULL; ++i, list = list->u.LIST.next) {
/* If the list is too long then error */
if (i == max)
return E_TOOMANY;
mk_agg_rel_attr(list->u.LIST.curr, relAttrs[i]);
}
return i;
}
//
// mk_relations - 从list中提取出table的名称
//
static int mk_relations(NODE *list, int max, char *relations[])
{
int i;
NODE *current;
/* for each element of the list... */
for (i = 0; list != NULL; ++i, list = list->u.LIST.next) {
/* If the list is too long then error */
if (i == max)
return E_TOOMANY;
current = list->u.LIST.curr;
relations[i] = current->u.RELATION.relname;
}
return i;
}
//
// mk_conditions - 从列表中提取出conditons
//
static int mk_conditions(NODE *list, int max, Condition conditions[])
{
int i;
NODE *current;
/* for each element of the list... */
for (i = 0; list != NULL; ++i, list = list->u.LIST.next) {
/* If the list is too long then error */
if (i == max)
return E_TOOMANY;
current = list->u.LIST.curr;
/* 条件构成 -> relname.attrname op relname.attrname */
conditions[i].lhsAttr.relname =
current->u.CONDITION.lhsRelattr->u.RELATTR.relname;
conditions[i].lhsAttr.attrname =
current->u.CONDITION.lhsRelattr->u.RELATTR.attrname;
conditions[i].op = current->u.CONDITION.op;
if (current->u.CONDITION.rhsRelattr) { /* 右操作数也是属性 */
conditions[i].bRhsIsAttr = true;
conditions[i].rhsAttr.relname =
current->u.CONDITION.rhsRelattr->u.RELATTR.relname;
conditions[i].rhsAttr.attrname =
current->u.CONDITION.rhsRelattr->u.RELATTR.attrname;
}
else { /* 右操作数是值 */
conditions[i].bRhsIsAttr = false;
mk_value(current->u.CONDITION.rhsValue, conditions[i].rhsValue);
}
}
return i;
}
//
// mk_rel_attr: converts a single relation-attribute (<relation, attribute> pair) into a RelAttr
//
static void mk_rel_attr(NODE *node, RelAttr &relAttr)
{
relAttr.relname = node->u.RELATTR.relname;
relAttr.attrname = node->u.RELATTR.attrname;
}
//
// mk_order_relattr: converts an int and a single relation-attribute (<relation, attribute> pair) into a int and a RelAttr
//
static void mk_order_relattr(NODE *node, int& order, RelAttr &relAttr)
{
order = node->u.ORDERATTR.order;
if (order != 0)
mk_rel_attr(node->u.ORDERATTR.relattr, relAttr);
}
int interp(NODE *n)
{
RC errval = 0;
switch (n->kind)
{
case N_EXIT:
stop = true;
break;
case N_CREATETABLE: /* Create Table */
{
int nattrs;
AttrInfo infos[MAXATTRS];
if (strlen(n->u.CREATETABLE.relname) > MAXNAME) {
print_error("create", E_TOOLONG);
break;
}
nattrs = mk_attr_infos(n->u.CREATETABLE.attrlist, MAXATTRS, infos);
if (nattrs < 0) {
print_error("create", nattrs);
break;
}
errval = smManager.createTable(n->u.CREATETABLE.relname, nattrs, infos);
break;
}
case N_DROPTABLE:
smManager.dropTable(n->u.DROPTABLE.relname);
break;
case N_DROPINDEX:
smManager.dropIndex(n->u.DROPINDEX.relname, n->u.DROPINDEX.attrname);
break;
case N_INSERT:
{
int nvals = 0;
Value values[MAXATTRS];
nvals = mk_values(n->u.INSERT.valuelist, MAXATTRS, values);
if (nvals < 0) {
print_error("insert", nvals);
break;
}
/* Make the call to insert */
errval = qlManager.insert(n->u.INSERT.relname, nvals, values);
break;
}
case N_LOAD:
errval = smManager.load(n->u.LOAD.relname, n->u.LOAD.filename);
break;
case N_PRINT:
errval = smManager.print(n->u.PRINT.relname);
break;
case N_HELP:
if (n->u.HELP.relname)
errval = smManager.help(n->u.HELP.relname);
else
errval = smManager.help();
break;
case N_QUERY: /* 查询语句 */
{
int nselattrs = 0;
AggRelAttr relAttrs[MAXATTRS];
int nrelations = 0; /* 表的数目 */
char* relations[MAXATTRS]; /* 表的名称 */
int nconditions = 0; /* 条件的数目 */
Condition conditions[MAXCONDS];
int order = 0; /* 升序or降序 */
RelAttr orderAttr; /* 按照orderAttr来排序 */
bool group = false;
RelAttr groupAttr; /* 按照groupAttr来分组 */
/* 开始解析所选择的属性 */
nselattrs = mk_agg_rel_attrs(n->u.QUERY.relattrlist, MAXATTRS, relAttrs);
if (nselattrs < 0) {
print_error("select", nselattrs);
break;
}
/* 开始解析table的名称了 */
nrelations = mk_relations(n->u.QUERY.rellist, MAXATTRS, relations);
if (nrelations < 0) {
print_error("select", nrelations);
break;
}
/* 开始解析条件 */
nconditions = mk_conditions(n->u.QUERY.conditionlist, MAXCONDS, conditions);
if (nconditions < 0) {
print_error("select", nconditions);
break;
}
/* Make the order by attr suitable for sending to Query */
/* 开始解析排序属性 */
mk_order_relattr(n->u.QUERY.orderrelattr, order, orderAttr);
/* Make the group by attr suitable for sending to Query */
mk_rel_attr(n->u.QUERY.grouprelattr, groupAttr);
if (groupAttr.attrname != NULL)
group = true;
/* 开始调用select函数 */
errval = qlManager.select(nselattrs, relAttrs, /* 选择的属性 */
nrelations, relations, /* 选择的表格 */
nconditions, conditions, /* 条件 */
order, orderAttr,
group, groupAttr);
break;
}
default:
break;
}
return errval;
}