Skip to content

Commit

Permalink
drop table implement
Browse files Browse the repository at this point in the history
  • Loading branch information
JFlanping committed Oct 19, 2023
1 parent 30ee6a2 commit 3e2ebda
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/observer/sql/executor/command_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions src/observer/sql/executor/drop_table_executor.cpp
Original file line number Diff line number Diff line change
@@ -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<int>(stmt->type()));

DropTableStmt *drop_table_stmt = static_cast<DropTableStmt *>(stmt);
const char *table_name = drop_table_stmt->table_name().c_str();
RC rc = session->get_current_db()->drop_table(table_name);
return rc;
}
18 changes: 18 additions & 0 deletions src/observer/sql/executor/drop_table_executor.h
Original file line number Diff line number Diff line change
@@ -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);
};
9 changes: 9 additions & 0 deletions src/observer/sql/stmt/drop_table_stmt.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
31 changes: 31 additions & 0 deletions src/observer/sql/stmt/drop_table_stmt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <string>
#include <vector>

#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_;
};
5 changes: 5 additions & 0 deletions src/observer/sql/stmt/stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
}
Expand Down
24 changes: 24 additions & 0 deletions src/observer/storage/db/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, Table *>::const_iterator iter = opened_tables_.find(table_name);
Expand Down
2 changes: 2 additions & 0 deletions src/observer/storage/db/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 5 additions & 1 deletion src/observer/storage/default/default_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions src/observer/storage/table/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <index_num;i++) {
((BplusTreeIndex*)indexes_[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)
{
// 加载元数据文件
Expand Down
2 changes: 2 additions & 0 deletions src/observer/storage/table/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class Table
int attribute_count,
const AttrInfoSqlNode attributes[]);

RC drop(const char *path, const char *name);

/**
* 打开一个表
* @param meta_file 保存表元数据的文件完整路径
Expand Down

0 comments on commit 3e2ebda

Please sign in to comment.