date and boolean data types added
This commit is contained in:
80
usql.cpp
80
usql.cpp
@@ -199,7 +199,7 @@ std::unique_ptr<Table> USql::execute_select(SelectFromTableNode &node) {
|
||||
return std::move(result);
|
||||
}
|
||||
|
||||
void USql::execute_distinct(SelectFromTableNode &node, Table *result) const {
|
||||
void USql::execute_distinct(SelectFromTableNode &node, Table *result) {
|
||||
if (!node.distinct) return;
|
||||
|
||||
auto compare_rows = [](const Row &a, const Row &b) { return a.compare(b) >= 0; };
|
||||
@@ -208,19 +208,19 @@ void USql::execute_distinct(SelectFromTableNode &node, Table *result) const {
|
||||
result->m_rows.erase(std::unique(result->m_rows.begin(), result->m_rows.end()), result->m_rows.end());
|
||||
}
|
||||
|
||||
void USql::execute_order_by(SelectFromTableNode &node, Table *table, Table *result) const {
|
||||
if (node.order_by.size() == 0) return;
|
||||
void USql::execute_order_by(SelectFromTableNode &node, Table *table, Table *result) {
|
||||
if (node.order_by.empty()) return;
|
||||
|
||||
auto compare_rows = [&node, &result, this](const Row &a, const Row &b) {
|
||||
for(auto order_by_col_def : node.order_by) {
|
||||
auto compare_rows = [&node, &result](const Row &a, const Row &b) {
|
||||
for(const auto& order_by_col_def : node.order_by) {
|
||||
ColDefNode col_def = result->get_column_def(order_by_col_def.col_index - 1); // TODO validate index
|
||||
ColValue *a_val = a.ith_column(col_def.order);
|
||||
ColValue *b_val = b.ith_column(col_def.order);
|
||||
|
||||
int compare = a_val->compare(b_val);
|
||||
|
||||
if (compare < 0) return order_by_col_def.ascending ? true : false;
|
||||
if (compare > 0) return order_by_col_def.ascending ? false : true;
|
||||
if (compare < 0) return order_by_col_def.ascending;
|
||||
if (compare > 0) return !order_by_col_def.ascending;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
@@ -228,7 +228,7 @@ void USql::execute_order_by(SelectFromTableNode &node, Table *table, Table *resu
|
||||
std::sort(result->m_rows.begin(), result->m_rows.end(), compare_rows);
|
||||
}
|
||||
|
||||
void USql::execute_offset_limit(OffsetLimitNode &node, Table *result) const {
|
||||
void USql::execute_offset_limit(OffsetLimitNode &node, Table *result) {
|
||||
if (node.offset > 0)
|
||||
result->m_rows.erase(result->m_rows.begin(),
|
||||
result->rows_count() > node.offset ? result->m_rows.begin() + node.offset : result->m_rows.end());
|
||||
@@ -270,7 +270,7 @@ std::unique_ptr<Table> USql::execute_delete(DeleteFromTableNode &node) {
|
||||
|
||||
table->m_rows.erase(
|
||||
std::remove_if(table->m_rows.begin(), table->m_rows.end(),
|
||||
[&node, table, this](Row &row){return eval_where(node.where.get(), table, row);}),
|
||||
[&node, table](Row &row){return eval_where(node.where.get(), table, row);}),
|
||||
table->m_rows.end());
|
||||
|
||||
affected_rows -= table->rows_count();
|
||||
@@ -295,7 +295,7 @@ std::unique_ptr<Table> USql::execute_update(UpdateTableNode &node) {
|
||||
static_cast<ArithmeticalOperatorNode &>(*node.values[i]),
|
||||
table, *row);
|
||||
|
||||
table->validate_column(&col_def, new_val.get());
|
||||
usql::Table::validate_column(&col_def, new_val.get());
|
||||
row->setColumnValue(&col_def, new_val.get());
|
||||
i++;
|
||||
}
|
||||
@@ -308,7 +308,7 @@ std::unique_ptr<Table> USql::execute_update(UpdateTableNode &node) {
|
||||
}
|
||||
|
||||
|
||||
bool USql::eval_where(Node *where, Table *table, Row &row) const {
|
||||
bool USql::eval_where(Node *where, Table *table, Row &row) {
|
||||
switch (where->node_type) { // no where clause
|
||||
case NodeType::true_node:
|
||||
return true;
|
||||
@@ -324,20 +324,25 @@ bool USql::eval_where(Node *where, Table *table, Row &row) const {
|
||||
}
|
||||
|
||||
|
||||
bool USql::eval_relational_operator(const RelationalOperatorNode &filter, Table *table, Row &row) const {
|
||||
bool USql::eval_relational_operator(const RelationalOperatorNode &filter, Table *table, Row &row) {
|
||||
std::unique_ptr<ValueNode> left_value = eval_value_node(table, row, filter.left.get());
|
||||
std::unique_ptr<ValueNode> right_value = eval_value_node(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();
|
||||
comparator = left_value->getIntegerValue() - right_value->getIntegerValue();
|
||||
} 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 if (left_value->node_type == NodeType::bool_value && right_value->node_type == NodeType::bool_value) {
|
||||
bool bl = left_value->getBooleanValue();
|
||||
bool br = right_value->getBooleanValue();
|
||||
comparator = bl == br ? 0 : 1; // TODO define it
|
||||
// TODO handle dates
|
||||
} else {
|
||||
// TODO throw exception
|
||||
}
|
||||
@@ -364,10 +369,8 @@ bool USql::eval_relational_operator(const RelationalOperatorNode &filter, Table
|
||||
std::unique_ptr<ValueNode> USql::eval_value_node(Table *table, Row &row, Node *node) {
|
||||
if (node->node_type == NodeType::database_value || node->node_type == NodeType::column_name) { // TODO sjednotit
|
||||
return eval_database_value_node(table, row, node);
|
||||
|
||||
} else if (node->node_type == NodeType::int_value || node->node_type == NodeType::float_value || node->node_type == NodeType::string_value) {
|
||||
} else if (node->node_type == NodeType::int_value || node->node_type == NodeType::float_value || node->node_type == NodeType::string_value || node->node_type == NodeType::bool_value) {
|
||||
return eval_literal_value_node(table, row, node);
|
||||
|
||||
} else if (node->node_type == NodeType::function) {
|
||||
return eval_function_value_node(table, row, node);
|
||||
} else if (node->node_type == NodeType::null_value) {
|
||||
@@ -382,15 +385,18 @@ std::unique_ptr<ValueNode> USql::eval_database_value_node(Table *table, Row &row
|
||||
ColDefNode col_def = table->get_column_def( dvl->col_name); // TODO optimize it to just get this def once
|
||||
auto db_value = row.ith_column(col_def.order);
|
||||
|
||||
if (col_def.type == ColumnType::integer_type) {
|
||||
if (col_def.type == ColumnType::integer_type)
|
||||
return std::make_unique<IntValueNode>(db_value->getIntValue());
|
||||
}
|
||||
if (col_def.type == ColumnType::float_type) {
|
||||
if (col_def.type == ColumnType::float_type)
|
||||
return std::make_unique<DoubleValueNode>(db_value->getDoubleValue());
|
||||
}
|
||||
if (col_def.type == ColumnType::varchar_type) {
|
||||
if (col_def.type == ColumnType::varchar_type)
|
||||
return std::make_unique<StringValueNode>(db_value->getStringValue());
|
||||
}
|
||||
if (col_def.type == ColumnType::bool_type)
|
||||
return std::make_unique<BooleanValueNode>(db_value->getBoolValue());
|
||||
// Date has no it's own value node (it is passed around as string)
|
||||
if (col_def.type == ColumnType::date_type)
|
||||
return std::make_unique<StringValueNode>(db_value->getStringValue());
|
||||
|
||||
throw Exception("unknown database value type");
|
||||
}
|
||||
|
||||
@@ -408,7 +414,11 @@ std::unique_ptr<ValueNode> USql::eval_literal_value_node(Table *table, Row &row,
|
||||
auto *ivl = static_cast<StringValueNode *>(node);
|
||||
return std::make_unique<StringValueNode>(ivl->value);
|
||||
|
||||
} else if (node->node_type == NodeType::bool_value) {
|
||||
auto *ivl = static_cast<BooleanValueNode *>(node);
|
||||
return std::make_unique<BooleanValueNode>(ivl->value);
|
||||
}
|
||||
// Date has no it's own value node (it is passed around as string)
|
||||
|
||||
throw Exception("invalid type");
|
||||
}
|
||||
@@ -438,10 +448,10 @@ std::unique_ptr<ValueNode> USql::eval_function_value_node(Table *table, Row &row
|
||||
std::string date = evaluatedPars[0]->getStringValue();
|
||||
std::string format = evaluatedPars[1]->getStringValue();
|
||||
long epoch_time = string_to_date(date, format);
|
||||
return std::make_unique<IntValueNode>(epoch_time);
|
||||
return std::make_unique<IntValueNode>(epoch_time); // No DateValueNode for now
|
||||
}
|
||||
if (fnc->function == "to_string") {
|
||||
long date = evaluatedPars[0]->getIntValue();
|
||||
long date = evaluatedPars[0]->getDateValue();
|
||||
std::string format = evaluatedPars[1]->getStringValue();
|
||||
std::string formated_date = date_to_string(date, format);
|
||||
return std::make_unique<StringValueNode>(formated_date);
|
||||
@@ -451,7 +461,7 @@ std::unique_ptr<ValueNode> USql::eval_function_value_node(Table *table, Row &row
|
||||
}
|
||||
|
||||
|
||||
bool USql::eval_logical_operator(LogicalOperatorNode &node, Table *pTable, Row &row) const {
|
||||
bool USql::eval_logical_operator(LogicalOperatorNode &node, Table *pTable, Row &row) {
|
||||
bool left = eval_relational_operator(static_cast<const RelationalOperatorNode &>(*node.left), pTable, row);
|
||||
|
||||
if ((node.op == LogicalOperatorType::and_operator && !left) || (node.op == LogicalOperatorType::or_operator && left))
|
||||
@@ -462,7 +472,7 @@ bool USql::eval_logical_operator(LogicalOperatorNode &node, Table *pTable, Row &
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<ValueNode> USql::eval_arithmetic_operator(ColumnType outType, ArithmeticalOperatorNode &node, Table *table, Row &row) const {
|
||||
std::unique_ptr<ValueNode> USql::eval_arithmetic_operator(ColumnType outType, ArithmeticalOperatorNode &node, Table *table, Row &row) {
|
||||
if (node.op == ArithmeticalOperatorType::copy_value) {
|
||||
return eval_value_node(table, row, node.left.get());
|
||||
}
|
||||
@@ -487,8 +497,8 @@ std::unique_ptr<ValueNode> USql::eval_arithmetic_operator(ColumnType outType, Ar
|
||||
}
|
||||
|
||||
} else if (outType == ColumnType::integer_type) {
|
||||
long l = ((ValueNode *) left.get())->getIntValue();
|
||||
long r = ((ValueNode *) right.get())->getIntValue();
|
||||
long l = ((ValueNode *) left.get())->getIntegerValue();
|
||||
long r = ((ValueNode *) right.get())->getIntegerValue();
|
||||
switch (node.op) {
|
||||
case ArithmeticalOperatorType::plus_operator:
|
||||
return std::make_unique<IntValueNode>(l + r);
|
||||
@@ -512,6 +522,7 @@ std::unique_ptr<ValueNode> USql::eval_arithmetic_operator(ColumnType outType, Ar
|
||||
throw Exception("implement me!!");
|
||||
}
|
||||
}
|
||||
// TODO date node should support addition and substraction
|
||||
|
||||
throw Exception("implement me!!");
|
||||
}
|
||||
@@ -519,16 +530,16 @@ std::unique_ptr<ValueNode> USql::eval_arithmetic_operator(ColumnType outType, Ar
|
||||
|
||||
std::unique_ptr<Table> USql::create_stmt_result_table(long code, const std::string &text, long affected_rows) {
|
||||
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, 48, false));
|
||||
result_tbl_col_defs.push_back(ColDefNode("affected_rows", ColumnType::integer_type, 0, 1, true));
|
||||
result_tbl_col_defs.emplace_back("code", ColumnType::integer_type, 0, 1, false);
|
||||
result_tbl_col_defs.emplace_back("desc", ColumnType::varchar_type, 1, 48, false);
|
||||
result_tbl_col_defs.emplace_back("affected_rows", ColumnType::integer_type, 0, 1, true);
|
||||
|
||||
auto table_def = std::make_unique<Table>("result", result_tbl_col_defs);
|
||||
|
||||
Row new_row = table_def->create_empty_row();
|
||||
new_row.setColumnValue(0, code);
|
||||
new_row.setColumnValue(1, text);
|
||||
new_row.setColumnValue(2, affected_rows);
|
||||
new_row.setIntColumnValue(0, code);
|
||||
new_row.setStringColumnValue(1, text);
|
||||
new_row.setIntColumnValue(2, affected_rows);
|
||||
table_def->add_row(new_row);
|
||||
|
||||
return std::move(table_def);
|
||||
@@ -554,4 +565,5 @@ void USql::check_table_not_exists(const std::string &name) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
Reference in New Issue
Block a user