Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
40 changes: 8 additions & 32 deletions src/gen/gen-base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

#include "format.hpp"
#include "params.hpp"

#include <fmt/args.h>
#include "template.hpp"

#include <cassert>

Expand Down Expand Up @@ -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<fmt::format_context> 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<fmt::format_context> 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<fmt::format_context> 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)
Expand Down
26 changes: 26 additions & 0 deletions src/gen/template.cpp
Original file line number Diff line number Diff line change
@@ -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 &params) {
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;
}
}
35 changes: 35 additions & 0 deletions src/gen/template.hpp
Original file line number Diff line number Diff line change
@@ -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 <fmt/args.h>

#include <string>

class template_t
{
public:
explicit template_t(std::string_view tmpl) : m_template(tmpl) {}

void set_params(params_t const &params);

std::string render() const;

private:
std::string m_template;
fmt::dynamic_format_arg_store<fmt::format_context> m_format_store;

}; // class template_t

#endif // OSM2PGSQL_TEMPLATE_HPP
Loading