|
11 | 11 |
|
12 | 12 | #include "format.hpp" |
13 | 13 | #include "params.hpp" |
14 | | - |
15 | | -#include <fmt/args.h> |
| 14 | +#include "template.hpp" |
16 | 15 |
|
17 | 16 | #include <cassert> |
18 | 17 |
|
@@ -84,43 +83,20 @@ std::string gen_base_t::context() |
84 | 83 | return gen_name.empty() ? "" : fmt::format(" '{}'", gen_name); |
85 | 84 | } |
86 | 85 |
|
87 | | -namespace { |
88 | | - |
89 | | -pg_result_t dbexec_internal( |
90 | | - pg_conn_t const &connection, std::string const &templ, |
91 | | - fmt::dynamic_format_arg_store<fmt::format_context> const &format_store) |
92 | | -{ |
93 | | - try { |
94 | | - auto const sql = fmt::vformat(templ, format_store); |
95 | | - return connection.exec(sql); |
96 | | - } catch (fmt::format_error const &e) { |
97 | | - log_error("Missing parameter for template: '{}'", templ); |
98 | | - throw; |
99 | | - } |
100 | | -} |
101 | | - |
102 | | -} // anonymous namespace |
103 | | - |
104 | 86 | pg_result_t gen_base_t::dbexec(std::string const &templ) |
105 | 87 | { |
106 | | - fmt::dynamic_format_arg_store<fmt::format_context> format_store; |
107 | | - for (auto const &[key, value] : get_params()) { |
108 | | - format_store.push_back(fmt::arg(key.c_str(), to_string(value))); |
109 | | - } |
110 | | - return dbexec_internal(connection(), templ, format_store); |
| 88 | + template_t sql_template{templ}; |
| 89 | + sql_template.set_params(get_params()); |
| 90 | + return connection().exec(sql_template.render()); |
111 | 91 | } |
112 | 92 |
|
113 | 93 | pg_result_t gen_base_t::dbexec(params_t const &tmp_params, |
114 | 94 | std::string const &templ) |
115 | 95 | { |
116 | | - fmt::dynamic_format_arg_store<fmt::format_context> format_store; |
117 | | - for (auto const &[key, value] : get_params()) { |
118 | | - format_store.push_back(fmt::arg(key.c_str(), to_string(value))); |
119 | | - } |
120 | | - for (auto const &[key, value] : tmp_params) { |
121 | | - format_store.push_back(fmt::arg(key.c_str(), to_string(value))); |
122 | | - } |
123 | | - return dbexec_internal(connection(), templ, format_store); |
| 96 | + template_t sql_template{templ}; |
| 97 | + sql_template.set_params(get_params()); |
| 98 | + sql_template.set_params(tmp_params); |
| 99 | + return connection().exec(sql_template.render()); |
124 | 100 | } |
125 | 101 |
|
126 | 102 | void gen_base_t::raster_table_preprocess(std::string const &table) |
|
0 commit comments