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(); }
/*创建数据表 找出要创建数据表的数据库;操作对应文件;利用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; }
void InsertData(Database& db) { Insert insert(&db.GetTable("table1")); insert.SetInt(0, 1); insert.SetString(1, "testString"); insert.Execute(); insert.SetInt(0, 2); insert.SetString(1, "anotherTest"); insert.Execute(); Insert insert2(&db.GetTable("table2")); insert2.SetString(0, "stringTest"); insert2.SetInt(1, 3); insert2.Execute(); insert2.SetString(0, "anotherTest"); insert2.SetInt(1, 4); insert2.Execute(); }
void API::Update(SQLUpdate& statement) { if (current_db_.length() == 0) throw NoDatabaseSelectedException(); Database *db = catalog_m_->GetDB(current_db_); if (db == NULL) throw DatabaseNotExistException(); Table *tb = db->GetTable(statement.get_tb_name()); if (tb == NULL) throw TableNotExistException(); RecordManager *rm = new RecordManager(catalog_m_, buffer_m_, current_db_); rm->Update(statement); delete rm; }
void API::CreateIndex(SQLCreateIndex& statement) { cout << "Creating Index: " << statement.get_index_name() << endl; if (current_db_.length() == 0) throw NoDatabaseSelectedException(); Database *db = catalog_m_->GetDB(current_db_); if (db->GetTable(statement.get_tb_name()) == NULL) throw TableNotExistException(); if (db->CheckIfIndexExists(statement.get_index_name()) ) throw IndexAlreadyExistsException(); IndexManager *im = new IndexManager(catalog_m_, buffer_m_, current_db_); im->CreateIndex(statement); delete im; }
/*删除数据表*/ void API::DropTable(SQLDropTable& sql_statement) { cout << "Droping table: " << 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(); } Table *tb = db->GetTable(sql_statement.get_table_name()); if (tb == NULL)/*判断该数据库是否存在数据表*/ { throw TableNotExistException(); } string file_name(path_ + current_database_ + "/" + sql_statement.get_table_name() + ".records");/*获取该数据表的文件地址*/ if (!boost::filesystem::exists(file_name))/*【file system】利用boost库判断该文件是否存在*/ { cout << sql_statement.get_table_name() +" 数据表文件不存在。" << endl; } else { boost::filesystem::remove(file_name);/*【file system】利用boost库删除该文件*/ cout << sql_statement.get_table_name() + "数据表文件已删除。" << endl; } cout << "删除数据表下索引文件。" << endl; for (unsigned int i = 0; i < tb->GetIndexNum(); i++) { string file_name(path_ + current_database_ + "/" + tb->GetIndex(i)->get_name() + ".index");/*获取该数据表的文件地址*/ if (!boost::filesystem::exists(file_name))/*【file system】利用boost库判断该文件是否存在*/ { cout << "索引文件不存在。" << endl; } else { boost::filesystem::remove(file_name); cout << "索引文件已删除" << endl; } } db->DropTable(sql_statement);/*【catalog manager】在目录中删除该数据表*/ cout << "目录文件已写入。" << endl; catalog_manager_->WriteArchiveFile();/*【catalog manager】在catalog中写文档*/ //cout << "删除数据表。" << endl; }
/*更新数据*/ void API::Update(SQLUpdate& sql_statement) { if (current_database_.length() == 0)/*判断当前数据库是否选定*/ { throw NoDatabaseSelectedException(); } Database *db = catalog_manager_->GetDB(current_database_); if (db == NULL)/*判断当前数据库是否存在*/ { throw DatabaseNotExistException(); } Table *tb = db->GetTable(sql_statement.get_tb_name()); if (tb == NULL)/*判断数据表是否存在是否存在*/ { throw TableNotExistException(); } RecordManager *rm = new RecordManager(catalog_manager_, buffer_manager_, current_database_); rm->Update(sql_statement); delete rm; //cout << "更新数据" << endl; }
void API::DropTable(SQLDropTable& statement) { cout << "Droping 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(); Table *tb = db->GetTable(statement.get_tb_name()); if (tb == NULL) throw TableNotExistException(); string file_name(path_ + current_db_ + "/" + statement.get_tb_name() + ".records"); if (!boost::filesystem::exists(file_name)) cout << "Table file doesn't exists!" << endl; else { boost::filesystem::remove(file_name); cout << "Table file deleted!" << endl; } cout << "Removing Index files!" << endl; for (unsigned int i = 0; i < tb->GetIndexNum(); i++) { string file_name(path_ + current_db_ + "/" + tb->GetIndex(i)->get_name() + ".index"); if (!boost::filesystem::exists(file_name)) cout << "Index file doesn't exist!" << endl; else { boost::filesystem::remove(file_name); cout << "Index file removed!" << endl; } } db->DropTable(statement); cout << "Catalog written!" << endl; catalog_m_->WriteArchiveFile(); }