more work towards working where

This commit is contained in:
VaclavT 2021-07-06 18:50:03 +02:00
parent e88255c3c3
commit 199a5f9e12
4 changed files with 64 additions and 9 deletions

View File

@ -152,19 +152,65 @@ bool Executor::evalRelationalOperator(const RelationalOperatorNode &filter, Tabl
std::unique_ptr<Node> left_value = evalNode(table, row, filter.left.get());
std::unique_ptr<Node> 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<IntValueNode *>(left_value.get());
IntValueNode *rvalue = static_cast<IntValueNode *>(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<IntValueNode *>(left_value.get());
FloatValueNode *rvalue = static_cast<FloatValueNode *>(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<IntValueNode *>(left_value.get());
StringValueNode *rvalue = static_cast<StringValueNode *>(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<FloatValueNode *>(left_value.get());
IntValueNode *rvalue = static_cast<IntValueNode *>(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<FloatValueNode *>(left_value.get());
FloatValueNode *rvalue = static_cast<FloatValueNode *>(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<FloatValueNode *>(left_value.get());
StringValueNode *rvalue = static_cast<StringValueNode *>(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<StringValueNode *>(left_value.get());
IntValueNode *rvalue = static_cast<IntValueNode *>(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<StringValueNode *>(left_value.get());
FloatValueNode *rvalue = static_cast<FloatValueNode *>(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<StringValueNode *>(left_value.get());
StringValueNode *rvalue = static_cast<StringValueNode *>(right_value.get());
comparator = lvalue->value.compare(rvalue->value);
}
IntValueNode *left_int_value = static_cast<IntValueNode *>(left_value.get());
IntValueNode *right_int_value = static_cast<IntValueNode *>(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<Node> Executor::evalNode(Table *table, std::vector<Row, std::all
DatabaseValueNode *dvl = static_cast<DatabaseValueNode *>(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<IntValueNode>(db_value->integerValue());
}
// TODO other cases
if (col_def.type == ColumnType::float_type) {
return std::make_unique<FloatValueNode>(db_value->floatValue());
}
if (col_def.type == ColumnType::varchar_type) {
return std::make_unique<StringValueNode>(db_value->stringValue());
}
} else if (filter->node_type == NodeType::int_value) {
IntValueNode *ivl = static_cast<IntValueNode *>(filter);

View File

@ -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;

View File

@ -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"

View File

@ -147,7 +147,7 @@ std::unique_ptr<Node> Parser::parse_select_from_table() {
std::unique_ptr<Node> 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();