diff --git a/src/middle-pgsql.cpp b/src/middle-pgsql.cpp index 102c020ee..26399f014 100644 --- a/src/middle-pgsql.cpp +++ b/src/middle-pgsql.cpp @@ -29,22 +29,6 @@ #include "pgsql-helper.hpp" #include "util.hpp" -/** - * Iterate over the result from a pgsql query and call the func with all - * the ids from the first column. - * - * \param result The result to iterate over. - * \param func Lambda taking an osmid_t as only parameter. - */ -template -void for_each_id(pg_result_t const &result, FUNC &&func) -{ - for (int i = 0; i < result.num_tuples(); ++i) { - auto const id = osmium::string_to_object_id(result.get_value(i, 0)); - std::forward(func)(id); - } -} - static std::string build_sql(options_t const &options, char const *templ) { std::string const using_tablespace{options.tblsslim_index.empty() @@ -402,30 +386,19 @@ void middle_pgsql_t::node_delete(osmid_t osm_id) } } -idlist_t middle_pgsql_t::get_ids(const char* stmt, osmid_t osm_id) -{ - idlist_t ids; - - auto const res = m_db_connection.exec_prepared(stmt, osm_id); - ids.reserve(res.num_tuples()); - for_each_id(res, [&ids](osmid_t id) { ids.push_back(id); }); - - return ids; -} - idlist_t middle_pgsql_t::get_ways_by_node(osmid_t osm_id) { - return get_ids("mark_ways_by_node", osm_id); + return get_ids_from_db(&m_db_connection, "mark_ways_by_node", osm_id); } idlist_t middle_pgsql_t::get_rels_by_node(osmid_t osm_id) { - return get_ids("mark_rels_by_node", osm_id); + return get_ids_from_db(&m_db_connection, "mark_rels_by_node", osm_id); } idlist_t middle_pgsql_t::get_rels_by_way(osmid_t osm_id) { - return get_ids("mark_rels_by_way", osm_id); + return get_ids_from_db(&m_db_connection, "mark_rels_by_way", osm_id); } 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, } auto const res = m_sql_conn.exec_prepared("get_way_list", id_list.get()); - idlist_t wayidspg; - for_each_id(res, [&wayidspg](osmid_t id) { - wayidspg.push_back(id); - }); + idlist_t const wayidspg = get_ids_from_result(res); // Match the list of ways coming from postgres in a different order // back to the list of ways given by the caller */ diff --git a/src/middle-pgsql.hpp b/src/middle-pgsql.hpp index 05a8ce48a..a663bcc61 100644 --- a/src/middle-pgsql.hpp +++ b/src/middle-pgsql.hpp @@ -128,8 +128,6 @@ struct middle_pgsql_t : public middle_t void buffer_store_tags(osmium::OSMObject const &obj, bool attrs); - idlist_t get_ids(const char* stmt, osmid_t osm_id); - table_desc m_tables[NUM_TABLES]; options_t const *m_options; diff --git a/src/pgsql-helper.cpp b/src/pgsql-helper.cpp index 15fa7c2af..1bd434cf2 100644 --- a/src/pgsql-helper.cpp +++ b/src/pgsql-helper.cpp @@ -2,6 +2,24 @@ #include "format.hpp" #include "pgsql-helper.hpp" +idlist_t get_ids_from_result(pg_result_t const &result) { + idlist_t ids; + ids.reserve(result.num_tuples()); + + for (int i = 0; i < result.num_tuples(); ++i) { + ids.push_back(osmium::string_to_object_id(result.get_value(i, 0))); + } + + return ids; +} + +idlist_t get_ids_from_db(pg_conn_t const *db_connection, char const *stmt, + osmid_t id) +{ + auto const res = db_connection->exec_prepared(stmt, id); + return get_ids_from_result(res); +} + void create_geom_check_trigger(pg_conn_t *db_connection, std::string const &schema, std::string const &table, diff --git a/src/pgsql-helper.hpp b/src/pgsql-helper.hpp index 3d85b7a3e..78147a0e5 100644 --- a/src/pgsql-helper.hpp +++ b/src/pgsql-helper.hpp @@ -5,6 +5,18 @@ #include "pgsql.hpp" +/** + * Iterate over the result from a pgsql query and generate a list of all the + * ids from the first column. + * + * \param result The result to iterate over. + * \returns A list of ids. + */ +idlist_t get_ids_from_result(pg_result_t const &result); + +idlist_t get_ids_from_db(pg_conn_t const *db_connection, char const *stmt, + osmid_t id); + void create_geom_check_trigger(pg_conn_t *db_connection, std::string const &schema, std::string const &table,