-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmanager.cpp
More file actions
219 lines (187 loc) · 5.45 KB
/
manager.cpp
File metadata and controls
219 lines (187 loc) · 5.45 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include"manager.h"
#include<algorithm>
Manager::Manager():num(0){}
void Manager::CreateDatabase(SQLCreateDatabase& statement){
databases.push_back(DataBase(statement.get_db_name()));
num++;
}
DataBase* Manager::GetDB(){
for(int i=0;i<num;i++)
if(databases[i].name==current_db)
return &(databases[i]);
return NULL;
}
DataBase* Manager::GetDB(string db){
for(int i=0;i<num;i++)
if(databases[i].name==db)
return &(databases[i]);
return NULL;
}
void Manager::CreateTable(SQLCreateTable& statement){
DataBase *db = GetDB();
db->CreateTable(statement);
}
void Manager::ShowDatabases()
{
//cout << "here" << endl;
if (num == 0)
{
cout << "No databases exist now." << endl;
cout << "Use 'create DataBase' command to create a new DataBase." << endl;
return;
}
cout<< "Database"<<endl;
vector<string> output;
for (int i=0; i<num; i++) output.push_back(databases[i].name);
std::sort(output.begin(), output.end());
for (auto &i: output) cout<< i << endl;
}
void Manager::ShowTables()
{
if (current_db.size() == 0) return;
DataBase *db = GetDB();
if (db == NULL) return;
if (db->GetTables().size() == 0)
{
cout << "No table exist now." << endl;
cout << "Use 'create table' command to create a new table." << endl;
return;
}
cout << "Tables_in_" << current_db << endl;
vector<string> output;
auto tables = db->GetTables();
for (auto tb = tables.begin(); tb != tables.end(); tb++)
output.push_back((*tb)->name);
std::sort(output.begin(), output.end());
for (auto &i: output) cout<< i << endl;
}
void Manager::ShowColumns(SQLShowColumns &statement){
if (current_db.size() == 0) return;
DataBase *db = GetDB();
if (db == NULL) return;
if (db->GetTables().size() == 0)
{
cout << "No table exist now." << endl;
cout << "Use 'create table' command to create a new table." << endl;
return;
}
Table *tb=db->getTB(statement.get_tb_name());
/*列出制定表项的各项信息,包括属性、属性类型、主键 信息。*/
tb->show(cout);
}
void Manager::DropDatabase(SQLDropDatabase& statement)
{
//to be finished: delete statement.get_db_name;
for (auto iter = databases.begin(); iter != databases.end(); iter++) {
if (iter->name == statement.get_db_name()) {
databases.erase(iter);
num--;
break;
}
}
if (statement.get_db_name() == current_db){
current_db = "";
}
}
void Manager::DropTable(SQLDropTable& statement)
{
if (current_db.length() == 0) return;
DataBase *db = GetDB();
if (db == NULL) return;
Table *tb = db->getTB(statement.get_tb_name());
if (tb == NULL) return;
db->DropTable(statement.get_tb_name());
}
void Manager::Use(SQLUse& statement)
{
DataBase *db = GetDB(statement.get_db_name());
if (db == NULL) return;
current_db = statement.get_db_name();
}
void Manager::Insert(SQLInsert& statement)
{
if (current_db.length() == 0) return;
DataBase *db = GetDB();
if (db == NULL) return;
string tb_name=statement.get_tb_name();
Table *tb=db->getTB(tb_name);
if (!tb->insert(statement.attrNames,statement.vals)) {
cout << "Insertion failed. " << endl;
}
}
void Manager::Select(SQLSelect& statement)
{
if (current_db.length() == 0) return;
Table *tb = GetDB()->getTB(statement.get_tb_name());
if (tb == NULL) return;
//WhereClause c(statement.s,statement.o);
//PrintableTable * res = tb->select(estatement.attrFilter,c);
PrintableTable * res = tb->select(statement.get_expressions(), statement.where_clause,
statement.get_group_by(), statement.get_order_by());
if (!statement.if_load_file()) {
res->print(cout);
} else {
ofstream fout(statement.get_load_file_name());
res->print(fout);
fout.close();
}
delete res;
}
void Manager::SelectUnion(vector<vector<string>> sql_vectors,vector<int>union_type)
{
if (current_db.length() == 0) return;
SQLSelect *statement = new SQLSelect(sql_vectors[0]);
Table *tb = GetDB()->getTB(statement->get_tb_name());
PrintableTable *res = tb->select(statement->get_expressions(), statement->where_clause,
statement->get_group_by(), statement->get_order_by());
for(int i=1;i<sql_vectors.size();i++){
SQLSelect *temp_st = new SQLSelect(sql_vectors[i]);
Table *temp_tb = GetDB()->getTB(temp_st->get_tb_name());
PrintableTable *temp_res = temp_tb->select(temp_st->get_expressions(), temp_st->where_clause,
temp_st->get_group_by(), temp_st->get_order_by());
if(union_type[i]==0)
res->unionTable(*temp_res);
else res->unionAllTable(*temp_res);
}
if (!statement->if_load_file()) {
res->print(cout);
} else {
ofstream fout(statement->get_load_file_name());
res->print(fout);
fout.close();
}
delete statement;
delete res;
}
void Manager::Delete(SQLDelete& statement)
{
if (current_db.length() == 0) return;
Table *tb = GetDB()->getTB(statement.get_tb_name());
if (tb == NULL) return;
// WhereClause c(statement.s,statement.o);
tb->del(statement.whereClause);
}
void Manager::Update(SQLUpdate& statement)
{
if (current_db.length() == 0) return;
DataBase *db = GetDB();
if (db == NULL) return;
Table *tb = db->getTB(statement.get_tb_name());
if (tb == NULL) return;
// WhereClause c(statement.s,statement.o);
if (!tb->update(statement.attrNames,statement.vals,statement.whereClause) ) {
cout << "Updating failed. Error Meesage: " << tb->getErrorMsg() << endl;
}
}
void Manager::Load(SQLLoad& statement)
{
if (current_db.length() == 0) return;
DataBase *db = GetDB();
if (db == NULL) return;
Table *tb = db->getTB(statement.get_tb_name());
if (tb == NULL) return;
for (auto i : statement.vals)
{
tb->insert(statement.attrNames, i);
}
}