some renamings

This commit is contained in:
2021-07-22 18:32:00 +02:00
parent dec99b823a
commit fc5fd32976
10 changed files with 171 additions and 170 deletions

139
usql.cpp
View File

@@ -8,14 +8,8 @@
namespace usql {
USql::USql(){
m_tables.reserve(16); // some initial size, to prevent immediate reallocation
}
std::unique_ptr<Table> USql::execute(const std::string &command) {
auto node = m_parser.parse(command);
std::unique_ptr<Node> node = m_parser.parse(command);
return execute(*node);
}
@@ -56,16 +50,16 @@ std::unique_ptr<Table> USql::execute_create_table(CreateTableNode &node) {
std::unique_ptr<Table> USql::execute_create_table_as_table(CreateTableAsSelectNode &node) {
// TODO check table does not exists
auto select = execute_select((SelectFromTableNode &) *node.select_table.get());
auto select = execute_select((SelectFromTableNode &) *node.select_table);
// create table
Table new_table{node.table_name, select.get()->m_col_defs};
Table new_table{node.table_name, select->m_col_defs};
m_tables.push_back(new_table);
// copy rows
// must be here, if rows are put into new_table, they are lost during m_tables.push_table
Table *table = find_table(node.table_name);
for( Row& orig_row : select.get()->m_rows) {
for( Row& orig_row : select->m_rows) {
table->addCopyOfRow(orig_row);
}
@@ -103,42 +97,28 @@ std::unique_ptr<Table> USql::execute_insert_into_table(InsertIntoTableNode &node
// append new_row
table_def->addRow(new_row);
return create_stmt_result_table(0, "insert succeded");
return create_stmt_result_table(0, "insert succeeded");
}
std::unique_ptr<Table> 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<ColDefNode> result_tbl_col_defs{};
std::vector<int> source_table_col_index{};
// new column order
for (int i = 0; i < node.cols_names->size(); i++) {
auto column = node.cols_names.get();
std::string new_col_name = column->operator[](i).name;
auto [ src_tbl_col_index, col_def ] = getColumnDefinition(table, &node.cols_names->operator[](i), i);
if (column->operator[](i).value->node_type == NodeType::column_name) {
ColDefNode cdef = table->get_column_def(new_col_name);
source_table_col_index.push_back(cdef.order);
auto col = ColDefNode(new_col_name, cdef.type, i, cdef.length, cdef.null);
result_tbl_col_defs.push_back(col);
} else {
// TODO replace this hardcoded values
ColDefNode cdef = ColDefNode{new_col_name, ColumnType::varchar_type, i, 64, true};
source_table_col_index.push_back(-1);
auto col = ColDefNode(new_col_name, cdef.type, i, cdef.length, cdef.null);
result_tbl_col_defs.push_back(col);
}
source_table_col_index.push_back(src_tbl_col_index);
result_tbl_col_defs.push_back(col_def);
}
auto result = std::make_unique<Table>("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
@@ -152,9 +132,9 @@ std::unique_ptr<Table> USql::execute_select(SelectFromTableNode &node) {
if (row_col_index == -1) {
// todo its function TODO col_names zmenit na colValues
auto evaluated_value = evalValueNode(table, *row, node.cols_names.get()->operator[](idx).value.get());
auto evaluated_value = evalValueNode(table, *row, node.cols_names->operator[](idx).value.get());
ValueNode *col_value = evaluated_value.get();
if (true /* !col_value->isNull() */) { // TODO sjednotit nasledujici
if (!col_value->isNull()) { // TODO sjednotit nasledujici
if (result_tbl_col_defs[idx].type == ColumnType::integer_type)
new_row.setColumnValue(idx, col_value->getIntValue());
if (result_tbl_col_defs[idx].type == ColumnType::float_type)
@@ -162,17 +142,19 @@ std::unique_ptr<Table> USql::execute_select(SelectFromTableNode &node) {
if (result_tbl_col_defs[idx].type == ColumnType::varchar_type)
new_row.setColumnValue(idx, col_value->getStringValue());
}
// TODO set to null
} else {
ColValue *col_value = row->ithColumn(row_col_index);
if (!col_value->isNull()) {
if (result_tbl_col_defs[idx].type == ColumnType::integer_type)
new_row.setColumnValue(idx, 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());
new_row.setColumnValue(idx, col_value->getIntValue());
else if (result_tbl_col_defs[idx].type == ColumnType::float_type)
new_row.setColumnValue(idx, col_value->getDoubleValue());
else if (result_tbl_col_defs[idx].type == ColumnType::varchar_type)
new_row.setColumnValue(idx, col_value->getStringValue());
}
// TODO set to null
}
}
@@ -184,10 +166,30 @@ std::unique_ptr<Table> USql::execute_select(SelectFromTableNode &node) {
return std::move(result);
}
std::tuple<int, ColDefNode> USql::getColumnDefinition(Table *table, SelectColNode *select_col_node, int col_order ) {
std::string new_col_name = select_col_node->name;
if (select_col_node->value->node_type == NodeType::column_name) {
ColDefNode cdef = table->get_column_def(new_col_name);
return std::make_tuple(cdef.order, cdef);
} else if (select_col_node->value->node_type == NodeType::function) {
auto node = static_cast<FunctionNode *>(select_col_node->value.get());
if (node->function == "to_string") {
ColDefNode cdef = ColDefNode{new_col_name, ColumnType::varchar_type, col_order, 64, true};
return std::make_tuple(-1, cdef);
} else if (node->function == "to_date") {
ColDefNode cdef = ColDefNode{new_col_name, ColumnType::integer_type, col_order, 1, true};
return std::make_tuple(-1, cdef);
}
throw Exception("Unsupported function");
}
throw Exception("Unsupported node type");
}
std::unique_ptr<Table> USql::execute_delete(DeleteFromTableNode &node) {
// TODO create plan for accessing rows
// find source table
Table *table = find_table(node.table_name);
@@ -202,13 +204,11 @@ std::unique_ptr<Table> USql::execute_delete(DeleteFromTableNode &node) {
}
}
return create_stmt_result_table(0, "delete succeded");
return create_stmt_result_table(0, "delete succeeded");
}
std::unique_ptr<Table> USql::execute_update(UpdateTableNode &node) {
// TODO create plan for accessing rows
// find source table
Table *table = find_table(node.table_name);
@@ -217,7 +217,7 @@ std::unique_ptr<Table> USql::execute_update(UpdateTableNode &node) {
// eval where for row
if (evalWhere(node.where.get(), table, *row)) {
int i = 0;
for (auto col : node.cols_names) {
for (const auto& col : node.cols_names) {
// TODO cache it like in select
ColDefNode cdef = table->get_column_def(col.name);
@@ -298,13 +298,13 @@ std::unique_ptr<Table> USql::execute_save(SaveTableNode &node) {
// rows
for (auto it = table_def->m_rows.begin() + 1; it != table_def->m_rows.end(); ++it) {
std::string csv_line = "";
std::string csv_line;
for(int i = 0; i < table_def->m_col_defs.size(); i++) {
if (i > 0) csv_line += ",";
auto col = it->ithColumn(i);
if (!col->isNull()) {
csv_line += col->stringValue(); // TODO handle enclosing commas etc
csv_line += col->getStringValue(); // TODO handle enclosing commas etc
}
}
out_string += "\n";
@@ -373,7 +373,7 @@ bool USql::evalRelationalOperator(const RelationalOperatorNode &filter, Table *t
}
std::unique_ptr<ValueNode> USql::evalValueNode(Table *table, Row &row, Node *node) const {
std::unique_ptr<ValueNode> USql::evalValueNode(Table *table, Row &row, Node *node) {
if (node->node_type == NodeType::database_value || node->node_type == NodeType::column_name) { // TODO sjednotit
return evalDatabaseValueNode(table, row, node);
@@ -387,35 +387,35 @@ std::unique_ptr<ValueNode> USql::evalValueNode(Table *table, Row &row, Node *nod
}
std::unique_ptr<ValueNode> USql::evalDatabaseValueNode(Table *table, Row &row, Node *node) const {
DatabaseValueNode *dvl = static_cast<DatabaseValueNode *>(node);
std::unique_ptr<ValueNode> USql::evalDatabaseValueNode(Table *table, Row &row, Node *node) {
auto *dvl = static_cast<DatabaseValueNode *>(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::__1::make_unique<IntValueNode>(db_value->integerValue());
return std::__1::make_unique<IntValueNode>(db_value->getIntValue());
}
if (col_def.type == ColumnType::float_type) {
return std::__1::make_unique<FloatValueNode>(db_value->floatValue());
return std::__1::make_unique<DoubleValueNode>(db_value->getDoubleValue());
}
if (col_def.type == ColumnType::varchar_type) {
return std::__1::make_unique<StringValueNode>(db_value->stringValue());
return std::__1::make_unique<StringValueNode>(db_value->getStringValue());
}
throw Exception("unknown database value type");
}
std::unique_ptr<ValueNode> USql::evalLiteralValueNode(Table *table, Row &row, Node *node) const {
std::unique_ptr<ValueNode> USql::evalLiteralValueNode(Table *table, Row &row, Node *node) {
if (node->node_type == NodeType::int_value) {
IntValueNode *ivl = static_cast<IntValueNode *>(node);
auto *ivl = static_cast<IntValueNode *>(node);
return std::make_unique<IntValueNode>(ivl->value);
} else if (node->node_type == NodeType::float_value) {
FloatValueNode *ivl = static_cast<FloatValueNode *>(node);
return std::make_unique<FloatValueNode>(ivl->value);
auto *ivl = static_cast<DoubleValueNode *>(node);
return std::make_unique<DoubleValueNode>(ivl->value);
} else if (node->node_type == NodeType::string_value) {
StringValueNode *ivl = static_cast<StringValueNode *>(node);
auto *ivl = static_cast<StringValueNode *>(node);
return std::make_unique<StringValueNode>(ivl->value);
}
@@ -424,12 +424,12 @@ std::unique_ptr<ValueNode> USql::evalLiteralValueNode(Table *table, Row &row, No
}
std::unique_ptr<ValueNode> USql::evalFunctionValueNode(Table *table, Row &row, Node *node) const {
FunctionNode *fnc = static_cast<FunctionNode *>(node);
std::unique_ptr<ValueNode> USql::evalFunctionValueNode(Table *table, Row &row, Node *node) {
auto *fnc = static_cast<FunctionNode *>(node);
std::vector<std::unique_ptr<ValueNode>> evaluatedPars;
for(int i = 0; i < fnc->params.size(); i++) {
evaluatedPars.push_back(evalValueNode(table, row, fnc->params[i].get()));
for(auto & param : fnc->params) {
evaluatedPars.push_back(evalValueNode(table, row, param.get()));
}
// TODO use some enum
@@ -464,8 +464,7 @@ std::unique_ptr<ValueNode> USql::evalFunctionValueNode(Table *table, Row &row, N
bool USql::evalLogicalOperator(LogicalOperatorNode &node, Table *pTable, Row &row) const {
bool left = evalRelationalOperator(static_cast<const RelationalOperatorNode &>(*node.left), pTable, row);
if ((node.op == LogicalOperatorType::and_operator && !left) ||
(node.op == LogicalOperatorType::or_operator && left))
if ((node.op == LogicalOperatorType::and_operator && !left) || (node.op == LogicalOperatorType::or_operator && left))
return left;
bool right = evalRelationalOperator(static_cast<const RelationalOperatorNode &>(*node.right), pTable, row);
@@ -486,19 +485,19 @@ std::unique_ptr<ValueNode> USql::evalArithmeticOperator(ColumnType outType, Arit
double r = ((ValueNode *) right.get())->getDoubleValue();
switch (node.op) {
case ArithmeticalOperatorType::plus_operator:
return std::make_unique<FloatValueNode>(l + r);
return std::make_unique<DoubleValueNode>(l + r);
case ArithmeticalOperatorType::minus_operator:
return std::make_unique<FloatValueNode>(l - r);
return std::make_unique<DoubleValueNode>(l - r);
case ArithmeticalOperatorType::multiply_operator:
return std::make_unique<FloatValueNode>(l * r);
return std::make_unique<DoubleValueNode>(l * r);
case ArithmeticalOperatorType::divide_operator:
return std::make_unique<FloatValueNode>(l / r);
return std::make_unique<DoubleValueNode>(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();
long l = ((ValueNode *) left.get())->getIntValue();
long r = ((ValueNode *) right.get())->getIntValue();
switch (node.op) {
case ArithmeticalOperatorType::plus_operator:
return std::make_unique<IntValueNode>(l + r);
@@ -527,7 +526,7 @@ std::unique_ptr<ValueNode> USql::evalArithmeticOperator(ColumnType outType, Arit
}
std::unique_ptr<Table> USql::create_stmt_result_table(long code, std::string text) {
std::unique_ptr<Table> USql::create_stmt_result_table(long code, const std::string& text) {
std::vector<ColDefNode> 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));
@@ -544,7 +543,7 @@ std::unique_ptr<Table> USql::create_stmt_result_table(long code, std::string tex
Table *USql::find_table(const std::string name) {
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)) {