Skip to content

Commit dab2049

Browse files
committed
Simplify database connection options code
1 parent 4bb963d commit dab2049

File tree

2 files changed

+21
-32
lines changed

2 files changed

+21
-32
lines changed

src/options.cpp

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <cstring>
99
#include <getopt.h>
1010
#include <osmium/version.hpp>
11-
#include <sstream>
1211
#include <stdexcept>
1312
#include <thread> // for number of threads
1413

@@ -257,33 +256,27 @@ void long_usage(char const *arg0, bool verbose = false)
257256

258257
} // anonymous namespace
259258

260-
database_options_t::database_options_t()
261-
: db(boost::none), username(boost::none), host(boost::none),
262-
password(boost::none), port(boost::none)
263-
{}
264-
265259
std::string database_options_t::conninfo() const
266260
{
267-
std::ostringstream out;
261+
std::string out{"fallback_application_name='osm2pgsql'"};
268262

269-
out << "fallback_application_name='osm2pgsql'";
270-
if (db) {
271-
out << " dbname='" << *db << "'";
263+
if (!db.empty()) {
264+
out += " dbname='{}'"_format(db);
272265
}
273-
if (username) {
274-
out << " user='" << *username << "'";
266+
if (!username.empty()) {
267+
out += " user='{}'"_format(username);
275268
}
276-
if (password) {
277-
out << " password='" << *password << "'";
269+
if (!password.empty()) {
270+
out += " password='{}'"_format(password);
278271
}
279-
if (host) {
280-
out << " host='" << *host << "'";
272+
if (!host.empty()) {
273+
out += " host='{}'"_format(host);
281274
}
282-
if (port) {
283-
out << " port='" << *port << "'";
275+
if (!port.empty()) {
276+
out += " port='{}'"_format(port);
284277
}
285278

286-
return out.str();
279+
return out;
287280
}
288281

289282
options_t::options_t()
@@ -581,11 +574,9 @@ options_t::options_t(int argc, char *argv[]) : options_t()
581574
check_options();
582575

583576
if (pass_prompt) {
584-
char *prompt = simple_prompt("Password:", 100, 0);
585-
if (prompt == nullptr) {
586-
database_options.password = boost::none;
587-
} else {
588-
database_options.password = std::string(prompt);
577+
char const *prompt = simple_prompt("Password:", 100, 0);
578+
if (prompt != nullptr) {
579+
database_options.password = prompt;
589580
}
590581
}
591582
}

src/options.hpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@
2222
/**
2323
* Database options, not specific to a table
2424
*/
25-
class database_options_t
25+
struct database_options_t
2626
{
27-
public:
28-
database_options_t();
29-
boost::optional<std::string> db;
30-
boost::optional<std::string> username;
31-
boost::optional<std::string> host;
32-
boost::optional<std::string> password;
33-
boost::optional<std::string> port;
27+
std::string db;
28+
std::string username;
29+
std::string host;
30+
std::string password;
31+
std::string port;
3432

3533
std::string conninfo() const;
3634
};

0 commit comments

Comments
 (0)