106 lines
5.5 KiB
C++
106 lines
5.5 KiB
C++
#pragma once
|
|
|
|
#include "settings.h"
|
|
#include "parser.h"
|
|
#include "table.h"
|
|
#include "index.h"
|
|
|
|
#include <string>
|
|
#include <list>
|
|
|
|
namespace usql {
|
|
|
|
class USql {
|
|
|
|
public:
|
|
USql();
|
|
std::unique_ptr<Table> execute(const std::string &command);
|
|
|
|
private:
|
|
std::unique_ptr<Table> execute(Node &node);
|
|
|
|
std::unique_ptr<Table> execute_create_table(const CreateTableNode &node);
|
|
bool execute_create_table_sys_catalogue(const CreateTableNode &node);
|
|
std::unique_ptr<Table> execute_create_index(const CreateIndexNode &node);
|
|
bool execute_create_index_sys_catalogue(const CreateIndexNode &node);
|
|
std::unique_ptr<Table> execute_create_table_as_table(const CreateTableAsSelectNode &node);
|
|
std::unique_ptr<Table> execute_load(const LoadIntoTableNode &node);
|
|
std::unique_ptr<Table> execute_save(const SaveTableNode &node);
|
|
std::unique_ptr<Table> execute_drop(const DropTableNode &node);
|
|
static std::unique_ptr<Table> execute_set(const SetNode &node);
|
|
static std::unique_ptr<Table> execute_show(const ShowNode &node);
|
|
|
|
std::unique_ptr<Table> execute_insert_into_table(const InsertIntoTableNode &node);
|
|
std::unique_ptr<Table> execute_select(SelectFromTableNode &node) const;
|
|
std::unique_ptr<Table> execute_delete(const DeleteFromTableNode &node);
|
|
std::unique_ptr<Table> execute_update(const UpdateTableNode &node);
|
|
|
|
|
|
private:
|
|
static bool eval_where(Node *where, Table *table, Row &row) ;
|
|
|
|
static std::unique_ptr<ValueNode> eval_value_node(Table *table, Row &row, Node *node, ColDefNode *col_def_node, ColValue *agg_func_value);
|
|
static std::unique_ptr<ValueNode> eval_database_value_node(Table *table, Row &row, Node *node);
|
|
static std::unique_ptr<ValueNode> eval_literal_value_node(Row &row, Node *node);
|
|
static std::unique_ptr<ValueNode> eval_function_value_node(Table *table, Row &row, Node *node, ColDefNode *col_def_node, ColValue *agg_func_value);
|
|
|
|
|
|
static bool eval_relational_operator(const RelationalOperatorNode &filter, Table *table, Row &row);
|
|
static bool eval_logical_operator(LogicalOperatorNode &node, Table *pTable, Row &row);
|
|
static std::unique_ptr<ValueNode> eval_arithmetic_operator(ColumnType outType, ArithmeticalOperatorNode &node, Table *table, Row &row);
|
|
|
|
|
|
static std::unique_ptr<Table> create_stmt_result_table(long code, const std::string &text, size_t affected_rows);
|
|
static std::tuple<int, ColDefNode> get_column_definition(Table *table, SelectColNode *select_col_node, int col_order);
|
|
static ColDefNode get_db_column_definition(Table *table, Node *node);
|
|
static std::tuple<int, ColDefNode> get_node_definition(Table *table, Node *select_col_node, const std::string & col_name, int col_order);
|
|
[[nodiscard]] Table *find_table(const std::string &name) const;
|
|
|
|
void check_table_not_exists(const std::string &name) const;
|
|
void check_index_not_exists(const std::string &index_name);
|
|
|
|
private:
|
|
static void execute_distinct(SelectFromTableNode &node, Table *result);
|
|
static void execute_order_by(SelectFromTableNode &node, Table *result);
|
|
static void execute_offset_limit(OffsetLimitNode &node, Table *result);
|
|
|
|
static void expand_asterix_char(SelectFromTableNode &node, Table *table) ;
|
|
static void setup_order_columns(std::vector<ColOrderNode> &node, Table *table) ;
|
|
|
|
static bool check_for_aggregate_only_functions(SelectFromTableNode &node, size_t result_cols_cnt) ;
|
|
|
|
static std::unique_ptr<ValueNode> lower_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars);
|
|
static std::unique_ptr<ValueNode> upper_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars);
|
|
static std::unique_ptr<ValueNode> coalesce_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars);
|
|
static std::unique_ptr<ValueNode> to_date_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars);
|
|
static std::unique_ptr<ValueNode> to_char_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars);
|
|
static std::unique_ptr<ValueNode> to_int_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars);
|
|
static std::unique_ptr<ValueNode> to_float_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars);
|
|
static std::unique_ptr<ValueNode> date_add_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars);
|
|
static std::unique_ptr<ValueNode> pp_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars);
|
|
|
|
static std::unique_ptr<ValueNode> max_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars, const ColDefNode *col_def_node, ColValue *agg_func_value);
|
|
static std::unique_ptr<ValueNode> min_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars, const ColDefNode *col_def_node, ColValue *agg_func_value);
|
|
|
|
static std::unique_ptr<ValueNode> count_function(ColValue *agg_func_value, const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars);
|
|
|
|
static void select_row(SelectFromTableNode &where_node,
|
|
Table *src_table, Row *src_row,
|
|
Table *rslt_table,
|
|
const std::vector<ColDefNode> &rslt_tbl_col_defs, const std::vector<int> &src_table_col_index,
|
|
bool is_aggregated);
|
|
|
|
std::pair<bool, std::vector<rowid_t>> probe_index_scan(const Node *where, Table *table) const;
|
|
std::pair<bool, std::vector<rowid_t>> look_for_usable_index(const Node *where, Table *table) const;
|
|
bool normalize_where(const Node *node) const;
|
|
|
|
Table::rows_scanner get_iterator(Table *table, const Node *where) const;
|
|
|
|
|
|
private:
|
|
Parser m_parser;
|
|
std::list<Table> m_tables;
|
|
};
|
|
|
|
|
|
} // namespace
|