pp function
This commit is contained in:
42
usql.cpp
42
usql.cpp
@@ -1,6 +1,7 @@
|
||||
#include "usql.h"
|
||||
#include "exception.h"
|
||||
#include "ml_date.h"
|
||||
#include "ml_string.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
@@ -284,6 +285,9 @@ std::tuple<int, ColDefNode> USql::get_node_definition(Table *table, Node * node,
|
||||
} else if (func_node->function == "to_date") {
|
||||
ColDefNode col_def = ColDefNode{col_name, ColumnType::integer_type, col_order, 1, true};
|
||||
return std::make_tuple(-1, col_def);
|
||||
} else if (func_node->function == "pp") {
|
||||
ColDefNode col_def = ColDefNode{col_name, ColumnType::varchar_type, col_order, 10, true};
|
||||
return std::make_tuple(-1, col_def);
|
||||
}
|
||||
throw Exception("Unsupported function");
|
||||
|
||||
@@ -524,6 +528,44 @@ std::unique_ptr<ValueNode> USql::eval_function_value_node(Table *table, Row &row
|
||||
std::string formatted_date = date_to_string(date, format);
|
||||
return std::make_unique<StringValueNode>(formatted_date);
|
||||
}
|
||||
if (fnc->function == "pp") {
|
||||
auto &parsed_value = evaluatedPars[0];
|
||||
|
||||
if (parsed_value->node_type == NodeType::int_value || parsed_value->node_type == NodeType::float_value) {
|
||||
std::string format = evaluatedPars.size() > 1 ? evaluatedPars[1]->getStringValue() : "";
|
||||
char buf[20] {0};
|
||||
double value = parsed_value->getDoubleValue();
|
||||
|
||||
if (format == "100%")
|
||||
std::snprintf(buf, 20, "%.2f%%", value);
|
||||
else if (value >= 1000000000000)
|
||||
std::snprintf(buf, 20, "%7.2fT", value/1000000000000);
|
||||
else if (value >= 1000000000)
|
||||
std::sprintf(buf, "%7.2fB", value/1000000000);
|
||||
else if (value >= 1000000)
|
||||
std::snprintf(buf, 20, "%7.2fM", value/1000000);
|
||||
else if (value >= 100000)
|
||||
std::snprintf(buf, 20, "%7.2fM", value/100000); // 0.12M
|
||||
else if (value <= -1000000000000)
|
||||
std::snprintf(buf, 20, "%7.2fT", value/1000000000000);
|
||||
else if (value <= -1000000000)
|
||||
std::snprintf(buf, 20, "%7.2fB", value/1000000000);
|
||||
else if (value <= -1000000)
|
||||
std::snprintf(buf, 20, "%7.2fM", value/1000000);
|
||||
else if (value <= -100000)
|
||||
std::snprintf(buf, 20, "%7.2fM", value/100000); // 0.12M
|
||||
else if (value == 0)
|
||||
buf[0]='0';
|
||||
else
|
||||
return std::make_unique<StringValueNode>(parsed_value->getStringValue());
|
||||
|
||||
std::string s {buf};
|
||||
return std::make_unique<StringValueNode>(string_padd(s.erase(s.find_last_not_of(" ")+1), 10, ' ', false));
|
||||
}
|
||||
|
||||
return std::make_unique<StringValueNode>(parsed_value->getStringValue());
|
||||
}
|
||||
|
||||
|
||||
throw Exception("invalid function");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user