Skip to content

Commit 5203419

Browse files
authored
Merge pull request #1374 from joto/for-each-id
Refactoring: Simplify function and pull it out into helper file
2 parents 06e44fa + f9d8093 commit 5203419

File tree

4 files changed

+34
-36
lines changed

4 files changed

+34
-36
lines changed

src/middle-pgsql.cpp

Lines changed: 4 additions & 34 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()
@@ -402,30 +386,19 @@ void middle_pgsql_t::node_delete(osmid_t osm_id)
402386
}
403387
}
404388

405-
idlist_t middle_pgsql_t::get_ids(const char* stmt, osmid_t osm_id)
406-
{
407-
idlist_t ids;
408-
409-
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;
414-
}
415-
416389
idlist_t middle_pgsql_t::get_ways_by_node(osmid_t osm_id)
417390
{
418-
return get_ids("mark_ways_by_node", osm_id);
391+
return get_ids_from_db(&m_db_connection, "mark_ways_by_node", osm_id);
419392
}
420393

421394
idlist_t middle_pgsql_t::get_rels_by_node(osmid_t osm_id)
422395
{
423-
return get_ids("mark_rels_by_node", osm_id);
396+
return get_ids_from_db(&m_db_connection, "mark_rels_by_node", osm_id);
424397
}
425398

426399
idlist_t middle_pgsql_t::get_rels_by_way(osmid_t osm_id)
427400
{
428-
return get_ids("mark_rels_by_way", osm_id);
401+
return get_ids_from_db(&m_db_connection, "mark_rels_by_way", osm_id);
429402
}
430403

431404
void middle_pgsql_t::way_set(osmium::Way const &way)
@@ -486,10 +459,7 @@ middle_query_pgsql_t::rel_way_members_get(osmium::Relation const &rel,
486459
}
487460

488461
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-
});
462+
idlist_t const wayidspg = get_ids_from_result(res);
493463

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

src/middle-pgsql.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,6 @@ struct middle_pgsql_t : public middle_t
128128

129129
void buffer_store_tags(osmium::OSMObject const &obj, bool attrs);
130130

131-
idlist_t get_ids(const char* stmt, osmid_t osm_id);
132-
133131
table_desc m_tables[NUM_TABLES];
134132

135133
options_t const *m_options;

src/pgsql-helper.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
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+
16+
idlist_t get_ids_from_db(pg_conn_t const *db_connection, char const *stmt,
17+
osmid_t id)
18+
{
19+
auto const res = db_connection->exec_prepared(stmt, id);
20+
return get_ids_from_result(res);
21+
}
22+
523
void create_geom_check_trigger(pg_conn_t *db_connection,
624
std::string const &schema,
725
std::string const &table,

src/pgsql-helper.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
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+
17+
idlist_t get_ids_from_db(pg_conn_t const *db_connection, char const *stmt,
18+
osmid_t id);
19+
820
void create_geom_check_trigger(pg_conn_t *db_connection,
921
std::string const &schema,
1022
std::string const &table,

0 commit comments

Comments
 (0)