add function query

This commit is contained in:
chatlanin 2022-11-09 13:50:36 +03:00
parent 7c79befcb2
commit 794658e57c
5 changed files with 114 additions and 11 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
build
.cache
subprojects/*
!subprojects/gtest.wrap
!subprojects/nlohmann_json.wrap

View File

@ -15,6 +15,7 @@
#include "math/matrix.hpp"
#include "math/vector.hpp"
#include "utils/utils.hpp"
#include "utils/func_query.hpp"
#include "security/validate_email.hpp"
#include "security/generate_uuid.hpp"
#include "security/is_string.hpp"
@ -245,8 +246,7 @@ int main(int argc, char *argv[])
hack::log()("============================================================");
hack::log()("security::validate_email");
{
// ec: security::validate_email
{// ex: security::validate_email
std::string email = "asdf@asdf.com";
hack::log()(hack::security::validate_email(email));
}
@ -254,16 +254,14 @@ int main(int argc, char *argv[])
hack::log()("============================================================");
hack::log()("security::generate_uuid");
{
// ec: security::generate_uuid
{// ex: security::generate_uuid
hack::log()(hack::security::generate_uuid());
}
hack::log()("============================================================");
hack::log()("security::is_string");
{
// ec: security::is_string
{// ex: security::is_string
std::string s {"test"};
hack::log()(hack::security::is_string<decltype ("test_string")>::value);
hack::log()(hack::security::is_string<decltype (s)>::value);
@ -275,14 +273,28 @@ int main(int argc, char *argv[])
hack::log()("============================================================");
hack::log()("utils::counter");
{
{// ex: counter
counter_test a, b, c;
hack::log()(c.id);
}
{// ex: case as string
switch(hack::utils::case_int("test"))
{
case hack::utils::case_int("test"): hack::log()("wow"); break;
case hack::utils::case_int("no_test"): hack::log()("ups"); break;
}
}
{// ex: query function
auto query = hack::utils::make_query("super_function", "1", "two");
hack::log()("query", query);
query = hack::utils::make_query("super_function", 1, 'c');
hack::log()("query", query);
hack::utils::json js { "test", "data" };
query = hack::utils::make_query("super_function", 1, 123.3f, js);
hack::log()("query", query);
}
}

View File

@ -22,6 +22,7 @@ inc = []
deps += dependency('boost')
deps += dependency('uuid')
deps += subproject('nlohmann_json').get_variable('nlohmann_json_dep')
args += '-luuid'

73
src/utils/func_query.hpp Normal file
View 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;
}
}

View File

@ -0,0 +1,11 @@
[wrap-file]
directory = nlohmann_json-3.10.5
lead_directory_missing = true
source_url = https://github.com/nlohmann/json/releases/download/v3.10.5/include.zip
source_filename = nlohmann_json-3.10.5.zip
source_hash = b94997df68856753b72f0d7a3703b7d484d4745c567f3584ef97c96c25a5798e
[provide]
nlohmann_json = nlohmann_json_dep