提交 ab89b1d5 编写于 作者: L Longda Feng

Rename parser's result command from Query to Command

上级 3653849b
...@@ -245,4 +245,24 @@ bool is_blank(const char *s) ...@@ -245,4 +245,24 @@ bool is_blank(const char *s)
return true; return true;
} }
/**
* 获取子串
* 从s中提取下标为n1~n2的字符组成一个新字符串,然后返回这个新串的首地址
*
* @param s
* @param n1
* @param n2
* @return
*/
char *substr(const char *s, int n1, int n2)
{
char *sp = (char *)malloc(sizeof(char) * (n2 - n1 + 2));
int i, j = 0;
for (i = n1; i <= n2; i++) {
sp[j++] = s[i];
}
sp[j] = 0;
return sp;
}
} // namespace common } // namespace common
...@@ -112,6 +112,19 @@ bool str_to_val(const std::string &str, T &val, std::ios_base &(*radix)(std::ios ...@@ -112,6 +112,19 @@ bool str_to_val(const std::string &str, T &val, std::ios_base &(*radix)(std::ios
template <class T> template <class T>
void val_to_str(const T &val, std::string &str, std::ios_base &(*radix)(std::ios_base &) = std::dec); void val_to_str(const T &val, std::string &str, std::ios_base &(*radix)(std::ios_base &) = std::dec);
bool is_blank(const char *s);
/**
* 获取子串
* 从s中提取下标为n1~n2的字符组成一个新字符串,然后返回这个新串的首地址
*
* @param s
* @param n1
* @param n2
* @return
*/
char *substr(const char *s, int n1, int n2);
/** /**
* get type's name * get type's name
*/ */
...@@ -152,7 +165,5 @@ std::string get_type_name(const T &val) ...@@ -152,7 +165,5 @@ std::string get_type_name(const T &val)
return sret; return sret;
} }
bool is_blank(const char *s);
} // namespace common } // namespace common
#endif // __COMMON_LANG_STRING_H__ #endif // __COMMON_LANG_STRING_H__
...@@ -21,7 +21,7 @@ See the Mulan PSL v2 for more details. */ ...@@ -21,7 +21,7 @@ See the Mulan PSL v2 for more details. */
class SessionEvent; class SessionEvent;
class Stmt; class Stmt;
class Query; class Command;
class SQLStageEvent : public common::StageEvent class SQLStageEvent : public common::StageEvent
{ {
...@@ -35,20 +35,20 @@ public: ...@@ -35,20 +35,20 @@ public:
} }
const std::string &sql() const { return sql_; } const std::string &sql() const { return sql_; }
const std::unique_ptr<Query> &query() const { return query_; } const std::unique_ptr<Command> &command() const { return command_; }
Stmt *stmt() const { return stmt_; } Stmt *stmt() const { return stmt_; }
std::unique_ptr<PhysicalOperator> &physical_operator() { return operator_; } std::unique_ptr<PhysicalOperator> &physical_operator() { return operator_; }
const std::unique_ptr<PhysicalOperator> &physical_operator() const { return operator_; } const std::unique_ptr<PhysicalOperator> &physical_operator() const { return operator_; }
void set_sql(const char *sql) { sql_ = sql; } void set_sql(const char *sql) { sql_ = sql; }
void set_query(std::unique_ptr<Query> query) { query_ = std::move(query); } void set_command(std::unique_ptr<Command> cmd) { command_ = std::move(cmd); }
void set_stmt(Stmt *stmt) { stmt_ = stmt; } void set_stmt(Stmt *stmt) { stmt_ = stmt; }
void set_operator(std::unique_ptr<PhysicalOperator> oper) { operator_ = std::move(oper); } void set_operator(std::unique_ptr<PhysicalOperator> oper) { operator_ = std::move(oper); }
private: private:
SessionEvent *session_event_ = nullptr; SessionEvent *session_event_ = nullptr;
std::string sql_; std::string sql_;
std::unique_ptr<Query> query_; std::unique_ptr<Command> command_;
Stmt *stmt_ = nullptr; Stmt *stmt_ = nullptr;
std::unique_ptr<PhysicalOperator> operator_; std::unique_ptr<PhysicalOperator> operator_;
}; };
......
...@@ -127,29 +127,30 @@ void ExecuteStage::callback_event(StageEvent *event, CallbackContext *context) ...@@ -127,29 +127,30 @@ void ExecuteStage::callback_event(StageEvent *event, CallbackContext *context)
RC ExecuteStage::handle_request(common::StageEvent *event) RC ExecuteStage::handle_request(common::StageEvent *event)
{ {
SQLStageEvent *sql_event = static_cast<SQLStageEvent *>(event); SQLStageEvent *sql_event = static_cast<SQLStageEvent *>(event);
SessionEvent *session_event = sql_event->session_event();
const std::unique_ptr<PhysicalOperator> &physical_operator = sql_event->physical_operator();
Stmt *stmt = sql_event->stmt();
Session *session = session_event->session();
Query *sql = sql_event->query().get();
const std::unique_ptr<PhysicalOperator> &physical_operator = sql_event->physical_operator();
if (physical_operator != nullptr) { if (physical_operator != nullptr) {
return handle_request_with_physical_operator(sql_event); return handle_request_with_physical_operator(sql_event);
} }
SessionEvent *session_event = sql_event->session_event();
Session *session = session_event->session();
Command *sql = sql_event->command().get();
Stmt *stmt = sql_event->stmt();
if (stmt != nullptr) { if (stmt != nullptr) {
switch (stmt->type()) { switch (stmt->type()) {
case StmtType::INSERT: { case StmtType::INSERT: {
do_insert(sql_event); do_insert(sql_event);
} break; } break;
case StmtType::UPDATE: { case StmtType::UPDATE: {
//do_update((UpdateStmt *)stmt, session_event); // do_update((UpdateStmt *)stmt, session_event);
} break; } break;
case StmtType::DELETE: { case StmtType::DELETE: {
do_delete(sql_event); do_delete(sql_event);
} break; } break;
default: { default: {
LOG_WARN("should not happen. please implenment"); LOG_WARN("should not happen. please implement this type:%d", stmt->type());
} break; } break;
} }
} else { } else {
...@@ -201,23 +202,32 @@ RC ExecuteStage::handle_request(common::StageEvent *event) ...@@ -201,23 +202,32 @@ RC ExecuteStage::handle_request(common::StageEvent *event)
} }
case SCF_ROLLBACK: { case SCF_ROLLBACK: {
Trx *trx = session_event->session()->current_trx(); Trx *trx = session_event->session()->current_trx();
RC rc = trx->rollback(); RC rc = trx->rollback();
session->set_trx_multi_operation_mode(false); session->set_trx_multi_operation_mode(false);
SqlResult *sql_result = new SqlResult; SqlResult *sql_result = new SqlResult;
sql_result->set_return_code(rc); sql_result->set_return_code(rc);
session_event->set_sql_result(sql_result); session_event->set_sql_result(sql_result);
} break; } break;
case SCF_EXIT: { case SCF_EXIT: {
// do nothing // do nothing
SqlResult *sql_result = new SqlResult; SqlResult *sql_result = new SqlResult;
sql_result->set_return_code(RC::SUCCESS); sql_result->set_return_code(RC::SUCCESS);
session_event->set_sql_result(sql_result); session_event->set_sql_result(sql_result);
} break; } break;
default: { default: {
LOG_ERROR("Unsupported command=%d\n", sql->flag); LOG_ERROR("Unsupported command=%d\n", sql->flag);
SqlResult *sql_result = new SqlResult; SqlResult *sql_result = new SqlResult;
sql_result->set_return_code(RC::UNIMPLENMENT); sql_result->set_return_code(RC::UNIMPLENMENT);
sql_result->set_state_string("Unsupported command"); sql_result->set_state_string("Unsupported command");
session_event->set_sql_result(sql_result); session_event->set_sql_result(sql_result);
} }
} }
...@@ -230,6 +240,7 @@ RC ExecuteStage::handle_request_with_physical_operator(SQLStageEvent *sql_event) ...@@ -230,6 +240,7 @@ RC ExecuteStage::handle_request_with_physical_operator(SQLStageEvent *sql_event)
RC rc = RC::SUCCESS; RC rc = RC::SUCCESS;
Stmt *stmt = sql_event->stmt(); Stmt *stmt = sql_event->stmt();
ASSERT(stmt != nullptr, "SQL Statement shouldn't be empty!");
std::unique_ptr<PhysicalOperator> &physical_operator = sql_event->physical_operator(); std::unique_ptr<PhysicalOperator> &physical_operator = sql_event->physical_operator();
ASSERT(physical_operator != nullptr, "physical operator should not be null"); ASSERT(physical_operator != nullptr, "physical operator should not be null");
...@@ -239,6 +250,7 @@ RC ExecuteStage::handle_request_with_physical_operator(SQLStageEvent *sql_event) ...@@ -239,6 +250,7 @@ RC ExecuteStage::handle_request_with_physical_operator(SQLStageEvent *sql_event)
case StmtType::SELECT: { case StmtType::SELECT: {
SelectStmt *select_stmt = static_cast<SelectStmt *>(stmt); SelectStmt *select_stmt = static_cast<SelectStmt *>(stmt);
bool with_table_name = select_stmt->tables().size() > 1; bool with_table_name = select_stmt->tables().size() > 1;
for (const Field &field : select_stmt->query_fields()) { for (const Field &field : select_stmt->query_fields()) {
if (with_table_name) { if (with_table_name) {
schema.append_cell(field.table_name(), field.field_name()); schema.append_cell(field.table_name(), field.field_name());
...@@ -255,9 +267,11 @@ RC ExecuteStage::handle_request_with_physical_operator(SQLStageEvent *sql_event) ...@@ -255,9 +267,11 @@ RC ExecuteStage::handle_request_with_physical_operator(SQLStageEvent *sql_event)
// 只有select返回结果 // 只有select返回结果
} break; } break;
} }
SqlResult *sql_result = new SqlResult; SqlResult *sql_result = new SqlResult;
sql_result->set_tuple_schema(schema); sql_result->set_tuple_schema(schema);
sql_result->set_operator(std::move(physical_operator)); sql_result->set_operator(std::move(physical_operator));
sql_event->session_event()->set_sql_result(sql_result); sql_event->session_event()->set_sql_result(sql_result);
return rc; return rc;
} }
...@@ -275,9 +289,10 @@ void end_trx_if_need(Session *session, Trx *trx, bool all_right) ...@@ -275,9 +289,10 @@ void end_trx_if_need(Session *session, Trx *trx, bool all_right)
void tuple_to_string(std::ostream &os, const Tuple &tuple) void tuple_to_string(std::ostream &os, const Tuple &tuple)
{ {
TupleCell cell;
RC rc = RC::SUCCESS; RC rc = RC::SUCCESS;
bool first_field = true; bool first_field = true;
TupleCell cell;
for (int i = 0; i < tuple.cell_num(); i++) { for (int i = 0; i < tuple.cell_num(); i++) {
rc = tuple.cell_at(i, cell); rc = tuple.cell_at(i, cell);
if (rc != RC::SUCCESS) { if (rc != RC::SUCCESS) {
...@@ -293,6 +308,7 @@ void tuple_to_string(std::ostream &os, const Tuple &tuple) ...@@ -293,6 +308,7 @@ void tuple_to_string(std::ostream &os, const Tuple &tuple)
cell.to_string(os); cell.to_string(os);
} }
} }
#if 0 #if 0
IndexScanOperator *try_to_create_index_scan_operator(FilterStmt *filter_stmt) IndexScanOperator *try_to_create_index_scan_operator(FilterStmt *filter_stmt)
{ {
...@@ -319,6 +335,7 @@ IndexScanOperator *try_to_create_index_scan_operator(FilterStmt *filter_stmt) ...@@ -319,6 +335,7 @@ IndexScanOperator *try_to_create_index_scan_operator(FilterStmt *filter_stmt)
} else { } else {
continue; continue;
} }
FieldExpr &left_field_expr = *(FieldExpr *)left; FieldExpr &left_field_expr = *(FieldExpr *)left;
const Field &field = left_field_expr.field(); const Field &field = left_field_expr.field();
const Table *table = field.table(); const Table *table = field.table();
...@@ -355,7 +372,6 @@ IndexScanOperator *try_to_create_index_scan_operator(FilterStmt *filter_stmt) ...@@ -355,7 +372,6 @@ IndexScanOperator *try_to_create_index_scan_operator(FilterStmt *filter_stmt)
} }
} }
FieldExpr &left_field_expr = *(FieldExpr *)left; FieldExpr &left_field_expr = *(FieldExpr *)left;
const Field &field = left_field_expr.field(); const Field &field = left_field_expr.field();
const Table *table = field.table(); const Table *table = field.table();
...@@ -423,10 +439,12 @@ IndexScanOperator *try_to_create_index_scan_operator(FilterStmt *filter_stmt) ...@@ -423,10 +439,12 @@ IndexScanOperator *try_to_create_index_scan_operator(FilterStmt *filter_stmt)
RC ExecuteStage::do_select(SQLStageEvent *sql_event) RC ExecuteStage::do_select(SQLStageEvent *sql_event)
{ {
#if 0 #if 0
SelectStmt *select_stmt = (SelectStmt *)(sql_event->stmt());
SessionEvent *session_event = sql_event->session_event();
RC rc = RC::SUCCESS; RC rc = RC::SUCCESS;
SessionEvent *session_event = sql_event->session_event();
SelectStmt *select_stmt = (SelectStmt *)(sql_event->stmt());
if (select_stmt->tables().size() != 1) { if (select_stmt->tables().size() != 1) {
LOG_WARN("select more than 1 tables is not supported"); LOG_WARN("select more than 1 tables is not supported");
rc = RC::UNIMPLENMENT; rc = RC::UNIMPLENMENT;
...@@ -438,17 +456,19 @@ RC ExecuteStage::do_select(SQLStageEvent *sql_event) ...@@ -438,17 +456,19 @@ RC ExecuteStage::do_select(SQLStageEvent *sql_event)
scan_oper = new TableScanOperator(select_stmt->tables()[0]); scan_oper = new TableScanOperator(select_stmt->tables()[0]);
} }
SqlResult *sql_result = new SqlResult;
PredicateOperator *pred_oper = new PredicateOperator(select_stmt->filter_stmt()); PredicateOperator *pred_oper = new PredicateOperator(select_stmt->filter_stmt());
pred_oper->add_child(scan_oper); pred_oper->add_child(scan_oper);
ProjectOperator *project_oper = new ProjectOperator; ProjectOperator *project_oper = new ProjectOperator;
project_oper->add_child(pred_oper); project_oper->add_child(pred_oper);
TupleSchema schema; TupleSchema schema;
for (const Field &field : select_stmt->query_fields()) { for (const Field &field : select_stmt->query_fields()) {
project_oper->add_projection(field.table(), field.meta()); project_oper->add_projection(field.table(), field.meta());
schema.append_cell(field.field_name()); schema.append_cell(field.field_name());
} }
SqlResult *sql_result = new SqlResult;
sql_result->set_tuple_schema(schema); sql_result->set_tuple_schema(schema);
sql_result->set_operator(project_oper); sql_result->set_operator(project_oper);
...@@ -484,46 +504,53 @@ RC ExecuteStage::do_select(SQLStageEvent *sql_event) ...@@ -484,46 +504,53 @@ RC ExecuteStage::do_select(SQLStageEvent *sql_event)
session_event->set_response(ss.str()); session_event->set_response(ss.str());
*/ */
session_event->set_sql_result(sql_result); session_event->set_sql_result(sql_result);
#endif #endif
return RC::SUCCESS; return RC::SUCCESS;
} }
RC ExecuteStage::do_help(SQLStageEvent *sql_event) RC ExecuteStage::do_help(SQLStageEvent *sql_event)
{ {
SessionEvent *session_event = sql_event->session_event(); SessionEvent *session_event = sql_event->session_event();
const char *strings[] = {
"show tables;", const char *strings[] = {"show tables;",
"desc `table name`;", "desc `table name`;",
"create table `table name` (`column name` `column type`, ...);", "create table `table name` (`column name` `column type`, ...);",
"create index `index name` on `table` (`column`);", "create index `index name` on `table` (`column`);",
"insert into `table` values(`value1`,`value2`);", "insert into `table` values(`value1`,`value2`);",
"update `table` set column=value [where `column`=`value`];", "update `table` set column=value [where `column`=`value`];",
"delete from `table` [where `column`=`value`];", "delete from `table` [where `column`=`value`];",
"select [ * | `columns` ] from `table`;" "select [ * | `columns` ] from `table`;"};
};
auto oper = new StringListPhysicalOperator(); auto oper = new StringListPhysicalOperator();
for (size_t i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) { for (size_t i = 0; i < sizeof(strings) / sizeof(strings[0]); i++) {
oper->append(strings[i]); oper->append(strings[i]);
} }
SqlResult *sql_result = new SqlResult; SqlResult *sql_result = new SqlResult;
TupleSchema schema; TupleSchema schema;
schema.append_cell("Commands"); schema.append_cell("Commands");
sql_result->set_tuple_schema(schema); sql_result->set_tuple_schema(schema);
sql_result->set_operator(std::unique_ptr<PhysicalOperator>(oper)); sql_result->set_operator(std::unique_ptr<PhysicalOperator>(oper));
session_event->set_sql_result(sql_result); session_event->set_sql_result(sql_result);
return RC::SUCCESS; return RC::SUCCESS;
} }
RC ExecuteStage::do_create_table(SQLStageEvent *sql_event) RC ExecuteStage::do_create_table(SQLStageEvent *sql_event)
{ {
const CreateTable &create_table = sql_event->query()->create_table;
SessionEvent *session_event = sql_event->session_event(); SessionEvent *session_event = sql_event->session_event();
Db *db = session_event->session()->get_current_db(); Db *db = session_event->session()->get_current_db();
const CreateTable &create_table = sql_event->command()->create_table;
const int attribute_count = static_cast<int>(create_table.attr_infos.size()); const int attribute_count = static_cast<int>(create_table.attr_infos.size());
RC rc = db->create_table(create_table.relation_name.c_str(), attribute_count, create_table.attr_infos.data()); RC rc = db->create_table(create_table.relation_name.c_str(), attribute_count, create_table.attr_infos.data());
SqlResult *sql_result = new SqlResult; SqlResult *sql_result = new SqlResult;
sql_result->set_return_code(rc); sql_result->set_return_code(rc);
sql_event->session_event()->set_sql_result(sql_result); sql_event->session_event()->set_sql_result(sql_result);
return rc; return rc;
} }
...@@ -532,8 +559,10 @@ RC ExecuteStage::do_create_index(SQLStageEvent *sql_event) ...@@ -532,8 +559,10 @@ RC ExecuteStage::do_create_index(SQLStageEvent *sql_event)
SqlResult *sql_result = new SqlResult; SqlResult *sql_result = new SqlResult;
SessionEvent *session_event = sql_event->session_event(); SessionEvent *session_event = sql_event->session_event();
session_event->set_sql_result(sql_result); session_event->set_sql_result(sql_result);
Db *db = session_event->session()->get_current_db(); Db *db = session_event->session()->get_current_db();
const CreateIndex &create_index = sql_event->query()->create_index;
const CreateIndex &create_index = sql_event->command()->create_index;
Table *table = db->find_table(create_index.relation_name.c_str()); Table *table = db->find_table(create_index.relation_name.c_str());
if (nullptr == table) { if (nullptr == table) {
sql_result->set_return_code(RC::SCHEMA_TABLE_NOT_EXIST); sql_result->set_return_code(RC::SCHEMA_TABLE_NOT_EXIST);
...@@ -541,6 +570,7 @@ RC ExecuteStage::do_create_index(SQLStageEvent *sql_event) ...@@ -541,6 +570,7 @@ RC ExecuteStage::do_create_index(SQLStageEvent *sql_event)
} }
RC rc = table->create_index(nullptr, create_index.index_name.c_str(), create_index.attribute_name.c_str()); RC rc = table->create_index(nullptr, create_index.index_name.c_str(), create_index.attribute_name.c_str());
sql_result->set_return_code(rc); sql_result->set_return_code(rc);
return rc; return rc;
} }
...@@ -550,44 +580,55 @@ RC ExecuteStage::do_show_tables(SQLStageEvent *sql_event) ...@@ -550,44 +580,55 @@ RC ExecuteStage::do_show_tables(SQLStageEvent *sql_event)
SqlResult *sql_result = new SqlResult; SqlResult *sql_result = new SqlResult;
SessionEvent *session_event = sql_event->session_event(); SessionEvent *session_event = sql_event->session_event();
session_event->set_sql_result(sql_result); session_event->set_sql_result(sql_result);
Db *db = session_event->session()->get_current_db(); Db *db = session_event->session()->get_current_db();
std::vector<std::string> all_tables; std::vector<std::string> all_tables;
db->all_tables(all_tables); db->all_tables(all_tables);
TupleSchema tuple_schema; TupleSchema tuple_schema;
tuple_schema.append_cell(TupleCellSpec("", "Tables_in_SYS", "Tables_in_SYS")); tuple_schema.append_cell(TupleCellSpec("", "Tables_in_SYS", "Tables_in_SYS"));
sql_result->set_tuple_schema(tuple_schema); sql_result->set_tuple_schema(tuple_schema);
auto oper = new StringListPhysicalOperator; auto oper = new StringListPhysicalOperator;
for (const std::string &s : all_tables) { for (const std::string &s : all_tables) {
oper->append(s); oper->append(s);
} }
sql_result->set_operator(std::unique_ptr<PhysicalOperator>(oper)); sql_result->set_operator(std::unique_ptr<PhysicalOperator>(oper));
return RC::SUCCESS; return RC::SUCCESS;
} }
RC ExecuteStage::do_desc_table(SQLStageEvent *sql_event) RC ExecuteStage::do_desc_table(SQLStageEvent *sql_event)
{ {
Query *query = sql_event->query().get();
Db *db = sql_event->session_event()->session()->get_current_db();
const char *table_name = query->desc_table.relation_name.c_str();
Table *table = db->find_table(table_name);
SqlResult *sql_result = new SqlResult; SqlResult *sql_result = new SqlResult;
sql_event->session_event()->set_sql_result(sql_result); sql_event->session_event()->set_sql_result(sql_result);
Command *cmd = sql_event->command().get();
const char *table_name = cmd->desc_table.relation_name.c_str();
Db *db = sql_event->session_event()->session()->get_current_db();
Table *table = db->find_table(table_name);
if (table != nullptr) { if (table != nullptr) {
TupleSchema tuple_schema; TupleSchema tuple_schema;
tuple_schema.append_cell(TupleCellSpec("", "Field", "Field")); tuple_schema.append_cell(TupleCellSpec("", "Field", "Field"));
tuple_schema.append_cell(TupleCellSpec("", "Type", "Type")); tuple_schema.append_cell(TupleCellSpec("", "Type", "Type"));
tuple_schema.append_cell(TupleCellSpec("", "Length", "Length")); tuple_schema.append_cell(TupleCellSpec("", "Length", "Length"));
// TODO add Key // TODO add Key
sql_result->set_tuple_schema(tuple_schema); sql_result->set_tuple_schema(tuple_schema);
auto oper = new StringListPhysicalOperator; auto oper = new StringListPhysicalOperator;
const TableMeta &table_meta = table->table_meta(); const TableMeta &table_meta = table->table_meta();
for (int i = table_meta.sys_field_num(); i < table_meta.field_num(); i++) { for (int i = table_meta.sys_field_num(); i < table_meta.field_num(); i++) {
const FieldMeta *field_meta = table_meta.field(i); const FieldMeta *field_meta = table_meta.field(i);
oper->append({field_meta->name(), attr_type_to_string(field_meta->type()), oper->append({field_meta->name(), attr_type_to_string(field_meta->type()), std::to_string(field_meta->len())});
std::to_string(field_meta->len())});
} }
sql_result->set_operator(std::unique_ptr<PhysicalOperator>(oper)); sql_result->set_operator(std::unique_ptr<PhysicalOperator>(oper));
} else { } else {
sql_result->set_return_code(RC::SCHEMA_TABLE_NOT_EXIST); sql_result->set_return_code(RC::SCHEMA_TABLE_NOT_EXIST);
sql_result->set_state_string("Table not exists"); sql_result->set_state_string("Table not exists");
} }
...@@ -596,15 +637,17 @@ RC ExecuteStage::do_desc_table(SQLStageEvent *sql_event) ...@@ -596,15 +637,17 @@ RC ExecuteStage::do_desc_table(SQLStageEvent *sql_event)
RC ExecuteStage::do_insert(SQLStageEvent *sql_event) RC ExecuteStage::do_insert(SQLStageEvent *sql_event)
{ {
Stmt *stmt = sql_event->stmt();
SessionEvent *session_event = sql_event->session_event(); SessionEvent *session_event = sql_event->session_event();
SqlResult *sql_result = new SqlResult; SqlResult *sql_result = new SqlResult;
session_event->set_sql_result(sql_result); session_event->set_sql_result(sql_result);
Session *session = session_event->session(); Session *session = session_event->session();
Db *db = session->get_current_db(); Db *db = session->get_current_db();
Trx *trx = session->current_trx(); Trx *trx = session->current_trx();
CLogManager *clog_manager = db->get_clog_manager(); CLogManager *clog_manager = db->get_clog_manager();
Stmt *stmt = sql_event->stmt();
if (stmt == nullptr) { if (stmt == nullptr) {
LOG_WARN("cannot find statement"); LOG_WARN("cannot find statement");
return RC::GENERIC_ERROR; return RC::GENERIC_ERROR;
...@@ -616,6 +659,7 @@ RC ExecuteStage::do_insert(SQLStageEvent *sql_event) ...@@ -616,6 +659,7 @@ RC ExecuteStage::do_insert(SQLStageEvent *sql_event)
RC rc = table->insert_record(trx, insert_stmt->value_amount(), insert_stmt->values()); RC rc = table->insert_record(trx, insert_stmt->value_amount(), insert_stmt->values());
if (rc == RC::SUCCESS) { if (rc == RC::SUCCESS) {
if (!session->is_trx_multi_operation_mode()) { if (!session->is_trx_multi_operation_mode()) {
CLogRecord *clog_record = nullptr; CLogRecord *clog_record = nullptr;
rc = clog_manager->clog_gen_record(CLogType::REDO_MTR_COMMIT, trx->get_current_id(), clog_record); rc = clog_manager->clog_gen_record(CLogType::REDO_MTR_COMMIT, trx->get_current_id(), clog_record);
if (rc != RC::SUCCESS || clog_record == nullptr) { if (rc != RC::SUCCESS || clog_record == nullptr) {
...@@ -645,23 +689,26 @@ RC ExecuteStage::do_insert(SQLStageEvent *sql_event) ...@@ -645,23 +689,26 @@ RC ExecuteStage::do_insert(SQLStageEvent *sql_event)
RC ExecuteStage::do_delete(SQLStageEvent *sql_event) RC ExecuteStage::do_delete(SQLStageEvent *sql_event)
{ {
#if 0 #if 0
Stmt *stmt = sql_event->stmt();
SessionEvent *session_event = sql_event->session_event(); SessionEvent *session_event = sql_event->session_event();
Session *session = session_event->session(); Session *session = session_event->session();
Db *db = session->get_current_db(); Db *db = session->get_current_db();
Trx *trx = session->current_trx(); Trx *trx = session->current_trx();
CLogManager *clog_manager = db->get_clog_manager(); CLogManager *clog_manager = db->get_clog_manager();
Stmt *stmt = sql_event->stmt();
if (stmt == nullptr) { if (stmt == nullptr) {
LOG_WARN("cannot find statement"); LOG_WARN("cannot find statement");
return RC::GENERIC_ERROR; return RC::GENERIC_ERROR;
} }
DeleteStmt *delete_stmt = (DeleteStmt *)stmt; DeleteStmt *delete_stmt = (DeleteStmt *)stmt;
TableScanOperator scan_oper(delete_stmt->table()); TableScanOperator scan_oper(delete_stmt->table());
PredicateOperator pred_oper(delete_stmt->filter_stmt()); PredicateOperator pred_oper(delete_stmt->filter_stmt());
pred_oper.add_child(&scan_oper); pred_oper.add_child(&scan_oper);
DeleteOperator delete_oper(delete_stmt, trx); DeleteOperator delete_oper(delete_stmt, trx);
delete_oper.add_child(&pred_oper); delete_oper.add_child(&pred_oper);
...@@ -669,8 +716,10 @@ RC ExecuteStage::do_delete(SQLStageEvent *sql_event) ...@@ -669,8 +716,10 @@ RC ExecuteStage::do_delete(SQLStageEvent *sql_event)
if (rc != RC::SUCCESS) { if (rc != RC::SUCCESS) {
session_event->set_response("FAILURE\n"); session_event->set_response("FAILURE\n");
} else { } else {
session_event->set_response("SUCCESS\n"); session_event->set_response("SUCCESS\n");
if (!session->is_trx_multi_operation_mode()) { if (!session->is_trx_multi_operation_mode()) {
CLogRecord *clog_record = nullptr; CLogRecord *clog_record = nullptr;
rc = clog_manager->clog_gen_record(CLogType::REDO_MTR_COMMIT, trx->get_current_id(), clog_record); rc = clog_manager->clog_gen_record(CLogType::REDO_MTR_COMMIT, trx->get_current_id(), clog_record);
if (rc != RC::SUCCESS || clog_record == nullptr) { if (rc != RC::SUCCESS || clog_record == nullptr) {
...@@ -689,16 +738,16 @@ RC ExecuteStage::do_delete(SQLStageEvent *sql_event) ...@@ -689,16 +738,16 @@ RC ExecuteStage::do_delete(SQLStageEvent *sql_event)
} }
} }
return rc; return rc;
#endif #endif
return RC::SUCCESS; return RC::SUCCESS;
} }
RC ExecuteStage::do_begin(SQLStageEvent *sql_event) RC ExecuteStage::do_begin(SQLStageEvent *sql_event)
{ {
RC rc = RC::SUCCESS;
SessionEvent *session_event = sql_event->session_event(); SessionEvent *session_event = sql_event->session_event();
SqlResult *sql_result = new SqlResult; SqlResult *sql_result = new SqlResult;
session_event->set_sql_result(sql_result); session_event->set_sql_result(sql_result);
Session *session = session_event->session(); Session *session = session_event->session();
Db *db = session->get_current_db(); Db *db = session->get_current_db();
Trx *trx = session->current_trx(); Trx *trx = session->current_trx();
...@@ -707,7 +756,7 @@ RC ExecuteStage::do_begin(SQLStageEvent *sql_event) ...@@ -707,7 +756,7 @@ RC ExecuteStage::do_begin(SQLStageEvent *sql_event)
session->set_trx_multi_operation_mode(true); session->set_trx_multi_operation_mode(true);
CLogRecord *clog_record = nullptr; CLogRecord *clog_record = nullptr;
rc = clog_manager->clog_gen_record(CLogType::REDO_MTR_BEGIN, trx->get_current_id(), clog_record); RC rc = clog_manager->clog_gen_record(CLogType::REDO_MTR_BEGIN, trx->get_current_id(), clog_record);
if (rc != RC::SUCCESS || clog_record == nullptr) { if (rc != RC::SUCCESS || clog_record == nullptr) {
sql_result->set_return_code(rc); sql_result->set_return_code(rc);
return rc; return rc;
...@@ -721,19 +770,20 @@ RC ExecuteStage::do_begin(SQLStageEvent *sql_event) ...@@ -721,19 +770,20 @@ RC ExecuteStage::do_begin(SQLStageEvent *sql_event)
RC ExecuteStage::do_commit(SQLStageEvent *sql_event) RC ExecuteStage::do_commit(SQLStageEvent *sql_event)
{ {
RC rc = RC::SUCCESS;
SessionEvent *session_event = sql_event->session_event(); SessionEvent *session_event = sql_event->session_event();
SqlResult *sql_result = new SqlResult; SqlResult *sql_result = new SqlResult;
session_event->set_sql_result(sql_result); session_event->set_sql_result(sql_result);
Session *session = session_event->session();
Db *db = session->get_current_db();
Trx *trx = session->current_trx();
CLogManager *clog_manager = db->get_clog_manager();
Session *session = session_event->session();
session->set_trx_multi_operation_mode(false); session->set_trx_multi_operation_mode(false);
Trx *trx = session->current_trx();
CLogRecord *clog_record = nullptr; CLogRecord *clog_record = nullptr;
rc = clog_manager->clog_gen_record(CLogType::REDO_MTR_COMMIT, trx->get_current_id(), clog_record);
Db *db = session->get_current_db();
CLogManager *clog_manager = db->get_clog_manager();
RC rc = clog_manager->clog_gen_record(CLogType::REDO_MTR_COMMIT, trx->get_current_id(), clog_record);
if (rc != RC::SUCCESS || clog_record == nullptr) { if (rc != RC::SUCCESS || clog_record == nullptr) {
sql_result->set_return_code(rc); sql_result->set_return_code(rc);
return rc; return rc;
...@@ -749,14 +799,16 @@ RC ExecuteStage::do_commit(SQLStageEvent *sql_event) ...@@ -749,14 +799,16 @@ RC ExecuteStage::do_commit(SQLStageEvent *sql_event)
RC ExecuteStage::do_clog_sync(SQLStageEvent *sql_event) RC ExecuteStage::do_clog_sync(SQLStageEvent *sql_event)
{ {
RC rc = RC::SUCCESS;
SqlResult *sql_result = new SqlResult; SqlResult *sql_result = new SqlResult;
SessionEvent *session_event = sql_event->session_event(); SessionEvent *session_event = sql_event->session_event();
session_event->set_sql_result(sql_result); session_event->set_sql_result(sql_result);
Db *db = session_event->session()->get_current_db(); Db *db = session_event->session()->get_current_db();
CLogManager *clog_manager = db->get_clog_manager(); CLogManager *clog_manager = db->get_clog_manager();
rc = clog_manager->clog_sync(); RC rc = clog_manager->clog_sync();
sql_result->set_return_code(rc); sql_result->set_return_code(rc);
return rc; return rc;
......
...@@ -17,7 +17,7 @@ See the Mulan PSL v2 for more details. */ ...@@ -17,7 +17,7 @@ See the Mulan PSL v2 for more details. */
#include "rc.h" #include "rc.h"
#include "common/log/log.h" #include "common/log/log.h"
RC parse(char *st, Query *sqln); RC parse(char *st, Command *sqln);
const char *ATTR_TYPE_NAME[] = {"undefined", "chars", "ints", "floats", "booleans"}; const char *ATTR_TYPE_NAME[] = {"undefined", "chars", "ints", "floats", "booleans"};
...@@ -62,16 +62,16 @@ int Value::length() ...@@ -62,16 +62,16 @@ int Value::length()
return 0; return 0;
} }
Query::Query() Command::Command()
: flag(SCF_ERROR) : flag(SCF_ERROR)
{ {
} }
Query::Query(enum SqlCommandFlag _flag) Command::Command(enum SqlCommandFlag _flag)
: flag(_flag) : flag(_flag)
{} {}
void ParsedSqlResult::add_command(std::unique_ptr<Query> command) void ParsedSqlResult::add_command(std::unique_ptr<Command> command)
{ {
sql_commands_.emplace_back(std::move(command)); sql_commands_.emplace_back(std::move(command));
} }
......
...@@ -130,6 +130,7 @@ struct CreateIndex { ...@@ -130,6 +130,7 @@ struct CreateIndex {
// struct of drop_index // struct of drop_index
struct DropIndex { struct DropIndex {
std::string index_name; // Index name std::string index_name; // Index name
std::string relation_name; //Relation name
}; };
struct DescTable { struct DescTable {
...@@ -141,9 +142,9 @@ struct LoadData { ...@@ -141,9 +142,9 @@ struct LoadData {
std::string file_name; std::string file_name;
}; };
class Query; class Command;
struct Explain { struct Explain {
std::unique_ptr<Query> query; std::unique_ptr<Command> cmd;
}; };
struct Error struct Error
...@@ -177,7 +178,7 @@ enum SqlCommandFlag { ...@@ -177,7 +178,7 @@ enum SqlCommandFlag {
SCF_EXPLAIN, SCF_EXPLAIN,
}; };
// struct of flag and sql_struct // struct of flag and sql_struct
class Query { class Command {
public: public:
enum SqlCommandFlag flag; enum SqlCommandFlag flag;
Error error; Error error;
...@@ -194,8 +195,8 @@ public: ...@@ -194,8 +195,8 @@ public:
Explain explain; Explain explain;
public: public:
Query(); Command();
Query(enum SqlCommandFlag flag); Command(enum SqlCommandFlag flag);
}; };
/** /**
...@@ -205,11 +206,11 @@ public: ...@@ -205,11 +206,11 @@ public:
class ParsedSqlResult class ParsedSqlResult
{ {
public: public:
void add_command(std::unique_ptr<Query> command); void add_command(std::unique_ptr<Command> command);
std::vector<std::unique_ptr<Query>> &commands() { return sql_commands_; } std::vector<std::unique_ptr<Command>> &commands() { return sql_commands_; }
private: private:
std::vector<std::unique_ptr<Query>> sql_commands_; std::vector<std::unique_ptr<Command>> sql_commands_;
}; };
const char *attr_type_to_string(AttrType type); const char *attr_type_to_string(AttrType type);
......
...@@ -138,8 +138,8 @@ RC ParseStage::handle_request(StageEvent *event) ...@@ -138,8 +138,8 @@ RC ParseStage::handle_request(StageEvent *event)
LOG_WARN("got multi sql commands but only 1 will be handled"); LOG_WARN("got multi sql commands but only 1 will be handled");
} }
std::unique_ptr<Query> query_result = std::move(parsed_sql_result.commands().front()); std::unique_ptr<Command> cmd = std::move(parsed_sql_result.commands().front());
if (query_result->flag == SCF_ERROR) { if (cmd->flag == SCF_ERROR) {
// set error information to event // set error information to event
sql_result->set_return_code(RC::SQL_SYNTAX); sql_result->set_return_code(RC::SQL_SYNTAX);
sql_result->set_state_string("Failed to parse sql"); sql_result->set_state_string("Failed to parse sql");
...@@ -148,6 +148,6 @@ RC ParseStage::handle_request(StageEvent *event) ...@@ -148,6 +148,6 @@ RC ParseStage::handle_request(StageEvent *event)
} }
delete sql_result; delete sql_result;
sql_event->set_query(std::move(query_result)); sql_event->set_command(std::move(cmd));
return RC::SUCCESS; return RC::SUCCESS;
} }
...@@ -101,9 +101,9 @@ void ResolveStage::handle_event(StageEvent *event) ...@@ -101,9 +101,9 @@ void ResolveStage::handle_event(StageEvent *event)
return ; return ;
} }
Query *query = sql_event->query().get(); Command *cmd = sql_event->command().get();
Stmt *stmt = nullptr; Stmt *stmt = nullptr;
RC rc = Stmt::create_stmt(db, *query, stmt); RC rc = Stmt::create_stmt(db, *cmd, stmt);
if (rc != RC::SUCCESS && rc != RC::UNIMPLENMENT) { if (rc != RC::SUCCESS && rc != RC::UNIMPLENMENT) {
LOG_WARN("failed to create stmt. rc=%d:%s", rc, strrc(rc)); LOG_WARN("failed to create stmt. rc=%d:%s", rc, strrc(rc));
SqlResult *sql_result = new SqlResult; SqlResult *sql_result = new SqlResult;
......
...@@ -111,9 +111,9 @@ extern int yydebug; ...@@ -111,9 +111,9 @@ extern int yydebug;
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
union YYSTYPE union YYSTYPE
{ {
#line 89 "yacc_sql.y" #line 79 "yacc_sql.y"
Query *query; Command *command;
Condition *condition; Condition *condition;
Value *value; Value *value;
enum CompOp comp; enum CompOp comp;
......
%{ %{
#include "sql/parser/parse_defs.h"
#include "sql/parser/yacc_sql.hpp"
#include "sql/parser/lex_sql.h"
#include "common/log/log.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <algorithm> #include <algorithm>
//获取子串 #include "common/log/log.h"
char *substr(const char *s,int n1,int n2)/*从s中提取下标为n1~n2的字符组成一个新字符串,然后返回这个新串的首地址*/ #include "common/lang/string.h"
{ #include "sql/parser/parse_defs.h"
char *sp = (char *)malloc(sizeof(char) * (n2 - n1 + 2)); #include "sql/parser/yacc_sql.hpp"
int i, j = 0; #include "sql/parser/lex_sql.h"
for (i = n1; i <= n2; i++) {
sp[j++] = s[i];
}
sp[j] = 0;
return sp;
}
int yyerror(YYLTYPE *llocp, ParsedSqlResult *sql_result, yyscan_t scanner, const char *msg) int yyerror(YYLTYPE *llocp, ParsedSqlResult *sql_result, yyscan_t scanner, const char *msg)
{ {
std::unique_ptr<Query> error_query = std::make_unique<Query>(SCF_ERROR); std::unique_ptr<Command> error_cmd = std::make_unique<Command>(SCF_ERROR);
error_query->error.error_msg = msg; error_cmd->error.error_msg = msg;
error_query->error.line = llocp->first_line; error_cmd->error.line = llocp->first_line;
error_query->error.column = llocp->first_column; error_cmd->error.column = llocp->first_column;
sql_result->add_command(std::move(error_query)); sql_result->add_command(std::move(error_cmd));
return 0; return 0;
} }
...@@ -87,7 +77,7 @@ int yyerror(YYLTYPE *llocp, ParsedSqlResult *sql_result, yyscan_t scanner, const ...@@ -87,7 +77,7 @@ int yyerror(YYLTYPE *llocp, ParsedSqlResult *sql_result, yyscan_t scanner, const
NE NE
%union { %union {
Query *query; Command *command;
Condition *condition; Condition *condition;
Value *value; Value *value;
enum CompOp comp; enum CompOp comp;
...@@ -126,37 +116,37 @@ int yyerror(YYLTYPE *llocp, ParsedSqlResult *sql_result, yyscan_t scanner, const ...@@ -126,37 +116,37 @@ int yyerror(YYLTYPE *llocp, ParsedSqlResult *sql_result, yyscan_t scanner, const
%type <rel_attr_list> select_attr %type <rel_attr_list> select_attr
%type <relation_list> rel_list %type <relation_list> rel_list
%type <rel_attr_list> attr_list %type <rel_attr_list> attr_list
%type <query> select %type <command> select
%type <query> insert %type <command> insert
%type <query> update %type <command> update
%type <query> delete %type <command> delete
%type <query> create_table %type <command> create_table
%type <query> drop_table %type <command> drop_table
%type <query> show_tables %type <command> show_tables
%type <query> desc_table %type <command> desc_table
%type <query> create_index %type <command> create_index
%type <query> drop_index %type <command> drop_index
%type <query> sync %type <command> sync
%type <query> begin %type <command> begin
%type <query> commit %type <command> commit
%type <query> rollback %type <command> rollback
%type <query> load_data %type <command> load_data
%type <query> explain %type <command> explain
%type <query> help %type <command> help
%type <query> exit %type <command> exit
%type <query> command %type <command> command_wrapper
// commands should be a list but I use a single command instead // commands should be a list but I use a single command instead
%type <query> commands %type <command> commands
%% %%
commands: command opt_semicolon //commands or sqls. parser starts here. commands: command_wrapper opt_semicolon //commands or sqls. parser starts here.
{ {
std::unique_ptr<Query> query_command = std::unique_ptr<Query>($1); std::unique_ptr<Command> sql_command = std::unique_ptr<Command>($1);
sql_result->add_command(std::move(query_command)); sql_result->add_command(std::move(sql_command));
} }
; ;
command: command_wrapper:
select select
| insert | insert
| update | update
...@@ -179,54 +169,54 @@ command: ...@@ -179,54 +169,54 @@ command:
exit: exit:
EXIT { EXIT {
$$ = new Query(SCF_EXIT); $$ = new Command(SCF_EXIT);
}; };
help: help:
HELP { HELP {
$$ = new Query(SCF_HELP); $$ = new Command(SCF_HELP);
}; };
sync: sync:
SYNC { SYNC {
$$ = new Query(SCF_SYNC); $$ = new Command(SCF_SYNC);
} }
; ;
begin: begin:
TRX_BEGIN { TRX_BEGIN {
$$ = new Query(SCF_BEGIN); $$ = new Command(SCF_BEGIN);
} }
; ;
commit: commit:
TRX_COMMIT { TRX_COMMIT {
$$ = new Query(SCF_COMMIT); $$ = new Command(SCF_COMMIT);
} }
; ;
rollback: rollback:
TRX_ROLLBACK { TRX_ROLLBACK {
$$ = new Query(SCF_ROLLBACK); $$ = new Command(SCF_ROLLBACK);
} }
; ;
drop_table: /*drop table 语句的语法解析树*/ drop_table: /*drop table 语句的语法解析树*/
DROP TABLE ID { DROP TABLE ID {
$$ = new Query(SCF_DROP_TABLE); $$ = new Command(SCF_DROP_TABLE);
$$->drop_table.relation_name = $3; $$->drop_table.relation_name = $3;
free($3); free($3);
}; };
show_tables: show_tables:
SHOW TABLES { SHOW TABLES {
$$ = new Query(SCF_SHOW_TABLES); $$ = new Command(SCF_SHOW_TABLES);
} }
; ;
desc_table: desc_table:
DESC ID { DESC ID {
$$ = new Query(SCF_DESC_TABLE); $$ = new Command(SCF_DESC_TABLE);
$$->desc_table.relation_name = $2; $$->desc_table.relation_name = $2;
free($2); free($2);
} }
...@@ -235,7 +225,7 @@ desc_table: ...@@ -235,7 +225,7 @@ desc_table:
create_index: /*create index 语句的语法解析树*/ create_index: /*create index 语句的语法解析树*/
CREATE INDEX ID ON ID LBRACE ID RBRACE CREATE INDEX ID ON ID LBRACE ID RBRACE
{ {
$$ = new Query(SCF_CREATE_INDEX); $$ = new Command(SCF_CREATE_INDEX);
CreateIndex &create_index = $$->create_index; CreateIndex &create_index = $$->create_index;
create_index.index_name = $3; create_index.index_name = $3;
create_index.relation_name = $5; create_index.relation_name = $5;
...@@ -247,17 +237,19 @@ create_index: /*create index 语句的语法解析树*/ ...@@ -247,17 +237,19 @@ create_index: /*create index 语句的语法解析树*/
; ;
drop_index: /*drop index 语句的语法解析树*/ drop_index: /*drop index 语句的语法解析树*/
DROP INDEX ID DROP INDEX ID ON ID
{ {
$$ = new Query(SCF_DROP_INDEX); $$ = new Command(SCF_DROP_INDEX);
$$->drop_index.index_name = $3; $$->drop_index.index_name = $3;
$$->drop_index.relation_name = $5;
free($3); free($3);
free($5);
} }
; ;
create_table: /*create table 语句的语法解析树*/ create_table: /*create table 语句的语法解析树*/
CREATE TABLE ID LBRACE attr_def attr_def_list RBRACE CREATE TABLE ID LBRACE attr_def attr_def_list RBRACE
{ {
$$ = new Query(SCF_CREATE_TABLE); $$ = new Command(SCF_CREATE_TABLE);
CreateTable &create_table = $$->create_table; CreateTable &create_table = $$->create_table;
create_table.relation_name = $3; create_table.relation_name = $3;
free($3); free($3);
...@@ -318,7 +310,7 @@ type: ...@@ -318,7 +310,7 @@ type:
insert: /*insert 语句的语法解析树*/ insert: /*insert 语句的语法解析树*/
INSERT INTO ID VALUES LBRACE value value_list RBRACE INSERT INTO ID VALUES LBRACE value value_list RBRACE
{ {
$$ = new Query(SCF_INSERT); $$ = new Command(SCF_INSERT);
$$->insertion.relation_name = $3; $$->insertion.relation_name = $3;
if ($7 != nullptr) { if ($7 != nullptr) {
$$->insertion.values.swap(*$7); $$->insertion.values.swap(*$7);
...@@ -357,7 +349,7 @@ value: ...@@ -357,7 +349,7 @@ value:
$$->float_value = $1; $$->float_value = $1;
} }
|SSS { |SSS {
char *tmp = substr($1,1,strlen($1)-2); char *tmp = common::substr($1,1,strlen($1)-2);
$$ = new Value; $$ = new Value;
$$->type = CHARS; $$->type = CHARS;
$$->string_value = tmp; $$->string_value = tmp;
...@@ -368,7 +360,7 @@ value: ...@@ -368,7 +360,7 @@ value:
delete: /* delete 语句的语法解析树*/ delete: /* delete 语句的语法解析树*/
DELETE FROM ID where DELETE FROM ID where
{ {
$$ = new Query(SCF_DELETE); $$ = new Command(SCF_DELETE);
$$->deletion.relation_name = $3; $$->deletion.relation_name = $3;
if ($4 != nullptr) { if ($4 != nullptr) {
$$->deletion.conditions.swap(*$4); $$->deletion.conditions.swap(*$4);
...@@ -380,7 +372,7 @@ delete: /* delete 语句的语法解析树*/ ...@@ -380,7 +372,7 @@ delete: /* delete 语句的语法解析树*/
update: /* update 语句的语法解析树*/ update: /* update 语句的语法解析树*/
UPDATE ID SET ID EQ value where UPDATE ID SET ID EQ value where
{ {
$$ = new Query(SCF_UPDATE); $$ = new Command(SCF_UPDATE);
$$->update.relation_name = $2; $$->update.relation_name = $2;
$$->update.attribute_name = $4; $$->update.attribute_name = $4;
$$->update.value = *$6; $$->update.value = *$6;
...@@ -395,7 +387,7 @@ update: /* update 语句的语法解析树*/ ...@@ -395,7 +387,7 @@ update: /* update 语句的语法解析树*/
select: /* select 语句的语法解析树*/ select: /* select 语句的语法解析树*/
SELECT select_attr FROM ID rel_list where SELECT select_attr FROM ID rel_list where
{ {
$$ = new Query(SCF_SELECT); $$ = new Command(SCF_SELECT);
if ($2 != nullptr) { if ($2 != nullptr) {
$$->selection.attributes.swap(*$2); $$->selection.attributes.swap(*$2);
delete $2; delete $2;
...@@ -570,7 +562,7 @@ comp_op: ...@@ -570,7 +562,7 @@ comp_op:
load_data: load_data:
LOAD DATA INFILE SSS INTO TABLE ID LOAD DATA INFILE SSS INTO TABLE ID
{ {
$$ = new Query(SCF_LOAD_DATA); $$ = new Command(SCF_LOAD_DATA);
$$->load_data.relation_name = $7; $$->load_data.relation_name = $7;
$$->load_data.file_name = $4; $$->load_data.file_name = $4;
free($7); free($7);
...@@ -578,10 +570,10 @@ load_data: ...@@ -578,10 +570,10 @@ load_data:
; ;
explain: explain:
EXPLAIN command EXPLAIN command_wrapper
{ {
$$ = new Query(SCF_EXPLAIN); $$ = new Command(SCF_EXPLAIN);
$$->explain.query = std::unique_ptr<Query>($2); $$->explain.cmd = std::unique_ptr<Command>($2);
} }
; ;
......
...@@ -23,7 +23,7 @@ ExplainStmt::ExplainStmt(std::unique_ptr<Stmt> child_stmt) ...@@ -23,7 +23,7 @@ ExplainStmt::ExplainStmt(std::unique_ptr<Stmt> child_stmt)
RC ExplainStmt::create(Db *db, const Explain &explain, Stmt *& stmt) RC ExplainStmt::create(Db *db, const Explain &explain, Stmt *& stmt)
{ {
Stmt *child_stmt = nullptr; Stmt *child_stmt = nullptr;
RC rc = Stmt::create_stmt(db, *explain.query, child_stmt); RC rc = Stmt::create_stmt(db, *explain.cmd, child_stmt);
if (rc != RC::SUCCESS) { if (rc != RC::SUCCESS) {
LOG_WARN("failed to create explain's child stmt. rc=%s", strrc(rc)); LOG_WARN("failed to create explain's child stmt. rc=%s", strrc(rc));
return rc; return rc;
......
...@@ -19,26 +19,26 @@ See the Mulan PSL v2 for more details. */ ...@@ -19,26 +19,26 @@ See the Mulan PSL v2 for more details. */
#include "sql/stmt/select_stmt.h" #include "sql/stmt/select_stmt.h"
#include "sql/stmt/explain_stmt.h" #include "sql/stmt/explain_stmt.h"
RC Stmt::create_stmt(Db *db, const Query &query, Stmt *&stmt) RC Stmt::create_stmt(Db *db, const Command &cmd, Stmt *&stmt)
{ {
stmt = nullptr; stmt = nullptr;
switch (query.flag) { switch (cmd.flag) {
case SCF_INSERT: { case SCF_INSERT: {
return InsertStmt::create(db, query.insertion, stmt); return InsertStmt::create(db, cmd.insertion, stmt);
} }
case SCF_DELETE: { case SCF_DELETE: {
return DeleteStmt::create(db, query.deletion, stmt); return DeleteStmt::create(db, cmd.deletion, stmt);
} }
case SCF_SELECT: { case SCF_SELECT: {
return SelectStmt::create(db, query.selection, stmt); return SelectStmt::create(db, cmd.selection, stmt);
} }
case SCF_EXPLAIN: { case SCF_EXPLAIN: {
return ExplainStmt::create(db, query.explain, stmt); return ExplainStmt::create(db, cmd.explain, stmt);
} }
default: { default: {
LOG_WARN("unknown query command"); LOG_INFO("Command::type %d doesn't need to create statement.", cmd.flag);
} }
break; break;
} }
......
...@@ -53,7 +53,7 @@ public: ...@@ -53,7 +53,7 @@ public:
virtual StmtType type() const = 0; virtual StmtType type() const = 0;
public: public:
static RC create_stmt(Db *db, const Query &query, Stmt *&stmt); static RC create_stmt(Db *db, const Command &cmd, Stmt *&stmt);
private: private:
}; };
......
...@@ -144,7 +144,7 @@ void DefaultStorageStage::handle_event(StageEvent *event) ...@@ -144,7 +144,7 @@ void DefaultStorageStage::handle_event(StageEvent *event)
SQLStageEvent *sql_event = static_cast<SQLStageEvent *>(event); SQLStageEvent *sql_event = static_cast<SQLStageEvent *>(event);
Query *sql = sql_event->query().get(); Command *cmd = sql_event->command().get();
SessionEvent *session_event = sql_event->session_event(); SessionEvent *session_event = sql_event->session_event();
...@@ -157,19 +157,19 @@ void DefaultStorageStage::handle_event(StageEvent *event) ...@@ -157,19 +157,19 @@ void DefaultStorageStage::handle_event(StageEvent *event)
RC rc = RC::SUCCESS; RC rc = RC::SUCCESS;
char response[256]; char response[256];
switch (sql->flag) { switch (cmd->flag) {
case SCF_LOAD_DATA: { case SCF_LOAD_DATA: {
/* /*
从文件导入数据,如果做性能测试,需要保持这些代码可以正常工作 从文件导入数据,如果做性能测试,需要保持这些代码可以正常工作
load data infile `your/file/path` into table `table-name`; load data infile `your/file/path` into table `table-name`;
*/ */
const char *table_name = sql->load_data.relation_name.c_str(); const char *table_name = cmd->load_data.relation_name.c_str();
const char *file_name = sql->load_data.file_name.c_str(); const char *file_name = cmd->load_data.file_name.c_str();
std::string result = load_data(dbname, table_name, file_name); std::string result = load_data(dbname, table_name, file_name);
snprintf(response, sizeof(response), "%s", result.c_str()); snprintf(response, sizeof(response), "%s", result.c_str());
} break; } break;
default: default:
snprintf(response, sizeof(response), "Unsupported sql: %d\n", sql->flag); snprintf(response, sizeof(response), "Unsupported sql: %d\n", cmd->flag);
break; break;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册