Skip to content

Commit 0becb10

Browse files
authored
Merge pull request #2277 from joto/new-template-class
Refactoring: Add new template_t class for rendering SQL templates
2 parents cc0ea60 + 3db3c81 commit 0becb10

File tree

4 files changed

+70
-32
lines changed

4 files changed

+70
-32
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ else()
268268
src/gen/gen-tile.cpp
269269
src/gen/params.cpp
270270
src/gen/raster.cpp
271+
src/gen/template.cpp
271272
src/gen/tracer.cpp)
272273
target_link_libraries(osm2pgsql-gen osm2pgsql_lib ${LIBS} ${POTRACE_LIBRARY} ${OpenCV_LIBS})
273274
endif()

src/gen/gen-base.cpp

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
#include "format.hpp"
1313
#include "params.hpp"
14-
15-
#include <fmt/args.h>
14+
#include "template.hpp"
1615

1716
#include <cassert>
1817

@@ -84,43 +83,20 @@ std::string gen_base_t::context()
8483
return gen_name.empty() ? "" : fmt::format(" '{}'", gen_name);
8584
}
8685

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-
10486
pg_result_t gen_base_t::dbexec(std::string const &templ)
10587
{
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());
11191
}
11292

11393
pg_result_t gen_base_t::dbexec(params_t const &tmp_params,
11494
std::string const &templ)
11595
{
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());
124100
}
125101

126102
void gen_base_t::raster_table_preprocess(std::string const &table)

src/gen/template.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* SPDX-License-Identifier: GPL-2.0-or-later
3+
*
4+
* This file is part of osm2pgsql (https://osm2pgsql.org/).
5+
*
6+
* Copyright (C) 2006-2024 by the osm2pgsql developer community.
7+
* For a full list of authors see the git log.
8+
*/
9+
10+
#include "template.hpp"
11+
12+
void template_t::set_params(params_t const &params) {
13+
for (auto const &[key, value] : params) {
14+
m_format_store.push_back(fmt::arg(key.c_str(), to_string(value)));
15+
}
16+
}
17+
18+
std::string template_t::render() const
19+
{
20+
try {
21+
return fmt::vformat(m_template, m_format_store);
22+
} catch (fmt::format_error const &e) {
23+
log_error("Missing parameter for template: '{}'", m_template);
24+
throw;
25+
}
26+
}

src/gen/template.hpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef OSM2PGSQL_TEMPLATE_HPP
2+
#define OSM2PGSQL_TEMPLATE_HPP
3+
4+
/**
5+
* SPDX-License-Identifier: GPL-2.0-or-later
6+
*
7+
* This file is part of osm2pgsql (https://osm2pgsql.org/).
8+
*
9+
* Copyright (C) 2006-2024 by the osm2pgsql developer community.
10+
* For a full list of authors see the git log.
11+
*/
12+
13+
#include "format.hpp"
14+
#include "params.hpp"
15+
16+
#include <fmt/args.h>
17+
18+
#include <string>
19+
20+
class template_t
21+
{
22+
public:
23+
explicit template_t(std::string_view tmpl) : m_template(tmpl) {}
24+
25+
void set_params(params_t const &params);
26+
27+
std::string render() const;
28+
29+
private:
30+
std::string m_template;
31+
fmt::dynamic_format_arg_store<fmt::format_context> m_format_store;
32+
33+
}; // class template_t
34+
35+
#endif // OSM2PGSQL_TEMPLATE_HPP

0 commit comments

Comments
 (0)