diff --git a/src/observer/sql/executor/command_executor.cpp b/src/observer/sql/executor/command_executor.cpp index 0d4ca7c..85d623c 100644 --- a/src/observer/sql/executor/command_executor.cpp +++ b/src/observer/sql/executor/command_executor.cpp @@ -17,6 +17,7 @@ See the Mulan PSL v2 for more details. */ #include "sql/stmt/stmt.h" #include "sql/executor/create_index_executor.h" #include "sql/executor/create_table_executor.h" +#include "sql/executor/drop_table_executor.h" #include "sql/executor/desc_table_executor.h" #include "sql/executor/help_executor.h" #include "sql/executor/show_tables_executor.h" @@ -41,6 +42,11 @@ RC CommandExecutor::execute(SQLStageEvent *sql_event) return executor.execute(sql_event); } break; + case StmtType::DROP_TABLE:{ + DropTableExecutor executor; + return executor.execute(sql_event); + } break; + case StmtType::DESC_TABLE: { DescTableExecutor executor; return executor.execute(sql_event); diff --git a/src/observer/sql/executor/drop_table_executor.cpp b/src/observer/sql/executor/drop_table_executor.cpp new file mode 100644 index 0000000..d73ddb7 --- /dev/null +++ b/src/observer/sql/executor/drop_table_executor.cpp @@ -0,0 +1,22 @@ +#include "sql/executor/drop_table_executor.h" + +#include "session/session.h" +#include "common/log/log.h" +#include "storage/table/table.h" +#include "sql/stmt/drop_table_stmt.h" +#include "event/sql_event.h" +#include "event/session_event.h" +#include "storage/db/db.h" + +RC DropTableExecutor::execute(SQLStageEvent *sql_event) +{ + Stmt *stmt = sql_event->stmt(); + Session *session = sql_event->session_event()->session(); + ASSERT(stmt->type() == StmtType::DROP_TABLE, + "drop table executor can not run this command: %d", static_cast(stmt->type())); + + DropTableStmt *drop_table_stmt = static_cast(stmt); + const char *table_name = drop_table_stmt->table_name().c_str(); + RC rc = session->get_current_db()->drop_table(table_name); + return rc; +} \ No newline at end of file diff --git a/src/observer/sql/executor/drop_table_executor.h b/src/observer/sql/executor/drop_table_executor.h new file mode 100644 index 0000000..e8adcaf --- /dev/null +++ b/src/observer/sql/executor/drop_table_executor.h @@ -0,0 +1,18 @@ +#pragma once + +#include "common/rc.h" + +class SQLStageEvent; + +/** + * @brief 删除表的执行器 + * @ingroup Executor + */ +class DropTableExecutor +{ +public: + DropTableExecutor() = default; + virtual ~DropTableExecutor() = default; + + RC execute(SQLStageEvent *sql_event); +}; \ No newline at end of file diff --git a/src/observer/sql/stmt/drop_table_stmt.cpp b/src/observer/sql/stmt/drop_table_stmt.cpp new file mode 100644 index 0000000..41828ef --- /dev/null +++ b/src/observer/sql/stmt/drop_table_stmt.cpp @@ -0,0 +1,9 @@ +#include "sql/stmt/drop_table_stmt.h" +#include "event/sql_debug.h" + +RC DropTableStmt::create(Db *db, const DropTableSqlNode &drop_table, Stmt *&stmt) +{ + stmt = new DropTableStmt(drop_table.relation_name); + sql_debug("drop table statement: table name %s", drop_table.relation_name.c_str()); + return RC::SUCCESS; +} diff --git a/src/observer/sql/stmt/drop_table_stmt.h b/src/observer/sql/stmt/drop_table_stmt.h new file mode 100644 index 0000000..af137b2 --- /dev/null +++ b/src/observer/sql/stmt/drop_table_stmt.h @@ -0,0 +1,31 @@ +#pragma once + +#include +#include + +#include "sql/stmt/stmt.h" + +class Db; + +/** + * @brief 表示删除表的语句 + * @ingroup Statement + * @details + */ +class DropTableStmt : public Stmt +{ +public: + DropTableStmt(const std::string &table_name) + : table_name_(table_name) + {} + virtual ~DropTableStmt() = default; + + StmtType type() const override { return StmtType::DROP_TABLE; } + + const std::string &table_name() const { return table_name_; } + + static RC create(Db *db, const DropTableSqlNode &drop_table, Stmt *&stmt); + +private: + std::string table_name_; +}; \ No newline at end of file diff --git a/src/observer/sql/stmt/stmt.cpp b/src/observer/sql/stmt/stmt.cpp index 7aa421d..2027d9a 100644 --- a/src/observer/sql/stmt/stmt.cpp +++ b/src/observer/sql/stmt/stmt.cpp @@ -18,6 +18,7 @@ See the Mulan PSL v2 for more details. */ #include "sql/stmt/delete_stmt.h" #include "sql/stmt/select_stmt.h" #include "sql/stmt/explain_stmt.h" +#include "sql/stmt/drop_table_stmt.h" #include "sql/stmt/create_index_stmt.h" #include "sql/stmt/create_table_stmt.h" #include "sql/stmt/desc_table_stmt.h" @@ -57,6 +58,10 @@ RC Stmt::create_stmt(Db *db, ParsedSqlNode &sql_node, Stmt *&stmt) return CreateTableStmt::create(db, sql_node.create_table, stmt); } + case SCF_DROP_TABLE:{ + return DropTableStmt::create(db,sql_node.drop_table,stmt); + } + case SCF_DESC_TABLE: { return DescTableStmt::create(db, sql_node.desc_table, stmt); } diff --git a/src/observer/storage/db/db.cpp b/src/observer/storage/db/db.cpp index c5bfab8..0d0ef22 100644 --- a/src/observer/storage/db/db.cpp +++ b/src/observer/storage/db/db.cpp @@ -101,6 +101,30 @@ RC Db::create_table(const char *table_name, int attribute_count, const AttrInfoS return RC::SUCCESS; } +RC Db::drop_table(const char *table_name){ + RC rc = RC::SUCCESS; + // check table_name + Table *table = find_table(table_name); + if (table == nullptr) { + LOG_WARN("%s doesn't exist, please check you input", table_name); + return RC::SCHEMA_TABLE_NOT_EXIST; + } + + // delete index and so on + std::string table_file_path = table_meta_file(path_.c_str(), table_name); + rc = table->drop(table_file_path.c_str(), table_name); + + if (rc != RC::SUCCESS) { + LOG_ERROR("Failed to drop table %s.", table_name); + delete table; + return rc; + } + + opened_tables_.erase(table_name); + LOG_INFO("Drop table success. table name=%s", table_name); + return RC::SUCCESS; +} + Table *Db::find_table(const char *table_name) const { std::unordered_map::const_iterator iter = opened_tables_.find(table_name); diff --git a/src/observer/storage/db/db.h b/src/observer/storage/db/db.h index aa5b4b6..869fb8c 100644 --- a/src/observer/storage/db/db.h +++ b/src/observer/storage/db/db.h @@ -47,6 +47,8 @@ class Db RC create_table(const char *table_name, int attribute_count, const AttrInfoSqlNode *attributes); + RC drop_table(const char *table_name); + Table *find_table(const char *table_name) const; Table *find_table(int32_t table_id) const; diff --git a/src/observer/storage/default/default_handler.cpp b/src/observer/storage/default/default_handler.cpp index 362d199..81db364 100644 --- a/src/observer/storage/default/default_handler.cpp +++ b/src/observer/storage/default/default_handler.cpp @@ -170,7 +170,11 @@ RC DefaultHandler::create_table( RC DefaultHandler::drop_table(const char *dbname, const char *relation_name) { - return RC::UNIMPLENMENT; + Db *db = find_db(dbname); + if(db==nullptr){ + return RC::SCHEMA_DB_NOT_EXIST; + } + return db->drop_table(relation_name); } Db *DefaultHandler::find_db(const char *dbname) const diff --git a/src/observer/storage/table/table.cpp b/src/observer/storage/table/table.cpp index c091bad..fdbb4ed 100644 --- a/src/observer/storage/table/table.cpp +++ b/src/observer/storage/table/table.cpp @@ -126,6 +126,47 @@ RC Table::create(int32_t table_id, return rc; } +RC Table::drop(const char *path, const char *name) +{ + RC rc = sync(); + if(rc!=RC::SUCCESS){ + return rc; + } + + // check + if (common::is_blank(name)) { + LOG_WARN("Name cannot be empty"); + return RC::INVALID_ARGUMENT; + } + LOG_INFO("Begin to drop table %s", name); + + std::string meta_file = table_meta_file(base_dir_.c_str(),name); + if(unlink(meta_file.c_str())!=0){ + LOG_ERROR("Failed to remove meta file"); + return RC::INTERNAL; + } + + std::string data_file = table_data_file(base_dir_.c_str(), name); + if(unlink(data_file.c_str())!=0){ + LOG_ERROR("Failed to remove data file"); + return RC::INTERNAL; + } + + + // delete indexes + const int index_num = table_meta_.index_num(); + for (int i = 0;i close(); + const IndexMeta* index_meta = table_meta_.index(i); + std::string index_file = table_index_file(base_dir_.c_str(),name,index_meta->name()); + if(unlink(index_file.c_str())!=0){ + LOG_ERROR("Failed to remove index,file = %s,errorno = %d",index_file.c_str(),errno); + } + } + + return rc; +} + RC Table::open(const char *meta_file, const char *base_dir) { // 加载元数据文件 diff --git a/src/observer/storage/table/table.h b/src/observer/storage/table/table.h index a5bb99e..44af683 100644 --- a/src/observer/storage/table/table.h +++ b/src/observer/storage/table/table.h @@ -54,6 +54,8 @@ class Table int attribute_count, const AttrInfoSqlNode attributes[]); + RC drop(const char *path, const char *name); + /** * 打开一个表 * @param meta_file 保存表元数据的文件完整路径