-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.hpp
More file actions
97 lines (87 loc) · 3.1 KB
/
Parser.hpp
File metadata and controls
97 lines (87 loc) · 3.1 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
#pragma once
#include <unordered_map>
#include "Column.hpp"
#include "Command.hpp"
#include "CommonTypes.hpp"
#include "Value.hpp"
#include "Database.hpp"
class Parser {
private:
// https://stackoverflow.com/a/3114231
std::unordered_map<std::string, void(Parser::*)()> handlers_;
std::string query_;
size_t pos_;
Database& database_;
struct ParseState {
CommandType current_command;
std::vector<std::string> current_columns_names;
std::vector<std::string> current_tables_names;
std::string current_table_name;
std::unordered_map<std::string, Value> current_values;
std::vector<std::vector<Value>> current_value_sets;
std::string where_clause;
std::vector<Column> current_columns_def;
ConstraintList current_constraints;
std::string filename;
std::string help_command;
auto reset () -> void {
current_command = CommandType::UNKNOWN; // by default
current_columns_names.clear();
current_tables_names.clear();
current_table_name.clear();
current_values.clear();
current_value_sets.clear();
where_clause.clear();
current_columns_def.clear();
current_constraints.clear();
filename.clear();
help_command.clear();
}
} state_;
void resetState();
std::string findNextKeyword();
std::string findNextToken();
void skipWhitespace();
bool isKeyword(const std::string& token) const;
// all operations work on state, hence no return values
// also, not sure if this is the correct form, kind of makes sense with out making an overkill
void handleSelect();
void handleFrom();
void handleWhere();
void handleCreate();
void handleTable();
void handleInsert();
void handleInto();
void handleValues();
void handleUpdate();
void handleSet();
void handleDelete();
void handleDrop();
void handleAlter();
void handleShow();
void handleSave();
void handleLoad();
void handleHelp();
std::unique_ptr<Command> buildCommand();
public:
explicit Parser(Database& database) : database_(database) {
handlers_["SELECT"] = &Parser::handleSelect;
handlers_["FROM"] = &Parser::handleFrom;
handlers_["WHERE"] = &Parser::handleWhere;
handlers_["CREATE"] = &Parser::handleCreate;
handlers_["TABLE"] = &Parser::handleTable;
handlers_["INSERT"] = &Parser::handleInsert;
handlers_["INTO"] = &Parser::handleInto;
handlers_["VALUES"] = &Parser::handleValues;
handlers_["UPDATE"] = &Parser::handleUpdate;
handlers_["SET"] = &Parser::handleSet;
handlers_["DELETE"] = &Parser::handleDelete;
handlers_["DROP"] = &Parser::handleDrop;
handlers_["ALTER"] = &Parser::handleAlter;
handlers_["SHOW"] = &Parser::handleShow;
handlers_["SAVE"] = &Parser::handleSave;
handlers_["LOAD"] = &Parser::handleLoad;
handlers_["HELP"] = &Parser::handleHelp;
}
std::unique_ptr<Command> parse(const std::string& query);
};