//已知表名,要获得该表信息 void Table::getTableInfo() { ifstream infile; string filename; filename = table_name + "_tableinfo.txt"; if (!isTableExist()){ //表不存在 throw Table_Index_Error("table", table_name); } infile.open(filename.c_str(), ios::in); if (!infile) throw File_openfail(filename.c_str()); int i = 0; while (!infile.eof()){ infile >> attrs[i].attr_name; infile >> attrs[i].attr_def_type; infile >> attrs[i].attr_type; infile >> attrs[i].attr_id; infile >> attrs[i].attr_len; i++; } attr_num = i - 1; CalcRecordLen(); }
void DatabaseDictionary::removeTable( const Context & context, const String & table_name) { if (!isTableExist(context, table_name)) throw Exception("Table " + name + "." + table_name + " doesn't exist.", ErrorCodes::UNKNOWN_TABLE); auto objects_map = external_dictionaries.getObjectsMap(); deleted_tables.insert(table_name); }
bool Table::dropTable() { string filename; string newTableList = ""; ifstream infile; ofstream outfile; if (!isTableExist()){ //该表不存在 cerr << "The table doesn't exist!" << endl; return false; //删除失败,返回false } //表存在,则删除或更新相关文件 //删除tablelist.txt中表名 infile.open("tablelist.txt", ios::in); //以只读模式打开文件,读取其中信息 if (!infile) throw File_openfail("tablelist.txt"); string tablename; while (!infile.eof()){ getline(infile, tablename); //每行只储存一个表名信息 if (tablename != "" && tablename != table_name){ //过滤掉要删除的表名,保留其余的表名信息,存在newTableList中 newTableList = newTableList + tablename + '\n'; } } outfile.open("tablelist.txt", ios::out); //打开tablelist.txt并清空 if (!outfile) throw File_openfail("tablelist.txt"); outfile << newTableList; //将保留的表名信息存入文件中 outfile.close(); //删除记录表的字段的文件 filename = table_name + "_tableinfo.txt"; remove(filename.c_str()); //移除文件 //删除记录表中所有索引信息的文件 filename = table_name + "_tableindexinfo.txt"; remove(filename.c_str()); //删除记录表中记录的文件 filename = table_name + "_tablereco"; remove(filename.c_str()); //移除文件 return true; }
//建表,更新或创建相关文件 bool Table::createTable() { string filename; ofstream outfile; if (isTableExist()){ //该表在数据库中已存在 cerr << "The table has already been existed!" << endl; return false; //建表失败,返回false } //表不存在,则建立新表 //更新储存表名的文件 outfile.open("tablelist.txt", ios::app); //以追加模式打开 if (!outfile) throw File_openfail("tablelist"); outfile << table_name << endl; //向文件中输出表名 outfile.close(); //新建储存该表的字段信息的文件 filename = table_name + "_tableinfo.txt"; outfile.open(filename.c_str(), ios::out); if (!outfile) throw File_openfail(filename.c_str()); for (int i = 0; i < attr_num; i++){ //将表的字段信息存在文件中 outfile << attrs[i].attr_name << " "; outfile << attrs[i].attr_def_type << " "; outfile << attrs[i].attr_type << " "; outfile << i << " "; outfile << attrs[i].attr_len << endl; } outfile.close(); //新建储存该表的记录信息的文件 filename = table_name + "_tablereco"; outfile.open(filename.c_str(), ios::out); if (!outfile) throw File_openfail(filename.c_str()); outfile.close(); //建立即可,此时不需要插入任何记录 return true; }