-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete.cpp
More file actions
534 lines (452 loc) · 11.6 KB
/
delete.cpp
File metadata and controls
534 lines (452 loc) · 11.6 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
#include <iostream>
#include <math.h> // isinf(), isnan()
#include "delete.h"
using namespace std;
//#define DEBUG
Token lookahead("", "", 0);
vector<Token> tokens;
int tokenIndex;
bool hasError;
Table *currentTable = NULL; // operate table
void deleteParse(Database& db, vector<Token> _tokens) {
tokenIndex = 0;
hasError = false;
currentTable = NULL;
lookahead = _tokens.front();
tokens = _tokens;
std::cout << std::boolalpha;
// predictive
matchName("delete");
matchName("from");
if (checkType("id")) {
std::string tableName = lookahead.getName();
if (db.isTableExist(tableName)) {
currentTable = &db.getTableRef(tableName);
nextToken();
} else {
hasError = true;
std::cerr << "Table doesn't exist." << std::endl;
return;
}
} else {
std::cerr << "ID error." << std::endl;
hasError = true;
}
std::vector<int> rowIndex = where_clause();
if (hasError) {
std::cerr << "Expression error." << std::endl;
return;
}
matchName(";");
if (!hasError) {
//deleteOperation(db, id, number, rop);
deleteOperation(db, rowIndex);
cout << "Delete operation success!" << endl;
} else {
cout << "Syntax error!" << endl;
}
}
std::vector<int> where_clause() {
#ifdef DEBUG
/**/std::cout << "where_clause()" << std::endl;
#endif
std::vector<bool> mask;
if (checkName("where")) {
// if there's a "where" token
nextToken(); // consume this token
mask = disjunct(); // compute disjunct and return
} else {
// else, consume no token and returns all true
mask = genVector(true);
}
std::vector<int> choose;
for (int i = mask.size() - 1; i >= 0; i--) {
if (mask[i] == true) choose.push_back(i);
}
return choose;
}
std::vector<bool> disjunct() {
std::vector<bool> result = genVector(false);
std::string op = "||";
bool stopFlag = false;
#ifdef DEBUG
/**/std::cout << "disjunct()" << std::endl;
#endif
for (; !stopFlag && !hasError;) {
// calculate the result
if (checkType("id") || checkType("num") || checkName("(")
|| checkName("!") || checkName("+") || checkName("-")) {
compare(result, op, conjunct());
} else {
hasError = true;
}
// if there's any "||" token, continue the calculation
if (checkName("||")) {
op = lookahead.getName();
nextToken();
} else if (checkName(")") || checkName(";")) {
stopFlag = true;
} else {
hasError = true;
}
}
#ifdef DEBUG
/**/std::cout << "~disjunct() " << hasError << std::endl;
#endif
return result;
}
std::vector<bool> conjunct() {
std::vector<bool> result = genVector(true);
std::string op = "&&";
bool stopFlag = false;
#ifdef DEBUG
/**/std::cout << "conjunct()" << std::endl;
#endif
for (; !stopFlag && !hasError;) {
// calculate the result
if (checkType("id") || checkType("num") || checkName("(")
|| checkName("!") || checkName("+") || checkName("-")) {
compare(result, op, _bool());
} else {
hasError = true;
}
// if there's any "&&" token, continue the calculation
if (checkName("&&")) {
op = lookahead.getName();
nextToken();
} else if (checkName("||") || checkName(")") || checkName(";")) {
stopFlag = true;
} else {
hasError = true;
}
}
#ifdef DEBUG
/**/std::cout << "~conjunct() " << hasError << std::endl;
#endif
return result;
}
std::vector<bool> _bool() {
std::vector<bool> result;
#ifdef DEBUG
/**/std::cout << "_bool()" << std::endl;
#endif
if (checkType("id") || checkType("num") || checkName("+")
|| checkName("-")) {
// rule: _bool -> comp
result = comp();
} else if (checkName("(")) {
// rule: _bool -> ( disjunct )
nextToken();
result = disjunct();
matchName(")");
} else if (checkName("!")) {
// rule: _bool -> ! bool
nextToken();
compare(result, "!", _bool());
} else {
hasError = true;
}
#ifdef DEBUG
/**/std::cout << "~_bool() " << hasError << std::endl;
#endif
return result;
}
std::vector<bool> comp() {
#ifdef DEBUG
/**/std::cout << "comp()" << std::endl;
#endif
std::vector<double> lhs = expr();
std::string op;
if (checkType("rop")) {
op = lookahead.getName();
nextToken();
#ifdef DEBUG
/**/std::cout << "rop()" << std::endl;
#endif
} else {
hasError = true;
}
std::vector<double> rhs = expr();
#ifdef DEBUG
/**/std::cout << "~comp() " << hasError << std::endl;
#endif
return compare(lhs, op, rhs);
}
std::vector<double> expr() {
std::vector<double> result = genVector(0.0);
std::string op = "+";
bool stopFlag = false;
#ifdef DEBUG
/**/std::cout << "expr()" << std::endl;
#endif
for (; !stopFlag && !hasError;) {
// calculate the result
if (checkType("id") || checkType("num") || checkName("+")
|| checkName("-")) {
operation(result, op, term());
} else {
hasError = true;
}
// if there's any "+" or "-" token, continue the calculation
if (checkName("+") || checkName("-")) {
op = lookahead.getName();
nextToken();
} else if (checkType("rop") || checkName("&&") || checkName("||")
|| checkName(")") || checkName(";")) {
stopFlag = true;
} else {
hasError = true;
}
}
#ifdef DEBUG
/**/std::cout << "~expr() " << hasError << std::endl;
#endif
return result;
}
std::vector<double> term() {
std::vector<double> result = genVector(1.0);
std::string op = "*";
bool stopFlag = false;
#ifdef DEBUG
/**/std::cout << "term()" << std::endl;
#endif
for (; !stopFlag && !hasError;) {
// calculate the result
if (checkType("id") || checkType("num") || checkName("+")
|| checkName("-")) {
operation(result, op, unary());
} else {
hasError = true;
}
// if there's any "*" or "/" token, continue the calculation
if (checkName("*") || checkName("/")) {
op = lookahead.getName();
nextToken();
} else if (checkType("rop") || checkName("&&") || checkName("||")
|| checkName("+") || checkName("-") || checkName(")")
|| checkName(";")) {
stopFlag = true;
} else {
hasError = true;
}
}
#ifdef DEBUG
/**/std::cout << "~term() " << hasError << std::endl;
#endif
return result;
}
std::vector<double> unary() {
std::vector<double> result;
#ifdef DEBUG
/**/std::cout << "unary()" << std::endl;
#endif
if (checkName("+") || checkName("-")) {
result = genVector(0.0);
std::string op = lookahead.getName();
nextToken();
operation(result, op, unary());
} else if (checkType("id")) {
result = genVector(lookahead.getName());
nextToken();
} else if (checkType("num")) {
result = genVector((double)lookahead.getNum());
nextToken();
} else {
hasError = true;
}
#ifdef DEBUG
/**/std::cout << "~unary() " << hasError << std::endl;
#endif
return result;
}
// generate a vector with same values
std::vector<bool> genVector(bool value) {
std::vector<bool> vect;
if (currentTable != NULL) {
for (int i = 0; i < currentTable->getNumberOfRows(); i++) {
vect.push_back(value);
}
}
return vect;
}
std::vector<double> genVector(double value) {
std::vector<double> vect;
if (currentTable != NULL) {
for (int i = 0; i < currentTable->getNumberOfRows(); i++) {
vect.push_back(value);
}
}
return vect;
}
// get column id and return a vector
std::vector<double> genVector(std::string columnName) {
std::vector<double> vect;
if (currentTable != NULL) {
if (currentTable->isColumnExist(columnName)) {
Column &column = currentTable->getColumnReference(columnName);
for (int i = 0; i < currentTable->getNumberOfRows(); i++) {
vect.push_back(column.getValue(i));
}
} else {
hasError = true;
std::cerr << "Column [" << columnName;
std::cerr << "] doesn't exist." << std::endl;
return vect;
}
}
return vect;
}
// accumulative logical operation
void compare(std::vector<bool> &result, std::string op,
std::vector<bool> value) {
#ifdef DEBUG
/**/std::cout << "compare() op: " << op << std::endl;
/**/std::cout << " result: " << result.size() << std::endl;
/**/std::cout << " value: " << value.size() <<std::endl;
#endif
if (op == "!") {
result.clear();
for (int i = 0; i < value.size(); i++) {
result.push_back(!value[i]);
}
} else if (result.size() != value.size()) {
hasError = true;
} else {
for (int i = 0; i < result.size(); i++) {
if (op == "&&") {
result[i] = result[i] && value[i];
} else if (op == "||") {
result[i] = result[i] || value[i];
} else {
#ifdef DEBUG
/**/std::cout << " hasError!" <<std::endl;
#endif
hasError = true;
break;
}
}
}
}
// relation operation
std::vector<bool> compare(std::vector<double> lhs,
std::string op, std::vector<double> rhs) {
std::vector<bool> result;
#ifdef DEBUG
/**/std::cout << "compare() op: " << op << std::endl;
/**/std::cout << " lhs: " << lhs.size() << std::endl;
/**/std::cout << " rhs: " << rhs.size() <<std::endl;
#endif
if (lhs.size() != rhs.size()) {
hasError = true;
} else {
for (int i = 0; i < lhs.size(); i++) {
if (op == "<") {
result.push_back(lhs[i] < rhs[i]);
} else if (op == ">") {
result.push_back(lhs[i] > rhs[i]);
} else if (op == "==") {
result.push_back(lhs[i] == rhs[i]);
} else if (op == "<>") {
result.push_back(lhs[i] != rhs[i]);
} else if (op == ">=") {
result.push_back(lhs[i] >= rhs[i]);
} else if (op == "<=") {
result.push_back(lhs[i] <= rhs[i]);
} else {
#ifdef DEBUG
/**/std::cout << " hasError!" <<std::endl;
#endif
hasError = true;
break;
}
}
}
return result;
}
// accumulative arithmetic operation
void operation(std::vector<double> &result, std::string op,
std::vector<double> value) {
if (result.size() != value.size()) {
hasError = true;
} else {
// here we provide an int-like result
// such as 8/5 returns 1 but not 1.6
// but we reserved some good characteristic of double
// that dividedByZero error won't crash our program
for (int i = 0; i < result.size(); i++) {
if (op == "+") {
result[i] += value[i];
} else if (op == "-") {
result[i] -= value[i];
} else if (op == "*") {
result[i] *= value[i];
} else if (op == "/") {
result[i] /= value[i];
// need for an int-cast result
if (!isinf(result[i] && !isnan(result[i]))) {
result[i] = (int)result[i];
}
} else {
hasError = true;
break;
}
}
}
}
// just check the type of the current token
bool checkType(std::string type) {
return lookahead.getType() == type;
}
// just check the name of the current token
bool checkName(std::string name) {
std::string lookaheadName = lookahead.getName();
if (checkType("keyword"))
lookaheadName = TransLow(lookaheadName);
return lookaheadName == name;
}
// move to next token
void nextToken() {
lookahead = tokens[tokenIndex + 1];
tokenIndex++;
#ifdef DEBUG
/**/std::cout << " lookahead: ";
/**/std::cout << lookahead.getType() << " ";
/**/std::cout << lookahead.getName() << " ";
/**/std::cout << lookahead.getNum() << std::endl;
#endif
}
// match a token by type and move to the next token
void matchType(string target) {
string str = lookahead.getType();
if (str.compare(target) == 0) {
if (tokenIndex + 1 < tokens.size()) {
nextToken();
}
} else
hasError = true;
}
// match a token by name and move to the next token
void matchName(string target) {
string str = lookahead.getName();
// keyword id case-insensitive
if (lookahead.getType() == "keyword")
str = TransLow(str);
if (str.compare(target) == 0) {
if (tokenIndex + 1 < tokens.size()) {
nextToken();
}
} else
hasError = true;
}
// change higher case to lower case
std::string TransLow(std::string str) {
for (int i = 0; i < str.size(); i++) {
if (str[i] >= 'A' && str[i] <= 'Z')
str[i] = str[i] + 32;
}
return str;
}
void deleteOperation(Database &db, std::vector<int> rowIndex) {
currentTable->deleteRows(rowIndex);
}
#undef DEBUG