-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.h
96 lines (88 loc) · 2.28 KB
/
functions.h
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
#ifndef __FUNCTIONS_H // INCLUDE GUARD, otherwise multiple definitions error
#define __FUNCTIONS_H
#include<iostream>
#include<string>
#include<vector>
#include"keywords.h"
#include"commonf.h"
using namespace std;
//Function PROTOTYPES
void interpret(string);
void listen();
//End of Function PROTOTYPES
/* LISTENER
Listens for SQL commands*/
void listen(){
string command;
cout << "\n[SQL]: ";
getline(cin, command, '\n');
interpret(command);
}
/* INTERPRETER
interprets the meaning and functions of a command*/
void interpret(string cmd){
vector<string> token;
// Extract Tokens
if (match(cmd, "")) return; // Otherwise assertion error
extractTokens(cmd, token);
// Clean Tokens
cleanTokens(token); //to clean the tokens and remove empty strings
vectorToLower(token, 0);
if (match(token[0],"create")){ // match for CREATE
token.erase(token.begin());
vectorToLower(token, 0);
if (match(token[0], "table")){ // match for create -> TABLE
token.erase(token.begin());
createTable(token);
}
else if (match(token[0], "database")){ // match for create -> DATABASE
token.erase(token.begin()+0);
createDatabase(token);
}
else{
cout << endl << "Error: illegal create command" << endl;
}
}
else
if (match(token[0], "use")){ // match for USE
if (useDatabase(token[1]))
cout << endl << "Database selected" << endl;
else
cout << endl << "Database not found" << endl;
}
else
if (match(token[0], "exit")){ // match for EXIT
exit(0);
}
else
if (match(token[0], "insert")){ // match for INSERT
if (match(token[1], "into")){
token.erase(token.begin(), token.begin()+2);
insert(token);
}
}
else
if (match(token[0], "drop")){ // match for DROP
token.erase(token.begin());
vectorToLower(token, 0);
if (match(token[0], "database")){ // match for drop -> DATABASE
dropDatabase(token[1]);
}
else
if (match(token[0], "table")){ // match for drop -> TABLE
dropTable(token[1]);
}
else
cout << endl << "Error: Illegal drop command" << endl;
}
else
if (match(token[0], "print")){
vector<vector<string>> p;
for (unsigned long i = 0; i < 6; ++i){
p.push_back(token);
}
printTable(p);
}
else cout << endl << "No command matched" << endl;
}
#endif