使用c++来实现一个简单的文件数据库功能
功能点1:建表
1.创建和表同名的文件。
2.在文件中存储表的信息,包括attribute的name,数量,类型,是否唯一。
3.添加主键信息。
//输入:表名
//输出:void
//功能:建立表文件
//异常:无异常处理(由catalog manager处理)
void createTableFile(std::string table_name) {
table_name = "./database/data/" + table_name;
FILE* f = fopen(table_name.c_str() , "w");
fclose(f);
}
功能点2:插入数据
1.获取终端输入的命令,判断是否符合sql语句的规则,判断insert语句的name,数量,类型是否和建表时的表信息一致。
2.将数据放入文件中。
//输入:表名、一个元组对象
//输出:void
//功能:向对应表内插入一条记录
//异常:由底层处理
//如果元组类型不匹配,抛出tuple_type_conflict异常
//如果主键冲突,抛出primary_key_conflict异常
//如果unique属性冲突,抛出unique_conflict异常
//如果表不存在,抛出table_not_exist异常
void API::insertRecord(std::string table_name , Tuple& tuple)
{
record.insertRecord(table_name, tuple);
return;
}
功能点3:查找数据
1.获取终端输入的命令,判断是否符合sql语句的规则,判断select条件的name,数量,类型是否和建表时的表信息一致。
2.遍历文件,将符合where条件的数据取出并返回。
//输入:表名、Where条件属性名、Where条件值域
//输出:Table类型对象(包含对应的属性元组)
//功能:返回包含所有目标属性满足Where条件的记录的表
//在多条件查询情况下,根据Where下的逻辑条件进行Table的拼接
//异常:由底层处理
//如果表不存在,抛出table_not_exist异常
//如果属性不存在,抛出attribute_not_exist异常
//如果Where条件中的两个数据类型不匹配,抛出data_type_conflict异常
Table API::selectRecord(std::string table_name, std::vector<std::string> target_attr, std::vector<Where> where, char operation)
{
if (target_attr.size() == 0) {
return record.selectRecord(table_name); //遍历文件
} else if (target_attr.size() == 1) {
return record.selectRecord(table_name, target_attr[0], where[0]);
} else {
Table table1 = record.selectRecord(table_name, target_attr[0], where[0]);
Table table2 = record.selectRecord(table_name, target_attr[1], where[1]);
if (operation)
return joinTable(table1, table2, target_attr[0], where[0]);
else
return unionTable(table1, table2, target_attr[0], where[0]);
}
}
//私有函数,用于多条件查询时的and条件合并
Table API::joinTable(Table &table1, Table &table2, std::string target_attr, Where where)
{
Table result_table(table1);
std::vector<Tuple>& result_tuple = result_table.getTuple();
std::vector<Tuple> tuple1 = table1.getTuple();
std::vector<Tuple> tuple2 = table2.getTuple();
int i;
Attribute attr = table1.getAttr();
for (i = 0; i < 32; i++)
if (attr.name[i] == target_attr)
break;
for (int j = 0; j < tuple2.size(); j++)
if (isSatisfied(tuple2[j], i, where))
result_tuple.push_back(tuple2[j]);
std::sort(result_tuple.begin(), result_tuple.end(), sortcmp);
return result_table;
std::back_inserter(result_tuple), calcmp);
}
//私有函数,用于多条件查询时的or条件合并
Table API::unionTable(Table &table1, Table &table2, std::string target_attr, Where where)
{
Table result_table(table1);
std::vector<Tuple>& result_tuple = result_table.getTuple();
std::vector<Tuple> tuple1 = table1.getTuple();
std::vector<Tuple> tuple2 = table2.getTuple();
result_tuple = tuple1;
//std::vector<Tuple>().swap(result_tuple);
int i;
Attribute attr = table1.getAttr();
for (i = 0; i < 32; i++)
if (attr.name[i] == target_attr)
break;
for (int j = 0; j < tuple2.size(); j++)
if (!isSatisfied(tuple2[j], i, where))
result_tuple.push_back(tuple2[j]);
std::sort(result_tuple.begin(), result_tuple.end(), sortcmp);
return result_table;
}
功能点4:删除数据
1.获取终端输入的命令,判断是否符合sql语句的规则,判断delete判断条件的name,数量,类型是否和建表时的表信息一致。
2.遍历文件,将符合where条件的数据标记删除,并返回结果。
//输入:表名、Where条件属性名、Where条件值域
//输出:void
//功能:删除对应条件下的Table内记录(不删除表文件)
//异常:由底层处理
//如果表不存在,抛出table_not_exist异常
//如果属性不存在,抛出attribute_not_exist异常
//如果Where条件中的两个数据类型不匹配,抛出data_type_conflict异常
int API::deleteRecord(std::string table_name , std::string target_attr , Where where)
{
int result;
if (target_attr == "")
result = record.deleteRecord(table_name);
else
result = record.deleteRecord(table_name, target_attr, where);
return result;
}
功能点5:删表
通过表名搜索文件,直接将文件删除,或将文件保存成备份
//输入:表名
//输出:是否删除成功
//功能:在数据库中删除一个表的元信息,及表内所有记录(删除表文件)
//异常:由底层处理
//如果表不存在,抛出table_not_exist异常
bool API::dropTable(std::string table_name)
{
record.dropTableFile(table_name);
catalog.dropTable(table_name);
return true;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)