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

Rename parser's result command from Query to Command

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