-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
480 lines (478 loc) · 17.7 KB
/
main.cpp
File metadata and controls
480 lines (478 loc) · 17.7 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
#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#include "TableManage.h"
#include "logs.h"
#include "permissions.h"
using namespace std;
int now_permission;
string now_user;
string modifylogic(string logic){
if(logic == "(" || logic == ")"|| logic == "and" || logic == "or" ) return logic;
for(int i = 0; i < logic.size(); i++){
if(logic[i] == '>' || logic[i] == '<'|| logic[i] == '='){
//cout<<logic<<' '<<i<<endl;
logic = logic[i] + logic;
logic[i+1] = ' ';
return logic;
}
}
return "";
}
// sql语句预处理
string sql_processor(string raw_sql){
// 大写转小写
for(auto &i : raw_sql){
i = tolower(i);
if (i > 127) return "error";
}
raw_sql.erase(remove(raw_sql.begin(), raw_sql.end(), '\n'),raw_sql.end()); // 删除换行符
raw_sql.erase(remove(raw_sql.begin(), raw_sql.end(), '\t'),raw_sql.end()); // 删除制表符
cout << "processed" << endl;
return raw_sql;
}
// sql语句解析
string DBname = "info";
bool execute(string sql){
// cout << sql << '\n' << endl;
if(sql.substr(0, 3) == "use"){
if(sql.substr(4, 8) == "database"){
DBname = sql.substr(13, sql.size() - 13);
if(_access(DBname.c_str(),0)){
cout<<"Database not found"<<endl;
DBname="";
log(now_user,"use database error",getTime());
return 1;//设置访问失败,不存在该数据库
}
cout<<"set Database to "<<DBname<<endl;
log(now_user,"use database success",getTime());
return 0;//修改成功
}
}
else if(sql.substr(0, 6) == "create") {
// create user username password permission
if (sql.substr(7, 4) == "user"){
if (!now_permission) {
cout << "permission denied" << endl;
log(now_user,"permission denied",getTime());
return 1;
}
int space_pos[12];
int count = 0;
for (int i = 12; i < sql.size(); i++)
{
if (sql[i] == ' ')
{
space_pos[count] = i;
count++;
}
}
string tokens[3];
tokens[0] = sql.substr(12, space_pos[0] - 12);
for (int i = 0; i < count; i++)
{
tokens[i+1] = sql.substr(space_pos[i] + 1, space_pos[i+1] - space_pos[i] - 1);
}
user temp_user;
temp_user.username = tokens[0];
temp_user.password = tokens[1];
temp_user.permission = tokens[2];
if (permissionQuery(temp_user.username) != -1) {
// 如果已经存在该用户
cout << "error: user already exist" << endl;
log(now_user,"error: user already exist",getTime());
return "error: user already exist";
}
createUser(temp_user);
cout << "create user " << temp_user.username << " succeeded" << endl;
}
// create标准格式: create database [databasename]
if (sql.substr(7, 8) == "database"){
if (!now_permission) {
cout << "permission denied" << endl;
log(now_user,"permission denied",getTime());
return 1;
}
int tem=createDatabase(sql.substr(16, sql.size() - 16));
if(tem){
cout<<"Failed:Database "<<sql.substr(16, sql.size() - 16)<<" already exists"<<endl;
log(now_user, "create database error", getTime());
}
else {
cout<<"Create Database succeeded"<<endl;
log(now_user, "create database succeeded", getTime());
}
return tem;
}
// create标准格式:create table [tablename] {col: type, col: type……}
if (sql.substr(7, 5) == "table"){
if (!now_permission) {
cout << "permission denied" << endl;
log(now_user, "permission denied", getTime());
return 1;
}
if(_access(DBname.c_str(),0)){
cout<<"Invalid Database name:"+DBname<<endl;
log(now_user, "invalid database name", getTime());
return 1;
}
string left_sql = sql.substr(13, sql.size()-13);
int space_pos = 0;
for (; space_pos < left_sql.size()&&left_sql[space_pos]!=' '; space_pos++);
table tbl={left_sql.substr(0, space_pos)};
for(int pos=space_pos+2;;){
int commapos=0,cutpos=0;
for(int i=pos;i<left_sql.size();i++){
if(left_sql[i]==':')cutpos=i;
else if(left_sql[i]==','||left_sql[i]=='}'){commapos=i;break;}
}
if(!commapos||!cutpos)break;
string cname=left_sql.substr(pos,cutpos-pos),ctype=left_sql.substr(cutpos+1,commapos-cutpos-1);
if(ctype.substr(0,3)=="int"){
int t=3;while(ctype[t]!=' '&&t<ctype.size())t++;
if(ctype[t+1]=='0'&&t<ctype.size())tbl.append(setint(cname,0,2));
else tbl.append(setint(cname,1,2));
}
else if(ctype.substr(0,4)=="tiny"){
int t=4;while(ctype[t]!=' '&&t<ctype.size())t++;
if(ctype[t+1]=='0'&&t<ctype.size())tbl.append(setint(cname,0,1));
else tbl.append(setint(cname,1,1));
}
else if(ctype.substr(0,4)=="long"){
int t=4;while(ctype[t]!=' '&&t<ctype.size())t++;
if(ctype[t+1]=='0'&&t<ctype.size())tbl.append(setint(cname,0,3));
else tbl.append(setint(cname,1,3));
}
else if(ctype.substr(0,4)=="date"){
int t=4;while(ctype[t]!=' '&&t<ctype.size())t++;
if(ctype[t+1]=='0'&&t<ctype.size())tbl.append(setdate(cname,0));
else tbl.append(setdate(cname,1));
}
else if(ctype.substr(0,4)=="char"){
int len=0,t=4;
for(;ctype[t]!=' '&&t<ctype.size();t++)len=len*10+ctype[t]-48;
if(!len)len=1;
if(ctype[t+1]=='0'&&t<ctype.size())tbl.append(setstring(cname,0,len));
else tbl.append(setstring(cname,1,len));
}
pos=commapos+1;
}
if(createTable(DBname,tbl)){
cout<<"Table "+tbl.tablename+" already exists"<<endl;
log(now_user, "table already exists", getTime());
return 1;
}
else{
cout<<"Table create succeded"<<endl;
log(now_user, "table create succeded", getTime());
return 0;
}
}
}
else if(sql.substr(0, 12) == "insert into "){
if(_access(DBname.c_str(),0)){
cout<<"Invalid Database name:"+DBname<<endl;
log(now_user, "invalid database name", getTime());
return 1;
}
vector<string>subs;
for(int st=12;;){
while(sql[st]==' '&&st<sql.size())st++;
if(st>=sql.size())break;
if(sql[st]=='('){
int ed=++st;while(sql[ed]!=')'&&ed<sql.size())ed++;
if(ed>=sql.size()){
cout<<"SQL syntax error"<<endl;
return 1;
}
subs.push_back(sql.substr(st,ed-st));
st=ed+1;
continue;
}
int ed=st;
while(sql[ed]!=' '&&sql[ed]!=')'&&ed<sql.size())ed++;
subs.push_back(sql.substr(st,ed-st));
st=ed+1;
}
string tablename=subs.front();
if(_access((DBname+"\\"+tablename+".stc").c_str(),0)){
cout<<"Table "+tablename+" not exist"<<endl;
return 1;
}
subs.erase(subs.begin());
string columns=*subs.begin(),vals=*(subs.begin()+2);strlist values;
for(int stc=0,stv=0;;){
int edc=stc,edv=stv;
while(columns[edc]!=','&&edc<columns.size())edc++;
while(vals[edv]!=','&&edv<vals.size())edv++;
values.append(columns.substr(stc,edc-stc)+' '+vals.substr(stv,edv-stv));
stc=edc+1,stv=edv+1;
if(stc>=columns.size()||stv>=vals.size())break;
}
if(insert(DBname,tablename,values)){
cout<<"Invalid data, please check"<<endl;
return 1;
}
else{
cout<<"Data inserted"<<endl;
log(now_user, "data inserted", getTime());
return 0;
}
}
else if(sql.substr(0, 12) == "delete from "){
// 标准delete语句:delete from [tablename] where [xxx] and ...
if(_access(DBname.c_str(),0)){
cout<<"Invalid Database name:"+DBname<<endl;
return 1;
}
vector<string>subs;
for(int st=12;;){
while(sql[st]==' '&&st<sql.size())st++;
if(st>=sql.size())break;
if(sql[st]=='('){subs.push_back("("),st++;continue;}
if(sql[st]==')'){subs.push_back(")"),st++;continue;}
int ed=st;
while(sql[ed]!=' '&&sql[ed]!=')'&&ed<sql.size())ed++;
subs.push_back(sql.substr(st,ed-st));
st=ed;
}
string tablename=subs.front();
if(_access((DBname+"\\"+tablename+".stc").c_str(),0)){
cout<<"Table "+tablename+" not exist"<<endl;
return 1;
}
subs.erase(subs.begin());
if(!subs.size()){
strlist aint={{""},0};
delet(DBname,tablename,aint);
cout<<"Delete done"<<endl;
log(now_user, "delete done", getTime());
return 0;
}
if(subs.size()==1){
cout<<"SQL syntax error"<<endl;
return 1;
}
*subs.begin()="(";subs.push_back(")");
for(auto it=subs.begin();it!=subs.end();it++)*it=modifylogic(*it);
vector<strlist>lgcs=BreakDown(subs);
for(auto it=lgcs.begin();it!=lgcs.end();it++)delet(DBname,tablename,*it);
cout<<"Delete done"<<endl;
log(now_user, "delete done", getTime());
return 0;
}
else if (sql.substr(0, 4) == "drop"){
if (!now_permission) {
cout << "permission denied" << endl;
return 1;
}
if(_access(DBname.c_str(),0)){
cout<<"Invalid Database name:"+DBname<<endl;
return 1;
}
int st=4;
while(sql[st]==' '&&st<sql.size()) st++;
if(st>=sql.size()){
cout<<"SQL syntax error"<<endl;
return 1;
}
int ed=st;
while(sql[ed]!=' '&&ed<sql.size()) ed++;
string op=sql.substr(st,ed-st);
if(op=="table"){
st=ed;
while(sql[st]==' '&&st<sql.size()) st++;
ed=st;
while(sql[ed]!=' '&&ed<sql.size()) ed++;
string tablename=sql.substr(st,ed-st);
if(dropTable(DBname,tablename)){
cout<<"Table "+tablename+" not exist"<<endl;
return 1;
}
else{
cout<<"Table dropped"<<endl;
return 0;
}
}
else if(op=="database"){
st=ed;while(sql[st]==' '&&st<sql.size())st++;
ed=st;while(sql[ed]!=' '&&ed<sql.size())ed++;
string dbname=sql.substr(st,ed-st);
if(dropDatabase(dbname)){
cout<<"Database "+dbname+" not exist"<<endl;
return 1;
}
else{
cout<<"Database dropped"<<endl;
log(now_user, "database dropped", getTime());
return 0;
}
}
cout<<"SQL syntax error"<<endl;
return 1;
}
else if(sql.substr(0, 6) == "select") {
// 标准的select语句:select [*]/[column] from [tablename] where [conditions]
if(_access(DBname.c_str(),0)){
cout<<"Invalid Database name:"+DBname<<endl;
return 1;
}
vector<string>subs;
for(int st=6;;){
while(sql[st]==' '&&st<sql.size())st++;
if(st>=sql.size())break;
if(sql[st]=='('){subs.push_back("("),st++;continue;}
if(sql[st]==')'){subs.push_back(")"),st++;continue;}
int ed=st;
while(sql[ed]!=' '&&sql[ed]!=')'&&ed<sql.size())ed++;
subs.push_back(sql.substr(st,ed-st));
st=ed;
}
string tablename=*(subs.begin()+2),columns=*subs.begin();
if(_access((DBname+"\\"+tablename+".stc").c_str(),0)){
cout<<"Table "+tablename+" not exist"<<endl;
return 1;
}
table tbl=getTablestruction(DBname,tablename);
set<string>nonslc;
if(columns=="*")goto skip;
for(int i=0;i<tbl.len;i++)nonslc.insert(tbl.cols[i].dataName);
for(int st=0;;){
int ed=st;while(columns[ed]!=','&&ed<columns.size())ed++;
string col=columns.substr(st,ed-st);
if(!nonslc.count(col)){
cout<<"Invalid column name "+col<<endl;
return 1;
}
nonslc.erase(col);
st=ed+1;if(st>columns.size())break;
}
skip:subs.erase(subs.begin());subs.erase(subs.begin());subs.erase(subs.begin());
if(subs.size()==1){
cout<<"SQL syntax error"<<endl;
return 1;
}
for(int i=0;i<tbl.len;i++)if(!nonslc.count(tbl.cols[i].dataName))cout<<tbl.cols[i].dataName+' '<<ends;
cout<<'\n'<<ends;
if(!subs.size()){
strlist aint={{""},0};vector<string>ans;
ans=lookup(DBname,tablename,aint,nonslc);
for(auto it=ans.cbegin();it!=ans.cend();it++) {
cout<<*it<<endl;
log(now_user, *it, getTime());
}
return 0;
}
*subs.begin()="(";subs.push_back(")");
for(auto it=subs.begin();it!=subs.end();it++)*it=modifylogic(*it);
vector<strlist>lgcs=BreakDown(subs);
for(auto it=lgcs.begin();it!=lgcs.end();it++){
vector<string>ans=lookup(DBname,tablename,*it,nonslc);
for(auto it=ans.cbegin();it!=ans.cend();it++){
cout<<*it<<endl;
log(now_user, "*it", getTime());
}
}
return 0;
}
/*else if(sql.substr(0, 6) == "update") {
// 标准的update语句:update [tablename] set [updates] where [conditions]
cout << "update" << endl;
int pos[20];
int count = 0;
for (int i = 7; i < sql.size(); i++)
{
if (sql[i] == ' ')
{
pos[count] = i; // 记录空格的位置
count++;
cout << pos[count-1] << endl;
}
}
string tokens[999];
tokens[0] = sql.substr(7, pos[0] - 7);
for (int i = 0; i < count; i++)
{
tokens[i+1] = sql.substr(pos[i] + 1, pos[i+1] - pos[i]);
cout << tokens[i] << endl;
}
cout << tokens[count] << endl;
string tablename = tokens[0];
}*/
else if(sql.substr(0, 4) == "view") {
int st=4;while(sql[st]==' '&&st<sql.size())st++;
if(st>=sql.size()){
cout<<"SQL syntax error"<<endl;
return 1;
}
int ed=st;while(sql[ed]!=' '&&ed<sql.size())ed++;
string op=sql.substr(st,ed-st);
if(op=="table"){
st=ed;while(sql[st]==' '&&st<sql.size())st++;
ed=st;while(sql[ed]!=' '&&ed<sql.size())ed++;
string tablename=sql.substr(st,ed-st);
table tbl=getTablestruction(DBname,tablename);
if(!tbl.len){
cout<<"Table "+tablename+" not exist"<<endl;
return 1;
}
else{
tbl.print();
return 0;
}
}
else if(op=="database"){
strlist tbln=getTablenames(DBname);
tbln.print();
log(now_user, "view database", getTime());
return 0;
}
cout<<"SQL syntax error"<<endl;
return 1;
}
else
{
cout<<"SQL syntax error"<<endl;
return 1;
}
return 1;
}
int main()
{
string sql_input;
// cin >> sql_input;
// sql_input = "create table test {column1:char20 0,column2:char,column3:char1 1,column4:date 0,column5:long}";
// sql_input = "use database test";
// sql_input = "create database test";
// sql_input = "insert into test (column1,column2,column3,column4) values (value1,value2,value3,value4)";
// insert into test (column1,column2,column3,column4) values (value1,value2,1,2023-03-01)
// sql_input = "delete from test <where column1=value1 and (column2>value2 or column3<value3)>";
// sql_input = "drop database test";
// sql_input = "drop table test";
// sql_input = "select * from test <where column1=value1 and (column2>value2 or column3<value3)>";
// sql_input = "update test set abc=efg where efg=def or opq=xyz and xyz=abc";not done yet
//<> means it's not necessary
string username, password;
cout << "login" << endl;
cin >> username >> password;
if (login(username, password))
{
now_user = username;
log(now_user,"login",getTime());
now_permission = permissionQuery(username);
while (1)
{
fflush(stdin);
getline(cin, sql_input);
if (sql_input == "exit") return 0;
sql_input = sql_processor(sql_input);
execute(sql_input);
}
}
else
{
cout << "wrong username or password" << endl;
}
//system("pause");
}