diff --git a/executor.cpp b/executor.cpp index fe09c46..5f96dbe 100644 --- a/executor.cpp +++ b/executor.cpp @@ -103,37 +103,7 @@ bool Executor::execute_select(SelectFromTableNode& node) { for (auto row = begin (table->m_rows); row != end (table->m_rows); ++row) { // eval where for row - bool where_true = true; - if (node.where->node_type == NodeType::true_node) { // no where clause - where_true = true; - } else { // evaluate where - RelationalOperatorNode& filter = static_cast(*node.where); - if (filter.op == RelationalOperatorType::greater) { - std::unique_ptr left_value; - if ((*filter.left).node_type == NodeType::database_value) { - DatabaseValueNode *dvl = static_cast(filter.left.get()); - 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) { - left_value = std::make_unique(db_value->integerValue()); - } - // TODO other cases - } - - std::unique_ptr right_value; - if ((*filter.right).node_type == NodeType::int_value) { - IntValueNode *ivl = static_cast(filter.right.get()); - right_value = std::make_unique(ivl->value); - } - - IntValueNode* left_int_value = static_cast(left_value.get()); - IntValueNode* right_int_value = static_cast(right_value.get()); - - where_true = left_int_value->value > right_int_value->value; - } - } - - if (where_true) { + if (evalWhere(node, table, row)) { // prepare empty row Row new_row = result.createEmptyRow(); @@ -159,3 +129,70 @@ bool Executor::execute_select(SelectFromTableNode& node) { return true; } +bool Executor::evalWhere(const SelectFromTableNode &node, Table *table, + std::vector>::iterator &row) const { + if (node.where->node_type == NodeType::true_node) { // no where clause + return true; + } + + if (node.where.get()->node_type == NodeType::relational_operator) { + RelationalOperatorNode &filter = static_cast(*node.where); + return evalRelationalOperator(filter, table, row); + } + +// if (node.where.get()->node_type == NodeType::logical_operator) { +// LogicalOperatorNode &filter = static_cast(*node.where); +// return evalLogicalOperator(filter, table, row); +// } + + return false; +} + +bool Executor::evalRelationalOperator(const RelationalOperatorNode &filter, Table *table, std::vector>::iterator &row) const { + 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 + } + + 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; + } + + if (filter.op == RelationalOperatorType::equal) { + return left_int_value->value == right_int_value->value; + } + + throw Exception("invalid relational operator"); +} + +std::unique_ptr Executor::evalNode(Table *table, std::vector>::iterator &row, Node *filter) const { + if (filter->node_type == NodeType::database_value) { + DatabaseValueNode *dvl = static_cast(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 + + } else if (filter->node_type == NodeType::int_value) { + IntValueNode *ivl = static_cast(filter); + return std::make_unique(ivl->value); + + } else if (filter->node_type == NodeType::float_value) { + FloatValueNode *ivl = static_cast(filter); + return std::make_unique(ivl->value); + + } else if (filter->node_type == NodeType::string_value) { + StringValueNode *ivl = static_cast(filter); + return std::make_unique(ivl->value); + } + + throw Exception("invalid type"); +} + diff --git a/executor.h b/executor.h index b77fcd7..0ef4689 100644 --- a/executor.h +++ b/executor.h @@ -22,4 +22,13 @@ private: private: std::vector m_tables; + bool evalWhere(const SelectFromTableNode &node, Table *table, + std::vector>::iterator &row) const; + + std::unique_ptr + evalNode(Table *table, std::vector>::iterator &row, + Node *filter) const; + + bool evalRelationalOperator(const RelationalOperatorNode &filter, Table *table, + std::vector>::iterator &row) const; };