small refactorings
This commit is contained in:
parent
b37b0b55ff
commit
b03462da6a
|
|
@ -96,8 +96,6 @@ namespace usql {
|
|||
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) {
|
||||
return (token_type == TokenType::equal || token_type == TokenType::not_equal ||
|
||||
token_type == TokenType::greater || token_type == TokenType::greater_equal ||
|
||||
|
|
|
|||
2
lexer.h
2
lexer.h
|
|
@ -95,8 +95,6 @@ namespace usql {
|
|||
|
||||
TokenType nextTokenType();
|
||||
|
||||
TokenType prevTokenType();
|
||||
|
||||
static bool isRelationalOperator(TokenType token_type);
|
||||
|
||||
static bool isLogicalOperator(TokenType token_type);
|
||||
|
|
|
|||
42
parser.h
42
parser.h
|
|
@ -51,7 +51,7 @@ namespace usql {
|
|||
struct Node {
|
||||
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;
|
||||
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) {}
|
||||
};
|
||||
|
||||
|
|
@ -78,7 +78,7 @@ namespace usql {
|
|||
std::unique_ptr<Node> value;
|
||||
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) {}
|
||||
};
|
||||
|
||||
|
|
@ -89,7 +89,7 @@ namespace usql {
|
|||
int length;
|
||||
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),
|
||||
null(nullable) {}
|
||||
};
|
||||
|
|
@ -98,7 +98,7 @@ namespace usql {
|
|||
std::string function; // TODO use enum
|
||||
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)) {}
|
||||
};
|
||||
|
||||
|
|
@ -107,7 +107,7 @@ namespace usql {
|
|||
};
|
||||
|
||||
struct ValueNode : Node {
|
||||
ValueNode(NodeType type) : Node(type) {}
|
||||
explicit ValueNode(NodeType type) : Node(type) {}
|
||||
|
||||
virtual bool isNull() { return false; }
|
||||
virtual long getIntegerValue() = 0;
|
||||
|
|
@ -135,7 +135,7 @@ namespace usql {
|
|||
struct IntValueNode : ValueNode {
|
||||
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; };
|
||||
double getDoubleValue() override { return (double) value; };
|
||||
|
|
@ -147,7 +147,7 @@ namespace usql {
|
|||
struct DoubleValueNode : ValueNode {
|
||||
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; };
|
||||
double getDoubleValue() override { return value; };
|
||||
|
|
@ -159,7 +159,7 @@ namespace usql {
|
|||
struct StringValueNode : ValueNode {
|
||||
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); };
|
||||
double getDoubleValue() override { return std::stod(value); };
|
||||
|
|
@ -171,7 +171,7 @@ namespace usql {
|
|||
struct BooleanValueNode : ValueNode {
|
||||
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; };
|
||||
double getDoubleValue() override { return (double) value; };
|
||||
|
|
@ -184,7 +184,7 @@ namespace usql {
|
|||
struct DatabaseValueNode : Node {
|
||||
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 {
|
||||
|
|
@ -245,7 +245,7 @@ namespace usql {
|
|||
std::vector<ColDefNode> cols_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 {
|
||||
|
|
@ -253,8 +253,8 @@ namespace usql {
|
|||
std::vector<DatabaseValueNode> cols_names;
|
||||
std::vector<std::unique_ptr<Node>> cols_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)) {}
|
||||
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(std::move(names)), cols_values(std::move(values)) {}
|
||||
};
|
||||
|
||||
struct SelectFromTableNode : Node {
|
||||
|
|
@ -266,14 +266,14 @@ namespace usql {
|
|||
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 {
|
||||
std::string table_name;
|
||||
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)) {}
|
||||
};
|
||||
|
||||
|
|
@ -283,7 +283,7 @@ namespace usql {
|
|||
std::vector<std::unique_ptr<Node>> values;
|
||||
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) :
|
||||
Node(NodeType::update_table), table_name(name), cols_names(names), values(std::move(vals)),
|
||||
where(std::move(where_clause)) {}
|
||||
|
|
@ -293,7 +293,7 @@ namespace usql {
|
|||
std::string table_name;
|
||||
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) {}
|
||||
};
|
||||
|
||||
|
|
@ -301,14 +301,14 @@ namespace usql {
|
|||
std::string table_name;
|
||||
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) {}
|
||||
};
|
||||
|
||||
struct DropTableNode : Node {
|
||||
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 {
|
||||
|
|
@ -330,7 +330,7 @@ namespace usql {
|
|||
struct ShowNode : Node {
|
||||
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
15
row.cpp
|
|
@ -56,24 +56,19 @@ Row::Row(const Row &other) {
|
|||
ColumnType col_type = other.m_columns[i]->getColType();
|
||||
switch (col_type) {
|
||||
case ColumnType::integer_type :
|
||||
setIntColumnValue(i,
|
||||
(static_cast<ColIntegerValue *>(other.m_columns[i].get())->getIntValue()));
|
||||
setIntColumnValue(i, (static_cast<ColIntegerValue *>(other.m_columns[i].get())->getIntValue()));
|
||||
break;
|
||||
case ColumnType::float_type :
|
||||
setFloatColumnValue(i,
|
||||
(static_cast<ColDoubleValue *>(other.m_columns[i].get())->getDoubleValue()));
|
||||
setFloatColumnValue(i, (static_cast<ColDoubleValue *>(other.m_columns[i].get())->getDoubleValue()));
|
||||
break;
|
||||
case ColumnType::varchar_type :
|
||||
setStringColumnValue(i,
|
||||
(static_cast<ColStringValue *>(other.m_columns[i].get())->getStringValue()));
|
||||
setStringColumnValue(i, (static_cast<ColStringValue *>(other.m_columns[i].get())->getStringValue()));
|
||||
break;
|
||||
case ColumnType::date_type :
|
||||
setDateColumnValue(i,
|
||||
(static_cast<ColDateValue *>(other.m_columns[i].get())->getDateValue()));
|
||||
setDateColumnValue(i, (static_cast<ColDateValue *>(other.m_columns[i].get())->getDateValue()));
|
||||
break;
|
||||
case ColumnType::bool_type :
|
||||
setBoolColumnValue(i,
|
||||
(static_cast<ColBooleanValue *>(other.m_columns[i].get())->getBoolValue()));
|
||||
setBoolColumnValue(i, (static_cast<ColBooleanValue *>(other.m_columns[i].get())->getBoolValue()));
|
||||
break;
|
||||
default:
|
||||
throw Exception("unsupported column type");
|
||||
|
|
|
|||
12
row.h
12
row.h
|
|
@ -38,7 +38,7 @@ namespace usql {
|
|||
|
||||
|
||||
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) {};
|
||||
|
||||
ColumnType getColType() override { return ColumnType::integer_type; };
|
||||
|
|
@ -55,7 +55,7 @@ namespace usql {
|
|||
|
||||
|
||||
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) {}
|
||||
|
||||
ColumnType getColType() override { return ColumnType::float_type; };
|
||||
|
|
@ -72,7 +72,7 @@ namespace usql {
|
|||
|
||||
|
||||
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) {};
|
||||
|
||||
ColumnType getColType() override { return ColumnType::varchar_type; };
|
||||
|
|
@ -88,7 +88,7 @@ namespace usql {
|
|||
};
|
||||
|
||||
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) {};
|
||||
|
||||
ColumnType getColType() override { return ColumnType::date_type; };
|
||||
|
|
@ -104,7 +104,7 @@ namespace usql {
|
|||
};
|
||||
|
||||
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) {};
|
||||
|
||||
ColumnType getColType() override { return ColumnType::bool_type; };
|
||||
|
|
@ -122,7 +122,7 @@ namespace usql {
|
|||
class Row {
|
||||
|
||||
public:
|
||||
Row(int cols_count);
|
||||
explicit Row(int cols_count);
|
||||
Row(const Row &other);
|
||||
|
||||
Row &operator=(Row other);
|
||||
|
|
|
|||
4
table.h
4
table.h
|
|
@ -15,8 +15,8 @@ namespace usql {
|
|||
ColDefNode get_column_def(const std::string &col_name);
|
||||
ColDefNode get_column_def(int col_index);
|
||||
|
||||
int columns_count() const { return (int) m_col_defs.size(); };
|
||||
size_t rows_count() const { return m_rows.size(); };
|
||||
[[nodiscard]] int columns_count() const { return (int) m_col_defs.size(); };
|
||||
[[nodiscard]] size_t rows_count() const { return m_rows.size(); };
|
||||
|
||||
Row& create_empty_row();
|
||||
void commit_row(const Row &row);
|
||||
|
|
|
|||
63
usql.cpp
63
usql.cpp
|
|
@ -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 ) {
|
||||
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 ) {
|
||||
if (node->node_type == NodeType::database_value) {
|
||||
auto dbval_node = static_cast<DatabaseValueNode *>(node);
|
||||
|
||||
ColDefNode src_cdef = table->get_column_def(dbval_node->col_name);
|
||||
ColDefNode cdef = ColDefNode{col_name, src_cdef.type, col_order, src_cdef.length, src_cdef.null};
|
||||
return std::make_tuple(src_cdef.order, cdef);
|
||||
ColDefNode src_col_def = table->get_column_def(dbval_node->col_name);
|
||||
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_col_def.order, col_def);
|
||||
|
||||
} else if (node->node_type == NodeType::function) {
|
||||
auto func_node = static_cast<FunctionNode *>(node);
|
||||
|
||||
if (func_node->function == "to_string") {
|
||||
ColDefNode cdef = ColDefNode{col_name, ColumnType::varchar_type, col_order, 32, true};
|
||||
return std::make_tuple(-1, cdef);
|
||||
ColDefNode col_def = ColDefNode{col_name, ColumnType::varchar_type, col_order, 32, true};
|
||||
return std::make_tuple(-1, col_def);
|
||||
} else if (func_node->function == "to_date") {
|
||||
ColDefNode cdef = ColDefNode{col_name, ColumnType::integer_type, col_order, 1, true};
|
||||
return std::make_tuple(-1, cdef);
|
||||
ColDefNode col_def = ColDefNode{col_name, ColumnType::integer_type, col_order, 1, true};
|
||||
return std::make_tuple(-1, col_def);
|
||||
}
|
||||
throw Exception("Unsupported function");
|
||||
|
||||
|
|
@ -324,25 +299,25 @@ std::tuple<int, ColDefNode> USql::get_node_definition(Table *table, Node * node,
|
|||
else
|
||||
col_type = ColumnType::integer_type;
|
||||
|
||||
ColDefNode cdef = ColDefNode{col_name, col_type, col_order, 1, true};
|
||||
return std::make_tuple(-1, cdef);
|
||||
ColDefNode col_def = ColDefNode{col_name, col_type, col_order, 1, true};
|
||||
return std::make_tuple(-1, col_def);
|
||||
|
||||
} else if (node->node_type == NodeType::logical_operator) {
|
||||
ColDefNode cdef = ColDefNode{col_name, ColumnType::bool_type, col_order, 1, true};
|
||||
return std::make_tuple(-1, cdef);
|
||||
ColDefNode col_def = ColDefNode{col_name, ColumnType::bool_type, col_order, 1, true};
|
||||
return std::make_tuple(-1, col_def);
|
||||
|
||||
} else if (node->node_type == NodeType::int_value) {
|
||||
ColDefNode cdef = ColDefNode{col_name, ColumnType::integer_type, col_order, 1, true};
|
||||
return std::make_tuple(-1, cdef);
|
||||
ColDefNode col_def = ColDefNode{col_name, ColumnType::integer_type, col_order, 1, true};
|
||||
return std::make_tuple(-1, col_def);
|
||||
|
||||
} else if (node->node_type == NodeType::float_value) {
|
||||
ColDefNode cdef = ColDefNode{col_name, ColumnType::float_type, col_order, 1, true};
|
||||
return std::make_tuple(-1, cdef);
|
||||
ColDefNode col_def = ColDefNode{col_name, ColumnType::float_type, col_order, 1, true};
|
||||
return std::make_tuple(-1, col_def);
|
||||
|
||||
} else if (node->node_type == NodeType::string_value) {
|
||||
// TODO right len
|
||||
ColDefNode cdef = ColDefNode{col_name, ColumnType::varchar_type, col_order, 64, true};
|
||||
return std::make_tuple(-1, cdef);
|
||||
ColDefNode col_def = ColDefNode{col_name, ColumnType::varchar_type, col_order, 64, true};
|
||||
return std::make_tuple(-1, col_def);
|
||||
}
|
||||
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") {
|
||||
long date = evaluatedPars[0]->getDateValue();
|
||||
std::string format = evaluatedPars[1]->getStringValue();
|
||||
std::string formated_date = date_to_string(date, format);
|
||||
return std::make_unique<StringValueNode>(formated_date);
|
||||
std::string formatted_date = date_to_string(date, format);
|
||||
return std::make_unique<StringValueNode>(formatted_date);
|
||||
}
|
||||
|
||||
throw Exception("invalid function");
|
||||
|
|
|
|||
4
usql.h
4
usql.h
|
|
@ -23,8 +23,8 @@ private:
|
|||
std::unique_ptr<Table> execute_load(LoadIntoTableNode &node);
|
||||
std::unique_ptr<Table> execute_save(SaveTableNode &node);
|
||||
std::unique_ptr<Table> execute_drop(DropTableNode &node);
|
||||
std::unique_ptr<Table> execute_set(SetNode &node);
|
||||
std::unique_ptr<Table> execute_show(ShowNode &node);
|
||||
static std::unique_ptr<Table> execute_set(SetNode &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_select(SelectFromTableNode &node);
|
||||
|
|
|
|||
Loading…
Reference in New Issue