-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathParseSelect.cpp
More file actions
190 lines (168 loc) · 5.94 KB
/
ParseSelect.cpp
File metadata and controls
190 lines (168 loc) · 5.94 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
//
// ParseFrom.cpp
// SQL parser and interpreter
//
// Created by 王璇 on 17/4/22.
// Copyright © 2017年 Xuan Wang. All rights reserved.
//
#include <stdio.h>
#include <string>
#include <cstring>
#include <map>
#include "lexanal.h"
#include "ScanTable.h"
#include "SelectNode.h"
using namespace std;
char *lex_anal(char *current_pos, char *token, int *token_type);
struct MyNode *E(char *&p, map<string, ScanTable*> &tablelist);
int ParseSelect(struct SelectNode *env, char* &p, map<string,ScanTable*> &tablelist, char* inp, int num_of_char){
char token[100] = {'\0'};
int token_code;
int tmp_code;
char* start;
char* tmp;
bool findFrom = false;
bool findWhere = false;
int m = 0;
//char expr[1000] = {'\0'};
env->NAttrsExp = 0;
env->NRels = 0;
start = p;
while(p < inp + num_of_char){
p = lex_anal(p, token, &token_code);
if(token_code == FROM || token_code == ','){
if(token_code == ','){
strncpy(env->AttrExp[env->NAttrsExp], start, p - start - 1);
env->NAttrsExp++;
start = p;
}
else{
findFrom = 1;
strncpy(env->AttrExp[env->NAttrsExp], start, p - start - 4);
m = p - start - 4;
while(env->AttrExp[env->NAttrsExp][m-1] == ' '){
env->AttrExp[env->NAttrsExp][m - 1] = '\0';
m--;
}
env->NAttrsExp++;
break;
}
}
}
if(findFrom == 0){
printf("Error: FROM expected in SELECT command\n");
return -1;
}
while(p < inp + num_of_char){
p = lex_anal(p, token, &token_code);
char tmp_token[100] = {'\0'};
if(token_code == IDENTIFIER){
env->Rel[env->NRels] = new ScanTable;
strcpy(env->Rel[env->NRels]->true_Relname, token);//store ScanTable->true_Relname
(env->Rel[env->NRels])->Open((env->Rel[env->NRels])->true_Relname);
(env->Rel[env->NRels])->Close();
strcpy(env->RelName[env->NRels],token);//store Relname:???optional
//peek the next token: 1)alias 2) , 3)where 4) others
tmp = lex_anal(p, tmp_token, &tmp_code);
// 1)alias
if(tmp_code == IDENTIFIER){
p = lex_anal(p, token, &token_code);//read away alias
string name(token, strlen(token));
tablelist.insert(pair<string, ScanTable*>(name, env->Rel[env->NRels]));
env->NRels++;
//peek the next token: 1), 2)where 3)others
tmp = lex_anal(p, tmp_token, &tmp_code);
if(tmp_code == ','){
p = lex_anal(p, token, &token_code);//read away ','
continue;
}
else if(tmp_code == WHERE){
p = lex_anal(p, token, &token_code);//read away where
findWhere = 1;
break;
}
else{
printf("Syntex error: relname alias XX\n");
return -1;
}
}
//2) ,
else if(tmp_code == ','){
string name(token, strlen(token));
tablelist.insert(pair<string, ScanTable*>(name, env->Rel[env->NRels]));
p = lex_anal(p, token, &token_code);//read away ','
env->NRels++;
continue;
}
//3) where
else if(tmp_code == WHERE){
string name(token, strlen(token));
tablelist.insert(pair<string, ScanTable*>(name, env->Rel[env->NRels]));
env->NRels++;
p = lex_anal(p, token, &token_code);//read away where
findWhere = 1;
break;
}
//4)others
else{
p = lex_anal(p, token, &token_code);
if(p == inp + num_of_char){
string name(token, strlen(token));
tablelist.insert(pair<string, ScanTable*>(name, env->Rel[env->NRels]));
env->NRels++;
break;
}
else{
printf("Syntex error in From clause\n");
return -1;
}
}
}
else{
p = lex_anal(p, token, &token_code);
if(p == inp + num_of_char){
string name(token, strlen(token));
tablelist.insert(pair<string, ScanTable*>(name, env->Rel[env->NRels]));
env->NRels++;
break;
}
else{
printf("Syntex error in From clause\n");
return -1;
}
}
}
//create attribute trees
char* pr1 = env->AttrExp[0];
pr1 = lex_anal(pr1, token, &token_code);
if(token_code == '*'){
if(env->NAttrsExp != 1){
printf("Syntex error in SELECT clause: * XX\n");
return -1;
}
else{
env->NAttrsExp = 0;
int num_AttrExpr = 0;
for(int j = 0; j < env->NRels; j++){
env->NAttrsExp += env->Rel[j]->n_fields;
for(int t = 0; t < env->Rel[j]->n_fields; t++){
strcpy(env->AttrExp[num_AttrExpr], env->Rel[j]->DataDes[t].fieldname);
num_AttrExpr++;
}
}
}
}
for(int i = 0; i < env->NAttrsExp; i++){
env->selectExp[i] = new MyNode;
char* pr2 = env->AttrExp[i];
env->selectExp[i] = E(pr2, tablelist);
if(env->selectExp[i] == NULL){
printf("Error in attribute expression\n");
return -1;
}
}
if(findWhere == 1)
return 1;
else
return 0;
}