usql update

This commit is contained in:
VaclavT 2021-09-16 23:49:32 +02:00
parent 1e0506e6cd
commit 8e94a01d2d
2 changed files with 28 additions and 13 deletions

View File

@ -41,14 +41,15 @@ private:
static std::unique_ptr<ValueNode> eval_function_value_node(Table *table, Row &row, Node *node, ColDefNode *col_def_node, ColValue *agg_func_value); static std::unique_ptr<ValueNode> eval_function_value_node(Table *table, Row &row, Node *node, ColDefNode *col_def_node, ColValue *agg_func_value);
static bool eval_relational_operator(const RelationalOperatorNode &filter, Table *table, Row &row) ; static bool eval_relational_operator(const RelationalOperatorNode &filter, Table *table, Row &row);
static bool eval_logical_operator(LogicalOperatorNode &node, Table *pTable, Row &row) ; static bool eval_logical_operator(LogicalOperatorNode &node, Table *pTable, Row &row);
static std::unique_ptr<ValueNode> eval_arithmetic_operator(ColumnType outType, ArithmeticalOperatorNode &node, Table *table, Row &row) ; static std::unique_ptr<ValueNode> eval_arithmetic_operator(ColumnType outType, ArithmeticalOperatorNode &node, Table *table, Row &row);
static std::unique_ptr<Table> create_stmt_result_table(long code, const std::string &text, size_t affected_rows); static std::unique_ptr<Table> create_stmt_result_table(long code, const std::string &text, size_t affected_rows);
static std::tuple<int, ColDefNode> get_column_definition(Table *table, SelectColNode *select_col_node, int col_order) ; static std::tuple<int, ColDefNode> get_column_definition(Table *table, SelectColNode *select_col_node, int col_order);
static std::tuple<int, ColDefNode> get_node_definition(Table *table, Node *select_col_node, const std::string & col_name, int col_order) ; static ColDefNode get_db_column_definition(Table *table, Node *node);
static std::tuple<int, ColDefNode> get_node_definition(Table *table, Node *select_col_node, const std::string & col_name, int col_order);
Table *find_table(const std::string &name); Table *find_table(const std::string &name);
void check_table_not_exists(const std::string &name); void check_table_not_exists(const std::string &name);

View File

@ -148,11 +148,18 @@ std::tuple<int, ColDefNode> USql::get_column_definition(Table *table, SelectColN
return get_node_definition(table, select_col_node->value.get(), select_col_node->name, col_order ); return get_node_definition(table, select_col_node->value.get(), select_col_node->name, col_order );
} }
ColDefNode USql::get_db_column_definition(Table *table, Node *node) {
if (node->node_type == NodeType::database_value) {
auto db_node = static_cast<DatabaseValueNode *>(node);
return table->get_column_def(db_node->col_name);
}
throw Exception("Undefined table node - get_db_column_definition");
}
std::tuple<int, ColDefNode> USql::get_node_definition(Table *table, Node * node, const std::string & col_name, int col_order ) { std::tuple<int, ColDefNode> USql::get_node_definition(Table *table, Node * node, const std::string & col_name, int col_order ) {
if (node->node_type == NodeType::database_value) { if (node->node_type == NodeType::database_value) {
auto dbval_node = static_cast<DatabaseValueNode *>(node); ColDefNode src_col_def = get_db_column_definition(table, node);
ColDefNode src_col_def = table->get_column_def(dbval_node->col_name);
ColDefNode col_def = ColDefNode{col_name, src_col_def.type, col_order, src_col_def.length, src_col_def.null}; ColDefNode col_def = ColDefNode{col_name, src_col_def.type, col_order, src_col_def.length, src_col_def.null};
return std::make_tuple(src_col_def.order, col_def); return std::make_tuple(src_col_def.order, col_def);
@ -169,12 +176,19 @@ std::tuple<int, ColDefNode> USql::get_node_definition(Table *table, Node * node,
ColDefNode col_def = ColDefNode{col_name, ColumnType::varchar_type, col_order, 10, true}; ColDefNode col_def = ColDefNode{col_name, ColumnType::varchar_type, col_order, 10, true};
return std::make_tuple(-1, col_def); return std::make_tuple(-1, col_def);
} else if (func_node->function == "lower" || func_node->function == "upper") { } else if (func_node->function == "lower" || func_node->function == "upper") {
// TODO get length // TODO get length, use get_db_column_definition
ColDefNode col_def = ColDefNode{col_name, ColumnType::varchar_type, col_order, 256, true}; ColDefNode col_def = ColDefNode{col_name, ColumnType::varchar_type, col_order, 256, true};
return std::make_tuple(-1, col_def); return std::make_tuple(-1, col_def);
} else if (func_node->function == "min" || func_node->function == "max") { } else if (func_node->function == "min" || func_node->function == "max") {
// TODO get correct type and length auto col_type= ColumnType::float_type;
ColDefNode col_def = ColDefNode{col_name, ColumnType::integer_type, col_order, 1, true}; int col_len = 1;
auto & v = func_node->params[0];
if (v->node_type == NodeType::database_value) {
ColDefNode src_col_def = get_db_column_definition(table, v.get());
col_type = src_col_def.type;
col_len = src_col_def.length;
}
ColDefNode col_def = ColDefNode{col_name, col_type, col_order, col_len, true};
return std::make_tuple(-1, col_def); return std::make_tuple(-1, col_def);
} else if (func_node->function == "count") { } else if (func_node->function == "count") {
ColDefNode col_def = ColDefNode{col_name, ColumnType::integer_type, col_order, 1, true}; ColDefNode col_def = ColDefNode{col_name, ColumnType::integer_type, col_order, 1, true};
@ -278,8 +292,8 @@ std::unique_ptr<Table> USql::execute_update(UpdateTableNode &node) {
// TODO cache it like in select // TODO cache it like in select
ColDefNode col_def = table->get_column_def(col.col_name); ColDefNode col_def = table->get_column_def(col.col_name);
std::unique_ptr<ValueNode> new_val = eval_arithmetic_operator(col_def.type, std::unique_ptr<ValueNode> new_val = eval_arithmetic_operator(col_def.type,
static_cast<ArithmeticalOperatorNode &>(*node.values[i]), static_cast<ArithmeticalOperatorNode &>(*node.values[i]),
table, *row); table, *row);
usql::Table::validate_column(&col_def, new_val.get()); usql::Table::validate_column(&col_def, new_val.get());
row->setColumnValue(&col_def, new_val.get()); row->setColumnValue(&col_def, new_val.get());