Skip to content

Commit

Permalink
Merge pull request #451 from mmd-osm/patch/cleanup31
Browse files Browse the repository at this point in the history
Refactor connopt to proper function
  • Loading branch information
mmd-osm authored Aug 18, 2024
2 parents e4cbb7b + 77506ac commit 6a1684b
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/backend/apidb/pgsql_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,29 @@

#include <functional>
#include <sstream>
#include <string>


namespace po = boost::program_options;

struct RequestContext;

namespace {

void connopt(std::ostringstream &ostr, const po::variables_map &options,
const std::string &param, const std::string &pg_param)
{
if (options.count("update-" + param))
{
ostr << " " << pg_param << "="
<< options["update-" + param].as< std::string >();
}
else if (options.count(param))
{
ostr << " " << pg_param << "=" << options[param].as< std::string >();
}
}

std::string connect_db_str(const po::variables_map &options) {
// build the connection string.
std::ostringstream ostr;
Expand All @@ -38,20 +54,12 @@ std::string connect_db_str(const po::variables_map &options) {
"name for update (API write) connections.");
}

#define CONNOPT(a,b) \
if (options.count("update-" a)) { \
ostr << " " << (b "=") << options["update-" a].as<std::string>(); \
} else if (options.count(a)) { \
ostr << " " << (b "=") << options[a].as<std::string>(); \
}

CONNOPT("dbname", "dbname")
CONNOPT("host", "host")
CONNOPT("username", "user")
CONNOPT("password", "password")
CONNOPT("dbport", "port")
connopt(ostr, options, "dbname", "dbname");
connopt(ostr, options, "host", "host");
connopt(ostr, options, "username", "user");
connopt(ostr, options, "password", "password");
connopt(ostr, options, "dbport", "port");

#undef CONNOPT
return ostr.str();
}

Expand Down

0 comments on commit 6a1684b

Please sign in to comment.