-
Notifications
You must be signed in to change notification settings - Fork 0
/
SqlParser.y
151 lines (131 loc) · 3.07 KB
/
SqlParser.y
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
%{
#include <cstdio>
#include <cstring>
#include <sys/times.h>
#include <unistd.h>
#include <climits>
#include <string>
#include "Bruinbase.h"
#include "SqlEngine.h"
#include "PageFile.h"
int sqllex(void);
void sqlerror(const char *str) { fprintf(stderr, "Error: %s\n", str); }
extern "C" { int sqlwrap() { return 1; } }
static void runSelect(int attr, const char* table, const std::vector<SelCond>& conds)
{
struct tms tmsbuf;
clock_t btime, etime;
int bpagecnt, epagecnt;
btime = times(&tmsbuf);
bpagecnt = PageFile::getPageReadCount();
SqlEngine::select(attr, table, conds);
etime = times(&tmsbuf);
epagecnt = PageFile::getPageReadCount();
fprintf(stderr, " -- %.3f seconds to run the select command. Read %d pages\n", ((float)(etime - btime))/sysconf(_SC_CLK_TCK), epagecnt - bpagecnt);
}
%}
%union {
int integer;
char* string;
SelCond* cond;
std::vector<SelCond>* conds;
}
%token SELECT FROM WHERE LOAD WITH INDEX QUIT COUNT AND OR
%token COMMA STAR LF
%token <string> INTEGER STRING ID
%token EQUAL NEQUAL LESS LESSEQUAL GREATER GREATEREQUAL
%type <integer> attributes attribute comparator
%type <string> table value
%type <cond> condition
%type <conds> conditions
%%
commands:
commands command
|
;
command:
load_command { fprintf(stdout, "Bruinbase> "); }
| select_command { fprintf(stdout, "Bruinbase> "); }
| quit_command
| error LF { fprintf(stdout, "Bruinbase> "); }
| LF { fprintf(stdout, "Bruinbase> "); }
;
quit_command:
QUIT { return 0; }
;
load_command:
LOAD table FROM STRING LF {
SqlEngine::load(std::string($2), std::string($4), false);
free($2);
free($4);
}
| LOAD table FROM STRING WITH INDEX LF {
SqlEngine::load(std::string($2), std::string($4), true);
free($2);
free($4);
}
;
select_command:
SELECT attributes FROM table LF {
std::vector<SelCond> conds;
runSelect($2, $4, conds);
free($4);
}
| SELECT attributes FROM table WHERE conditions LF {
runSelect($2, $4, *$6);
free($4);
for (unsigned i = 0; i < $6->size(); i++) {
free((*$6)[i].value);
}
delete $6;
}
;
conditions:
condition {
std::vector<SelCond>* v = new std::vector<SelCond>;
v->push_back(*$1);
$$ = v;
delete $1;
}
| conditions AND condition {
$1->push_back(*$3);
$$ = $1;
delete $3;
}
;
condition:
attribute comparator value {
SelCond* c = new SelCond;
c->attr = $1;
c->comp = static_cast<SelCond::Comparator>($2);
c->value = $3;
$$ = c;
}
;
attributes:
attribute { $$ = $1; }
| STAR { $$ = 3; }
| COUNT { $$ = 4; }
;
attribute:
ID {
if (strcasecmp($1, "key") == 0) $$=1;
else if (strcasecmp($1, "value") == 0) $$=2;
else sqlerror("wrong attribute name. neither key or value");
free($1);
}
value:
INTEGER { $$ = $1; }
| STRING { $$ = $1; }
;
table:
ID { $$ = $1; }
;
comparator:
EQUAL { $$ = SelCond::EQ; }
| NEQUAL { $$ = SelCond::NE; }
| LESS { $$ = SelCond::LT; }
| GREATER { $$ = SelCond::GT; }
| LESSEQUAL { $$ = SelCond::LE; }
| GREATEREQUAL { $$ = SelCond::GE; }
;