Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -13,6 +13,7 @@ See the Mulan PSL v2 for more details. */
//

#include "sql/executor/command_executor.h"
#include "sql/executor/drop_table_executor.h" // 👈 必须加在文件最上面
#include "common/log/log.h"
#include "event/sql_event.h"
#include "sql/executor/analyze_table_executor.h"
Expand Down Expand Up @@ -63,6 +64,11 @@ RC CommandExecutor::execute(SQLStageEvent *sql_event)
rc = executor.execute(sql_event);
} break;

case StmtType::DROP_TABLE: {
DropTableExecutor executor;
return executor.execute(sql_event);
}

case StmtType::BEGIN: {
TrxBeginExecutor executor;
rc = executor.execute(sql_event);
Expand Down
26 changes: 26 additions & 0 deletions src/observer/sql/executor/drop_table_executor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 原有保留
#include "sql/executor/execute_stage.h"
#include "sql/executor/drop_table_executor.h"
#include "sql/stmt/drop_table_stmt.h"
#include "storage/db/db.h"
#include "session/session.h"
#include "common/sys/rc.h"

// ✅ 修正后 真实存在的头文件
#include "event/sql_event.h" // SQLStageEvent 完整定义就在这里!
#include "event/session_event.h" // SessionEvent 完整定义
#include "sql/stmt/stmt.h" // Stmt 基类完整定义

RC DropTableExecutor::execute(SQLStageEvent *sql_event)
{
// 1. 获取 Stmt 对象
Stmt *stmt = sql_event->stmt();
DropTableStmt *drop_stmt = static_cast<DropTableStmt *>(stmt);

// 2. 获取当前数据库
Session *session = sql_event->session_event()->session();
Db *db = session->get_current_db();

// 3. 调用存储层删除表
return db->drop_table(drop_stmt->table_name().c_str());
}
11 changes: 11 additions & 0 deletions src/observer/sql/executor/drop_table_executor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#include "common/sys/rc.h"

// 只保留前置声明,不包含任何完整头
// class SQLStageEvent;

class DropTableExecutor
{
public:
RC execute(SQLStageEvent *sql_event);
};
10 changes: 10 additions & 0 deletions src/observer/sql/stmt/drop_table_stmt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "sql/stmt/drop_table_stmt.h"
#include "storage/db/db.h"

RC DropTableStmt::create(Db *db, const DropTableSqlNode &drop_table, Stmt *&stmt)
{
// 当前只保存表名,不做额外的存在性检查
// 存在性检查可以在执行阶段做
stmt = new DropTableStmt(drop_table.relation_name);
return RC::SUCCESS;
}
21 changes: 21 additions & 0 deletions src/observer/sql/stmt/drop_table_stmt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "common/lang/string.h"
#include "sql/stmt/stmt.h"

class Db;

class DropTableStmt : public Stmt
{
public:
DropTableStmt(const string &table_name) : table_name_(table_name) {}
virtual ~DropTableStmt() = default;

StmtType type() const override { return StmtType::DROP_TABLE; }
const string &table_name() const { return table_name_; }

static RC create(Db *db, const DropTableSqlNode &drop_table, Stmt *&stmt);

private:
string table_name_;
};
12 changes: 7 additions & 5 deletions src/observer/sql/stmt/stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ See the Mulan PSL v2 for more details. */
#include "sql/stmt/show_tables_stmt.h"
#include "sql/stmt/trx_begin_stmt.h"
#include "sql/stmt/trx_end_stmt.h"
#include "sql/stmt/drop_table_stmt.h"

bool stmt_type_ddl(StmtType type)
{
Expand Down Expand Up @@ -76,7 +77,7 @@ RC Stmt::create_stmt(Db *db, ParsedSqlNode &sql_node, Stmt *&stmt)
return DescTableStmt::create(db, sql_node.desc_table, stmt);
}

case SCF_ANALYZE_TABLE: {
case SCF_ANALYZE_TABLE: {
return AnalyzeTableStmt::create(db, sql_node.analyze_table, stmt);
}

Expand Down Expand Up @@ -113,9 +114,10 @@ RC Stmt::create_stmt(Db *db, ParsedSqlNode &sql_node, Stmt *&stmt)
return CalcStmt::create(sql_node.calc, stmt);
}

default: {
LOG_INFO("Command::type %d doesn't need to create statement.", sql_node.flag);
} break;
case SCF_DROP_TABLE: {
return DropTableStmt::create(db, sql_node.drop_table, stmt);
}

default: LOG_WARN("unknown sql statement type: %d", sql_node.flag); return RC::UNIMPLEMENTED;
}
return RC::UNIMPLEMENTED;
}
57 changes: 52 additions & 5 deletions src/observer/storage/db/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ Db::~Db()
LOG_INFO("Db has been closed: %s", name_.c_str());
}

