162 lines
5.5 KiB
C++
162 lines
5.5 KiB
C++
#include "executor.h"
|
|
#include "exception.h"
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
Executor::Executor() {
|
|
m_tables.clear();
|
|
}
|
|
|
|
Table* Executor::find_table(const std::string name) {
|
|
auto name_cmp = [name](Table t){ return t.m_name == name; };
|
|
auto table_def = std::find_if(begin(m_tables), end(m_tables), name_cmp );
|
|
if (table_def != std::end(m_tables)) {
|
|
return table_def.operator->();
|
|
} else {
|
|
throw Exception("table not found (" + name + ")");
|
|
}
|
|
}
|
|
|
|
|
|
bool Executor::execute(Node& node) {
|
|
// TODO optimize node here
|
|
switch (node.node_type) {
|
|
case NodeType::create_table:
|
|
return execute_create_table(static_cast<CreateTableNode &>(node));
|
|
case NodeType::insert_into:
|
|
return execute_insert_into_table(static_cast<InsertIntoTableNode &>(node));
|
|
case NodeType::select_from:
|
|
return execute_select(static_cast<SelectFromTableNode &>(node));
|
|
default:
|
|
// TODO error message
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
bool Executor::execute_create_table(CreateTableNode& node) {
|
|
// TODO check table does not exists
|
|
Table table{node.table_name, node.cols_defs};
|
|
m_tables.push_back(table);
|
|
|
|
return true;
|
|
}
|
|
|
|
bool Executor::execute_insert_into_table(InsertIntoTableNode& node) {
|
|
// TODO check column names.size = values.size
|
|
|
|
// find table
|
|
Table* table_def = find_table(node.table_name);
|
|
|
|
// prepare empty new_row
|
|
Row new_row = table_def->createEmptyRow();
|
|
|
|
// copy values
|
|
for(size_t i=0; i<node.cols_names.size(); i++) {
|
|
auto colNameNode = node.cols_names[i];
|
|
ColDefNode col_def = table_def->get_column_def(colNameNode.name);
|
|
|
|
// TODO validate value
|
|
|
|
if (col_def.type == ColumnType::integer_type) {
|
|
new_row.setColumnValue(col_def.order, std::stoi(node.cols_values[i].value));
|
|
} else if (col_def.type == ColumnType::float_type) {
|
|
new_row.setColumnValue(col_def.order, std::stof(node.cols_values[i].value));
|
|
} else {
|
|
new_row.setColumnValue(col_def.order, node.cols_values[i].value);
|
|
}
|
|
}
|
|
|
|
// TODO check not null columns
|
|
|
|
// append new_row
|
|
table_def->addRow(new_row);
|
|
|
|
return true;
|
|
}
|
|
|
|
bool Executor::execute_select(SelectFromTableNode& node) {
|
|
// TODO create plan for accessing rows
|
|
|
|
// find source table
|
|
Table* table = find_table(node.table_name);
|
|
|
|
// create result table
|
|
std::vector<ColDefNode> result_tbl_col_defs{};
|
|
std::vector<int> source_table_col_index{};
|
|
int i = 0; // new column order
|
|
for(ColNameNode rc : node.cols_names) {
|
|
ColDefNode cdef = table->get_column_def(rc.name);
|
|
source_table_col_index.push_back(cdef.order);
|
|
|
|
auto col = ColDefNode(rc.name, cdef.type, i, cdef.length, cdef.null);
|
|
result_tbl_col_defs.push_back(col);
|
|
|
|
i++;
|
|
}
|
|
Table result {"result", result_tbl_col_defs};
|
|
|
|
|
|
// execute access plan
|
|
|
|
|
|
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) {
|
|
// prepare empty row
|
|
Row new_row = result.createEmptyRow();
|
|
|
|
// copy column values
|
|
for(auto idx=0; idx<result.columns_count(); idx++) {
|
|
auto row_col_index = source_table_col_index[idx];
|
|
ColValue *col_value = row->ithColum(row_col_index);
|
|
if (result_tbl_col_defs[idx].type == ColumnType::integer_type)
|
|
new_row.setColumnValue(idx, ((ColIntegerValue*)col_value)->integerValue());
|
|
if (result_tbl_col_defs[idx].type == ColumnType::float_type)
|
|
new_row.setColumnValue(idx, col_value->floatValue());
|
|
if (result_tbl_col_defs[idx].type == ColumnType::varchar_type)
|
|
new_row.setColumnValue(idx, col_value->stringValue());
|
|
}
|
|
|
|
// add row to result
|
|
result.m_rows.push_back(new_row);
|
|
}
|
|
}
|
|
|
|
result.print();
|
|
|
|
return true;
|
|
}
|
|
|