-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinterpreter.cpp
More file actions
executable file
·1040 lines (1012 loc) · 29.9 KB
/
Copy pathinterpreter.cpp
File metadata and controls
executable file
·1040 lines (1012 loc) · 29.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
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Interpreter.h"
#include<cstdlib>
#include<iterator>
#include<algorithm>
string read_input()
{
string SQL;
string temp;
char str[100];
bool finish=false;
while(!finish)
{
cin>>str;
temp=str;
SQL=SQL+" "+temp;
if(SQL.at(SQL.length()-1)==';')
{
SQL[SQL.length()-1] = ' ';
SQL+=";";
finish=true;
}
}
//将输入大写转化为小写
//SQL.MakeLower();
//返回用户输入
transform(SQL.begin(), SQL.end(), SQL.begin(), ::tolower);
return SQL;
}
void get_attribute(string temp,SQL_CLAUSE &sql_cla)
{
int start=0,end,index;
string T,C;
TableAttr attr;
attr.index=0;
temp+=" ";
//获得属性名
end=temp.find(' ',start);
T=temp.substr(start,end-start);
start=end+1;
// sql+=T+" ";
int i;
for (i=0; i<T.length(); i++) {
attr.attrName[i]=T[i];
}
attr.attrName[i]=0;
//获得数据类型
while(temp.at(start)==' ')
start++;
end=temp.find(' ',start);
T=temp.substr(start,end-start);
start=end+1;
//若无,打印出错信息
if(T == "")
{
cout<<"error : error in create table statement!"<<endl;
sql_cla.type=ERROR;
return;
}
//若为int
else if(T=="int")
attr.type=CTG_INT;
//若为float
else if(T=="float")
attr.type=CTG_FLOAT;
//其他
else
{
index=T.find('(');
C=T.substr(0, index);
index++;
//若有误,打印出错信息
if(C == "")
{
cout<<"error: "<<T<<"---is not a valid data type definition!"<<endl;
sql_cla.type=ERROR;
return;
}
//若为char
else if(C=="char")
{
C=T.substr(index,T.length()-index-1);
if(C == "")
{
cout<<"error: the length of the data type char has not been specified!"<<endl;
sql_cla.type=ERROR;
return;
}
else{
attr.type=CTG_CHAR;
attr.amount=atoi(C.c_str());
}
}
//若为非法信息,打印出错信息
else
{
cout<<"error: "<<C<<"---is not a valid key word!"<<endl;
sql_cla.type=ERROR;
return ;
}
}
//是否有附加信息
while(start<temp.length()&&temp.at(start)==' ')
start++;
if(start<temp.length())
{
//若为unique定义,保存信息
end=temp.find(' ',start);
T=temp.substr(start,end-start);
if(T=="unique")
{
attr.unique=1;
}
//若为非法信息,打印出错信息
else
{
cout<<"error: "<<temp<<"---is not a valid key word!"<<endl;
sql_cla.type=ERROR;
return ;
}
}
//若无附加信息
else
attr.unique=0;
sql_cla.attrAmount++;
sql_cla.attr.push_back(attr);
return ;
}
void create_table(string SQL,int start,SQL_CLAUSE &sql_cla)
{
string temp,sql,T;
sql_cla.attrAmount=0;
sql_cla.attr.clear();
int index,end,length;
//获取表名
while(SQL.at(start)==' ')
start++;
index=start;
if((end=SQL.find('(',start))==-1)
{
cout<<"error: missing ( in the statement!"<<endl;
sql_cla.type=ERROR;
return ;
}
temp=SQL.substr(start,end-start);
start=end+1;
if(!(temp == ""))
{
while(SQL.at(start)==' ')
start++;
length=temp.length()-1;
while(temp.at(length)==' ')
length--;
temp=temp.substr(0, length+1);
sql_cla.name=temp;
}
//若无,打印出错信息
if(temp == "")
{
cout<<"error: error in create table statement!"<<endl;
sql_cla.type=ERROR;
return ;
}
//若为非法信息,打印出错信息
else if(temp.find(' ')!=-1)
{
cout<<"error: "<<temp<<"---is not a valid table name!"<<endl;
sql_cla.type=ERROR;
return ;
}
else
{
sql=temp+",";
//获取每个属性
while((end=SQL.find(',',start))!=-1)
{
temp=SQL.substr(start,end-start);
start=end+1;
//若有空属性,打印出错信息
if(temp == "")
{
cout<<"error: error in create table statement!"<<endl;
sql_cla.type=ERROR;
return ;
}
//保存属性
else
{
get_attribute(temp, sql_cla);
if(sql_cla.type==ERROR)
return ;
}
while(SQL.at(start)==' ')
start++;
}
//获得最后属性
temp=SQL.substr(start,SQL.length()-start-1);
length=temp.length()-1;
while(temp.at(length)!=')'&&length>=0)
length--;
//若无,打印出错信息
if(length<1)
{
cout<<"error: error in create table statement!"<<endl;
sql_cla.type=ERROR;
return ;
}
//存储属性
else
{
temp=temp.substr(0, length);
end=SQL.find(' ',start);
T=SQL.substr(start,end-start);
start=end+1;
//若为主键定义
if(T=="primary")
{
//判断是否有关键字key
temp+=")";
while(SQL.at(start)==' ')
start++;
end=SQL.find('(',start);
T=SQL.substr(start,end-start);
start=end+1;
length=T.length()-1;
while(T.at(length)==' ')
length--;
T=T.substr(0, length+1);
//若为空,打印出错信息
if(T == "")
{
cout<<"syntax error: syntax error in create table statement!"<<endl;
cout<<"\t missing key word key!"<<endl;
sql_cla.type=ERROR;
return ;
}
//若有,继续验证
else if(T=="key")
{
//获取主键属性名
while(SQL.at(start)==' ')
start++;
end=SQL.find(')',start);
T=SQL.substr(start,end-start);
length=T.length()-1;
while(T.at(length)==' ')
length--;
T=T.substr(0, length+1);
//若无,打印出错信息
if(T == "")
{
cout<<"error : missing primary key attribute name!"<<endl;
sql_cla.type=ERROR;
return ;
}
//若为非法信息,打印出错信息
else if(T.find(' ')!=-1)
{
cout<<"error : "<<T<<"---is not a valid primary key attribute name!"<<endl;
sql_cla.type=ERROR;
return ;
}
//保存主键
else
{
for (vector<TableAttr>::iterator iter=sql_cla.attr.begin(); iter!=sql_cla.attr.end(); iter++) {
if (T==iter->attrName) {
iter->primary=1;
iter->index=0;
break;
}
}
// for (auto i:sql_cla.attr) {
// if(T==i.attrName){
// i.primary=1;
// i.index=1;
// break;
// }
// }
sql_cla.type=CREATETABLE;
}
}
//若为非法信息,打印出错信息
else
{
cout<<"error : "<<T<<"---is not a valid key word!"<<endl;
sql_cla.type=ERROR;
return ;
}
}
//若为一般属性
else
{
get_attribute(temp,sql_cla);
}
}
}
sql_cla.type=CREATETABLE;
ConvertToArray(sql_cla.att, sql_cla.attr);
return ;
}
SQL_CLAUSE create_database(string SQL,int start)
{
SQL_CLAUSE sql_cla;
int index,end;
string temp;
//获取第三个单词
while(SQL.at(start)==' ')
start++;
index=start;
end=SQL.find(' ',start);
temp=SQL.substr(start,end-start);
start=end+1;
//若无,打印出错信息
if(temp=="")
{
cout<<"error: database name has not been specified!"<<endl;
sql_cla.type=ERROR;
}
else
{
while(SQL.at(start)==' ')
start++;
//若为非法信息,打印出错信息
if(SQL.at(start)!=';'||start!=SQL.length()-1)
{
cout<<"error12:"<<SQL.substr(index,SQL.length()-index-2)<<"---is not a valid database name!"<<endl;
sql_cla.type=ERROR;
}
//返回drop database语句的内部形式
else{
sql_cla.name=temp;
sql_cla.type=CREATEDATABASE;
}
}
sql_cla.type=CREATEDATABASE;
return sql_cla;
}
/////////////////////////////////////////////////////////////////////////////////////////////
//验证create index on语句是否有效
void create_index_on(string SQL,int start,SQL_CLAUSE &sql_cla)
{
string temp;
int end,length;
//获取表名
while(SQL.at(start)==' ')
start++;
end=SQL.find('(',start);
temp=SQL.substr(start,end-start);
start=end+1;
//若无,打印出错信息
if(temp=="")
{
cout<<"syntax error: syntax error for create index statement!"<<endl;
cout<<"\t missing ( !"<<endl;
sql_cla.type=ERROR;
return ;
}
else
{
//检验是否为有效文件名
length=temp.length()-1;
while(temp.at(length)==' ')
length--;
temp=temp.substr(0,length+1);
//有效
if(temp.find(' ')==-1)
{
sql_cla.tableName=temp;//置表名
//获取属性名
while(SQL.at(start)==' ')
start++;
end=SQL.find(')',start);
temp=SQL.substr(start,end-start);
start=end+1;
//若无,打印出错信息
if(temp=="")
{
cout<<"syntax error: syntax error for create index statement!"<<endl;
cout<<"\t missing ) !"<<endl;
sql_cla.type=ERROR;
return ;
}
else
{
//检验是否为有效属性名
length=temp.length()-1;
while(temp.at(length)==' ')
length--;
temp=temp.substr(0,length+1);
//有效
if(temp.find(' ')==-1)
{
sql_cla.attrName=temp;
while(SQL.at(start)==' ')
start++;
if(SQL.at(start)!=';'||start!=SQL.length()-1)
{
cout<<"syntax error: syntax error for quit!"<<endl;
sql_cla.type=ERROR;
return ;
}
//返回create index语句的内部形式
else
sql_cla.type=CREATEINDEX;
}
//无效,打印出错信息
else
{
cout<<"error:"<<" "<<temp<<"---is not a valid attribute name!"<<endl;
sql_cla.type=ERROR;
return ;
}
}
}
//无效,打印出错信息
else
{
cout<<"error:"<<" "<<temp<<"---is not a valid table name!"<<endl;
sql_cla.type=ERROR;
return ;
}
}
return ;
}
//验证create index语句是否有效
SQL_CLAUSE create_index(string SQL,int start)
{
string temp;
SQL_CLAUSE sql_cla;
int end;
//获取第三个单词
while(SQL.at(start)==' ')
start++;
end=SQL.find(' ',start);
temp=SQL.substr(start,end-start);
start=end+1;
//若无,打印出错信息
if(temp=="")
{
cout<<"syntax error: syntax error for create index statement!"<<endl;
sql_cla.type=ERROR;
}
else
{
sql_cla.name=temp;
//获取第四个单词
while(SQL.at(start)==' ')
start++;
end=SQL.find(' ',start);
temp=SQL.substr(start,end-start);
start=end+1;
//若无,打印出错信息
if(temp=="")
{
cout<<"syntax error: syntax error for create index statement!"<<endl;
sql_cla.type=ERROR;
}
//若为on,继续验证
else if(temp=="on")
create_index_on(SQL,start,sql_cla);
//若为非法信息,打印非法信息
else
{
cout<<"syntax error:"<<" "<<temp<<"---is not a valid key word!"<<endl;
sql_cla.type=ERROR;
}
}
return sql_cla;
}
/////////////////////////////////////////////////////////////////////////////////////////////
//验证create语句是否有效
void create_clause(string SQL,int start,SQL_CLAUSE &sql_cla)
{
string temp;
int end;
//获取第二个单词
while(SQL.at(start)==' ')
start++;
end=SQL.find(' ',start);
temp=SQL.substr(start,end-start);
start=end+1;
//若无,打印出错信息
if(temp=="")
{
cout<<"syntax error: syntax error for create statement!"<<endl;
sql_cla.type=ERROR;
}
//若为database,继续验证
else if(temp=="database")
sql_cla=create_database(SQL,start);
//若为table,继续验证
else if(temp=="table")
create_table(SQL,start,sql_cla);
//若为index,继续验证
else if(temp=="index")
sql_cla=create_index(SQL,start);
//若为错误信息,打印出错信息
else
{
cout<<"syntax error:"<<" "<<temp<<"---is not a valid key word!"<<endl;
sql_cla.type=ERROR;
}
//返回create语句的内部形式
}
//获取用户输入,并对输入作有效性检查,若正确,返回语句的内部表示形式
void Interpreter(SQL_CLAUSE &sql_cla,string SQL)
{
string temp;
string sql;
int start=0,end;
if (SQL=="") {
//获取用户输入
SQL=read_input();
}
//用户从文件输入一行代码:
else
SQL=SQL;
//获取输入的第一个单词
while(SQL.at(start)==' ')
start++;
end=SQL.find(' ',start);
temp=SQL.substr(start,end-start);
start=end+1;
//若无输入,打印出错信息
if(temp=="")
{
cout<<"syntax error: empty statement!"<<endl;
sql_cla.type=ERROR;
}
//若为create语句
else if(temp=="create")
create_clause(SQL,start,sql_cla);
//若为drop语句
else if(temp=="drop")
sql_cla=drop_clause(SQL,start);
//若为select语句
else if(temp=="select")
ExplainStatement(SQL.substr(0,SQL.length()-2),sql_cla);
//若为insert语句
else if(temp=="insert")
ExplainStatement(SQL.substr(0,SQL.length()-2),sql_cla);
//若为delete语句
else if(temp=="delete")
ExplainStatement(SQL.substr(0,SQL.length()-2),sql_cla);
//若为use语句
else if(temp=="use")
sql_cla=use_clause(SQL,start);
//若为execfile语句
else if(temp=="execfile")
sql_cla=execfile_clause(SQL,start);
//若为quit语句
else if(temp=="quit")
sql_cla=quit_clause(SQL,start);
//获取帮助
else if(temp=="help")
sql_cla.type=HELP;
//若为非法语句
else
{
cout<<"syntax error:"<<" "<<temp<<"---is not a valid key word!"<<endl;
sql_cla.type=ERROR;
}
//返回输入语句的内部形式
}
vector<string> split( string str, string pattern)
{
vector<string> ret;
if(pattern.empty()) return ret;
size_t start=0,index=str.find_first_of(pattern,0);
while(index!=str.npos)
{
if(start!=index)
ret.push_back(str.substr(start,index-start));
start=index+1;
index=str.find_first_of(pattern,start);
}
if(!str.substr(start).empty())
ret.push_back(str.substr(start));
return ret;
}
bool getCond(Condition* c,string s)
{
if(s=="=")
*c=EQUAL;
else if(s=="<")
*c=LESS;
else if(s=="<=")
*c=LESSEQUAL;
else if(s==">")
*c=GREAT;
else if(s==">=")
*c=GREATEQUAL;
else if(s=="!=")
*c=NOTEQUAL;
else return 0;
return 1;
}
bool convert(struct TableHead& tableHead, struct TableAttr* tableAttr,vector<string>& info,char* insert_info)
{
if(tableHead.attrAmount!=info.size())
{
cout << "Convert Failed: wrong size" << endl;
return 0;
}
stringstream ss;
int a=0;
string b;
float c;
string tmp;
for(int i=0;i<tableHead.attrAmount;i++)
{
switch(tableAttr[i].type)
{
case 0:
for(int j=0;j<info.at(i).size();j++)
{
if(info.at(i).at(j)<'0' || info.at(i).at(j)>'9')
{
cout << "attribute's type is not match" << endl;
return 0;
}
}
ss << info.at(i);
ss >> a;
memcpy(insert_info,&a,4);
insert_info = insert_info + 4;
break;
case 1:
tmp = info.at(i);
if((tmp.find("\'")!=0)||(tmp.rfind("\'")!=tmp.size()-1))
{
cout << info.at(i) << endl;
cout << info.at(i).find("\'") << " " << info.at(i).rfind("\'") <<endl;
cout << "attribute's type is not match" << endl;
return 0;
}
for(int k=0;k<tableAttr[i].amount;k++)
{
if(k<info.at(i).size()-2)
insert_info[k] = info.at(i).at(k+1);
else insert_info[k] = 0;
}
insert_info = insert_info + tableAttr[i].amount;
break;
case 2:
for(int j=0;j<info.at(i).size();j++)
{
if((info.at(i).at(j)<'0' || info.at(i).at(j)>'9')&&info.at(i).at(j)!='.')
{
cout << "attribute's type is not match" << endl;
return 0;
}
}
ss << info.at(i);
ss >> c;
memcpy(insert_info,&c,4);
insert_info = insert_info + 4;
break;
default:
cout << "Convert Failed: unknown type.\n";
return 0;
break;
}
ss.clear();
}
return 1;
}
void ExplainStatement(string statement,SQL_CLAUSE &x)
{
x.correct = 1;
vector<string> result=split(statement," ");
if(result.at(0)=="select")
{
x.type = SELECT;
if(result.at(1)=="*")
{
if(result.at(2)=="from")
{
x.name = result.at(3);
if(result.size()>5 && result.size()%4==0)
{
const int CondNum= result.size()/4 - 1;
x.condAmount = CondNum;
for(int i=4;i<result.size();i=i+4)
{
if(result.at(i)=="where"||result.at(i)=="and")
{
x.v[i/4-1].setValueName(result.at(i+1));
x.correct=getCond(x.cond+i/4-1,result.at(i+2));
int a;
string b;
float c;
string s = result.at(i+3);
stringstream ss;
if(s.at(0)=='\''&&s.at(s.size()-1)=='\'')
{
b = s.substr(1,s.size()-2);
x.v[i/4-1].setString(b);
}
else
{
ss << s;
if(s.find('.')!=string::npos)
{
ss >> c;
x.v[i/4-1].setFloat(c);
}
else
{
ss >> a;
x.v[i/4-1].setInt(a);
}
}
}
else x.correct = 0;
}
}
else if(result.size()==4) x.condAmount = 0;
else x.correct = 0;
}
else x.correct = 0;
}
else x.correct = 0;
}
else if(result.at(0)=="delete")
{
x.type = DELETE;
if(result.at(1)=="from")
{
x.name = result.at(2);
if(result.size()>4 && (result.size()+1)%4==0)
{
const int CondNum= result.size()/4;
x.condAmount = CondNum;
for(int i=3;i<result.size();i=i+4)
{
if(result.at(i)=="where"||result.at(i)=="and")
{
x.v[i/4].setValueName(result.at(i+1));
x.correct=getCond(x.cond+i/4,result.at(i+2));
int a;
string b;
float c;
string s = result.at(i+3);
stringstream ss;
if(s.at(0)=='\''&&s.at(s.size()-1)=='\'')
{
b = s.substr(1,s.size()-2);
x.v[i/4].setString(b);
}
else
{
ss << s;
if(s.find('.')!=string::npos)
{
ss >> c;
x.v[i/4].setFloat(c);
}
else
{
ss >> a;
x.v[i/4].setInt(a);
}
}
}
else x.correct = 0;
}
}
else if(result.size()==3) x.condAmount = 0;
else x.correct = 0;
}
else x.correct = 0;
}
else if(result.at(0)=="insert")
{
x.type = INSERT;
if(result.at(1)=="into")
{
x.name = result.at(2);
if(result.size()==4)
{
if(result.at(3).find("values(")==0&&result.at(3).rfind(")")==result.at(3).size()-1)
{
string s = result.at(3).substr(7,result.at(3).size()-8);
x.value = split(s,",");
}
else x.correct = 0;
}
else x.correct = 0;
}
else x.correct = 0;
}
else
{
x.correct = 0;
}
return ;
}
//验证drop database语句是否有效
void drop_database(string SQL,int start, SQL_CLAUSE & sql_cla)
{
int pos1, pos2, pos3;
pos1 = SQL.find_first_not_of(' ', start);
pos2 = SQL.find_first_of(' ', pos1);
pos3 = SQL.find_first_not_of(' ', pos2);
if (pos1 == string::npos || pos2 == string::npos || pos3 == string::npos || SQL.at(pos3) != ';')
{
sql_cla.type = ERROR;
return ;
}
sql_cla.type = DROPDATABASE;
sql_cla.name = SQL.substr(pos1, pos2-pos1);
return ;
}
//验证drop table语句是否有效
void drop_table(string SQL,int start, SQL_CLAUSE & sql_cla)
{
int pos1, pos2, pos3;
pos1 = SQL.find_first_not_of(' ', start);
pos2 = SQL.find_first_of(' ', pos1);
pos3 = SQL.find_first_not_of(' ', pos2);
if (pos1 == string::npos || pos2 == string::npos || pos3 == string::npos || SQL.at(pos3) != ';')
{
sql_cla.type = ERROR;
return ;
}
sql_cla.type = DROPTABLE;
sql_cla.name = SQL.substr(pos1, pos2-pos1);
return ;
}
//验证drop index语句是否有效
void drop_index(string SQL,int start, SQL_CLAUSE & sql_cla)
{
int pos1, pos2, pos3;
pos1 = SQL.find_first_not_of(' ', start);
pos2 = SQL.find_first_of(' ', pos1);
pos3 = SQL.find_first_not_of(' ', pos2);
if (pos1 == string::npos || pos2 == string::npos || pos3 == string::npos || SQL.at(pos3) != ';')
{
sql_cla.type = ERROR;
return ;
}
sql_cla.type = DROPINDEX;
sql_cla.name = SQL.substr(pos1, pos2-pos1);
return ;
}
//验证drop语句是否有效
SQL_CLAUSE drop_clause(string SQL,int start)
{
SQL_CLAUSE ret;
string st;
int pos1, pos2;
while ((pos1 = SQL.find_first_of('\t')) != string::npos)
SQL.replace(pos1, 1, " ");
while (SQL.at(0) == ' ')
SQL.erase(0, 1);
pos1 = SQL.find_first_not_of(' ', 0);
pos2 = SQL.find_first_of(' ', pos1);
if (pos1 == string::npos || pos2 == string::npos || SQL.substr(pos1, pos2-pos1) != "drop")
{
ret.type = ERROR;
return ret;
}
pos1 = SQL.find_first_not_of(' ', pos2);
pos2 = SQL.find_first_of(' ', pos1);
if (pos1 == string::npos || pos2 == string::npos)
{
ret.type = ERROR;
return ret;
}
if (SQL.substr(pos1, pos2-pos1) == "database")
{
drop_database(SQL, pos2, ret);
return ret;
}
if (SQL.substr(pos1, pos2-pos1) == "table")
{
drop_table(SQL, pos2, ret);
return ret;
}
if (SQL.substr(pos1, pos2-pos1) == "index")
{
drop_index(SQL, pos2, ret);
return ret;
}
ret.type = ERROR;
return ret;
}
//验证use语句是否有效
SQL_CLAUSE use_clause(string SQL,int start)
{
SQL_CLAUSE ret;
string st;
int pos1, pos2, pos3;
while ((pos1 = SQL.find_first_of('\t')) != string::npos)
SQL.replace(pos1, 1, " ");
while (SQL.at(0) == ' ')
SQL.erase(0, 1);
pos1 = SQL.find_first_not_of(' ', 0);
pos2 = SQL.find_first_of(' ', pos1);
if (pos1 == string::npos || pos2 == string::npos || SQL.substr(pos1, pos2-pos1) != "use")
{
ret.type = ERROR;
return ret;
}
pos1 = SQL.find_first_not_of(' ', pos2);
pos2 = SQL.find_first_of(' ', pos1);
if (pos1 == string::npos || pos2 == string::npos || SQL.substr(pos1, pos2-pos1) != "database")
{
ret.type = ERROR;
return ret;
}
pos1 = SQL.find_first_not_of(' ', pos2);
pos2 = SQL.find_first_of(' ', pos1);
pos3 = SQL.find_first_not_of(' ', pos2);
if (pos1 == string::npos || pos2 == string::npos || pos3 == string::npos || SQL.at(pos3) != ';')
{
ret.type = ERROR;
return ret;
}
ret.type = USE;
ret.name = SQL.substr(pos1, pos2-pos1);
return ret;
}
//验证execfile语句是否有效
SQL_CLAUSE execfile_clause(string SQL,int start)
{
SQL_CLAUSE ret;
string st;
int pos1, pos2, pos3;
while ((pos1 = SQL.find_first_of('\t')) != string::npos)
SQL.replace(pos1, 1, " ");
while (SQL.at(0) == ' ')
SQL.erase(0, 1);
pos1 = SQL.find_first_not_of(' ', 0);
pos2 = SQL.find_first_of(' ', pos1);
if (pos1 == string::npos || pos2 == string::npos || SQL.substr(pos1, pos2-pos1) != "execfile")
{
ret.type = ERROR;
return ret;
}
pos1 = SQL.find_first_not_of(' ', pos2);
pos2 = SQL.find_first_of(' ', pos1);
pos3 = SQL.find_first_not_of(' ', pos2);
if (pos1 == string::npos || pos2 == string::npos || pos3 == string::npos || SQL.at(pos3) != ';')
{
ret.type = ERROR;
return ret;
}
ret.type = EXECFILE;
ret.name = SQL.substr(pos1, pos2-pos1);
return ret;
}
//验证quit语句是否有效
SQL_CLAUSE quit_clause(string SQL,int start)
{
SQL_CLAUSE ret;
string st;
int pos1, pos2, pos3;
while ((pos1 = SQL.find_first_of('\t')) != string::npos)
SQL.replace(pos1, 1, " ");
while (SQL.at(0) == ' ')
SQL.erase(0, 1);
pos1 = SQL.find_first_not_of(' ', 0);
pos2 = SQL.find_first_of(' ', pos1);
if (pos1 == string::npos || pos2 == string::npos || SQL.substr(pos1, pos2-pos1) != "quit")
{
ret.type = ERROR;
return ret;
}
pos3 = SQL.find_first_not_of(' ', pos2);
if (pos3 == string::npos || SQL.at(pos3) != ';')
{
ret.type = ERROR;