-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLstatement.cpp
More file actions
259 lines (243 loc) · 6.93 KB
/
SQLstatement.cpp
File metadata and controls
259 lines (243 loc) · 6.93 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
#include "MiniSQL.h"
inline bool isUnique(string s)
{
if (s == "1")
return true;
else
return false;
}
inline TYPE getType(string s)
{
if (s == "+")
return MYINT;
else if (s == "-")
return MYFLOAT;
else
return MYCHAR;
}
void SQLstatement::outputinfo()
{
cout << "----------Table----------" << endl;
cout << "SQL type : " << type << endl;
cout << "tableName : " << tableName << endl;
cout << "indexName : " << indexName << endl;
cout << "insert value : " << content << endl;
cout << "---------Attribute---------" << endl;
for (size_t i = 0; i < attributes.size(); i++)
{
cout << "name: " << attributes[i].name << endl;
cout << "type: " << attributes[i].type << endl;
cout << "length: " << attributes[i].length << endl;
cout << "isPrimary: " << attributes[i].isPrimaryKey << endl;
cout << "isUnique: " << attributes[i].isUnique << endl;
}
cout << "----------Conditions----------" << endl;
for (size_t i = 0; i < conditions.size(); i++)
{
cout << "attrName : " << conditions[i].attribute.name << endl;
cout << "relation type : " << conditions[i].relationType << endl;
cout << "value : " << conditions[i].value << endl;
}
}
RELATION_TYPE SQLstatement::getRelationType(string oper)
{
if (oper == "=") return EQUAL;
else if (oper == ">") return GREATER;
else if (oper == "<") return SMALLER;
else if (oper == ">=") return GREATER_EQUAL;
else if (oper == "<=") return SMALLER_EQUAL;
else if (oper == "<>") return NOT_EQUAL;
else return UNDEFINED_RELATION;
// 有个坑 : 操作符不合法没有处理; TO-DO
}
Condition SQLstatement::genCondition(string exp)
{
string op = "<>=!";
int front = exp.find_first_of(op, 0);
int end = exp.find_first_not_of(op, front);
string attrName = exp.substr(0, front);
RELATION_TYPE relationType = getRelationType(exp.substr(front, end - front));
if (relationType == UNDEFINED_RELATION)
{
cout << "undefined RELATION_TYPE, select all tuple instead" << endl;
}
string value = exp.substr(end);
//cout << attr << endl << oper << endl << value << endl;
return Condition(Attribute(attrName), value, relationType);
}
STATEMENT_TYPE SQLstatement::identify(string type_code)
{
if (type_code == "99") return ILLEGAL;
else if (type_code == "20") return SELECT;
else if (type_code == "21") return SELECT_WHERE;
else if (type_code == "30") return INSERT;
else if (type_code == "40") return MYDELETE;
else if (type_code == "41") return DELETE_WHERE;
else if (type_code == "00") return CREATE_DATABASE;
else if (type_code == "01") return CREATE_TABLE;
else if (type_code == "02") return CREATE_INDEX;
else if (type_code == "10") return DROP_DATABASE;
else if (type_code == "11") return DROP_TABLE;
else if (type_code == "12") return DROP_INDEX;
else if (type_code == "50") return USE;
else if (type_code == "60") return EXECFILE;
else if (type_code == "70") return QUIT;
else if (type_code == "80") return HELP;
else return ILLEGAL;
}
SQLstatement::SQLstatement(string SQL)
{
int start = 2;
int end = 0;
string temp;
string type_code = SQL.substr(0, 2);
this->type = identify(type_code);
switch (this->type)
{
case SELECT:
end = SQL.find_first_of(' ', start);
tableName = SQL.substr(start, end - start);
start = end + 1;
while (true)
{
if (!start) break;
end = SQL.find_first_of(',', start);
temp = SQL.substr(start, end - start);
// select属性添加
attributes.push_back(Attribute(temp));
start = end + 1;
}
break;
case SELECT_WHERE:
int conditionstart; // 记录条件开始位置
end = SQL.find_first_of(' ', start);
tableName = SQL.substr(start, end - start);
start = end + 1;
conditionstart = SQL.find_first_of(' ', start);
//cout << conditionstart << endl;
// 属性解析
while (true)
{
if (!start) break;
end = SQL.find_first_of(',', start);
if (end == -1)
temp = SQL.substr(start, conditionstart - start);
else
temp = SQL.substr(start, end - start);
// select属性添加
attributes.push_back(Attribute(temp));
start = end + 1;
}
// 条件解析
start = conditionstart + 1;
while (true)
{
if (!start) break; // 条件取完
end = SQL.find_first_of("&|", start);
temp = SQL.substr(start, end - start); // 取条件
// 生成并放入vector
conditions.push_back(genCondition(temp));
start = end + 1;
}
break;
case INSERT:
end = SQL.find_first_of(' ', start);
tableName = SQL.substr(start, end - start);
start = end + 1; // 读取表名
// insert内容
content = SQL.substr(start);
break;
case MYDELETE:
tableName = SQL.substr(start);
// 读取表名
break;
case DELETE_WHERE:
end = SQL.find_first_of(' ', start);
tableName = SQL.substr(start, end - start);
start = end + 1;
// where条件生成
while (true)
{
if (!start) break; // 条件取完
end = SQL.find_first_of("&|", start);
temp = SQL.substr(start, end - start); // 取条件
// 生成并放入vector
conditions.push_back(genCondition(temp));
start = end + 1;
}
break;
case CREATE_DATABASE:break;
case CREATE_TABLE:
end = SQL.find_first_of(',', start);
tableName = SQL.substr(start, end - start);
start = end + 1; // 读取表名
// 属性解析
while (true)
{
int first, second;
end = SQL.find_first_of(',', start);
if (end == -1) break;
temp = SQL.substr(start, end - start);
first = SQL.find_first_of(' ', start);
second = SQL.find_first_of(' ', first + 1);
if (second == -1) // 读到primarykey声明
{
string primaryKey = SQL.substr(start, first - start);
for (size_t i = 0; i < attributes.size(); i++)
{
if (attributes[i].name == primaryKey)
{
attributes[i].isPrimaryKey = true;
break;
} // 可能存在primaryKey指明的属性不存在 TO-DO
}
}
else
{
string attrname = SQL.substr(start, first - start);
TYPE attrtype = getType(SQL.substr(first + 1, second - first - 1));
bool attrisUnique = isUnique(SQL.substr(second + 1, end - second - 1));
int length = 4;
if (attrtype == MYCHAR)
{
length = atoi(SQL.substr(first + 1, second - first - 1).c_str()) + 1;
}
attributes.push_back(Attribute(attrname, attrtype, length, false, attrisUnique));
}
start = end + 1;
}
break;
case CREATE_INDEX:
end = SQL.find_first_of(' ', start);
indexName = SQL.substr(start, end - start);
start = end + 1; // 读取index名
end = SQL.find_first_of(' ', start);
tableName = SQL.substr(start, end - start);
start = end + 1;
attributes.push_back(Attribute(SQL.substr(start)));
break;
case DROP_DATABASE:break;
case DROP_TABLE:
tableName = SQL.substr(start);
// 读取表名
break;
case DROP_INDEX:
indexName = SQL.substr(start);
// 取得indexName
break;
case USE:break;
case EXECFILE:
tableName = SQL.substr(start);
// 用tableName存放执行的文件名
break;
case QUIT:break;
case HELP:break;
default:break;
}
}
SQLstatement::SQLstatement(STATEMENT_TYPE type, string tableName){}
SQLstatement::~SQLstatement()
{
attributes.clear();
conditions.clear();
}