RC Db::init(const char *name, const char *dbpath, const char *trx_kit_name, const char *log_handler_name, const char *storage_engine)
RC Db::init(const char *name, const char *dbpath, const char *trx_kit_name, const char *log_handler_name,
const char *storage_engine)
{
RC rc = RC::SUCCESS;

Expand All @@ -64,7 +65,7 @@ RC Db::init(const char *name, const char *dbpath, const char *trx_kit_name, cons
}

oceanbase::ObLsmOptions options;
filesystem::path lsm_path = filesystem::path(dbpath) / "lsm";
filesystem::path lsm_path = filesystem::path(dbpath) / "lsm";
filesystem::create_directory(lsm_path);

rc = oceanbase::ObLsm::open(options, lsm_path, &lsm_);
Expand Down Expand Up @@ -150,7 +151,8 @@ RC Db::init(const char *name, const char *dbpath, const char *trx_kit_name, cons
return rc;
}

RC Db::create_table(const char *table_name, span<const AttrInfoSqlNode> attributes, const vector<string>& primary_keys, const StorageFormat storage_format)
RC Db::create_table(const char *table_name, span<const AttrInfoSqlNode> attributes, const vector<string> &primary_keys,
const StorageFormat storage_format)
{
RC rc = RC::SUCCESS;
// check table_name
Expand All @@ -163,8 +165,15 @@ RC Db::create_table(const char *table_name, span<const AttrInfoSqlNode> attribut
string table_file_path = table_meta_file(path_.c_str(), table_name);
Table *table = new Table();
int32_t table_id = next_table_id_++;
rc = table->create(this, table_id, table_file_path.c_str(), table_name, path_.c_str(), attributes, primary_keys, storage_format,
get_storage_engine());
rc = table->create(this,
table_id,
table_file_path.c_str(),
table_name,
path_.c_str(),
attributes,
primary_keys,
storage_format,
get_storage_engine());
if (rc != RC::SUCCESS) {
LOG_ERROR("Failed to create table %s.", table_name);
delete table;
Expand Down Expand Up @@ -412,6 +421,44 @@ RC Db::init_dblwr_buffer()
return RC::SUCCESS;
}

RC Db::drop_table(const char *table_name)
{
RC rc = RC::SUCCESS;

// 1. 检查表是否存在
auto iter = opened_tables_.find(table_name);
if (iter == opened_tables_.end()) {
LOG_WARN("Table not exist. db=%s, table_name=%s", name_.c_str(), table_name);
return RC::SCHEMA_TABLE_NOT_EXIST;
}

Table *table = iter->second;

// 2. 从内存映射中移除
opened_tables_.erase(iter);

// 3. 获取文件路径
string table_meta_path = table_meta_file(path_.c_str(), table_name);
string table_data_path = table_data_file(path_.c_str(), table_name);
string table_lob_path = table_lob_file(path_.c_str(), table_name);

// 4. 删除 Table 对象(释放内存,关闭文件句柄)
delete table;

// 5. 删除磁盘文件
if (filesystem::exists(table_meta_path)) {
filesystem::remove(table_meta_path);
}
if (filesystem::exists(table_data_path)) {
filesystem::remove(table_data_path);
}
if (filesystem::exists(table_lob_path)) {
filesystem::remove(table_lob_path);
}

return rc;
}

LogHandler &Db::log_handler() { return *log_handler_; }
BufferPoolManager &Db::buffer_pool_manager() { return *buffer_pool_manager_; }
TrxKit &Db::trx_kit() { return *trx_kit_; }
2 changes: 2 additions & 0 deletions src/observer/storage/db/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class Db
RC create_table(const char *table_name, span<const AttrInfoSqlNode> attributes, const vector<string> &primary_keys,
const StorageFormat storage_format = StorageFormat::ROW_FORMAT);

RC drop_table(const char *table_name);

/**
* @brief 根据表名查找表
*/
Expand Down
Loading