Skip to content

Commit 23726b4

Browse files
committed
Replace for_each_id() function by get_ids_from_result() helper
1 parent 06e44fa commit 23726b4

File tree

3 files changed

+22
-26
lines changed

3 files changed

+22
-26
lines changed

src/middle-pgsql.cpp

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,6 @@
2929
#include "pgsql-helper.hpp"
3030
#include "util.hpp"
3131

32-
/**
33-
* Iterate over the result from a pgsql query and call the func with all
34-
* the ids from the first column.
35-
*
36-
* \param result The result to iterate over.
37-
* \param func Lambda taking an osmid_t as only parameter.
38-
*/
39-
template <typename FUNC>
40-
void for_each_id(pg_result_t const &result, FUNC &&func)
41-
{
42-
for (int i = 0; i < result.num_tuples(); ++i) {
43-
auto const id = osmium::string_to_object_id(result.get_value(i, 0));
44-
std::forward<FUNC>(func)(id);
45-
}
46-
}
47-
4832
static std::string build_sql(options_t const &options, char const *templ)
4933
{
5034
std::string const using_tablespace{options.tblsslim_index.empty()
@@ -404,13 +388,8 @@ void middle_pgsql_t::node_delete(osmid_t osm_id)
404388

405389
idlist_t middle_pgsql_t::get_ids(const char* stmt, osmid_t osm_id)
406390
{
407-
idlist_t ids;
408-
409391
auto const res = m_db_connection.exec_prepared(stmt, osm_id);
410-
ids.reserve(res.num_tuples());
411-
for_each_id(res, [&ids](osmid_t id) { ids.push_back(id); });
412-
413-
return ids;
392+
return get_ids_from_result(res);
414393
}
415394

416395
idlist_t middle_pgsql_t::get_ways_by_node(osmid_t osm_id)
@@ -486,10 +465,7 @@ middle_query_pgsql_t::rel_way_members_get(osmium::Relation const &rel,
486465
}
487466

488467
auto const res = m_sql_conn.exec_prepared("get_way_list", id_list.get());
489-
idlist_t wayidspg;
490-
for_each_id(res, [&wayidspg](osmid_t id) {
491-
wayidspg.push_back(id);
492-
});
468+
idlist_t const wayidspg = get_ids_from_result(res);
493469

494470
// Match the list of ways coming from postgres in a different order
495471
// back to the list of ways given by the caller */

src/pgsql-helper.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
#include "format.hpp"
33
#include "pgsql-helper.hpp"
44

5+
idlist_t get_ids_from_result(pg_result_t const &result) {
6+
idlist_t ids;
7+
ids.reserve(result.num_tuples());
8+
9+
for (int i = 0; i < result.num_tuples(); ++i) {
10+
ids.push_back(osmium::string_to_object_id(result.get_value(i, 0)));
11+
}
12+
13+
return ids;
14+
}
15+
516
void create_geom_check_trigger(pg_conn_t *db_connection,
617
std::string const &schema,
718
std::string const &table,

src/pgsql-helper.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55

66
#include "pgsql.hpp"
77

8+
/**
9+
* Iterate over the result from a pgsql query and generate a list of all the
10+
* ids from the first column.
11+
*
12+
* \param result The result to iterate over.
13+
* \returns A list of ids.
14+
*/
15+
idlist_t get_ids_from_result(pg_result_t const &result);
16+
817
void create_geom_check_trigger(pg_conn_t *db_connection,
918
std::string const &schema,
1019
std::string const &table,

0 commit comments

Comments
 (0)