Repository navigation
Expand file tree
/
Copy pathQueryEditor.cpp
More file actions
161 lines (141 loc) · 5.63 KB
/
Copy pathQueryEditor.cpp
File metadata and controls
161 lines (141 loc) · 5.63 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
#include "QueryEditor.h"
QueryEditor::QueryEditor(shared_ptr<Database> d) : database(d) {}
void QueryEditor::start() {
vector<string> inputQueries = editor();
for (auto inputQuery: inputQueries) {
StringManipulator::removeDuplicateSpaces(inputQuery);
if (inputQuery.empty())
return;
cout << endl << "INPUT QUERY: " << inputQuery << endl;
try {
startQuery(inputQuery);
} catch (exception& e) {
cout << e.what() << endl;
cout << "Press any key to continue..." << endl;
getch();
}
}
}
string QueryEditor::colorSingleQuotations(const string &str) const {
string result;
bool openQuot = false;
for(const char& c : str) {
if(c == '\'') {
openQuot = !openQuot;
if(openQuot)
result += QUOTECOLOR;
else {
result += c + RESETCOLOR;
continue;
}
}
result += c;
}
return result;
}
string QueryEditor::colorDoubleQuotations(const string &str) const {
string result;
bool openQuot = false;
for(const char& c : str) {
if(c == '\"') {
openQuot = !openQuot;
if(openQuot)
result += DQUOTECOLOR;
else {
result += c + RESETCOLOR;
continue;
}
}
result += c;
}
return result;
}
string QueryEditor::colorQuotations(const string &str) const {
string res = str;
res = colorDoubleQuotations(res);
res = colorSingleQuotations(res);
return res;
}
void QueryEditor::printHeaderAndClearConsole() const {
clearConsole();
cout << "\033[47m" << " SQL Query Editor " << "\033[41m" << " X "
<< StringManipulator::instance().RESETCOLOR() << endl;
}
string QueryEditor::colorVectorValuesInString(string str, vector<string> vect, const string &COLOR) const {
regex pattern;
// Sort keywords in descending order of length
std::sort(vect.begin(), vect.end(), [](const string &a, const string &b) {
return a.length() > b.length();
});
vector<string> keywordReplacementStrings;
keywordReplacementStrings.reserve(vect.size());
for (const string ®: vect) {
keywordReplacementStrings.push_back(COLOR + reg + RESETCOLOR);
}
for (int i = 0; i < vect.size(); i++) {
pattern = regex("\\b" + vect[i] + "\\b", regex_constants::icase);
str = regex_replace(str, pattern, keywordReplacementStrings[i]);
}
return str;
}
void QueryEditor::paintingConsole(string &lineWithLineFeed) const {
vector<string> keywords{"SELECT", "FROM", "WHERE", "INSERT", "INTO", "CREATE", "SHOW","TABLES","TABLE", "DROP", "UPDATE" ,"SET", "DELETE", "JOIN","INNER", "AND", "OR", "VALUES","ON"};
lineWithLineFeed = colorVectorValuesInString(lineWithLineFeed, keywords, KEYWORDCOLOR);
if (regex_search(lineWithLineFeed, regex("from|update|into", regex_constants::icase)))
lineWithLineFeed = colorVectorValuesInString(lineWithLineFeed, database->getAllTableNames(), TABLECOLOR);
if (regex_search(lineWithLineFeed, regex("select|where|set", regex_constants::icase)))
lineWithLineFeed = colorVectorValuesInString(lineWithLineFeed, database->getAllHeaders(), COLOMNCOLOR);
lineWithLineFeed = colorQuotations(lineWithLineFeed);
}
vector<string> QueryEditor::editor() const {
printHeaderAndClearConsole();
string sqlQuery;
string line;
string lineWithLineFeed;
int lineCounter = 0;
while (true) {
cout << StringManipulator::instance().GRAYCOLOR() << to_string(++lineCounter)
<< StringManipulator::instance().RESETCOLOR() << " ";
getline(std::cin, line);
string lineWithNum = StringManipulator::instance().GRAYCOLOR() + to_string(lineCounter) +
StringManipulator::instance().RESETCOLOR() + " " + line;
lineWithLineFeed += (lineWithLineFeed.empty() ? lineWithNum : '\n' + lineWithNum);
printHeaderAndClearConsole();
paintingConsole(lineWithLineFeed);
cout << lineWithLineFeed << endl;
if (line.empty())
break;
sqlQuery.append(sqlQuery.empty() ? line : (" " + line));
}
vector<string> sqlQueries = StringManipulator::splitString(sqlQuery, ';');
return sqlQueries;
}
void QueryEditor::startQuery(const string &query) {
std::shared_ptr<Statement> type = nullptr;
if (regex_match(query, regex("^\\s*select.*", regex_constants::icase))) {
type = std::make_shared<Select>(query, database);
} else if (regex_match(query, regex("^\\s*create.*", regex_constants::icase))) {
type = std::make_shared<Create>(query, database);
} else if (regex_match(query, regex("^\\s*drop.*", regex_constants::icase))) {
type = std::make_shared<Drop>(query, database);
} else if(regex_match(query, regex("^\\s*insert.*",regex_constants::icase))) {
type = std::make_shared<Insert>(query, database);
} else if(regex_match(query, regex("^\\s*update.*",regex_constants::icase))) {
type = std::make_shared<Update>(query, database);
} else if(regex_match(query, regex("^\\s*delete.*",regex_constants::icase))) {
type = std::make_shared<Delete>(query, database);
}else if(regex_match(query, regex("^\\s*show\\s+tables\\s*", regex_constants::icase))) {
cout << *database;
return;
}
if (!type)
throw ENoKeywordsException("[SYNTAX_ERROR] No keywords detected. Can't detect query.");
type->execute();
}
void QueryEditor::clearConsole() const {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}