small refactorings

This commit is contained in:
VaclavT 2021-08-15 13:24:16 +02:00
parent b37b0b55ff
commit b03462da6a
8 changed files with 55 additions and 89 deletions

View File

@ -96,8 +96,6 @@ namespace usql {
return m_index < m_tokens.size() - 1 ? m_tokens[m_index + 1].type : TokenType::eof; return m_index < m_tokens.size() - 1 ? m_tokens[m_index + 1].type : TokenType::eof;
} }
TokenType Lexer::prevTokenType() { return m_index > 0 ? m_tokens[m_index - 1].type : TokenType::undef; }
bool Lexer::isRelationalOperator(TokenType token_type) { bool Lexer::isRelationalOperator(TokenType token_type) {
return (token_type == TokenType::equal || token_type == TokenType::not_equal || return (token_type == TokenType::equal || token_type == TokenType::not_equal ||
token_type == TokenType::greater || token_type == TokenType::greater_equal || token_type == TokenType::greater || token_type == TokenType::greater_equal ||

View File

@ -95,8 +95,6 @@ namespace usql {
TokenType nextTokenType(); TokenType nextTokenType();
TokenType prevTokenType();
static bool isRelationalOperator(TokenType token_type); static bool isRelationalOperator(TokenType token_type);
static bool isLogicalOperator(TokenType token_type); static bool isLogicalOperator(TokenType token_type);

View File

@ -51,7 +51,7 @@ namespace usql {
struct Node { struct Node {
NodeType node_type; NodeType node_type;
Node(const NodeType type) : node_type(type) {} explicit Node(const NodeType type) : node_type(type) {}
}; };
@ -60,7 +60,7 @@ namespace usql {
int col_index; int col_index;
bool ascending; bool ascending;
ColOrderNode(const std::string name, bool asc) : Node(NodeType::column_order), col_name(name), col_index(-1), ascending(asc) {} ColOrderNode(const std::string& name, bool asc) : Node(NodeType::column_order), col_name(name), col_index(-1), ascending(asc) {}
ColOrderNode(int index, bool asc) : Node(NodeType::database_value), col_name(""), col_index(index), ascending(asc) {} ColOrderNode(int index, bool asc) : Node(NodeType::database_value), col_name(""), col_index(index), ascending(asc) {}
}; };
@ -78,7 +78,7 @@ namespace usql {
std::unique_ptr<Node> value; std::unique_ptr<Node> value;
std::string name; std::string name;
SelectColNode(std::unique_ptr<Node> column, std::string alias) : SelectColNode(std::unique_ptr<Node> column, const std::string &alias) :
Node(NodeType::database_value), value(std::move(column)), name(alias) {} Node(NodeType::database_value), value(std::move(column)), name(alias) {}
}; };
@ -89,7 +89,7 @@ namespace usql {
int length; int length;
bool null; bool null;
ColDefNode(const std::string col_name, ColumnType col_type, int col_order, int col_len, bool nullable) : ColDefNode(const std::string& col_name, ColumnType col_type, int col_order, int col_len, bool nullable) :
Node(NodeType::column_def), name(col_name), type(col_type), order(col_order), length(col_len), Node(NodeType::column_def), name(col_name), type(col_type), order(col_order), length(col_len),
null(nullable) {} null(nullable) {}
}; };
@ -98,7 +98,7 @@ namespace usql {
std::string function; // TODO use enum std::string function; // TODO use enum
std::vector<std::unique_ptr<Node>> params; std::vector<std::unique_ptr<Node>> params;
FunctionNode(const std::string func_name, std::vector<std::unique_ptr<Node>> pars) : FunctionNode(const std::string& func_name, std::vector<std::unique_ptr<Node>> pars) :
Node(NodeType::function), function(func_name), params(std::move(pars)) {} Node(NodeType::function), function(func_name), params(std::move(pars)) {}
}; };
@ -107,7 +107,7 @@ namespace usql {
}; };
struct ValueNode : Node { struct ValueNode : Node {
ValueNode(NodeType type) : Node(type) {} explicit ValueNode(NodeType type) : Node(type) {}
virtual bool isNull() { return false; } virtual bool isNull() { return false; }
virtual long getIntegerValue() = 0; virtual long getIntegerValue() = 0;
@ -135,7 +135,7 @@ namespace usql {
struct IntValueNode : ValueNode { struct IntValueNode : ValueNode {
long value; long value;
IntValueNode(long value) : ValueNode(NodeType::int_value), value(value) {} explicit IntValueNode(long value) : ValueNode(NodeType::int_value), value(value) {}
long getIntegerValue() override { return value; }; long getIntegerValue() override { return value; };
double getDoubleValue() override { return (double) value; }; double getDoubleValue() override { return (double) value; };
@ -147,7 +147,7 @@ namespace usql {
struct DoubleValueNode : ValueNode { struct DoubleValueNode : ValueNode {
double value; double value;
DoubleValueNode(double value) : ValueNode(NodeType::float_value), value(value) {} explicit DoubleValueNode(double value) : ValueNode(NodeType::float_value), value(value) {}
long getIntegerValue() override { return (long) value; }; long getIntegerValue() override { return (long) value; };
double getDoubleValue() override { return value; }; double getDoubleValue() override { return value; };
@ -159,7 +159,7 @@ namespace usql {
struct StringValueNode : ValueNode { struct StringValueNode : ValueNode {
std::string value; std::string value;
StringValueNode(std::string value) : ValueNode(NodeType::string_value), value(value) {} explicit StringValueNode(const std::string &value) : ValueNode(NodeType::string_value), value(value) {}
long getIntegerValue() override { return std::stoi(value); }; long getIntegerValue() override { return std::stoi(value); };
double getDoubleValue() override { return std::stod(value); }; double getDoubleValue() override { return std::stod(value); };
@ -171,7 +171,7 @@ namespace usql {
struct BooleanValueNode : ValueNode { struct BooleanValueNode : ValueNode {
bool value; bool value;
BooleanValueNode(bool value) : ValueNode(NodeType::bool_value), value(value) {} explicit BooleanValueNode(bool value) : ValueNode(NodeType::bool_value), value(value) {}
long getIntegerValue() override { return (long) value; }; long getIntegerValue() override { return (long) value; };
double getDoubleValue() override { return (double) value; }; double getDoubleValue() override { return (double) value; };
@ -184,7 +184,7 @@ namespace usql {
struct DatabaseValueNode : Node { struct DatabaseValueNode : Node {
std::string col_name; std::string col_name;
DatabaseValueNode(std::string name) : Node(NodeType::database_value), col_name(name) {} explicit DatabaseValueNode(const std::string &name) : Node(NodeType::database_value), col_name(name) {}
}; };
enum class LogicalOperatorType { enum class LogicalOperatorType {
@ -245,7 +245,7 @@ namespace usql {
std::vector<ColDefNode> cols_defs; std::vector<ColDefNode> cols_defs;
CreateTableNode(const std::string& name, std::vector<ColDefNode> defs) : CreateTableNode(const std::string& name, std::vector<ColDefNode> defs) :
Node(NodeType::create_table), table_name(name), cols_defs(defs) {} Node(NodeType::create_table), table_name(name), cols_defs(std::move(defs)) {}
}; };
struct InsertIntoTableNode : Node { struct InsertIntoTableNode : Node {
@ -253,8 +253,8 @@ namespace usql {
std::vector<DatabaseValueNode> cols_names; std::vector<DatabaseValueNode> cols_names;
std::vector<std::unique_ptr<Node>> cols_values; std::vector<std::unique_ptr<Node>> cols_values;
InsertIntoTableNode(const std::string name, std::vector<DatabaseValueNode> names, std::vector<std::unique_ptr<Node>> values) : InsertIntoTableNode(const std::string& name, std::vector<DatabaseValueNode> names, std::vector<std::unique_ptr<Node>> values) :
Node(NodeType::insert_into), table_name(name), cols_names(names), cols_values(std::move(values)) {} Node(NodeType::insert_into), table_name(name), cols_names(std::move(names)), cols_values(std::move(values)) {}
}; };
struct SelectFromTableNode : Node { struct SelectFromTableNode : Node {
@ -266,14 +266,14 @@ namespace usql {
bool distinct; bool distinct;
SelectFromTableNode(std::string name, std::unique_ptr<std::vector<SelectColNode>> names, std::unique_ptr<Node> where_clause, std::vector<ColOrderNode> orderby, OffsetLimitNode offlim, bool distinct_): SelectFromTableNode(std::string name, std::unique_ptr<std::vector<SelectColNode>> names, std::unique_ptr<Node> where_clause, std::vector<ColOrderNode> orderby, OffsetLimitNode offlim, bool distinct_):
Node(NodeType::select_from), table_name(name), cols_names(std::move(names)), where(std::move(where_clause)), order_by(orderby), offset_limit(offlim), distinct(distinct_) {} Node(NodeType::select_from), table_name(std::move(name)), cols_names(std::move(names)), where(std::move(where_clause)), order_by(std::move(orderby)), offset_limit(offlim), distinct(distinct_) {}
}; };
struct CreateTableAsSelectNode : Node { struct CreateTableAsSelectNode : Node {
std::string table_name; std::string table_name;
std::unique_ptr<Node> select_table; std::unique_ptr<Node> select_table;
CreateTableAsSelectNode(const std::string name, std::unique_ptr<Node> table) : CreateTableAsSelectNode(const std::string& name, std::unique_ptr<Node> table) :
Node(NodeType::create_table_as_select), table_name(name), select_table(std::move(table)) {} Node(NodeType::create_table_as_select), table_name(name), select_table(std::move(table)) {}
}; };
@ -283,7 +283,7 @@ namespace usql {
std::vector<std::unique_ptr<Node>> values; std::vector<std::unique_ptr<Node>> values;
std::unique_ptr<Node> where; std::unique_ptr<Node> where;
UpdateTableNode(std::string name, std::vector<DatabaseValueNode> names, std::vector<std::unique_ptr<Node>> vals, UpdateTableNode(const std::string &name, std::vector<DatabaseValueNode> names, std::vector<std::unique_ptr<Node>> vals,
std::unique_ptr<Node> where_clause) : std::unique_ptr<Node> where_clause) :
Node(NodeType::update_table), table_name(name), cols_names(names), values(std::move(vals)), Node(NodeType::update_table), table_name(name), cols_names(names), values(std::move(vals)),
where(std::move(where_clause)) {} where(std::move(where_clause)) {}
@ -293,7 +293,7 @@ namespace usql {
std::string table_name; std::string table_name;
std::string filename; std::string filename;
LoadIntoTableNode(const std::string name, std::string file) : LoadIntoTableNode(const std::string& name, const std::string &file) :
Node(NodeType::load_table), table_name(name), filename(file) {} Node(NodeType::load_table), table_name(name), filename(file) {}
}; };
@ -301,14 +301,14 @@ namespace usql {
std::string table_name; std::string table_name;
std::string filename; std::string filename;
SaveTableNode(const std::string& name, std::string file) : SaveTableNode(const std::string& name, const std::string &file) :
Node(NodeType::save_table), table_name(name), filename(file) {} Node(NodeType::save_table), table_name(name), filename(file) {}
}; };
struct DropTableNode : Node { struct DropTableNode : Node {
std::string table_name; std::string table_name;
DropTableNode(const std::string& name) : Node(NodeType::drop_table), table_name(name) {} explicit DropTableNode(const std::string& name) : Node(NodeType::drop_table), table_name(name) {}
}; };
struct DeleteFromTableNode : Node { struct DeleteFromTableNode : Node {
@ -330,7 +330,7 @@ namespace usql {
struct ShowNode : Node { struct ShowNode : Node {
std::string name; std::string name;
ShowNode(const std::string& name_) : Node(NodeType::show), name(name_) {} explicit ShowNode(const std::string& name_) : Node(NodeType::show), name(name_) {}
}; };

15
row.cpp
View File

@ -56,24 +56,19 @@ Row::Row(const Row &other) {
ColumnType col_type = other.m_columns[i]->getColType(); ColumnType col_type = other.m_columns[i]->getColType();
switch (col_type) { switch (col_type) {
case ColumnType::integer_type : case ColumnType::integer_type :
setIntColumnValue(i, setIntColumnValue(i, (static_cast<ColIntegerValue *>(other.m_columns[i].get())->getIntValue()));
(static_cast<ColIntegerValue *>(other.m_columns[i].get())->getIntValue()));
break; break;
case ColumnType::float_type : case ColumnType::float_type :
setFloatColumnValue(i, setFloatColumnValue(i, (static_cast<ColDoubleValue *>(other.m_columns[i].get())->getDoubleValue()));
(static_cast<ColDoubleValue *>(other.m_columns[i].get())->getDoubleValue()));
break; break;
case ColumnType::varchar_type : case ColumnType::varchar_type :
setStringColumnValue(i, setStringColumnValue(i, (static_cast<ColStringValue *>(other.m_columns[i].get())->getStringValue()));
(static_cast<ColStringValue *>(other.m_columns[i].get())->getStringValue()));
break; break;
case ColumnType::date_type : case ColumnType::date_type :
setDateColumnValue(i, setDateColumnValue(i, (static_cast<ColDateValue *>(other.m_columns[i].get())->getDateValue()));
(static_cast<ColDateValue *>(other.m_columns[i].get())->getDateValue()));
break; break;
case ColumnType::bool_type : case ColumnType::bool_type :
setBoolColumnValue(i, setBoolColumnValue(i, (static_cast<ColBooleanValue *>(other.m_columns[i].get())->getBoolValue()));
(static_cast<ColBooleanValue *>(other.m_columns[i].get())->getBoolValue()));
break; break;
default: default:
throw Exception("unsupported column type"); throw Exception("unsupported column type");

12
row.h
View File

@ -38,7 +38,7 @@ namespace usql {
struct ColIntegerValue : ColValue { struct ColIntegerValue : ColValue {
ColIntegerValue(long value) : m_integer(value) {}; explicit ColIntegerValue(long value) : m_integer(value) {};
ColIntegerValue(const ColIntegerValue &other) : m_integer(other.m_integer) {}; ColIntegerValue(const ColIntegerValue &other) : m_integer(other.m_integer) {};
ColumnType getColType() override { return ColumnType::integer_type; }; ColumnType getColType() override { return ColumnType::integer_type; };
@ -55,7 +55,7 @@ namespace usql {
struct ColDoubleValue : ColValue { struct ColDoubleValue : ColValue {
ColDoubleValue(double value) : m_double(value) {}; explicit ColDoubleValue(double value) : m_double(value) {};
ColDoubleValue(const ColDoubleValue &other) : m_double(other.m_double) {} ColDoubleValue(const ColDoubleValue &other) : m_double(other.m_double) {}
ColumnType getColType() override { return ColumnType::float_type; }; ColumnType getColType() override { return ColumnType::float_type; };
@ -72,7 +72,7 @@ namespace usql {
struct ColStringValue : ColValue { struct ColStringValue : ColValue {
ColStringValue(const std::string &value) : m_string(value) {}; explicit ColStringValue(const std::string &value) : m_string(value) {};
ColStringValue(const ColStringValue &other) : m_string(other.m_string) {}; ColStringValue(const ColStringValue &other) : m_string(other.m_string) {};
ColumnType getColType() override { return ColumnType::varchar_type; }; ColumnType getColType() override { return ColumnType::varchar_type; };
@ -88,7 +88,7 @@ namespace usql {
}; };
struct ColDateValue : ColValue { struct ColDateValue : ColValue {
ColDateValue(long value) : m_date(value) {}; explicit ColDateValue(long value) : m_date(value) {};
ColDateValue(const ColDateValue &other) : m_date(other.m_date) {}; ColDateValue(const ColDateValue &other) : m_date(other.m_date) {};
ColumnType getColType() override { return ColumnType::date_type; }; ColumnType getColType() override { return ColumnType::date_type; };
@ -104,7 +104,7 @@ namespace usql {
}; };
struct ColBooleanValue : ColValue { struct ColBooleanValue : ColValue {
ColBooleanValue(bool value) : m_bool(value) {}; explicit ColBooleanValue(bool value) : m_bool(value) {};
ColBooleanValue(const ColBooleanValue &other) : m_bool(other.m_bool) {}; ColBooleanValue(const ColBooleanValue &other) : m_bool(other.m_bool) {};
ColumnType getColType() override { return ColumnType::bool_type; }; ColumnType getColType() override { return ColumnType::bool_type; };
@ -122,7 +122,7 @@ namespace usql {
class Row { class Row {
public: public:
Row(int cols_count); explicit Row(int cols_count);
Row(const Row &other); Row(const Row &other);
Row &operator=(Row other); Row &operator=(Row other);

View File

@ -15,8 +15,8 @@ namespace usql {
ColDefNode get_column_def(const std::string &col_name); ColDefNode get_column_def(const std::string &col_name);
ColDefNode get_column_def(int col_index); ColDefNode get_column_def(int col_index);
int columns_count() const { return (int) m_col_defs.size(); }; [[nodiscard]] int columns_count() const { return (int) m_col_defs.size(); };
size_t rows_count() const { return m_rows.size(); }; [[nodiscard]] size_t rows_count() const { return m_rows.size(); };
Row& create_empty_row(); Row& create_empty_row();
void commit_row(const Row &row); void commit_row(const Row &row);

View File

@ -265,50 +265,25 @@ void USql::execute_offset_limit(OffsetLimitNode &node, Table *result) {
std::tuple<int, ColDefNode> USql::get_column_definition(Table *table, SelectColNode *select_col_node, int col_order ) { std::tuple<int, ColDefNode> USql::get_column_definition(Table *table, SelectColNode *select_col_node, int col_order ) {
return get_node_definition(table, select_col_node->value.get(), select_col_node->name, col_order ); return get_node_definition(table, select_col_node->value.get(), select_col_node->name, col_order );
// if (select_col_node->value->node_type == NodeType::database_value) {
// ColDefNode src_cdef = table->get_column_def(new_col_name);
// ColDefNode cdef = ColDefNode{new_col_name, src_cdef.type, col_order, src_cdef.length, src_cdef.null};
// return std::make_tuple(src_cdef.order, cdef);
//
// } else if (select_col_node->value->node_type == NodeType::function) {
// auto node = static_cast<FunctionNode *>(select_col_node->value.get());
//
// if (node->function == "to_string") {
// ColDefNode cdef = ColDefNode{new_col_name, ColumnType::varchar_type, col_order, 32, true};
// return std::make_tuple(-1, cdef);
// } else if (node->function == "to_date") {
// ColDefNode cdef = ColDefNode{new_col_name, ColumnType::integer_type, col_order, 1, true};
// return std::make_tuple(-1, cdef);
// }
// throw Exception("Unsupported function");
//
// } else if (select_col_node->value->node_type == NodeType::arithmetical_operator) {
// // TODO return correct type
// // hierarchicaly go throuhg and deduce right type
// ColDefNode cdef = ColDefNode{new_col_name, ColumnType::float_type, col_order, 1, true};
// return std::make_tuple(-1, cdef);
// }
// throw Exception("Unsupported node type");
} }
std::tuple<int, ColDefNode> USql::get_node_definition(Table *table, Node * node, const std::string & col_name, int col_order ) { std::tuple<int, ColDefNode> USql::get_node_definition(Table *table, Node * node, const std::string & col_name, int col_order ) {
if (node->node_type == NodeType::database_value) { if (node->node_type == NodeType::database_value) {
auto dbval_node = static_cast<DatabaseValueNode *>(node); auto dbval_node = static_cast<DatabaseValueNode *>(node);
ColDefNode src_cdef = table->get_column_def(dbval_node->col_name); ColDefNode src_col_def = table->get_column_def(dbval_node->col_name);
ColDefNode cdef = ColDefNode{col_name, src_cdef.type, col_order, src_cdef.length, src_cdef.null}; ColDefNode col_def = ColDefNode{col_name, src_col_def.type, col_order, src_col_def.length, src_col_def.null};
return std::make_tuple(src_cdef.order, cdef); return std::make_tuple(src_col_def.order, col_def);
} else if (node->node_type == NodeType::function) { } else if (node->node_type == NodeType::function) {
auto func_node = static_cast<FunctionNode *>(node); auto func_node = static_cast<FunctionNode *>(node);
if (func_node->function == "to_string") { if (func_node->function == "to_string") {
ColDefNode cdef = ColDefNode{col_name, ColumnType::varchar_type, col_order, 32, true}; ColDefNode col_def = ColDefNode{col_name, ColumnType::varchar_type, col_order, 32, true};
return std::make_tuple(-1, cdef); return std::make_tuple(-1, col_def);
} else if (func_node->function == "to_date") { } else if (func_node->function == "to_date") {
ColDefNode cdef = ColDefNode{col_name, ColumnType::integer_type, col_order, 1, true}; ColDefNode col_def = ColDefNode{col_name, ColumnType::integer_type, col_order, 1, true};
return std::make_tuple(-1, cdef); return std::make_tuple(-1, col_def);
} }
throw Exception("Unsupported function"); throw Exception("Unsupported function");
@ -324,25 +299,25 @@ std::tuple<int, ColDefNode> USql::get_node_definition(Table *table, Node * node,
else else
col_type = ColumnType::integer_type; col_type = ColumnType::integer_type;
ColDefNode cdef = ColDefNode{col_name, col_type, col_order, 1, true}; ColDefNode col_def = ColDefNode{col_name, col_type, col_order, 1, true};
return std::make_tuple(-1, cdef); return std::make_tuple(-1, col_def);
} else if (node->node_type == NodeType::logical_operator) { } else if (node->node_type == NodeType::logical_operator) {
ColDefNode cdef = ColDefNode{col_name, ColumnType::bool_type, col_order, 1, true}; ColDefNode col_def = ColDefNode{col_name, ColumnType::bool_type, col_order, 1, true};
return std::make_tuple(-1, cdef); return std::make_tuple(-1, col_def);
} else if (node->node_type == NodeType::int_value) { } else if (node->node_type == NodeType::int_value) {
ColDefNode cdef = ColDefNode{col_name, ColumnType::integer_type, col_order, 1, true}; ColDefNode col_def = ColDefNode{col_name, ColumnType::integer_type, col_order, 1, true};
return std::make_tuple(-1, cdef); return std::make_tuple(-1, col_def);
} else if (node->node_type == NodeType::float_value) { } else if (node->node_type == NodeType::float_value) {
ColDefNode cdef = ColDefNode{col_name, ColumnType::float_type, col_order, 1, true}; ColDefNode col_def = ColDefNode{col_name, ColumnType::float_type, col_order, 1, true};
return std::make_tuple(-1, cdef); return std::make_tuple(-1, col_def);
} else if (node->node_type == NodeType::string_value) { } else if (node->node_type == NodeType::string_value) {
// TODO right len // TODO right len
ColDefNode cdef = ColDefNode{col_name, ColumnType::varchar_type, col_order, 64, true}; ColDefNode col_def = ColDefNode{col_name, ColumnType::varchar_type, col_order, 64, true};
return std::make_tuple(-1, cdef); return std::make_tuple(-1, col_def);
} }
throw Exception("Unsupported node type"); throw Exception("Unsupported node type");
} }
@ -546,8 +521,8 @@ std::unique_ptr<ValueNode> USql::eval_function_value_node(Table *table, Row &row
if (fnc->function == "to_string") { if (fnc->function == "to_string") {
long date = evaluatedPars[0]->getDateValue(); long date = evaluatedPars[0]->getDateValue();
std::string format = evaluatedPars[1]->getStringValue(); std::string format = evaluatedPars[1]->getStringValue();
std::string formated_date = date_to_string(date, format); std::string formatted_date = date_to_string(date, format);
return std::make_unique<StringValueNode>(formated_date); return std::make_unique<StringValueNode>(formatted_date);
} }
throw Exception("invalid function"); throw Exception("invalid function");

4
usql.h
View File

@ -23,8 +23,8 @@ private:
std::unique_ptr<Table> execute_load(LoadIntoTableNode &node); std::unique_ptr<Table> execute_load(LoadIntoTableNode &node);
std::unique_ptr<Table> execute_save(SaveTableNode &node); std::unique_ptr<Table> execute_save(SaveTableNode &node);
std::unique_ptr<Table> execute_drop(DropTableNode &node); std::unique_ptr<Table> execute_drop(DropTableNode &node);
std::unique_ptr<Table> execute_set(SetNode &node); static std::unique_ptr<Table> execute_set(SetNode &node);
std::unique_ptr<Table> execute_show(ShowNode &node); static std::unique_ptr<Table> execute_show(ShowNode &node);
std::unique_ptr<Table> execute_insert_into_table(InsertIntoTableNode &node); std::unique_ptr<Table> execute_insert_into_table(InsertIntoTableNode &node);
std::unique_ptr<Table> execute_select(SelectFromTableNode &node); std::unique_ptr<Table> execute_select(SelectFromTableNode &node);