diff --git a/CMakeLists.txt b/CMakeLists.txt index e35c5c092..41a89dd60 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -268,6 +268,7 @@ else() src/gen/gen-tile.cpp src/gen/params.cpp src/gen/raster.cpp + src/gen/template.cpp src/gen/tracer.cpp) target_link_libraries(osm2pgsql-gen osm2pgsql_lib ${LIBS} ${POTRACE_LIBRARY} ${OpenCV_LIBS}) endif() diff --git a/src/gen/gen-base.cpp b/src/gen/gen-base.cpp index f71c13f9b..a13298c62 100644 --- a/src/gen/gen-base.cpp +++ b/src/gen/gen-base.cpp @@ -11,8 +11,7 @@ #include "format.hpp" #include "params.hpp" - -#include +#include "template.hpp" #include @@ -84,43 +83,20 @@ std::string gen_base_t::context() return gen_name.empty() ? "" : fmt::format(" '{}'", gen_name); } -namespace { - -pg_result_t dbexec_internal( - pg_conn_t const &connection, std::string const &templ, - fmt::dynamic_format_arg_store const &format_store) -{ - try { - auto const sql = fmt::vformat(templ, format_store); - return connection.exec(sql); - } catch (fmt::format_error const &e) { - log_error("Missing parameter for template: '{}'", templ); - throw; - } -} - -} // anonymous namespace - pg_result_t gen_base_t::dbexec(std::string const &templ) { - fmt::dynamic_format_arg_store format_store; - for (auto const &[key, value] : get_params()) { - format_store.push_back(fmt::arg(key.c_str(), to_string(value))); - } - return dbexec_internal(connection(), templ, format_store); + template_t sql_template{templ}; + sql_template.set_params(get_params()); + return connection().exec(sql_template.render()); } pg_result_t gen_base_t::dbexec(params_t const &tmp_params, std::string const &templ) { - fmt::dynamic_format_arg_store format_store; - for (auto const &[key, value] : get_params()) { - format_store.push_back(fmt::arg(key.c_str(), to_string(value))); - } - for (auto const &[key, value] : tmp_params) { - format_store.push_back(fmt::arg(key.c_str(), to_string(value))); - } - return dbexec_internal(connection(), templ, format_store); + template_t sql_template{templ}; + sql_template.set_params(get_params()); + sql_template.set_params(tmp_params); + return connection().exec(sql_template.render()); } void gen_base_t::raster_table_preprocess(std::string const &table) diff --git a/src/gen/template.cpp b/src/gen/template.cpp new file mode 100644 index 000000000..78d6bed80 --- /dev/null +++ b/src/gen/template.cpp @@ -0,0 +1,26 @@ +/** + * SPDX-License-Identifier: GPL-2.0-or-later + * + * This file is part of osm2pgsql (https://osm2pgsql.org/). + * + * Copyright (C) 2006-2024 by the osm2pgsql developer community. + * For a full list of authors see the git log. + */ + +#include "template.hpp" + +void template_t::set_params(params_t const ¶ms) { + for (auto const &[key, value] : params) { + m_format_store.push_back(fmt::arg(key.c_str(), to_string(value))); + } +} + +std::string template_t::render() const +{ + try { + return fmt::vformat(m_template, m_format_store); + } catch (fmt::format_error const &e) { + log_error("Missing parameter for template: '{}'", m_template); + throw; + } +} diff --git a/src/gen/template.hpp b/src/gen/template.hpp new file mode 100644 index 000000000..1fbb324c5 --- /dev/null +++ b/src/gen/template.hpp @@ -0,0 +1,35 @@ +#ifndef OSM2PGSQL_TEMPLATE_HPP +#define OSM2PGSQL_TEMPLATE_HPP + +/** + * SPDX-License-Identifier: GPL-2.0-or-later + * + * This file is part of osm2pgsql (https://osm2pgsql.org/). + * + * Copyright (C) 2006-2024 by the osm2pgsql developer community. + * For a full list of authors see the git log. + */ + +#include "format.hpp" +#include "params.hpp" + +#include + +#include + +class template_t +{ +public: + explicit template_t(std::string_view tmpl) : m_template(tmpl) {} + + void set_params(params_t const ¶ms); + + std::string render() const; + +private: + std::string m_template; + fmt::dynamic_format_arg_store m_format_store; + +}; // class template_t + +#endif // OSM2PGSQL_TEMPLATE_HPP