-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterpreter.cpp
98 lines (89 loc) · 1.88 KB
/
Interpreter.cpp
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
/**
* @file
* @brief Interpreter is a module of miniSQL project.
* @author elf@ZJU
* @version 1.0
*
* transform the SQL into miniSQL inner functions
*/
#include "Interpreter.h"
/**
* @param void
* @exception none
* @return int
*/
int MyClass::fun(){
return 0;
}
/**
* @param in an istream or an ifstream
* @exception all the DB errors
* @return void
*
* Get a command from in(maybe multiple lines) ending with a ';'.
* Handle the SQL one character by one.
* //Transfer it to Interpreter module to handle.
*/
void HandleOneSQL(istream &in){
string sql; ///< the string of a SQL command
string line; ///< the string of a line
bool inQuote = false; ///< in quote symbol
// Get a command
cout << "\nminiSQL> ";
while(getline(in, line)){
//inQuote = CheckLeftQuote(line);
if(in != cin)
cout << line << endl;
sql += ' ' + line;
if(line.find(';') != -1)
break;
cout << " -> ";
}
//---------------------------------------------------------------------
// Handle the SQL command
switch(GetCmdType(sql)){
case ExecFileType:
//string fileName = "test.sql";
ExecFile(string("test.sql"));
break;
case QuitType:
throw ErQuit();
break;
default:
cout << sql << endl;
}
}
/**
* @param sql string &, the sql command
* @return CmdType, the command type enum
*
* determine the command type and return an enum
*/
CmdType GetCmdType(string &sql){
if(sql.find("execfile") != -1){
return ExecFileType;
}
else if(sql.find("quit") != -1){
return QuitType;
}
else
return CreateTableType;
}
/**
* @param fileName string &, the name of the SQL file
* @return void
*
* Execute the SQL commands from the file.
* If existing any exception, stop the execution.
*/
void ExecFile(string &fileName){
ifstream fin(fileName.c_str());
// if unable to open the file, throw an exception
if(!fin){
throw ErSQLFile();
}
while(!fin.eof()){
HandleOneSQL(fin);
}
fin.close();
}