Skip to content

Commit 46ef18e

Browse files
committed
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.
1 parent 229d832 commit 46ef18e

28 files changed

+173
-192
lines changed

src/command-line-app.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,44 @@ bool command_line_app_t::want_version() const { return count("--version"); }
3232

3333
void command_line_app_t::init_database_options()
3434
{
35-
add_option("-d,--database", m_database_options.db)
35+
add_option_function<std::string>("-d,--database",
36+
[&](std::string const &value) {
37+
m_connection_params.set("dbname",
38+
value);
39+
})
3640
->description("Database name or PostgreSQL conninfo string.")
3741
->type_name("DB")
3842
->group("Database options");
3943

40-
add_option("-U,--user", m_database_options.username)
44+
add_option_function<std::string>("-U,--user",
45+
[&](std::string const &value) {
46+
m_connection_params.set("user", value);
47+
})
4148
->description("Database user.")
4249
->type_name("USERNAME")
4350
->group("Database options");
4451

45-
add_flag_function(
46-
"-W,--password",
47-
[&](int64_t) { m_database_options.password = util::get_password(); })
52+
add_flag_function("-W,--password",
53+
[&](int64_t) {
54+
m_connection_params.set("password",
55+
util::get_password());
56+
})
4857
->description("Force password prompt.")
4958
->group("Database options");
5059

51-
add_option("-H,--host", m_database_options.host)
60+
add_option_function<std::string>("-H,--host",
61+
[&](std::string const &value) {
62+
m_connection_params.set("host", value);
63+
})
5264
->description(
5365
"Database server hostname or unix domain socket location.")
5466
->type_name("HOST")
5567
->group("Database options");
5668

57-
add_option("-P,--port", m_database_options.port)
69+
add_option_function<std::string>("-P,--port",
70+
[&](std::string const &value) {
71+
m_connection_params.set("port", value);
72+
})
5873
->description("Database server port.")
5974
->type_name("PORT")
6075
->group("Database options");

src/command-line-app.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ class command_line_app_t : public CLI::App
2626

2727
bool want_version() const;
2828

29-
database_options_t database_options() const noexcept
29+
connection_params_t connection_params() const noexcept
3030
{
31-
return m_database_options;
31+
return m_connection_params;
3232
}
3333

3434
private:
35-
database_options_t m_database_options;
35+
connection_params_t m_connection_params;
3636

3737
void init_database_options();
3838
void init_logging_options();

src/command-line-parser.cpp

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -32,48 +32,6 @@
3232
#include <stdexcept>
3333
#include <thread> // for number of threads
3434

35-
static bool compare_prefix(std::string const &str,
36-
std::string const &prefix) noexcept
37-
{
38-
return std::strncmp(str.c_str(), prefix.c_str(), prefix.size()) == 0;
39-
}
40-
41-
std::string build_conninfo(database_options_t const &opt)
42-
{
43-
if (compare_prefix(opt.db, "postgresql://") ||
44-
compare_prefix(opt.db, "postgres://")) {
45-
return opt.db;
46-
}
47-
48-
util::string_joiner_t joiner{' '};
49-
joiner.add("fallback_application_name='osm2pgsql'");
50-
51-
if (std::strchr(opt.db.c_str(), '=') != nullptr) {
52-
joiner.add(opt.db);
53-
return joiner();
54-
}
55-
56-
joiner.add("client_encoding='UTF8'");
57-
58-
if (!opt.db.empty()) {
59-
joiner.add(fmt::format("dbname='{}'", opt.db));
60-
}
61-
if (!opt.username.empty()) {
62-
joiner.add(fmt::format("user='{}'", opt.username));
63-
}
64-
if (!opt.password.empty()) {
65-
joiner.add(fmt::format("password='{}'", opt.password));
66-
}
67-
if (!opt.host.empty()) {
68-
joiner.add(fmt::format("host='{}'", opt.host));
69-
}
70-
if (!opt.port.empty()) {
71-
joiner.add(fmt::format("port='{}'", opt.port));
72-
}
73-
74-
return joiner();
75-
}
76-
7735
static osmium::Box parse_bbox_param(std::string const &arg)
7836
{
7937
double minx = NAN;
@@ -730,7 +688,7 @@ options_t parse_command_line(int argc, char *argv[])
730688

731689
check_options(&options);
732690

733-
options.conninfo = build_conninfo(app.database_options());
691+
options.conninfo = app.connection_params();
734692

735693
if (!options.slim) {
736694
options.middle_database_format = 0;

src/db-copy.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,11 @@ void db_deleter_by_type_and_id_t::delete_rows(std::string const &table,
8181
conn->exec(sql.data());
8282
}
8383

84-
db_copy_thread_t::db_copy_thread_t(std::string const &conninfo)
84+
db_copy_thread_t::db_copy_thread_t(connection_params_t const &conninfo)
8585
{
86-
// conninfo is captured by copy here, because we don't know wether the
87-
// reference will still be valid once we get around to running the thread
86+
// Connection params are captured by copy here, because we don't know
87+
// whether the reference will still be valid once we get around to running
88+
// the thread.
8889
m_worker = std::thread{thread_t{conninfo, &m_shared}};
8990
}
9091

@@ -119,7 +120,8 @@ void db_copy_thread_t::finish()
119120
}
120121
}
121122

122-
db_copy_thread_t::thread_t::thread_t(std::string conninfo, shared *shared)
123+
db_copy_thread_t::thread_t::thread_t(connection_params_t conninfo,
124+
shared *shared)
123125
: m_conninfo(std::move(conninfo)), m_shared(shared)
124126
{}
125127

src/db-copy.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ struct db_cmd_finish_t : public db_cmd_t
249249
class db_copy_thread_t
250250
{
251251
public:
252-
explicit db_copy_thread_t(std::string const &conninfo);
252+
explicit db_copy_thread_t(connection_params_t const &conninfo);
253253

254254
db_copy_thread_t(db_copy_thread_t const &) = delete;
255255
db_copy_thread_t &operator=(db_copy_thread_t const &) = delete;
@@ -290,7 +290,7 @@ class db_copy_thread_t
290290
class thread_t
291291
{
292292
public:
293-
thread_t(std::string conninfo, shared *shared);
293+
thread_t(connection_params_t conninfo, shared *shared);
294294

295295
void operator()();
296296

@@ -300,7 +300,7 @@ class db_copy_thread_t
300300
void finish_copy();
301301
void delete_rows(db_cmd_copy_t *buffer);
302302

303-
std::string m_conninfo;
303+
connection_params_t m_conninfo;
304304
std::unique_ptr<pg_conn_t> m_conn;
305305

306306
// Target for copy operation currently ongoing.

src/expire-output.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <system_error>
1818

1919
std::size_t expire_output_t::output(quadkey_list_t const &tile_list,
20-
std::string const &conninfo) const
20+
connection_params_t const &conninfo) const
2121
{
2222
std::size_t num = 0;
2323
if (!m_filename.empty()) {
@@ -51,9 +51,9 @@ std::size_t expire_output_t::output_tiles_to_file(
5151
return count;
5252
}
5353

54-
std::size_t
55-
expire_output_t::output_tiles_to_table(quadkey_list_t const &tiles_at_maxzoom,
56-
std::string const &conninfo) const
54+
std::size_t expire_output_t::output_tiles_to_table(
55+
quadkey_list_t const &tiles_at_maxzoom,
56+
connection_params_t const &conninfo) const
5757
{
5858
auto const qn = qualified_name(m_schema, m_table);
5959

src/expire-output.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <utility>
1919

2020
class pg_conn_t;
21+
class connection_params_t;
2122

2223
/**
2324
* Output for tile expiry.
@@ -52,7 +53,7 @@ class expire_output_t
5253
void set_maxzoom(uint32_t maxzoom) noexcept { m_maxzoom = maxzoom; }
5354

5455
std::size_t output(quadkey_list_t const &tile_list,
55-
std::string const &conninfo) const;
56+
connection_params_t const &conninfo) const;
5657

5758
/**
5859
* Write the list of tiles to a file.
@@ -66,10 +67,11 @@ class expire_output_t
6667
* Write the list of tiles to a database table.
6768
*
6869
* \param tiles_at_maxzoom The list of tiles at maximum zoom level
69-
* \param conninfo database connection info
70+
* \param conninfo Database connection parameters
7071
*/
71-
std::size_t output_tiles_to_table(quadkey_list_t const &tiles_at_maxzoom,
72-
std::string const &conninfo) const;
72+
std::size_t
73+
output_tiles_to_table(quadkey_list_t const &tiles_at_maxzoom,
74+
connection_params_t const &conninfo) const;
7375

7476
/**
7577
* Create table for tiles.

src/flex-table.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ bool flex_table_t::has_columns_with_expire() const noexcept
241241
[](auto const &column) { return column.has_expire(); });
242242
}
243243

244-
void table_connection_t::connect(std::string const &conninfo)
244+
void table_connection_t::connect(connection_params_t const &conninfo)
245245
{
246246
assert(!m_db_connection);
247247

src/flex-table.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ class table_connection_t
259259
{
260260
}
261261

262-
void connect(std::string const &conninfo);
262+
void connect(connection_params_t const &conninfo);
263263

264264
void start(bool append);
265265

src/gen/osm2pgsql-gen.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,9 @@ class tile_processor_t
195195
std::size_t m_num_tiles;
196196
};
197197

198-
void run_tile_gen(std::string const &conninfo, gen_base_t *master_generalizer,
199-
params_t params, uint32_t zoom,
198+
void run_tile_gen(connection_params_t const &conninfo,
199+
gen_base_t *master_generalizer, params_t params,
200+
uint32_t zoom,
200201
std::vector<std::pair<uint32_t, uint32_t>> *queue,
201202
std::mutex *mut, unsigned int n)
202203
{
@@ -231,9 +232,9 @@ void run_tile_gen(std::string const &conninfo, gen_base_t *master_generalizer,
231232
class genproc_t
232233
{
233234
public:
234-
genproc_t(std::string const &filename, std::string conninfo,
235-
std::string dbschema, bool append, bool updatable,
236-
uint32_t jobs);
235+
genproc_t(std::string const &filename,
236+
connection_params_t conninfo, std::string dbschema,
237+
bool append, bool updatable, uint32_t jobs);
237238

238239
int app_define_table()
239240
{
@@ -512,7 +513,7 @@ class genproc_t
512513
std::vector<flex_table_t> m_tables;
513514
std::vector<expire_output_t> m_expire_outputs;
514515

515-
std::string m_conninfo;
516+
connection_params_t m_conninfo;
516517
std::string m_dbschema;
517518
uint32_t m_jobs;
518519
bool m_append;
@@ -524,7 +525,7 @@ TRAMPOLINE(app_define_expire_output, define_expire_output)
524525
TRAMPOLINE(app_run_gen, run_gen)
525526
TRAMPOLINE(app_run_sql, run_sql)
526527

527-
genproc_t::genproc_t(std::string const &filename, std::string conninfo,
528+
genproc_t::genproc_t(std::string const &filename, connection_params_t conninfo,
528529
std::string dbschema, bool append, bool updatable,
529530
uint32_t jobs)
530531
: m_conninfo(std::move(conninfo)), m_dbschema(std::move(dbschema)),
@@ -694,15 +695,15 @@ int main(int argc, char *argv[])
694695
jobs);
695696
}
696697

697-
auto const conninfo = build_conninfo(app.database_options());
698+
auto const connection_params = app.connection_params();
698699

699700
log_debug("Checking database capabilities...");
700701
{
701-
pg_conn_t const db_connection{conninfo};
702+
pg_conn_t const db_connection{connection_params};
702703
init_database_capabilities(db_connection);
703704
}
704705

705-
properties_t properties{conninfo, middle_dbschema};
706+
properties_t properties{connection_params, middle_dbschema};
706707
properties.load();
707708

708709
if (style.empty()) {
@@ -719,7 +720,8 @@ int main(int argc, char *argv[])
719720
}
720721

721722
bool const updatable = properties.get_bool("updatable", false);
722-
genproc_t gen{style, conninfo, dbschema, append, updatable, jobs};
723+
genproc_t gen{style, connection_params, dbschema,
724+
append, updatable, jobs};
723725
gen.run();
724726

725727
osmium::MemoryUsage const mem;

0 commit comments

Comments
 (0)