-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
232 lines (221 loc) · 8.93 KB
/
main.cpp
File metadata and controls
232 lines (221 loc) · 8.93 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
220
221
222
223
224
225
226
227
228
229
230
231
232
/*以下三个头文件为自己在项目中创建实现*/
#include "parser/parser.h"
#include "catalog/catalog_manager.h"
#include "record/record_manager.h"
/*以下这些为通过自己平时知识储备得得知的头文件*/
#include <vector>
#include <memory>
#include <iostream>
// #include <string>
// #include <cctype>
/*以下为通过网络搜索和大模型推荐的头文件*/
#include <algorithm>
#include <fstream>
// #include <sstream>
// #include <filesystem>
using namespace std;
// 去除字符串首尾空格
static string trim(string s)
{
s.erase(s.begin(), find_if(s.begin(), s.end(), [](char c)
{ return !isspace(c); }));
s.erase(find_if(s.rbegin(), s.rend(), [](char c)
{ return !isspace(c); })
.base(),
s.end());
return s;
}
// 去除首尾空格和末尾分号
static string clean(string s)
{
s.erase(s.begin(), find_if(s.begin(), s.end(), [](char c)
{ return !isspace(c); }));
s.erase(find_if(s.rbegin(), s.rend(), [](char c)
{ return !isspace(c) && c != ';'; })
.base(),
s.end());
return s;
}
// 主函数 - 数据库系统的入口点
int main()
{
string sql;
cout << "hello, welcome to MiniDB by YGX\n";
cout << "Type 'exit' to quit\n\n";
// 主循环
while (true)
{
cout << "SQL> "; // 提示符
getline(cin, sql); // 读取用户输入的SQL语句
sql = clean(sql);
// 检查退出命令
if (sql == "exit")
break;
// 跳过空输入
if (sql.empty())
continue;
// 解析SQL语句,生成命令对象
auto cmd = Parser::parse(sql);
// 根据命令类型执行相应的操作
if (cmd->type == CommandType::CREATE)
{
// 处理CREATE TABLE命令
auto create = static_cast<CreateCommand *>(cmd.get());
if (CatalogManager::createTable(create->tableName, create->columns))
{
cout << "Table '" << create->tableName << "' created successfully with "
<< create->columns.size() << " columns.\n";
}
else
{
cout << "Failed to create table '" << create->tableName << "'. "
<< "Please check if the table already exists or you have write permissions.\n";
}
}
else if (cmd->type == CommandType::INSERT)
{
// 处理INSERT命令
auto insert = static_cast<InsertCommand *>(cmd.get());
if (RecordManager::insertRecord(insert->tableName, insert->values))
{
cout << "Successfully inserted " << insert->values.size()
<< " values into table '" << insert->tableName << "'.\n";
}
else
{
cout << "Failed to insert data into table '" << insert->tableName << "'. "
<< "Please check if the table exists and the data format is correct.\n";
}
}
else if (cmd->type == CommandType::SELECT)
{
// 处理SELECT命令
auto select = static_cast<SelectCommand *>(cmd.get());
if (select->condition.empty())
{
// 无条件查询:返回所有记录
auto result = RecordManager::selectAll(select->tableName);
if (result.empty())
{
cout << "No records found in table '" << select->tableName << "'.\n";
}
else
{
cout << "Found " << result.size() << " record(s) in table '" << select->tableName << "':\n";
cout << "----------------------------------------\n";
for (const auto &row : result)
{
for (const auto &f : row)
cout << f << "\t";
cout << "\n";
}
cout << "----------------------------------------\n";
}
}
else
{
// 条件查询:根据WHERE条件筛选记录
size_t eq = select->condition.find('=');
string col = trim(select->condition.substr(0, eq));
string val = trim(select->condition.substr(eq + 1));
auto result = RecordManager::selectWhere(select->tableName, col, val);
if (result.empty())
{
cout << "No records found in table '" << select->tableName
<< "' where " << col << " = " << val << ".\n";
}
else
{
cout << "Found " << result.size() << " record(s) in table '" << select->tableName
<< "' where " << col << " = " << val << ":\n";
cout << "----------------------------------------\n";
for (const auto &row : result)
{
for (const auto &f : row)
cout << f << "\t";
cout << "\n";
}
cout << "----------------------------------------\n";
}
}
}
else if (cmd->type == CommandType::DELETE)
{
// 处理DELETE命令
auto del = static_cast<DeleteCommand *>(cmd.get());
size_t eq = del->condition.find('=');
string col = trim(del->condition.substr(0, eq));
string val = trim(del->condition.substr(eq + 1));
int count = RecordManager::deleteWhere(del->tableName, col, val);
if (count > 0)
{
cout << "Successfully deleted " << count << " record(s) from table '"
<< del->tableName << "' where " << col << " = " << val << ".\n";
}
else
{
cout << "No records found in table '" << del->tableName
<< "' where " << col << " = " << val << " to delete.\n";
}
}
else if (cmd->type == CommandType::UPDATE)
{
// 处理UPDATE命令
auto update = static_cast<UpdateCommand *>(cmd.get());
size_t eq = update->condition.find('=');
string whereCol = trim(update->condition.substr(0, eq));
string whereVal = trim(update->condition.substr(eq + 1));
int count = RecordManager::updateWhere(update->tableName, update->setColumn, update->setValue, whereCol, whereVal);
if (count > 0)
{
cout << "Successfully updated " << count << " record(s) in table '"
<< update->tableName << "' where " << whereCol << " = " << whereVal << ".\n";
}
else
{
cout << "No records updated in table '" << update->tableName
<< "' where " << whereCol << " = " << whereVal << ".\n";
}
}
else if (cmd->type == CommandType::DROP)
{
// 处理DROP TABLE命令
auto drop = static_cast<DropCommand *>(cmd.get());
if (CatalogManager::dropTable(drop->tableName))
{
cout << "Table '" << drop->tableName << "' dropped successfully.\n";
}
else
{
cout << "Failed to drop table '" << drop->tableName << "'. Please check if the table exists.\n";
}
}
else if (cmd->type == CommandType::EXPORT)
{
// 处理EXPORT TABLE命令
auto exportCmd = static_cast<ExportTableCommand *>(cmd.get());
if (RecordManager::exportToCSV(exportCmd->tableName, exportCmd->filePath))
{
cout << "Table '" << exportCmd->tableName << "' exported to '" << exportCmd->filePath << "' successfully.\n";
}
else
{
cout << "Failed to export table '" << exportCmd->tableName << "' to '" << exportCmd->filePath << "'. Please check if the table exists and the path is correct.\n";
}
}
else
{
// 未知命令类型,该部分由大模型生成
cout << "Unrecognized SQL command. Supported commands:\n";
cout << " - CREATE TABLE <table_name> (<column_definitions>)\n";
cout << " - DROP TABLE <table_name>\n";
cout << " - INSERT INTO <table_name> VALUES (<values>)\n";
cout << " - SELECT * FROM <table_name> [WHERE <condition>]\n";
cout << " - DELETE FROM <table_name> WHERE <condition>\n";
cout << " - UPDATE <table_name> SET <column> = <value> WHERE <condition>\n";
cout << " - EXPORT TABLE <table_name> TO <file_path>\n";
}
}
cout << "\nThank you for using MiniDB. Goodbye!\n";
return 0;
}