add function query
This commit is contained in:
73
src/utils/func_query.hpp
Normal file
73
src/utils/func_query.hpp
Normal file
@@ -0,0 +1,73 @@
|
||||
#pragma once
|
||||
|
||||
#include <regex>
|
||||
|
||||
#include "string/string_concat_helper.hpp"
|
||||
#include "concepts/concepts.hpp"
|
||||
#include "nlohmann/json.hpp"
|
||||
|
||||
namespace hack::utils
|
||||
{
|
||||
using json = nlohmann::json;
|
||||
|
||||
template<hack::concepts::is_string First>
|
||||
std::string make_one(First f)
|
||||
{
|
||||
f = std::regex_replace(f, std::regex("'"), "[quote]");
|
||||
return std::string("'") + f + std::string("',");
|
||||
}
|
||||
|
||||
inline std::string make_one(const char* f)
|
||||
{
|
||||
auto f_str = std::string(f);
|
||||
f_str = std::regex_replace(f_str, std::regex("'"), "[quote]");
|
||||
return std::string("'") + f_str + std::string("',");
|
||||
}
|
||||
|
||||
template<typename First>
|
||||
requires std::integral<First>
|
||||
std::string make_one(First f)
|
||||
{
|
||||
auto f_str = std::to_string(f);
|
||||
f_str = std::regex_replace(f_str, std::regex("'"), "[quote]");
|
||||
return std::string("'") + f_str + std::string("',");
|
||||
}
|
||||
|
||||
inline std::string make_one(float f)
|
||||
{
|
||||
auto f_str = std::to_string(f);
|
||||
f_str = std::regex_replace(f_str, std::regex("'"), "[quote]");
|
||||
return std::string("'") + f_str + std::string("',");
|
||||
}
|
||||
inline std::string make_one(json f)
|
||||
{
|
||||
std::string f_str = nlohmann::to_string(f);
|
||||
// f_str.erase(std::remove(f_str.begin(), f_str.end(), '\''), f_str.end());
|
||||
f_str = std::regex_replace(f_str, std::regex("'"), "[quote]");
|
||||
|
||||
return hack::string::str_concat + "'" + f_str + "'::jsonb,";
|
||||
}
|
||||
|
||||
// это заглушкa при компиляции пустых данных
|
||||
template<typename... Args>
|
||||
std::string make() { return ""; }
|
||||
|
||||
template<typename First, typename... Args>
|
||||
std::string make(const First f, const Args... args)
|
||||
{
|
||||
auto param = make_one(f);
|
||||
param += make(args...);
|
||||
return param;
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
std::string make_query(const std::string func, const Args... args)
|
||||
{
|
||||
std::string query = "SELECT s_func." + func + "(";
|
||||
query += make(args...);
|
||||
query.replace(query.find_last_of(','), 1, "");
|
||||
query += ");";
|
||||
return query;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user