-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.hpp
176 lines (132 loc) · 4.2 KB
/
types.hpp
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
#ifndef TYPES
#define TYPES
#include <map>
#include <string>
#include <vector>
enum class Token {
CREATE,
INSERT,
SELECT,
FROM,
INTO,
TABLE,
ALL_ATTRIBUTES,
IDENTIFIER,
VALUE,
RPAREN,
LPAREN,
VALUES,
TYPE_INT,
TYPE_STRING,
COMMA,
DROP,
WHERE,
EQUAL,
SENTINEL
};
class Statement {
protected:
std::string table_name;
public:
Statement(std::string table_name) : table_name(table_name){};
virtual ~Statement() = default;
virtual void execute() = 0;
};
class CreateStatement : public Statement {
// std::map<std::string, std::string> colname_to_datatype;
std::vector<std::pair<std::string, std::string>> colname_to_datatype;
public:
CreateStatement()
: Statement(""),
colname_to_datatype(std::vector<std::pair<std::string, std::string>>()){};
CreateStatement(std::string table_name,
std::vector<std::pair<std::string, std::string>> colname_to_datatype)
: Statement(table_name), colname_to_datatype(colname_to_datatype){};
~CreateStatement() override{};
void execute() override;
void set_table_name(const std::string &table_name) {
this->table_name = table_name;
}
std::string get_table_name() const { return this->table_name; }
void add_mapping(const std::string &colname, const std::string &datatype) {
colname_to_datatype.push_back(std::make_pair(colname, datatype));
}
std::vector<std::pair<std::string, std::string>> &get_mappings() {
return colname_to_datatype;
}
};
class InsertStatement : public Statement {
std::vector<std::vector<std::string>> tuples;
public:
InsertStatement(std::string table_name,
std::vector<std::vector<std::string>> tuples)
: Statement(table_name), tuples(tuples){};
InsertStatement()
: Statement(""), tuples(std::vector<std::vector<std::string>>()){};
~InsertStatement() override{};
void execute() override;
std::string get_table_name() const { return table_name; }
void set_table_name(const std::string &table_name) {
this->table_name = table_name;
}
void add_tuple(const std::vector<std::string> &tuple) {
tuples.push_back(tuple);
}
std::vector<std::vector<std::string>> &get_tuples() { return tuples; }
};
class SelectStatement : public Statement {
std::vector<std::string> attributes;
bool select_all;
Token where_operator;
std::pair<std::string, std::string> where_filter;
public:
SelectStatement(std::string table_name, std::vector<std::string> attributes)
: Statement(table_name),
attributes(attributes),
select_all(false),
where_operator(Token::SENTINEL),
where_filter(std::make_pair("", "")){};
SelectStatement()
: Statement(""),
attributes(std::vector<std::string>()),
select_all(false),
where_operator(Token::SENTINEL),
where_filter(std::make_pair("", "")){};
~SelectStatement() override{};
void execute() override;
std::string get_table_name() const { return table_name; }
void set_table_name(const std::string &table_name) {
this->table_name = table_name;
}
void add_attribute(const std::string &attribute) {
this->attributes.push_back(attribute);
}
std::vector<std::string> &get_attributes() { return attributes; }
void set_select_all(const bool &select_all) { this->select_all = select_all; }
const bool &get_select_all() const { return select_all; }
void set_where_operator(const Token &where_operator) {
this->where_operator = where_operator;
}
const Token get_where_operator() const noexcept {
return this->where_operator;
}
void set_where_filter(const std::string &attribute,
const std::string &condition) {
this->where_filter = std::make_pair(attribute, condition);
}
std::pair<std::string, std::string> &get_where_filter() noexcept {
return this->where_filter;
}
};
class DropStatement : public Statement {
public:
DropStatement(std::string table_name) : Statement(table_name){};
DropStatement() : Statement(""){};
~DropStatement() override{};
void execute() override;
void set_table_name(const std::string &table_name) {
this->table_name = table_name;
}
const std::string &get_table_name() const { return table_name; }
};
#endif