使用c++来实现一个简单的数据库功能

该文描述了一个用C++编写的简单文件数据库系统,包括创建表、插入记录、按条件查找和删除数据以及删除表的功能。系统对SQL语句进行解析,并对异常如数据类型冲突、主键冲突等进行了处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值