/*创建数据表 找出要创建数据表的数据库;操作对应文件;利用catalog Manager实现对应数据表的创建*/ void API::CreateTable(SQLCreateTable& sql_statement) { cout << "创建表: " << sql_statement.get_table_name() << endl; if (current_database_.length() == 0) throw NoDatabaseSelectedException();/*判断当前数据库是否选中*/ Database *db = catalog_manager_->GetDB(current_database_); if (db == NULL) throw DatabaseNotExistException();/*【catalog Manager】判断当前数据库是否存在*/ if (db->GetTable(sql_statement.get_table_name()) != NULL) throw TableAlreadyExistsException();/*【catalog Manager】判断当前数据库是否已有该数据表*/ string file_name(path_ + current_database_ + "/" + sql_statement.get_table_name() + ".records"); boost::filesystem::path folder_path(file_name);/*【file System】利用boost库为该table创建文件*/ if (boost::filesystem::exists(folder_path))/*【file System】利用boost库判断该文件地址是否已存在。存在则删除。不存在则继续执行*/ { boost::filesystem::remove_all(folder_path);/*【file System】利用boost库删除该文件地址对应文件*/ cout << "数据表文件已存在,并已删除。" << endl; } ofstream ofs(file_name);/*【file System】创建该文件地址对应文件*/ ofs.close(); cout << "数据表文件已创建。" << endl; db->CreateTable(sql_statement);/*【catalog Manager】在该database中创建该数据表*/ cout << "目录管理已写入" << endl<<endl; //【catalog Manager】在catalog中写文档 catalog_manager_->WriteArchiveFile(); //cout << "在当前数据库中创建数据表" + sql_statement.get_table_name()<< endl; }
Database GetDatabase() { Database db; Field f1("testInt", Field::FieldType::int32, 10); Field f2("testStr", Field::FieldType::fixedchar, 30); /*Table schema1; schema1.AddField(f1); schema1.AddField(f2); db.CreateTable("table1", schema1);*/ CreateTable create(&db); create.SetName("table1"); create.AddField(f1); create.AddField(f2); create.Execute(); Assert::IsTrue(db.HasTable("table1")); Assert::AreEqual(2, db.GetTable("table1").GetNumFields()); Field f3("strTest", Field::FieldType::fixedchar, 30); Field f4("intTest", Field::FieldType::int32, 10); Table schema2; schema2.AddField(f3); schema2.AddField(f4); db.CreateTable("table2", schema2); Assert::IsTrue(db.HasTable("table2")); Assert::AreEqual(2, db.GetTable("table2").GetNumFields()); return db; }
void API::CreateTable(SQLCreateTable& statement) { cout << "Creating table: " << statement.get_tb_name() << endl; if (current_db_.length() == 0) throw NoDatabaseSelectedException(); Database *db = catalog_m_->GetDB(current_db_); if (db == NULL) throw DatabaseNotExistException(); if (db->GetTable(statement.get_tb_name()) != NULL) throw TableAlreadyExistsException(); string file_name(path_ + current_db_ + "/" + statement.get_tb_name() + ".records"); boost::filesystem::path folder_path(file_name); if (boost::filesystem::exists(folder_path)) { boost::filesystem::remove_all(folder_path); cout << "Table file exists and deleted!" << endl; } ofstream ofs(file_name); ofs.close(); cout << "Table file created!" << endl; db->CreateTable(statement); cout << "Catalog written!" << endl; catalog_m_->WriteArchiveFile(); }