Compare commits
2 Commits
fc3a0dc230
...
65abc2fd07
| Author | SHA1 | Date | |
|---|---|---|---|
| 65abc2fd07 | |||
| 55ee694f6f |
13
ml_util.cpp
13
ml_util.cpp
@@ -4,11 +4,13 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
// #include <filesystem>
|
||||
|
||||
std::vector<std::string> commands {};
|
||||
|
||||
// kryplove z applu a jejich problemy s filesystemem
|
||||
/*
|
||||
std::string get_history_file_dir() {
|
||||
std::string filename{".ml_history.txt"};
|
||||
const char *t = std::getenv("HOME");
|
||||
@@ -16,6 +18,15 @@ std::string get_history_file_dir() {
|
||||
if (t == nullptr) return std::filesystem::temp_directory_path() / filename;
|
||||
else return std::filesystem::path(std::string{t}) /filename;
|
||||
}
|
||||
*/
|
||||
|
||||
std::string get_history_file_dir() {
|
||||
std::string file{"/.ml_history.txt"};
|
||||
const char *t = std::getenv("HOME");
|
||||
|
||||
if (t == nullptr) return "/tmp/" + file;
|
||||
else return std::string{t} + "/" + file;
|
||||
}
|
||||
|
||||
MlEnvironment * repl_env = nullptr;
|
||||
|
||||
|
||||
@@ -7,12 +7,12 @@ namespace usql {
|
||||
|
||||
std::string column_type_name(const ColumnType type) {
|
||||
if (type == ColumnType::integer_type) return "integer_type";
|
||||
if (type == ColumnType::float_type) return "float_type";
|
||||
if (type == ColumnType::float_type) return "float_type";
|
||||
if (type == ColumnType::varchar_type) return "varchar_type";
|
||||
if (type == ColumnType::date_type) return "date_type";
|
||||
if (type == ColumnType::bool_type) return "bool_type";
|
||||
if (type == ColumnType::date_type) return "date_type";
|
||||
if (type == ColumnType::bool_type) return "bool_type";
|
||||
|
||||
throw Exception("invalid column type: " + (int)type);
|
||||
throw Exception("invalid column type: " + std::to_string((int)type));
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -132,6 +132,7 @@ struct FunctionNode : Node {
|
||||
pp,
|
||||
lower,
|
||||
upper,
|
||||
coalesce,
|
||||
min,
|
||||
max,
|
||||
count
|
||||
@@ -144,6 +145,7 @@ struct FunctionNode : Node {
|
||||
if (str=="pp") return Type::pp;
|
||||
if (str=="lower") return Type::lower;
|
||||
if (str=="upper") return Type::upper;
|
||||
if (str=="coalesce") return Type::coalesce;
|
||||
if (str=="min") return Type::min;
|
||||
if (str=="max") return Type::max;
|
||||
if (str=="count") return Type::count;
|
||||
@@ -158,15 +160,16 @@ struct FunctionNode : Node {
|
||||
if (type == Type::pp) return "pp";
|
||||
if (type == Type::lower) return "lower";
|
||||
if (type == Type::upper) return "upper";
|
||||
if (type == Type::coalesce) return "coalesce";
|
||||
if (type == Type::min) return "min";
|
||||
if (type == Type::max) return "max";
|
||||
if (type == Type::count) return "count";
|
||||
|
||||
throw Exception("invalid function: " + (int)type);
|
||||
throw Exception("invalid function: " + std::to_string((int)type));
|
||||
};
|
||||
|
||||
|
||||
Type function;
|
||||
Type function;
|
||||
std::vector<std::unique_ptr<Node>> params;
|
||||
|
||||
FunctionNode(std::string func_name, std::vector<std::unique_ptr<Node>> pars) :
|
||||
|
||||
@@ -173,6 +173,9 @@ std::unique_ptr<ValueNode> USql::eval_function_value_node(Table *table, Row &row
|
||||
evaluatedPars.push_back(eval_value_node(table, row, param.get(), nullptr, nullptr));
|
||||
}
|
||||
|
||||
// coalesce function can have first parameter null, so must be calles before following "return null"
|
||||
if (fnc->function == FunctionNode::Type::coalesce) return coalesce_function(evaluatedPars);
|
||||
|
||||
if (evaluatedPars.empty() || evaluatedPars[0]->isNull())
|
||||
return std::make_unique<NullValueNode>();
|
||||
|
||||
|
||||
@@ -73,6 +73,7 @@ private:
|
||||
|
||||
static std::unique_ptr<ValueNode> lower_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars);
|
||||
static std::unique_ptr<ValueNode> upper_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars);
|
||||
static std::unique_ptr<ValueNode> coalesce_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars);
|
||||
static std::unique_ptr<ValueNode> to_date_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars);
|
||||
static std::unique_ptr<ValueNode> to_string_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars);
|
||||
static std::unique_ptr<ValueNode> date_add_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars);
|
||||
|
||||
@@ -239,6 +239,13 @@ std::tuple<int, ColDefNode> USql::get_node_definition(Table *table, Node * node,
|
||||
// TODO get length, use get_db_column_definition
|
||||
ColDefNode col_def = ColDefNode{col_name, ColumnType::varchar_type, col_order, 256, true};
|
||||
return std::make_tuple(FUNCTION_CALL, col_def);
|
||||
} else if (func_node->function == FunctionNode::Type::coalesce) {
|
||||
// TODO handle cases here
|
||||
if (func_node->params.empty()) throw Exception("Coalesce without parameters");
|
||||
if (func_node->params[0]->node_type != NodeType::database_value) throw Exception("Coalesce first parameter must be database column");
|
||||
ColDefNode tbl_col_def = get_db_column_definition(table, func_node->params[0].get());
|
||||
ColDefNode col_def = ColDefNode{col_name, tbl_col_def.type, col_order, tbl_col_def.length, true};
|
||||
return std::make_tuple(FUNCTION_CALL, col_def);
|
||||
} else if (func_node->function == FunctionNode::Type::min || func_node->function == FunctionNode::Type::max) {
|
||||
auto col_type= ColumnType::float_type;
|
||||
size_t col_len = 1;
|
||||
|
||||
@@ -34,6 +34,13 @@ std::unique_ptr<ValueNode> USql::date_add_function(const std::vector<std::unique
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<ValueNode> USql::lower_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars) {
|
||||
std::string str = evaluatedPars[0]->getStringValue();
|
||||
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) -> unsigned char { return tolower(c); });
|
||||
|
||||
return std::make_unique<StringValueNode>(str);
|
||||
}
|
||||
|
||||
std::unique_ptr<ValueNode> USql::upper_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars) {
|
||||
std::string str = evaluatedPars[0]->getStringValue();
|
||||
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) -> unsigned char { return toupper(c); });
|
||||
@@ -41,11 +48,21 @@ std::unique_ptr<ValueNode> USql::upper_function(const std::vector<std::unique_pt
|
||||
return std::make_unique<StringValueNode>(str);
|
||||
}
|
||||
|
||||
std::unique_ptr<ValueNode> USql::lower_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars) {
|
||||
std::string str = evaluatedPars[0]->getStringValue();
|
||||
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) -> unsigned char { return tolower(c); });
|
||||
|
||||
return std::make_unique<StringValueNode>(str);
|
||||
std::unique_ptr<ValueNode> USql::coalesce_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars) {
|
||||
for(const auto & par : evaluatedPars) {
|
||||
if (!par->isNull()) {
|
||||
// TODO implement rest and take it out as a function
|
||||
if (par->node_type == NodeType::int_value)
|
||||
return std::make_unique<IntValueNode>(par->getIntegerValue());
|
||||
if (par->node_type == NodeType::float_value)
|
||||
return std::make_unique<DoubleValueNode>(par->getDoubleValue());
|
||||
if (par->node_type == NodeType::string_value)
|
||||
return std::make_unique<StringValueNode>(par->getStringValue());
|
||||
if (par->node_type == NodeType::bool_value)
|
||||
return std::make_unique<BooleanValueNode>(par->getBooleanValue());
|
||||
}
|
||||
}
|
||||
return std::make_unique<NullValueNode>();
|
||||
}
|
||||
|
||||
std::unique_ptr<ValueNode> USql::pp_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars) {
|
||||
|
||||
Reference in New Issue
Block a user