From 199a5f9e12f07965eeea9d5de3045259b351323c Mon Sep 17 00:00:00 2001 From: VaclavT Date: Tue, 6 Jul 2021 18:50:03 +0200 Subject: [PATCH] more work towards working where --- executor.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++------ lexer.cpp | 1 + main.cpp | 4 +++- parser.cpp | 2 +- 4 files changed, 64 insertions(+), 9 deletions(-) diff --git a/executor.cpp b/executor.cpp index 5f96dbe..598e070 100644 --- a/executor.cpp +++ b/executor.cpp @@ -152,19 +152,65 @@ bool Executor::evalRelationalOperator(const RelationalOperatorNode &filter, Tabl std::unique_ptr left_value = evalNode(table, row, filter.left.get()); std::unique_ptr right_value = evalNode(table, row, filter.right.get()); - if (left_value->node_type == NodeType::int_value) { - // zjisti typy a vzit hodnoty k porovnani + double comparator; + + if (left_value->node_type == NodeType::int_value && right_value->node_type == NodeType::int_value) { + IntValueNode *lvalue = static_cast(left_value.get()); + IntValueNode *rvalue = static_cast(right_value.get()); + comparator = lvalue->value - rvalue->value; + } + if (left_value->node_type == NodeType::int_value && right_value->node_type == NodeType::float_value) { + IntValueNode *lvalue = static_cast(left_value.get()); + FloatValueNode *rvalue = static_cast(right_value.get()); + comparator = (double)lvalue->value - rvalue->value; + } + if (left_value->node_type == NodeType::int_value && right_value->node_type == NodeType::string_value) { + IntValueNode *lvalue = static_cast(left_value.get()); + StringValueNode *rvalue = static_cast(right_value.get()); + comparator = std::to_string(lvalue->value).compare(rvalue->value); + } + + + if (left_value->node_type == NodeType::float_value && right_value->node_type == NodeType::int_value) { + FloatValueNode *lvalue = static_cast(left_value.get()); + IntValueNode *rvalue = static_cast(right_value.get()); + comparator = lvalue->value - (double)rvalue->value; + } + if (left_value->node_type == NodeType::float_value && right_value->node_type == NodeType::float_value) { + FloatValueNode *lvalue = static_cast(left_value.get()); + FloatValueNode *rvalue = static_cast(right_value.get()); + comparator = lvalue->value - rvalue->value; + } + if (left_value->node_type == NodeType::float_value && right_value->node_type == NodeType::string_value) { + FloatValueNode *lvalue = static_cast(left_value.get()); + StringValueNode *rvalue = static_cast(right_value.get()); + comparator = std::to_string(lvalue->value).compare(rvalue->value); + } + + + if (left_value->node_type == NodeType::string_value && right_value->node_type == NodeType::int_value) { + StringValueNode *lvalue = static_cast(left_value.get()); + IntValueNode *rvalue = static_cast(right_value.get()); + comparator = lvalue->value.compare(std::to_string(rvalue->value)); + } + if (left_value->node_type == NodeType::string_value && right_value->node_type == NodeType::float_value) { + StringValueNode *lvalue = static_cast(left_value.get()); + FloatValueNode *rvalue = static_cast(right_value.get()); + comparator = lvalue->value.compare(std::to_string(rvalue->value)); + } + if (left_value->node_type == NodeType::string_value && right_value->node_type == NodeType::string_value) { + StringValueNode *lvalue = static_cast(left_value.get()); + StringValueNode *rvalue = static_cast(right_value.get()); + comparator = lvalue->value.compare(rvalue->value); } - IntValueNode *left_int_value = static_cast(left_value.get()); - IntValueNode *right_int_value = static_cast(right_value.get()); if (filter.op == RelationalOperatorType::greater) { - return left_int_value->value > right_int_value->value; + return comparator > 0.0; } if (filter.op == RelationalOperatorType::equal) { - return left_int_value->value == right_int_value->value; + return comparator == 0.0; } throw Exception("invalid relational operator"); @@ -175,10 +221,16 @@ std::unique_ptr Executor::evalNode(Table *table, std::vector(filter); ColDefNode col_def = table->get_column_def(dvl->col_name); // TODO optimize it to just get this def once auto db_value = row->ithColum(col_def.order); + if (col_def.type == ColumnType::integer_type) { return std::make_unique(db_value->integerValue()); } - // TODO other cases + if (col_def.type == ColumnType::float_type) { + return std::make_unique(db_value->floatValue()); + } + if (col_def.type == ColumnType::varchar_type) { + return std::make_unique(db_value->stringValue()); + } } else if (filter->node_type == NodeType::int_value) { IntValueNode *ivl = static_cast(filter); diff --git a/lexer.cpp b/lexer.cpp index a9dbec3..e8de4e2 100644 --- a/lexer.cpp +++ b/lexer.cpp @@ -99,6 +99,7 @@ TokenType Lexer::type(const std::string &token) { std::regex double_regex("[0-9]+\\.[0-9]+"); std::regex identifier_regex("[A-Za-z]+[A-Za-z0-9_#]*"); + // TODO 'one is evaluated as identifier if (token == ";") return TokenType::semicolon; diff --git a/main.cpp b/main.cpp index 685f397..4d04f2e 100644 --- a/main.cpp +++ b/main.cpp @@ -17,7 +17,9 @@ int main(int argc, char *argv[]) { "insert into a (i, s) values(2, 'two')", "insert into a (i, s) values(3, 'two')", "insert into a (i, s) values(4, 'four')", - "select i, s from a where i > 2" + "select i, s from a where i > 2", + "select i, s from a where i = 1", + "select i, s from a where s = 'two'" // "update a set s = 'three' where i = 3" // "delete from a where i = 3" // "select i, s from a where i > 0" diff --git a/parser.cpp b/parser.cpp index 61a5ba8..3030c3a 100644 --- a/parser.cpp +++ b/parser.cpp @@ -147,7 +147,7 @@ std::unique_ptr Parser::parse_select_from_table() { std::unique_ptr Parser::parse_where_clause() { // TODO add support for multiple filters - // TODO add support for parenthises + // TODO add support for parenthesis auto left = parse_operand_node(); auto operation = parse_operator();