diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3f823cf --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +build +.cache +subprojects/* +!subprojects/gtest.wrap +!subprojects/nlohmann_json.wrap + diff --git a/bin/main.cpp b/bin/main.cpp index 924f635..4f5670d 100644 --- a/bin/main.cpp +++ b/bin/main.cpp @@ -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::value); hack::log()(hack::security::is_string::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); } - 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: 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); } } diff --git a/meson.build b/meson.build index 58175cc..25a8b3a 100644 --- a/meson.build +++ b/meson.build @@ -22,6 +22,7 @@ inc = [] deps += dependency('boost') deps += dependency('uuid') +deps += subproject('nlohmann_json').get_variable('nlohmann_json_dep') args += '-luuid' diff --git a/src/utils/func_query.hpp b/src/utils/func_query.hpp new file mode 100644 index 0000000..3892b34 --- /dev/null +++ b/src/utils/func_query.hpp @@ -0,0 +1,73 @@ +#pragma once + +#include + +#include "string/string_concat_helper.hpp" +#include "concepts/concepts.hpp" +#include "nlohmann/json.hpp" + +namespace hack::utils +{ + using json = nlohmann::json; + + template + 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 + requires std::integral + 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 + std::string make() { return ""; } + + template + std::string make(const First f, const Args... args) + { + auto param = make_one(f); + param += make(args...); + return param; + } + + template + 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; + } + +} diff --git a/subprojects/nlohmann_json.wrap b/subprojects/nlohmann_json.wrap new file mode 100644 index 0000000..095ff54 --- /dev/null +++ b/subprojects/nlohmann_json.wrap @@ -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 + +