From 46ef18e7d07d33111f89c21b485b0e411385247e Mon Sep 17 00:00:00 2001 From: Jochen Topf Date: Wed, 31 Jan 2024 15:29:12 +0100 Subject: [PATCH] Store db connection parameters in a their own class The connection parameters (such as dbname, user, host, ...) are stored in their original format and not concatenated into a conninfo string any more. We are now using a different database connection function from libpq that takes them in this form. This allows us to get rid of the code that transforms those parameters into the conninfo string that takes various forms of connection parameters into account, especially around the URI form of the connection parameters. The libpq has this code anyway, see also the last parameter "expand_dbname" of the PQconnectdbParams() function (https://www.postgresql.org/docs/current/libpq-connect.html). This also allow us to have slightly different database connection parameters for different database connection. This is used here to add the connection id into the fallback_application_name parameter. This connection id also reported when using --log-sql. This way you can correlate the ids from the log with the information shown in pg_stat_activity. This commit looks large, because it changes the type std::string to connection_parameters_t in many places. I have kept the parameter name "conninfo" in an effort to keep the commit small, the next commit will change that also. --- src/command-line-app.cpp | 29 +++++++++---- src/command-line-app.hpp | 6 +-- src/command-line-parser.cpp | 44 +------------------- src/db-copy.cpp | 10 +++-- src/db-copy.hpp | 6 +-- src/expire-output.cpp | 8 ++-- src/expire-output.hpp | 10 +++-- src/flex-table.cpp | 2 +- src/flex-table.hpp | 2 +- src/gen/osm2pgsql-gen.cpp | 24 ++++++----- src/middle-pgsql.cpp | 6 ++- src/middle-pgsql.hpp | 5 ++- src/options.hpp | 17 ++------ src/osmdata.cpp | 2 +- src/osmdata.hpp | 3 +- src/output-flex.cpp | 2 +- src/output-gazetteer.hpp | 1 + src/pgsql-params.hpp | 42 +++++++++++++++++++ src/pgsql.cpp | 29 +++++++++++-- src/pgsql.hpp | 3 +- src/properties.cpp | 2 +- src/properties.hpp | 8 ++-- src/table.cpp | 3 +- src/table.hpp | 5 ++- tests/CMakeLists.txt | 1 - tests/common-pg.hpp | 19 +++++++-- tests/test-options-database.cpp | 74 --------------------------------- tests/test-properties.cpp | 2 +- 28 files changed, 173 insertions(+), 192 deletions(-) create mode 100644 src/pgsql-params.hpp delete mode 100644 tests/test-options-database.cpp diff --git a/src/command-line-app.cpp b/src/command-line-app.cpp index e4247cc48..08d9161e0 100644 --- a/src/command-line-app.cpp +++ b/src/command-line-app.cpp @@ -32,29 +32,44 @@ bool command_line_app_t::want_version() const { return count("--version"); } void command_line_app_t::init_database_options() { - add_option("-d,--database", m_database_options.db) + add_option_function("-d,--database", + [&](std::string const &value) { + m_connection_params.set("dbname", + value); + }) ->description("Database name or PostgreSQL conninfo string.") ->type_name("DB") ->group("Database options"); - add_option("-U,--user", m_database_options.username) + add_option_function("-U,--user", + [&](std::string const &value) { + m_connection_params.set("user", value); + }) ->description("Database user.") ->type_name("USERNAME") ->group("Database options"); - add_flag_function( - "-W,--password", - [&](int64_t) { m_database_options.password = util::get_password(); }) + add_flag_function("-W,--password", + [&](int64_t) { + m_connection_params.set("password", + util::get_password()); + }) ->description("Force password prompt.") ->group("Database options"); - add_option("-H,--host", m_database_options.host) + add_option_function("-H,--host", + [&](std::string const &value) { + m_connection_params.set("host", value); + }) ->description( "Database server hostname or unix domain socket location.") ->type_name("HOST") ->group("Database options"); - add_option("-P,--port", m_database_options.port) + add_option_function("-P,--port", + [&](std::string const &value) { + m_connection_params.set("port", value); + }) ->description("Database server port.") ->type_name("PORT") ->group("Database options"); diff --git a/src/command-line-app.hpp b/src/command-line-app.hpp index 7dc5202ad..11a9eef08 100644 --- a/src/command-line-app.hpp +++ b/src/command-line-app.hpp @@ -26,13 +26,13 @@ class command_line_app_t : public CLI::App bool want_version() const; - database_options_t database_options() const noexcept + connection_params_t connection_params() const noexcept { - return m_database_options; + return m_connection_params; } private: - database_options_t m_database_options; + connection_params_t m_connection_params; void init_database_options(); void init_logging_options(); diff --git a/src/command-line-parser.cpp b/src/command-line-parser.cpp index f9759f0e0..bc391997b 100644 --- a/src/command-line-parser.cpp +++ b/src/command-line-parser.cpp @@ -32,48 +32,6 @@ #include #include // for number of threads -static bool compare_prefix(std::string const &str, - std::string const &prefix) noexcept -{ - return std::strncmp(str.c_str(), prefix.c_str(), prefix.size()) == 0; -} - -std::string build_conninfo(database_options_t const &opt) -{ - if (compare_prefix(opt.db, "postgresql://") || - compare_prefix(opt.db, "postgres://")) { - return opt.db; - } - - util::string_joiner_t joiner{' '}; - joiner.add("fallback_application_name='osm2pgsql'"); - - if (std::strchr(opt.db.c_str(), '=') != nullptr) { - joiner.add(opt.db); - return joiner(); - } - - joiner.add("client_encoding='UTF8'"); - - if (!opt.db.empty()) { - joiner.add(fmt::format("dbname='{}'", opt.db)); - } - if (!opt.username.empty()) { - joiner.add(fmt::format("user='{}'", opt.username)); - } - if (!opt.password.empty()) { - joiner.add(fmt::format("password='{}'", opt.password)); - } - if (!opt.host.empty()) { - joiner.add(fmt::format("host='{}'", opt.host)); - } - if (!opt.port.empty()) { - joiner.add(fmt::format("port='{}'", opt.port)); - } - - return joiner(); -} - static osmium::Box parse_bbox_param(std::string const &arg) { double minx = NAN; @@ -730,7 +688,7 @@ options_t parse_command_line(int argc, char *argv[]) check_options(&options); - options.conninfo = build_conninfo(app.database_options()); + options.conninfo = app.connection_params(); if (!options.slim) { options.middle_database_format = 0; diff --git a/src/db-copy.cpp b/src/db-copy.cpp index d61e386b8..4fd5132f8 100644 --- a/src/db-copy.cpp +++ b/src/db-copy.cpp @@ -81,10 +81,11 @@ void db_deleter_by_type_and_id_t::delete_rows(std::string const &table, conn->exec(sql.data()); } -db_copy_thread_t::db_copy_thread_t(std::string const &conninfo) +db_copy_thread_t::db_copy_thread_t(connection_params_t const &conninfo) { - // conninfo is captured by copy here, because we don't know wether the - // reference will still be valid once we get around to running the thread + // Connection params are captured by copy here, because we don't know + // whether the reference will still be valid once we get around to running + // the thread. m_worker = std::thread{thread_t{conninfo, &m_shared}}; } @@ -119,7 +120,8 @@ void db_copy_thread_t::finish() } } -db_copy_thread_t::thread_t::thread_t(std::string conninfo, shared *shared) +db_copy_thread_t::thread_t::thread_t(connection_params_t conninfo, + shared *shared) : m_conninfo(std::move(conninfo)), m_shared(shared) {} diff --git a/src/db-copy.hpp b/src/db-copy.hpp index 563187547..58302c97e 100644 --- a/src/db-copy.hpp +++ b/src/db-copy.hpp @@ -249,7 +249,7 @@ struct db_cmd_finish_t : public db_cmd_t class db_copy_thread_t { public: - explicit db_copy_thread_t(std::string const &conninfo); + explicit db_copy_thread_t(connection_params_t const &conninfo); db_copy_thread_t(db_copy_thread_t const &) = delete; db_copy_thread_t &operator=(db_copy_thread_t const &) = delete; @@ -290,7 +290,7 @@ class db_copy_thread_t class thread_t { public: - thread_t(std::string conninfo, shared *shared); + thread_t(connection_params_t conninfo, shared *shared); void operator()(); @@ -300,7 +300,7 @@ class db_copy_thread_t void finish_copy(); void delete_rows(db_cmd_copy_t *buffer); - std::string m_conninfo; + connection_params_t m_conninfo; std::unique_ptr m_conn; // Target for copy operation currently ongoing. diff --git a/src/expire-output.cpp b/src/expire-output.cpp index 41b7a0c34..911b23a0f 100644 --- a/src/expire-output.cpp +++ b/src/expire-output.cpp @@ -17,7 +17,7 @@ #include std::size_t expire_output_t::output(quadkey_list_t const &tile_list, - std::string const &conninfo) const + connection_params_t const &conninfo) const { std::size_t num = 0; if (!m_filename.empty()) { @@ -51,9 +51,9 @@ std::size_t expire_output_t::output_tiles_to_file( return count; } -std::size_t -expire_output_t::output_tiles_to_table(quadkey_list_t const &tiles_at_maxzoom, - std::string const &conninfo) const +std::size_t expire_output_t::output_tiles_to_table( + quadkey_list_t const &tiles_at_maxzoom, + connection_params_t const &conninfo) const { auto const qn = qualified_name(m_schema, m_table); diff --git a/src/expire-output.hpp b/src/expire-output.hpp index 8fa4d5e6a..28416b6dd 100644 --- a/src/expire-output.hpp +++ b/src/expire-output.hpp @@ -18,6 +18,7 @@ #include class pg_conn_t; +class connection_params_t; /** * Output for tile expiry. @@ -52,7 +53,7 @@ class expire_output_t void set_maxzoom(uint32_t maxzoom) noexcept { m_maxzoom = maxzoom; } std::size_t output(quadkey_list_t const &tile_list, - std::string const &conninfo) const; + connection_params_t const &conninfo) const; /** * Write the list of tiles to a file. @@ -66,10 +67,11 @@ class expire_output_t * Write the list of tiles to a database table. * * \param tiles_at_maxzoom The list of tiles at maximum zoom level - * \param conninfo database connection info + * \param conninfo Database connection parameters */ - std::size_t output_tiles_to_table(quadkey_list_t const &tiles_at_maxzoom, - std::string const &conninfo) const; + std::size_t + output_tiles_to_table(quadkey_list_t const &tiles_at_maxzoom, + connection_params_t const &conninfo) const; /** * Create table for tiles. diff --git a/src/flex-table.cpp b/src/flex-table.cpp index f154e02e5..940e9779f 100644 --- a/src/flex-table.cpp +++ b/src/flex-table.cpp @@ -241,7 +241,7 @@ bool flex_table_t::has_columns_with_expire() const noexcept [](auto const &column) { return column.has_expire(); }); } -void table_connection_t::connect(std::string const &conninfo) +void table_connection_t::connect(connection_params_t const &conninfo) { assert(!m_db_connection); diff --git a/src/flex-table.hpp b/src/flex-table.hpp index 70481db5a..bdfe09ccf 100644 --- a/src/flex-table.hpp +++ b/src/flex-table.hpp @@ -259,7 +259,7 @@ class table_connection_t { } - void connect(std::string const &conninfo); + void connect(connection_params_t const &conninfo); void start(bool append); diff --git a/src/gen/osm2pgsql-gen.cpp b/src/gen/osm2pgsql-gen.cpp index aff975cb4..d63f9687c 100644 --- a/src/gen/osm2pgsql-gen.cpp +++ b/src/gen/osm2pgsql-gen.cpp @@ -195,8 +195,9 @@ class tile_processor_t std::size_t m_num_tiles; }; -void run_tile_gen(std::string const &conninfo, gen_base_t *master_generalizer, - params_t params, uint32_t zoom, +void run_tile_gen(connection_params_t const &conninfo, + gen_base_t *master_generalizer, params_t params, + uint32_t zoom, std::vector> *queue, std::mutex *mut, unsigned int n) { @@ -231,9 +232,9 @@ void run_tile_gen(std::string const &conninfo, gen_base_t *master_generalizer, class genproc_t { public: - genproc_t(std::string const &filename, std::string conninfo, - std::string dbschema, bool append, bool updatable, - uint32_t jobs); + genproc_t(std::string const &filename, + connection_params_t conninfo, std::string dbschema, + bool append, bool updatable, uint32_t jobs); int app_define_table() { @@ -512,7 +513,7 @@ class genproc_t std::vector m_tables; std::vector m_expire_outputs; - std::string m_conninfo; + connection_params_t m_conninfo; std::string m_dbschema; uint32_t m_jobs; bool m_append; @@ -524,7 +525,7 @@ TRAMPOLINE(app_define_expire_output, define_expire_output) TRAMPOLINE(app_run_gen, run_gen) TRAMPOLINE(app_run_sql, run_sql) -genproc_t::genproc_t(std::string const &filename, std::string conninfo, +genproc_t::genproc_t(std::string const &filename, connection_params_t conninfo, std::string dbschema, bool append, bool updatable, uint32_t jobs) : m_conninfo(std::move(conninfo)), m_dbschema(std::move(dbschema)), @@ -694,15 +695,15 @@ int main(int argc, char *argv[]) jobs); } - auto const conninfo = build_conninfo(app.database_options()); + auto const connection_params = app.connection_params(); log_debug("Checking database capabilities..."); { - pg_conn_t const db_connection{conninfo}; + pg_conn_t const db_connection{connection_params}; init_database_capabilities(db_connection); } - properties_t properties{conninfo, middle_dbschema}; + properties_t properties{connection_params, middle_dbschema}; properties.load(); if (style.empty()) { @@ -719,7 +720,8 @@ int main(int argc, char *argv[]) } bool const updatable = properties.get_bool("updatable", false); - genproc_t gen{style, conninfo, dbschema, append, updatable, jobs}; + genproc_t gen{style, connection_params, dbschema, + append, updatable, jobs}; gen.run(); osmium::MemoryUsage const mem; diff --git a/src/middle-pgsql.cpp b/src/middle-pgsql.cpp index b6f367816..bad1595b9 100644 --- a/src/middle-pgsql.cpp +++ b/src/middle-pgsql.cpp @@ -159,7 +159,8 @@ void middle_pgsql_t::table_desc::drop_table( util::human_readable_duration(timer.stop())); } -void middle_pgsql_t::table_desc::build_index(std::string const &conninfo) const +void middle_pgsql_t::table_desc::build_index( + connection_params_t const &conninfo) const { if (m_create_fw_dep_indexes.empty()) { return; @@ -1295,7 +1296,8 @@ void middle_pgsql_t::after_relations() } middle_query_pgsql_t::middle_query_pgsql_t( - std::string const &conninfo, std::shared_ptr cache, + connection_params_t const &conninfo, + std::shared_ptr cache, std::shared_ptr persistent_cache, middle_pgsql_options const &options) : m_sql_conn(conninfo), m_cache(std::move(cache)), diff --git a/src/middle-pgsql.hpp b/src/middle-pgsql.hpp index 3f81dcd5a..b67183818 100644 --- a/src/middle-pgsql.hpp +++ b/src/middle-pgsql.hpp @@ -55,7 +55,8 @@ class middle_query_pgsql_t : public middle_query_t { public: middle_query_pgsql_t( - std::string const &conninfo, std::shared_ptr cache, + connection_params_t const &conninfo, + std::shared_ptr cache, std::shared_ptr persistent_cache, middle_pgsql_options const &options); @@ -150,7 +151,7 @@ struct middle_pgsql_t : public middle_t void drop_table(pg_conn_t const &db_connection) const; ///< Open a new database connection and build index on this table. - void build_index(std::string const &conninfo) const; + void build_index(connection_params_t const &conninfo) const; std::string m_create_table; std::vector m_prepare_queries; diff --git a/src/options.hpp b/src/options.hpp index 7b4c63b01..996e889d8 100644 --- a/src/options.hpp +++ b/src/options.hpp @@ -10,6 +10,8 @@ * For a full list of authors see the git log. */ +#include "pgsql-params.hpp" + #include #include @@ -37,18 +39,6 @@ enum class hstore_column : char all = 2 }; -/// Database connection options. -struct database_options_t -{ - std::string db; - std::string username; - std::string host; - std::string password; - std::string port; -}; - -std::string build_conninfo(database_options_t const &opt); - /** * Structure for storing command-line and other options */ @@ -56,7 +46,8 @@ struct options_t { command_t command = command_t::process; - std::string conninfo; ///< connection info for database + /// Parameters for initializing database connections + connection_params_t conninfo; std::string prefix{"planet_osm"}; ///< prefix for table names bool prefix_is_set = false; diff --git a/src/osmdata.cpp b/src/osmdata.cpp index c7709e4b2..6ddb261ef 100644 --- a/src/osmdata.cpp +++ b/src/osmdata.cpp @@ -171,7 +171,7 @@ namespace { class multithreaded_processor { public: - multithreaded_processor(std::string const &conninfo, + multithreaded_processor(connection_params_t const &conninfo, std::shared_ptr const &mid, std::shared_ptr output, std::size_t thread_count) diff --git a/src/osmdata.hpp b/src/osmdata.hpp index 0da8d7f17..8492ec812 100644 --- a/src/osmdata.hpp +++ b/src/osmdata.hpp @@ -25,6 +25,7 @@ #include "dependency-manager.hpp" #include "osmtypes.hpp" +#include "pgsql-params.hpp" class middle_t; class output_t; @@ -79,7 +80,7 @@ class osmdata_t : public osmium::handler::Handler std::shared_ptr m_mid; std::shared_ptr m_output; - std::string m_conninfo; + connection_params_t m_conninfo; // Bounding box for node import (or invalid Box if everything should be // imported). diff --git a/src/output-flex.cpp b/src/output-flex.cpp index 2d2418a4f..1bcddcc7d 100644 --- a/src/output-flex.cpp +++ b/src/output-flex.cpp @@ -1231,7 +1231,7 @@ void output_flex_t::start() static void create_expire_tables(std::vector const &expire_outputs, - std::string const &conninfo) + connection_params_t const &conninfo) { if (std::all_of(expire_outputs.begin(), expire_outputs.end(), [](auto const &expire_output) { diff --git a/src/output-gazetteer.hpp b/src/output-gazetteer.hpp index a068984a6..0f75a6a6b 100644 --- a/src/output-gazetteer.hpp +++ b/src/output-gazetteer.hpp @@ -16,6 +16,7 @@ #include #include "gazetteer-style.hpp" +#include "options.hpp" #include "osmtypes.hpp" #include "output.hpp" #include "reprojection.hpp" diff --git a/src/pgsql-params.hpp b/src/pgsql-params.hpp new file mode 100644 index 000000000..fd0230d96 --- /dev/null +++ b/src/pgsql-params.hpp @@ -0,0 +1,42 @@ +#ifndef OSM2PGSQL_PGSQL_PARAMS_HPP +#define OSM2PGSQL_PGSQL_PARAMS_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 +#include +#include +#include + +/** + * PostgreSQL connection parameters. + */ +class connection_params_t +{ +public: + connection_params_t() { m_params["client_encoding"] = "UTF8"; } + + void set(std::string const ¶m, std::string const &value) + { + m_params[param] = value; + } + + auto begin() const noexcept { return m_params.begin(); } + + auto end() const noexcept { return m_params.end(); } + +private: + std::map m_params; + +}; // class connection_params_t + +#endif // OSM2PGSQL_PGSQL_PARAMS_HPP diff --git a/src/pgsql.cpp b/src/pgsql.cpp index 13866bf99..031caf012 100644 --- a/src/pgsql.cpp +++ b/src/pgsql.cpp @@ -28,13 +28,36 @@ std::size_t pg_result_t::affected_rows() const noexcept std::atomic pg_conn_t::connection_id{0}; -pg_conn_t::pg_conn_t(std::string const &conninfo) -: m_conn(PQconnectdb(conninfo.c_str())), - m_connection_id(connection_id.fetch_add(1)) +static PGconn *open_connection(connection_params_t const &connection_params, + std::uint32_t id) { + std::vector keywords; + std::vector values; + + for (auto const &[k, v] : connection_params) { + keywords.push_back(k.c_str()); + values.push_back(v.c_str()); + } + + std::string const app_name{fmt::format("osm2pgsql/C{}", id)}; + keywords.push_back("fallback_application_name"); + values.push_back(app_name.c_str()); + + keywords.push_back(nullptr); + values.push_back(nullptr); + + return PQconnectdbParams(keywords.data(), values.data(), 1); +} + +pg_conn_t::pg_conn_t(connection_params_t const &conninfo) +: m_connection_id(connection_id.fetch_add(1)) +{ + m_conn.reset(open_connection(conninfo, m_connection_id)); + if (!m_conn) { throw std::runtime_error{"Connecting to database failed."}; } + if (PQstatus(m_conn.get()) != CONNECTION_OK) { throw fmt_error("Connecting to database failed: {}.", error_msg()); } diff --git a/src/pgsql.hpp b/src/pgsql.hpp index 9e653fc91..38b9dd445 100644 --- a/src/pgsql.hpp +++ b/src/pgsql.hpp @@ -17,6 +17,7 @@ */ #include "format.hpp" +#include "pgsql-params.hpp" #include @@ -151,7 +152,7 @@ class binary_param : public std::string_view class pg_conn_t { public: - explicit pg_conn_t(std::string const &conninfo); + explicit pg_conn_t(connection_params_t const &conninfo); /** * Run the specified SQL command. diff --git a/src/properties.cpp b/src/properties.cpp index aa154d859..09fbb58af 100644 --- a/src/properties.cpp +++ b/src/properties.cpp @@ -19,7 +19,7 @@ static constexpr char const *const properties_table = "osm2pgsql_properties"; -properties_t::properties_t(std::string conninfo, std::string schema) +properties_t::properties_t(connection_params_t conninfo, std::string schema) : m_conninfo(std::move(conninfo)), m_schema(std::move(schema)), m_has_properties_table(has_table(m_schema, properties_table)) { diff --git a/src/properties.hpp b/src/properties.hpp index e8324c4c8..3e2339478 100644 --- a/src/properties.hpp +++ b/src/properties.hpp @@ -18,6 +18,8 @@ * configuration consistent between imports and updates. */ +#include "pgsql-params.hpp" + #include #include #include @@ -30,12 +32,12 @@ class properties_t /** * Create new properties store. * - * \param conninfo Connection info used to connect to the database. + * \param conninfo Parameters used to connect to the database. * \param schema The schema used for storing the data, * * \pre You must have called init_database_capabilities() before this. */ - properties_t(std::string conninfo, std::string schema); + properties_t(connection_params_t conninfo, std::string schema); std::string get_string(std::string const &property, std::string const &default_value) const; @@ -95,7 +97,7 @@ class properties_t std::string table_name() const; std::map m_properties; - std::string m_conninfo; + connection_params_t m_conninfo; std::string m_schema; bool m_has_properties_table; diff --git a/src/table.cpp b/src/table.cpp index d8f8b446c..9875a125f 100644 --- a/src/table.cpp +++ b/src/table.cpp @@ -71,7 +71,8 @@ void table_t::connect() m_sql_conn->exec("SET synchronous_commit = off"); } -void table_t::start(std::string const &conninfo, std::string const &table_space) +void table_t::start(connection_params_t const &conninfo, + std::string const &table_space) { if (m_sql_conn) { throw fmt_error("{} cannot start, its already started.", diff --git a/src/table.hpp b/src/table.hpp index 1b42d7abb..ca97ba073 100644 --- a/src/table.hpp +++ b/src/table.hpp @@ -37,7 +37,8 @@ class table_t table_t(table_t const &other, std::shared_ptr const ©_thread); - void start(std::string const &conninfo, std::string const &table_space); + void start(connection_params_t const &conninfo, + std::string const &table_space); void stop(bool updateable, bool enable_hstore_index, std::string const &table_space_index); @@ -69,7 +70,7 @@ class table_t void generate_copy_column_list(); - std::string m_conninfo; + connection_params_t m_conninfo; std::shared_ptr m_target; std::string m_type; std::unique_ptr m_sql_conn; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 099c34a92..5288cce41 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -59,7 +59,6 @@ set_test(test-geom-transform LABELS NoDB) set_test(test-json-writer LABELS NoDB) set_test(test-middle) set_test(test-node-locations LABELS NoDB) -set_test(test-options-database LABELS NoDB) set_test(test-options-parse LABELS NoDB) set_test(test-options-projection) set_test(test-ordered-index LABELS NoDB) diff --git a/tests/common-pg.hpp b/tests/common-pg.hpp index bed23944f..6af7d58bc 100644 --- a/tests/common-pg.hpp +++ b/tests/common-pg.hpp @@ -37,7 +37,10 @@ namespace testing::pg { class conn_t : public pg_conn_t { public: - conn_t(std::string const &conninfo) : pg_conn_t(conninfo) {} + conn_t(connection_params_t const &connection_params) + : pg_conn_t(connection_params) + { + } std::string result_as_string(std::string const &cmd) const { @@ -100,7 +103,9 @@ class tempdb_t tempdb_t() noexcept { try { - conn_t conn{"dbname=postgres"}; + connection_params_t connection_params; + connection_params.set("dbname", "postgres"); + conn_t conn{connection_params}; m_db_name = fmt::format("osm2pgsql-test-{}-{}", getpid(), time(nullptr)); @@ -138,7 +143,9 @@ class tempdb_t return; } try { - conn_t conn{"dbname=postgres"}; + connection_params_t connection_params; + connection_params.set("dbname", "postgres"); + conn_t conn{connection_params}; conn.exec(R"(DROP DATABASE IF EXISTS "{}")", m_db_name); } catch (...) { fprintf(stderr, "DROP DATABASE failed. Ignored.\n"); @@ -148,7 +155,11 @@ class tempdb_t conn_t connect() const { return conn_t{conninfo()}; } - std::string conninfo() const { return "dbname=" + m_db_name; } + connection_params_t conninfo() const { + connection_params_t params; + params.set("dbname", m_db_name); + return params; + } private: std::string m_db_name; diff --git a/tests/test-options-database.cpp b/tests/test-options-database.cpp deleted file mode 100644 index 6fd652adb..000000000 --- a/tests/test-options-database.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/** - * 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 - -#include "options.hpp" - -/** - * Tests that the conninfo strings are appropriately generated - * This test is stricter than it needs to be, as it also cares about order, - * but the current implementation always uses the same order, and attempting to - * parse a conninfo string is complex. - */ -TEST_CASE("Connection info parsing with dbname", "[NoDB]") -{ - database_options_t db; - CHECK(build_conninfo(db) == - "fallback_application_name='osm2pgsql' client_encoding='UTF8'"); - db.db = "foo"; - CHECK(build_conninfo(db) == "fallback_application_name='osm2pgsql' " - "client_encoding='UTF8' dbname='foo'"); -} - -TEST_CASE("Connection info parsing with user", "[NoDB]") -{ - database_options_t db; - db.username = "bar"; - CHECK(build_conninfo(db) == "fallback_application_name='osm2pgsql' " - "client_encoding='UTF8' user='bar'"); -} - -TEST_CASE("Connection info parsing with password", "[NoDB]") -{ - database_options_t db; - db.password = "bar"; - CHECK(build_conninfo(db) == "fallback_application_name='osm2pgsql' " - "client_encoding='UTF8' password='bar'"); -} - -TEST_CASE("Connection info parsing with host", "[NoDB]") -{ - database_options_t db; - db.host = "bar"; - CHECK(build_conninfo(db) == "fallback_application_name='osm2pgsql' " - "client_encoding='UTF8' host='bar'"); -} - -TEST_CASE("Connection info parsing with port", "[NoDB]") -{ - database_options_t db; - db.port = "bar"; - CHECK(build_conninfo(db) == "fallback_application_name='osm2pgsql' " - "client_encoding='UTF8' port='bar'"); -} - -TEST_CASE("Connection info parsing with complete info", "[NoDB]") -{ - database_options_t db; - db.db = "foo"; - db.username = "bar"; - db.password = "baz"; - db.host = "bzz"; - db.port = "123"; - CHECK(build_conninfo(db) == - "fallback_application_name='osm2pgsql' client_encoding='UTF8' " - "dbname='foo' " - "user='bar' password='baz' host='bzz' port='123'"); -} diff --git a/tests/test-properties.cpp b/tests/test-properties.cpp index b35ccb0fa..0d925c124 100644 --- a/tests/test-properties.cpp +++ b/tests/test-properties.cpp @@ -15,7 +15,7 @@ TEST_CASE("Store and retrieve properties (memory only)") { - properties_t properties{"", "public"}; + properties_t properties{connection_params_t{}, "public"}; properties.set_string("foo", "firstvalue"); properties.set_string("foo", "bar"); // overwriting is okay