#include "usql.h" #include "exception.h" #include "csvreader.h" #include #include namespace usql { std::unique_ptr uSQL::execute(const std::string &command) { auto node = m_parser.parse(command); return execute(*node); } std::unique_ptr
uSQL::execute(Node &node) { // TODO optimize execution nodes here switch (node.node_type) { case NodeType::create_table: return execute_create_table(static_cast(node)); case NodeType::insert_into: return execute_insert_into_table(static_cast(node)); case NodeType::select_from: return execute_select(static_cast(node)); case NodeType::delete_from: return execute_delete(static_cast(node)); case NodeType::update_table: return execute_update(static_cast(node)); case NodeType::load_table: return execute_load(static_cast(node)); default: return create_stmt_result_table(-1, "unknown statement"); } } std::unique_ptr
uSQL::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 create_stmt_result_table(0, "table created"); } std::unique_ptr
uSQL::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++) { ColDefNode col_def = table_def->get_column_def(node.cols_names[i].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); } } // append new_row table_def->addRow(new_row); return create_stmt_result_table(0, "insert succeded"); } std::unique_ptr
uSQL::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 result_tbl_col_defs{}; std::vector source_table_col_index{}; int i = 0; // new column order for (auto 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++; } auto result = std::make_unique
("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 if (evalWhere(node.where.get(), table, row)) { // 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->ithColumn(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); } } return std::move(result); } std::unique_ptr
uSQL::execute_delete(DeleteFromTableNode &node) { // TODO create plan for accessing rows // find source table Table *table = find_table(node.table_name); // execute access plan auto it = table->m_rows.begin(); for (; it != table->m_rows.end();) { if (evalWhere(node.where.get(), table, it)) { // TODO this can be really expensive operation it = table->m_rows.erase(it); } else { ++it; } } return create_stmt_result_table(0, "delete succeded"); } std::unique_ptr
uSQL::execute_update(UpdateTableNode &node) { // TODO create plan for accessing rows // find source table Table *table = find_table(node.table_name); // execute access plan for (auto row = begin(table->m_rows); row != end(table->m_rows); ++row) { // eval where for row if (evalWhere(node.where.get(), table, row)) { int i = 0; for (auto col : node.cols_names) { // TODO cache it like in select ColDefNode cdef = table->get_column_def(col.name); std::unique_ptr new_val = evalArithmetic(cdef.type, static_cast(*node.values[i]), table, row); if (cdef.type == ColumnType::integer_type) { row->setColumnValue(cdef.order, new_val->getIntValue()); } else if (cdef.type == ColumnType::float_type) { row->setColumnValue(cdef.order, new_val->getDoubleValue()); } else if (cdef.type == ColumnType::varchar_type) { row->setColumnValue(cdef.order, new_val->getStringValue()); } else { throw Exception("Implement me!"); } i++; } } } return create_stmt_result_table(0, "delete succeeded"); } std::unique_ptr
uSQL::execute_load(LoadIntoTableNode &node) { // find source table Table *table_def = find_table(node.table_name); // read data std::ifstream ifs(node.filename); std::string content((std::istreambuf_iterator(ifs)), (std::istreambuf_iterator())); CsvReader csvparser{}; auto csv = csvparser.parseCSV(content); std::vector &colDefs = table_def->m_col_defs; for (auto it = csv.begin() + 1; it != csv.end(); ++it) { std::vector csv_line = *it; // prepare empty new_row Row new_row = table_def->createEmptyRow(); // copy values for (size_t i = 0; i < table_def->columns_count(); i++) { ColDefNode col_def = table_def->get_column_def(colDefs[i].name); // TODO validate value if (col_def.type == ColumnType::integer_type) { new_row.setColumnValue(col_def.order, std::stoi(csv_line[i])); } else if (col_def.type == ColumnType::float_type) { new_row.setColumnValue(col_def.order, std::stof(csv_line[i])); } else { new_row.setColumnValue(col_def.order, csv_line[i]); } } // append new_row table_def->addRow(new_row); } return create_stmt_result_table(0, "load succeeded"); } bool uSQL::evalWhere(Node *where, Table *table, std::vector>::iterator &row) const { switch (where->node_type) { // no where clause case NodeType::true_node: return true; case NodeType::relational_operator: // just one condition return evalRelationalOperator(*((RelationalOperatorNode *) where), table, row); case NodeType::logical_operator: return evalLogicalOperator(*((LogicalOperatorNode *) where), table, row); default: throw Exception("Wrong node type"); } return false; } bool uSQL::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()); double comparator; if (left_value->node_type == NodeType::int_value && right_value->node_type == NodeType::int_value) { comparator = left_value->getIntValue() - right_value->getIntValue(); } else if ((left_value->node_type == NodeType::int_value && right_value->node_type == NodeType::float_value) || (left_value->node_type == NodeType::float_value && right_value->node_type == NodeType::int_value) || (left_value->node_type == NodeType::float_value && right_value->node_type == NodeType::float_value)) { comparator = left_value->getDoubleValue() - right_value->getDoubleValue(); } else if (left_value->node_type == NodeType::string_value || right_value->node_type == NodeType::string_value) { comparator = left_value->getStringValue().compare(right_value->getStringValue()); } else { // TODO throw exception } switch (filter.op) { case RelationalOperatorType::equal: return comparator == 0.0; case RelationalOperatorType::not_equal: return comparator != 0.0; case RelationalOperatorType::greater: return comparator > 0.0; case RelationalOperatorType::greater_equal: return comparator >= 0.0; case RelationalOperatorType::lesser: return comparator < 0.0; case RelationalOperatorType::lesser_equal: return comparator <= 0.0; } throw Exception("invalid relational operator"); } std::unique_ptr uSQL::evalNode(Table *table, std::vector>::iterator &row, Node *node) const { if (node->node_type == NodeType::database_value) { DatabaseValueNode *dvl = static_cast(node); ColDefNode col_def = table->get_column_def( dvl->col_name); // TODO optimize it to just get this def once auto db_value = row->ithColumn(col_def.order); if (col_def.type == ColumnType::integer_type) { return std::make_unique(db_value->integerValue()); } 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 (node->node_type == NodeType::int_value) { IntValueNode *ivl = static_cast(node); return std::make_unique(ivl->value); } else if (node->node_type == NodeType::float_value) { FloatValueNode *ivl = static_cast(node); return std::make_unique(ivl->value); } else if (node->node_type == NodeType::string_value) { StringValueNode *ivl = static_cast(node); return std::make_unique(ivl->value); } throw Exception("invalid type"); } bool uSQL::evalLogicalOperator(LogicalOperatorNode &node, Table *pTable, std::vector>::iterator &iter) const { bool left = evalRelationalOperator(static_cast(*node.left), pTable, iter); if ((node.op == LogicalOperatorType::and_operator && !left) || (node.op == LogicalOperatorType::or_operator && left)) return left; bool right = evalRelationalOperator(static_cast(*node.right), pTable, iter); return right; } std::unique_ptr uSQL::evalArithmetic(ColumnType outType, ArithmeticalOperatorNode &node, Table *table, std::vector>::iterator &row) const { if (node.op == ArithmeticalOperatorType::copy_value) { return evalNode(table, row, node.left.get()); } std::unique_ptr left = evalNode(table, row, node.left.get()); std::unique_ptr right = evalNode(table, row, node.right.get()); if (outType == ColumnType::float_type) { double l = ((ValueNode *) left.get())->getDoubleValue(); double r = ((ValueNode *) right.get())->getDoubleValue(); switch (node.op) { case ArithmeticalOperatorType::plus_operator: return std::make_unique(l + r); case ArithmeticalOperatorType::minus_operator: return std::make_unique(l - r); case ArithmeticalOperatorType::multiply_operator: return std::make_unique(l * r); case ArithmeticalOperatorType::divide_operator: return std::make_unique(l / r); default: throw Exception("implement me!!"); } } else if (outType == ColumnType::integer_type) { int l = ((ValueNode *) left.get())->getIntValue(); int r = ((ValueNode *) right.get())->getIntValue(); switch (node.op) { case ArithmeticalOperatorType::plus_operator: return std::make_unique(l + r); case ArithmeticalOperatorType::minus_operator: return std::make_unique(l - r); case ArithmeticalOperatorType::multiply_operator: return std::make_unique(l * r); case ArithmeticalOperatorType::divide_operator: return std::make_unique(l / r); default: throw Exception("implement me!!"); } } else if (outType == ColumnType::varchar_type) { std::string l = ((ValueNode *) left.get())->getStringValue(); std::string r = ((ValueNode *) right.get())->getStringValue(); switch (node.op) { case ArithmeticalOperatorType::plus_operator: return std::make_unique(l + r); default: throw Exception("implement me!!"); } } throw Exception("implement me!!"); } Table *uSQL::find_table(const std::string name) { auto name_cmp = [name](const 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 + ")"); } } std::unique_ptr
uSQL::create_stmt_result_table(int code, std::string text) { std::vector result_tbl_col_defs{}; result_tbl_col_defs.push_back(ColDefNode("code", ColumnType::integer_type, 0, 1, false)); result_tbl_col_defs.push_back(ColDefNode("desc", ColumnType::varchar_type, 1, 255, false)); auto table_def = std::make_unique
("result", result_tbl_col_defs); Row new_row = table_def->createEmptyRow(); new_row.setColumnValue(0, code); new_row.setColumnValue(1, text); table_def->addRow(new_row); return std::move(table_def); } }