Repository navigation
Expand file tree
/
Copy pathInterpreter.cpp
More file actions
635 lines (587 loc) · 17.2 KB
/
Copy pathInterpreter.cpp
File metadata and controls
635 lines (587 loc) · 17.2 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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#include "Interpreter.h"
#include "API.h"
#include <stdlib.h>
#include <string.h>
#include <fstream>
string getUserCommand()
{
char* command; //用户输入的命令
command = (char*)malloc(1024);
char ch;
int i = 0;
while (1){
cin.get(ch);
command[i++] = ch;
if (ch == ';') break;
else if (ch == '\n'){
cout << " ->";
}
}
command[i] = '\0'; //结束符
string s(command);
delete[] command;
return s;
}
void handleWithCommand(string command)
{
int i, j, opos, npos;
i = 0; j = 0;
opos = command.find_first_not_of(" \n", 0);
npos = command.find_first_of(" ", opos);
string comtype = command.substr(opos, npos - opos);
//判断命令类型
if (comtype == "execfile"){
//打开存储待插入记录的文件
opos = command.find_first_not_of(" ", npos);
npos = command.find_first_of(";", opos);
string filename = command.substr(opos, npos - opos);
ifstream infile;
char buf[1024];
infile.open("./student/" + filename, ios::in);
while (!infile.eof()){
memset(buf, 0, 1024);
infile.getline(buf, 1024);
if (infile.eof()) break;
string cmd(buf);
handleWithCommand(cmd);
}
cout << "Query OK." << endl;
}
else if (comtype == "create"){
opos = npos + 1;
npos = command.find_first_of(" ", opos);
string object = command.substr(opos, npos - opos);
if (object == "table"){
//建表命令
opos = npos + 1;
npos = command.find_first_of(" (", opos);
string tbname = command.substr(opos, npos - opos);
//创建建表的语法树
FieldTree* T;
T = createCreateTableTree(tbname, command.substr(npos, command.length() - npos));
//处理语法树,传参给API
API_Create_Table(tbname, T);
/*Table Table(tbname);
Table.initializeTable(T);
Table.createTable(); //此处为调用API中create table命令*/
cout << "Query OK, 0 rows affected." << endl;
return;
}
else if (object == "index"){
//建索引命令
opos = npos + 1;
opos = command.find_first_not_of(" ", opos); //索引名开始的位置
npos = command.find_first_of(" ", opos); //索引名结束后的第一个空格位置
string indexname = command.substr(opos, npos - opos);
//创建索引结构
IndexStruct* I;
I = createCreateIndexStruct(indexname, command.substr(npos, command.length() - npos));
//传参给API
API_Create_Index(*I);
/*Index Idx(indexname);
Idx.initializeIndex(I);
Idx.createIndex(); //调用建索引的API*/
}
else{
//非法命令
throw Grammer_error(object);
}
}
else if (comtype == "drop"){
opos = command.find_first_not_of(" ", npos);
npos = command.find_first_of(" ", opos);
string dropOrient = command.substr(opos, npos - opos);
if (dropOrient == "table"){
//删除表命令
opos = command.find_first_not_of(" ", npos);
npos = command.find_first_of(" ;", opos);
string tbname = command.substr(opos, npos - opos);
//下一部分非空格符应只有分号
opos = command.find_first_not_of(" ", npos);
if (command.at(opos) != ';'){
//语法错误
throw Grammer_error(tbname);
}
//传参给API
Table T(tbname);
T.getTableInfo();
API_Drop_Table(T);
/*
T.dropTable(); //调用API的删除表命令*/
}
else if (dropOrient == "index"){
//删除索引命令
opos = command.find_first_not_of(" ", npos);
npos = command.find_first_of(" ;", opos);
string idxname = command.substr(opos, npos - opos);
//下一部分非空格符应只有分号
opos = command.find_first_not_of(" ", npos);
if (command.at(opos) != ';'){
//语法错误
throw Grammer_error(idxname);
}
//传参给API
API_Drop_Index(idxname);
/*Index Idx(idxname);
Idx.dropIndex(); //调用API中删除索引的命令*/
}
else{
//非法命令
throw Grammer_error(dropOrient);
}
}
else if (comtype == "insert"){
//插入记录指令
opos = command.find_first_not_of(" ", npos);
npos = command.find_first_of(" ", opos);
string into = command.substr(opos, npos - opos);
if (into == "into"){
//符合语法要求
//表名
opos = command.find_first_not_of(" ", npos);
npos = command.find_first_of(" ", opos);
string tbname = command.substr(opos, npos - opos);
//"values"
opos = command.find_first_not_of(" ", npos);
npos = command.find_first_of(" (", opos);
string values = command.substr(opos, npos - opos);
if (values == "values"){
//符合语法要求
TupleStruct* tup;
tup = CreateTupleStruct(command.substr(npos, command.length() - npos));
//传tuple给API
Table tbl(tbname);
tbl.getTableInfo();
Tuple tuple(tbname);
tuple.initializeTuple(tup);
API_Insert(tbl, tuple);
/*
tuple.Insert(); //调用API中的insert命令*/
}
else{
//不符合语法要求
throw Grammer_error(into);
}
}
else{
//不符合语法要求
throw Grammer_error(into);
}
cout << "Query OK, 1 rows affected." << endl;
}
else if (comtype == "select"){
//select * from student;
//select * from student where...
opos = command.find_first_not_of(" ", npos);
npos = command.find_first_of(" ", opos);
string selectall = command.substr(opos, npos - opos);
if (selectall == "*"){
//符合语法要求
opos = command.find_first_not_of(" ", npos);
npos = command.find_first_of(" ", opos);
string from = command.substr(opos, npos - opos);
if (from == "from"){
//符合语法要求
opos = command.find_first_not_of(" ", npos);
npos = command.find_first_of(" ;", opos);
string tbname = command.substr(opos, npos - opos);
list<Condition> conditionlist; //储存查询条件
conditionlist = CreateConditionlist(command.substr(npos, command.length() - npos));
//传参给API
Table tbl(tbname);
tbl.getTableInfo();
API_Select(tbl,conditionlist);
/*vector<int> offsetlist;
//API从索引里查找,返回offserlist
Tuple tuple(tbname);
tuple.Select(offsetlist, conditionlist); //API调用select命令*/
}
else{
//不符合语法要求
throw Grammer_error(from);
}
}
else{
//不符合语法要求
throw Grammer_error(selectall);
}
}
else if (comtype == "delete"){
//delete from student;
//delete from student where sno = ‘88888888’;
opos = command.find_first_not_of(" ", npos);
npos = command.find_first_of(" ", opos);
string from = command.substr(opos, npos - opos);
if (from == "from"){
//符合语法要求
opos = command.find_first_not_of(" ", npos);
npos = command.find_first_of(" ;", opos);
string tbname = command.substr(opos, npos - opos);
list<Condition> conditionlist; //储存查询条件
conditionlist = CreateConditionlist(command.substr(npos, command.length() - npos));
//传参给API
Table tbl(tbname);
tbl.getTableInfo();
API_Delete(tbl, conditionlist);
/*vector<int> offsetlist;
//API从索引里查找,返回offserlist
Tuple tuple(tbname);
tuple.Delete(offsetlist, conditionlist); //API调用delete命令*/
}
else{
//不符合语法要求
throw Grammer_error(from);
}
}
else{
//非法命令
throw Grammer_error(comtype);
}
}
/*创建建表的语法树
create table student(
sno char(8),
sname char(16) unique,
sage int,
sgender char(1),
primary key(sno)
);
*/
//表名为tbname, 字段域为fieldmodule部分 (括号内(包括括号)的部分)
FieldTree* createCreateTableTree(string tbname, string fieldmodule)
{
int i, opos, npos, endpos;
int primary_num;
string primary_attr = "";
FieldTree* T;
FieldNode* Tnode, *Lnode;
string field; //一条字段信息
T = new FieldTree;
T->tablename = tbname;
T->field = new CreateTableNode;//树根节点
T->field->next = NULL;
Lnode = T->field;
i = 0;
primary_num = 0;
opos = fieldmodule.find_first_not_of(" (\n");
while (1){
fieldmodule = fieldmodule.substr(opos, fieldmodule.length() - opos);
npos = fieldmodule.find_first_of(",", 0);
if (npos == -1){
//最后一个字段
//右括号和分号结束
npos = fieldmodule.find_first_of(";", 0);
}
else npos++;
opos = fieldmodule.find_first_not_of(" ", 0);
if (opos == -1 || fieldmodule.at(opos) == ';') break; //语句结束
string field = fieldmodule.substr(0, npos);
//sno char(8) unique,
//primary key(sno))
//确定字段名
opos = field.find_first_not_of(" \n(", 0);
npos = field.find_first_of(" ", opos);
if (npos == -1 || opos > npos){
//字段为空,报错,提示至少要有一个属性
cerr << "At least one attribute is aquired." << endl;
throw Grammer_error(field);
}
string def = field.substr(opos, npos - opos);
if (def == "primary"){
//主键声明语句,而非字段定义语句
opos = field.find_first_not_of(" ", npos);
npos = field.find_first_of(" (", opos);
string key = field.substr(opos, npos - opos);
if (key == "key"){
opos = field.find_first_not_of(" (", npos);
npos = field.find_first_of(" )", opos);
if (opos == -1 || npos == -1){
//括号不匹配
throw Grammer_error(def + " " + key);
return NULL;
}
primary_num++;
if (primary_num == 2){
//主键声明不止一个
cerr << "Only one primary key is allowed!" << endl;
return NULL;
}
primary_attr = field.substr(opos, npos - opos);
}
else{
//语法错误
throw Grammer_error(def);
return NULL;
}
}
else{
Tnode = new CreateTableNode; //创建新节点
Lnode->next = Tnode;
Tnode->next = NULL;
Tnode->attr_name = field.substr(opos, npos - opos); //字段名
//确定字段属性
opos = field.find_first_not_of(" \n,", npos);
npos = field.find_first_of(" \n(),", opos);
string type = field.substr(opos, npos - opos);
if (type == "char"){
Tnode->attr_type = 0;
opos = field.find_first_not_of(" (\n", npos);
npos = field.find_first_of(')', opos);
if (opos == -1 || (opos != -1 && npos == -1)){
//没有括号 or 左右括号不匹配
throw Grammer_error(type);
return NULL;
}
int len = atoi(field.substr(opos, npos - opos).c_str()); //char类型属性的长度
Tnode->attr_len = len;
}
else if (type == "int"){
Tnode->attr_type = 1;
Tnode->attr_len = 4;
}
else if (type == "float"){
Tnode->attr_type = 2;
Tnode->attr_len = 4;
}
else{
//字段类型非法
cerr << type << " is not a legal type." << endl;
throw Grammer_error(type);
return NULL;
}
//确定字段定义的约束属性
opos = field.find_first_not_of(" )\n", npos);
if (opos == -1 || opos == field.length() - 1){
//未显式定义约束条件
Tnode->attr_def_type = -1;
}
else{
//显式定义了约束条件
npos = field.find_first_of(" \n),", opos);
string deftype = field.substr(opos, npos - opos);
if (deftype == "primary"){
//主键定义
opos = field.find_first_not_of(" \n", npos);
npos = field.find_first_of(" ,)\n", opos);
string key = field.substr(opos, npos - opos);
if (key == "key"){
//检查key后还有没有非法符号
opos = field.find_first_not_of(" \n", npos);
if (opos == -1 || (field.at(opos) != ',' && field.at(opos) != ')')){
//语法错误
throw Grammer_error(key);
}
primary_num++;
Tnode->attr_def_type = 0; //primary
}
else{
//语法错误
throw Grammer_error(deftype);
}
}
else if (deftype == "unique"){
//检查unique后还有没有非法符号
opos = field.find_first_not_of(" \n", npos);
if (opos == -1 || (field.at(opos) != ',' && field.at(opos) != ')')){
//语法错误
throw Grammer_error(deftype);
}
Tnode->attr_def_type = 1; //unique
}
else{
//非法定义
throw Grammer_error(deftype);
}
}//判断约束条件
Lnode = Tnode;
//下一条字段信息
}//字段定义语句
opos = 0;
while (opos < field.length()) opos++;
}//while
int flag = 0;
if (primary_attr != ""){
//有附加的主键定义
primary_num = 0;
for (Tnode = T->field->next; Tnode != NULL; Tnode = Tnode->next){
if (Tnode->attr_name == primary_attr){
flag = 1;
Tnode->attr_def_type = 0;
}
if (Tnode->attr_def_type == 0)
primary_num++;
}
if (!flag){
//没有主键定义的该字段
cout<< "There is no attribute \"" << primary_attr << "\"" << endl;
return NULL;
}
if (primary_num >= 2){
//多于一个主键定义
cout << "Only one primary key is allowed!" << endl;
return NULL;
}
if (primary_num == 0){
//没有主键定义
cout << "At least one attribute is acquired! 1" << endl;
}
}
else if (primary_num == 0){
cout << "At least one attribute is acquired! 2" << endl;
}
return T;
}
/*创建储存索引信息的结构
create index stunameidx on student ( sname );
*/
//idxname为索引名,info为索引名之后的信息
IndexStruct* createCreateIndexStruct(string idxname, string info)
{
IndexStruct* Indexs;
int i, opos, npos;
opos = 0;
opos = info.find_first_not_of(" ", opos);
npos = info.find_first_of(" ", opos);
string on = info.substr(opos, npos - opos);
if (on == "on"){
//符合语法要求
Indexs = new IndexStruct;
Indexs->index_name = idxname;
Indexs->attr_name = "";
Indexs->table_name = ""; //初始化
//下一部分为索引所在的表的名字
opos = info.find_first_not_of(" ", npos);
npos = info.find_first_of(" (", opos);
string tbname = info.substr(opos, npos - opos);
Indexs->table_name = tbname; //表名
//下一部分为索引建立的字段名
opos = info.find_first_not_of(" (", npos);
npos = info.find_first_of(" )", opos);
string attrname = info.substr(opos, npos - opos);
Indexs->attr_name = attrname; //字段名
//下一部分非空格符应只有分号
opos = info.find_first_not_of(" )", npos);
if (info.at(opos) != ';'){
//语法错误
cout << "SQL syntax error near \"" << attrname << "\"" << endl;
return NULL;
}
return Indexs;
}
else{
//不符合语法要求,报错
cout << "SQL syntax error near \"" << idxname <<" "<< on << "\"" << endl;
return NULL;
}
return NULL;
}
//insert into student values(‘12345678’, ’wy’, 22, ’M’);
//tupinfo为values后[(...);]部分
TupleStruct* CreateTupleStruct(string tupinfo)
{
int opos, npos;
int num; //属性个数
TupleStruct* tup;
tup = new TupleStruct;
num = 0;
opos = tupinfo.find_first_not_of(" \n(", 0);
while (1){
tupinfo = tupinfo.substr(opos, tupinfo.length() - opos);
npos = tupinfo.find_first_of(",)", 0);
if (npos == -1){
//所有属性值都已处理完
npos = tupinfo.find_first_not_of(" \n");
if (tupinfo.at(npos) != ';'){
//语法错误
cout << "SQL syntax error near \"" << tupinfo.at(npos) << "\"" << endl;
return NULL;
}
else break; //处理完毕
}
string value = tupinfo.substr(0, npos + 1); //获得一个属性值(包括','或')')
opos = value.find_first_not_of(" '", 0);
npos = value.find_first_of("' ,)", opos);
if (npos == -1) npos = value.length();
tup->tuplevalues[num++] = value.substr(opos, npos - opos); //获得处理后的属性值(去除首尾空格和')
opos = 0;
while (opos < value.length()) opos++;
//处理下一个属性值
}//循环处理括号中每个属性值
tup->value_num = num;
return tup;
}
//建立储存查询条件的结构
//where sage > 20 and sgender = ‘F’;
list<Condition> CreateConditionlist(string condinfo)
{
list<Condition> condlist;
int opos, npos;
condlist.clear();
//判断是否有where语句
opos = condinfo.find_first_not_of(" ", 0);
if (condinfo.at(opos) == ';'){
//没有where语句,查询条件为空
return condlist;
}
else{
//有where语句
npos = condinfo.find_first_of(" ", opos);
string where = condinfo.substr(opos, npos - opos);
if (where == "where"){
//符合语法要求
opos = npos; //从where后面的语句开始处理
while (1){
condinfo = condinfo.substr(opos, condinfo.length() - opos);
opos = condinfo.find_first_not_of(" \n", 0);
npos = condinfo.find("and", opos);
if (opos == -1 || condinfo.at(opos) == ';') break; //语句结束
if (npos == string::npos){
//最后一个查询条件
//分号结束语句(分号前可能有空格)
npos = condinfo.find_first_of(";", opos);
npos++;
}
else npos = condinfo.find_first_of(" \n", npos);
string condstr = condinfo.substr(opos, npos - opos);
//id >= '10010' (and/;)
//所要查询的属性名
opos = condstr.find_first_not_of(" \n", 0);
npos = condstr.find_first_of(" \n", opos);
if (opos == -1 || npos == -1){
//语法错误
throw Grammer_error(condstr);
}
string attr = condstr.substr(opos, npos - opos);
//查询的比较条件(== / >= / <...)
opos = condstr.find_first_not_of(" \n", npos);
npos = condstr.find_first_of(" \n", opos);
if (opos == -1 || npos == -1){
//语法错误
throw Grammer_error(condstr);
}
string type = condstr.substr(opos, npos - opos);
if (type != "=" && type != ">" && type != ">="
&& type != "<" && type != "<=" && type != "<>"){
//比较符号错误
throw Grammer_error(type);
}
//比较值
opos = condstr.find_first_not_of(" ';\n", npos);
npos = condstr.find_first_of(" ';\n", opos); //可能以分号结尾
if (opos == -1 || npos == -1){
//语法错误
throw Grammer_error(condstr);
}
string value = condstr.substr(opos, npos - opos);
struct Condition oneCondition;
oneCondition.compare_attr = attr;
oneCondition.compare_type = type;
oneCondition.compare_value = value;
condlist.push_back(oneCondition); //向链表中插入一条记录
while (opos++ < condstr.length());
} //while
}
else throw Grammer_error(where); //抛出异常
}
return condlist;
}