split ugliness into parts

This commit is contained in:
VaclavT 2021-07-05 09:11:09 +02:00
parent b4711985b3
commit e88255c3c3
2 changed files with 77 additions and 31 deletions

View File

@ -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<RelationalOperatorNode &>(*node.where);
if (filter.op == RelationalOperatorType::greater) {
std::unique_ptr<Node> left_value;
if ((*filter.left).node_type == NodeType::database_value) {
DatabaseValueNode *dvl = static_cast<DatabaseValueNode*>(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<IntValueNode>(db_value->integerValue());
}
// TODO other cases
}
std::unique_ptr<Node> right_value;
if ((*filter.right).node_type == NodeType::int_value) {
IntValueNode *ivl = static_cast<IntValueNode*>(filter.right.get());
right_value = std::make_unique<IntValueNode>(ivl->value);
}
IntValueNode* left_int_value = static_cast<IntValueNode *>(left_value.get());
IntValueNode* right_int_value = static_cast<IntValueNode *>(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<Row, std::allocator<Row>>::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<RelationalOperatorNode &>(*node.where);
return evalRelationalOperator(filter, table, row);
}
// if (node.where.get()->node_type == NodeType::logical_operator) {
// LogicalOperatorNode &filter = static_cast<LogicalOperatorNode &>(*node.where);
// return evalLogicalOperator(filter, table, row);
// }
return false;
}
bool Executor::evalRelationalOperator(const RelationalOperatorNode &filter, Table *table, std::vector<Row, std::allocator<Row>>::iterator &row) const {
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
}
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;
}
if (filter.op == RelationalOperatorType::equal) {
return left_int_value->value == right_int_value->value;
}
throw Exception("invalid relational operator");
}
std::unique_ptr<Node> Executor::evalNode(Table *table, std::vector<Row, std::allocator<Row>>::iterator &row, Node *filter) const {
if (filter->node_type == NodeType::database_value) {
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
} else if (filter->node_type == NodeType::int_value) {
IntValueNode *ivl = static_cast<IntValueNode *>(filter);
return std::make_unique<IntValueNode>(ivl->value);
} else if (filter->node_type == NodeType::float_value) {
FloatValueNode *ivl = static_cast<FloatValueNode*>(filter);
return std::make_unique<FloatValueNode>(ivl->value);
} else if (filter->node_type == NodeType::string_value) {
StringValueNode *ivl = static_cast<StringValueNode*>(filter);
return std::make_unique<StringValueNode>(ivl->value);
}
throw Exception("invalid type");
}

View File

@ -22,4 +22,13 @@ private:
private:
std::vector<Table> m_tables;
bool evalWhere(const SelectFromTableNode &node, Table *table,
std::vector<Row, std::allocator<Row>>::iterator &row) const;
std::unique_ptr<Node>
evalNode(Table *table, std::vector<Row, std::allocator<Row>>::iterator &row,
Node *filter) const;
bool evalRelationalOperator(const RelationalOperatorNode &filter, Table *table,
std::vector<Row, std::allocator<Row>>::iterator &row) const;
};