Repository navigation
Expand file tree
/
Copy pathcommand_processor.cpp
More file actions
192 lines (146 loc) · 4.65 KB
/
Copy pathcommand_processor.cpp
File metadata and controls
192 lines (146 loc) · 4.65 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
#include "command_processor.h"
#include <fstream>
namespace ColaSQLCommand {
CommandProcessor& CommandProcessor::GetInstance() {
static CommandProcessor instance;
return instance;
}
void CommandProcessor::Start(const std::string& accountName, const std::string& password) {
// TODO: Login
DataProcessor::GetInstance().CreateUser(accountName, password);
DataProcessor::GetInstance().Login(accountName, password);
// TODO: Welcome information
seq.clear();
int ret;
int outputPrompt = true;
while(true) { // 主循环
if(outputPrompt) {
std::cout << GetPrompt();
}
// input
std::string input;
std::getline(std::cin, input);
// char ch = getchar();
// while(ch != '\r' && ch != '\n') {
// input.push_back(ch);
// ch = getchar();
// }
// quit
if(input.length() >= 4 && input.substr(0, 4) == "quit") {
break;
}
if(input.length() >= 3 && input.substr(0, 3) == "run") {
std::cout << std::endl << RunScript(input) << std::endl;
continue;
}
std::string output = Run(input);
if(output == "") {
outputPrompt = true;
} else {
outputPrompt = true;
std::cout << std::endl << output << std::endl;
}
}
}
std::string CommandProcessor::Run(std::string input) {
if(input == "") return "";
// preprocess
Preprocess(input);
// tokenize
int ret = Tokenize(input, seq);
if(ret == -1) { // 语句异常(语句中包含不正常的分号)
seq.clear();
return "Error: Statement error!";
}
if(ret == 1) { // 语句未结束
return "";
}
// parse
std::string output;
output = _parser.Parse(seq);
seq.clear();
return output;
}
std::string CommandProcessor::ComplexSelect(std::string input, std::vector<std::vector<std::any>>& result){
if(input == "") return "";
// preprocess
Preprocess(input);
// tokenize
int ret = Tokenize(input, seq);
if(ret == -1) { // 语句异常(语句中包含不正常的分号)
seq.clear();
return "Error: Statement error!";
}
if(ret == 1) { // 语句未结束
return "";
}
// parse
std::string output;
output = _parser.ParseSelect(seq, result);
seq.clear();
return output;
}
std::string CommandProcessor::RunScript(std::string path) {
if(path.length() <= 4) return "Incomplete statement.";
path = path.substr(3);
path.erase(0, path.find_first_not_of(" "));
path.erase(path.find_last_not_of(" ") + 1);
std::cout << "Opening: " << path << std::endl;
std::ifstream in(path, std::ifstream::in);
if(!in.is_open()) {
return "Failed to open file";
}
std::string result = "";
std::string input;
while(std::getline(in, input)) { // 主循环
if(input.length() >= 3 && input.substr(0, 3) == "run") {
result += RunScript(input) + "\n";
continue;
}
std::string output = Run(input);
if(output != "") {
result += output + "\n";
}
}
return result;
}
CommandProcessor::CommandProcessor() {}
std::string CommandProcessor::GetPrompt() {
return "Co1aSQL > ";
}
// 将input追加进result
// 注意,本函数不会清空result,只会在后面追加!
// 若返回 0,则说明语句已结束
// 若返回 1,则说明语句未结束
// 若返回 -1,则说明语句异常(语句中包含不正常的分号)
int CommandProcessor::Tokenize(std::string input, std::vector<std::string>& result) {
bool haveEnd = false; // 是否以分号结尾
if(input[input.length() - 1] == ';') {
haveEnd = true;
input = input.substr(0, input.length() - 1); // 删去分号
}
input = input + " ";
for(int st = 0, i = 0; i < input.length(); i++) {
if(input[i] == ';') return -1; // ERROR
if(input[i] != ' ' && input[i] != '\t') continue;
if(i - st >= 1) result.push_back(input.substr(st, i - st));
st = i + 1;
}
return haveEnd ? 0 : 1;
}
int CommandProcessor::Preprocess(std::string& str) {
if(str.empty()) return -1;
// 删去前后多余空格 (trim)
str.erase(0, str.find_first_not_of(" "));
str.erase(str.find_last_not_of(" ") + 1);
int del = 'a'-'A';
for(int i = 0; i < str.length(); i++) {
if('A' <= str[i] && str[i] <= 'Z') { // 全小写
str[i] += del;
} else if(str[i] == ',' || str[i] == '(' || str[i] == ')') { // 替换字符
str[i] = ' ';
}
}
return 0;
}
} // ColaSQLCommand