-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommands.hpp
More file actions
278 lines (225 loc) · 7.66 KB
/
Commands.hpp
File metadata and controls
278 lines (225 loc) · 7.66 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
#pragma once
#include <vector>
#include <string>
#include "Command.hpp"
#include "CommonTypes.hpp"
#include "Column.hpp"
#include "Value.hpp"
/*
*
*
* ===== DDL =====
*
*
*/
class CreateCommand : public Command {
private:
std::string table_name_;
std::vector<Column> columns_;
ConstraintList constraints_;
public:
CreateCommand(std::string table_name,
std::vector<Column> columns,
ConstraintList constraints = {}) // constraints empty by default
: Command(CommandType::CREATE),
table_name_(std::move(table_name)),
columns_(std::move(columns)),
constraints_(std::move(constraints)) {}
const std::string& getTableName() const;
const std::vector<Column>& getColumns() const;
const ConstraintList& getConstraints() const;
std::string toString() const override;
};
class AlterCommand : public Command {
public:
enum class AlterType {
ADD,
DROP,
RENAME,
};
private:
std::string table_name_;
AlterType alter_type_;
std::string column_name_;
// fields for specific alter types
// (drop doesnt need any as it takes from column_name_)
std::string new_column_name_; // for rename
Column new_column_; // for adding
public:
// here there will be many constructors for different alters
// for ADD
AlterCommand(std::string table_name, Column new_column) : Command(CommandType::ALTER),
table_name_(std::move(table_name)),
alter_type_(AlterType::ADD),
new_column_(std::move(new_column)) {}
// for DROP
//AlterCommand(std::string table_name, std::string column_name, AlterType type = AlterType::DROP) : Command(CommandType::ALTER),
AlterCommand(std::string table_name, std::string column_name) : Command(CommandType::ALTER),
table_name_(std::move(table_name)),
alter_type_(AlterType::DROP),
column_name_(std::move(column_name)),
new_column_name_(), // for neovim lsp to leave me alone, maybe could use std::optional?
new_column_() {} // same
// for RENAME
AlterCommand(std::string table_name, std::string column_name, std::string new_column_name) : Command(CommandType::ALTER),
table_name_(std::move(table_name)),
alter_type_(AlterType::RENAME),
column_name_(std::move(column_name)),
new_column_name_(std::move(new_column_name)),
new_column_() {}
// rest
const std::string& getTableName() const;
AlterType getAlterType() const;
const std::string& getColumnName() const;
const std::string& getNewColumnName() const;
const Column& getNewColumn() const;
std::string toString() const override;
};
class DropCommand : public Command {
private:
std::string table_name_;
public:
explicit DropCommand(std::string table_name) : Command(CommandType::DROP), table_name_(std::move(table_name)) {}
const std::string& getTableName() const;
std::string toString() const override;
};
/*
*
*
* ===== DML =====
*
*
*/
class InsertCommand : public Command {
private:
std::string table_name_;
std::vector<std::string> column_names_;
std::vector<std::vector<Value>> values_;
public:
InsertCommand(std::string table_name,
std::vector<std::string> column_names,
std::vector<std::vector<Value>> values)
: Command(CommandType::INSERT),
table_name_(std::move(table_name)),
column_names_(std::move(column_names)),
values_(std::move(values)) {}
const std::string& getTableName() const;
const std::vector<std::string>& getColumnNames() const;
const std::vector<std::vector<Value>>& getValues() const;
std::string toString() const override;
};
class UpdateCommand : public Command {
private:
std::string table_name_;
std::unordered_map<std::string, Value> column_values_;
std::string where_clause_; // if where is not specified then all records in table are updated
public:
UpdateCommand(std::string table_name,
std::unordered_map<std::string, Value> column_values,
std::string where_clause = "")
: Command(CommandType::UPDATE),
table_name_(std::move(table_name)),
column_values_(std::move(column_values)),
where_clause_(std::move(where_clause)) {}
const std::string& getTableName() const;
const std::unordered_map<std::string, Value>& getColumnValues() const;
const std::string& getWhereClause() const;
std::string toString() const override;
};
class DeleteCommand : public Command {
private:
std::string table_name_;
std::string where_clause_; // same as where clause in update command
public:
DeleteCommand(std::string table_name, std::string where_clause = "")
: Command(CommandType::DELETE),
table_name_(std::move(table_name)),
where_clause_(std::move(where_clause)) {}
const std::string& getTableName() const;
const std::string& getWhereClause() const;
std::string toString() const override;
};
/*
*
*
* ===== DQL =====
*
*
*/
class SelectCommand : public Command {
private:
std::vector<std::string> column_names_;
std::vector<std::string> table_names_;
std::string where_clause_;
public:
SelectCommand(std::vector<std::string> column_names,
std::vector<std::string> table_names,
std::string where_clause = "")
: Command(CommandType::SELECT),
column_names_(std::move(column_names)),
table_names_(std::move(table_names)),
where_clause_(std::move(where_clause)) {}
const std::vector<std::string>& getColumnNames() const;
const std::vector<std::string>& getTableNames() const;
const std::string& getWhereClause() const;
std::string toString() const override;
};
/*
*
*
* ===== File operations =====
*
*
*/
class SaveCommand : public Command {
private:
std::string filename_;
public:
explicit SaveCommand(std::string filename) : Command(CommandType::SAVE), filename_(std::move(filename)) {}
const std::string& getFilename() const;
std::string toString() const override;
};
// TODO: maybe add the destination table name as a parameter?
class LoadCommand : public Command {
private:
std::string filename_;
public:
explicit LoadCommand(std::string filename) : Command(CommandType::LOAD), filename_(std::move(filename)) {}
const std::string& getFilename() const;
std::string toString() const override;
};
class ShowCommand : public Command {
public:
enum class ShowType {
TABLES,
COLUMNS
};
private:
ShowType show_type_;
std::string table_name_;
public:
explicit ShowCommand(ShowType show_type = ShowType::TABLES) : Command(CommandType::SHOW), show_type_(show_type) {}
ShowCommand(std::string table_name) : Command(CommandType::SHOW),
show_type_(ShowType::COLUMNS),
table_name_(std::move(table_name)) {}
ShowType getShowType() const;
const std::string& getTableName() const;
std::string toString() const override;
};
/*
*
*
* ===== Help Command =====
*
*
*/
class HelpCommand : public Command {
private:
std::string command_name_; // optional specific command to get help for
public:
explicit HelpCommand() : Command(CommandType::HELP), command_name_() {}
explicit HelpCommand(std::string command_name) : Command(CommandType::HELP), command_name_(std::move(command_name)) {}
const std::string& getCommandName() const { return command_name_; }
bool hasSpecificCommand() const { return !command_name_.empty(); }
std::string toString() const override;
};