Skip to content

Commit 632a0a9

Browse files
committed
增加关闭输出功能
1 parent 1f3311d commit 632a0a9

File tree

5 files changed

+30
-39
lines changed

5 files changed

+30
-39
lines changed

README.md

+2-35
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ peglib: [[https://github.com/yhirose/cpp-peglib]]
88

99
tabulate: [[https://github.com/p-ranav/tabulate]]
1010

11-
12-
注意:对于尚未完成的文件,暂时使用Yusen1997的文件作为替换。在完成后,请将ext或src文件夹内的文件替换。
13-
1411
编译:
1512

1613
```
@@ -20,36 +17,6 @@ cmake ..
2017
make
2118
```
2219

20+
- `execfile`会计算执行文件的时间,注意该时间不包括输出表格的时间。
21+
- 可以添加参数运行:`./minisql without_io`,这时SELECT最终不会输出表格,方便测量时间
2322

24-
PEG Grammer:
25-
26-
```bash
27-
SQL <- SELECT / CREATE / DROP_TABLE / INDEX / DROP_INDEX / INSERT / DELETE / QUIT / EXEC
28-
SELECT <- 'select' '*' 'from' any_name ';' / 'select' '*' 'from' any_name 'where' any_name any_cond (any_logic any_name any_cond)* ';'
29-
DELETE <- 'delete' 'from' any_name ';' / 'delete' 'from' any_name 'where' any_name any_cond (any_logic any_name any_cond)* ';'
30-
CREATE <- 'create' 'table' any_name '(' CREATE_def* 'primary key' '(' any_name ')' ')'';'
31-
CREATE_def <- any_name any_type 'unique' ',' / any_name any_type ','
32-
DROP_TABLE <- 'drop' 'table' any_name ';'
33-
INDEX <- 'create' 'index' any_name 'on' any_name '(' any_name ')' ';'
34-
DROP_INDEX <- 'drop' 'index' any_name ';'
35-
INSERT <- 'insert' 'into' any_name 'values' '(' (any_val ','?)* ')' ';'
36-
QUIT <- 'quit;'
37-
EXEC <- 'execfile' any_name ';'
38-
any_type <- 'int' / 'float' / 'char' '(' any_int ')'
39-
any_name <- < [a-z0-9]+ >
40-
any_cond <- any_op ' ' any_val
41-
any_logic <- w_and / w_or
42-
any_op <- [<=>]*
43-
any_val <- any_string / any_float / any_int
44-
any_string <- < '\'' [a-z0-9A-Z-_]+ '\'' >
45-
any_int <- < '-'?[0-9]+ >
46-
any_float <- < '-'?[0-9]*'.'[0-9]+ >
47-
w_and <- 'and'
48-
w_or <- 'or'
49-
%whitespace <- [ \t]*
50-
```
51-
52-
53-
TODO
54-
55-
- delete from lyy;报错

ext/exception.h

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class input_format_error : public std::exception {
4242
const char* what() const throw() { return "Input format error!"; }
4343
};
4444

45+
class file_not_exist : public std::exception {
46+
const char* what() const throw() { return "File not exist!"; }
47+
};
48+
4549
class exit_command : public std::exception {};
4650

4751
class unique_conflict : public std::exception {

ext/interpreter.h

+3
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,13 @@ class Interpreter {
8282
//功能:根据文件路径读取文件信息,并用于数据库的操作
8383
void EXEC_FILE(const SemanticValues &vs);
8484

85+
void closeIO();
86+
8587
private:
8688
std::map<std::string, std::string> idx2table;
8789
peg::parser parser;
8890
chrono::duration<double> io_timer;
91+
bool IO = true;
8992
};
9093

9194
template <class Type>

src/interpreter.cpp

+11-4
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,12 @@ void Interpreter::EXEC_SELECT(const SemanticValues &vs) {
169169
}
170170
API api;
171171
output_table = api.selectRecord(table_name, target_name, where_select);
172-
chrono::steady_clock::time_point time_start = chrono::steady_clock::now();
173-
output_table.showTable();
174-
chrono::steady_clock::time_point time_end = chrono::steady_clock::now();
175-
io_timer += chrono::duration_cast<chrono::duration<double>>(time_end - time_start);
172+
if (IO) {
173+
chrono::steady_clock::time_point time_start = chrono::steady_clock::now();
174+
output_table.showTable();
175+
chrono::steady_clock::time_point time_end = chrono::steady_clock::now();
176+
io_timer += chrono::duration_cast<chrono::duration<double>>(time_end - time_start);
177+
}
176178
}
177179

178180
void Interpreter::EXEC_CREATE_TABLE(const SemanticValues &vs) {
@@ -257,6 +259,9 @@ void Interpreter::EXEC_FILE(const SemanticValues &vs) {
257259
io_timer = chrono::duration<double>::zero();
258260
string filename = any_cast<string>(vs[0]);
259261
ifstream ifs(filename);
262+
if (!ifs.is_open()) {
263+
throw file_not_exist();
264+
}
260265
int num = 0;
261266
while (getQuery(ifs, 1)) {
262267
++num;
@@ -266,3 +271,5 @@ void Interpreter::EXEC_FILE(const SemanticValues &vs) {
266271
cout << ">>> " << num << " query finish in " << time_used.count() << "s" << endl;
267272
ifs.close();
268273
}
274+
275+
void Interpreter::closeIO() { IO = false; }

src/main.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ BufferManager buffer_manager(MAXFRAMESIZE);
77
Interpreter query;
88

99
int main(int argc, const char* argv[]) {
10+
system("mkdir -p database");
11+
system("mkdir -p ./database/index");
12+
system("mkdir -p ./database/catalog");
13+
system("mkdir -p ./database/data");
14+
system("touch ./database/catalog/catalog_file");
15+
if (argc > 1) {
16+
if (string(argv[1]) == "without_io") {
17+
query.closeIO();
18+
}
19+
}
1020
std::cout << ">>> Welcome to MiniSQL" << std::endl;
1121
try {
1222
while (1) {

0 commit comments

Comments
 (0)