postgis plugin: avoid printing the password if connection fails - amends 19deb86591

This commit is contained in:
Dane Springmeyer 2012-11-27 18:37:22 -08:00
parent c95b8ff8d7
commit 9afaf091b1
2 changed files with 21 additions and 10 deletions

View file

@ -44,16 +44,21 @@ extern "C" {
class Connection class Connection
{ {
public: public:
Connection(std::string const& connection_str) Connection(std::string const& connection_str,boost::optional<std::string> const& password)
: cursorId(0), : cursorId(0),
closed_(false) closed_(false)
{ {
conn_ = PQconnectdb(connection_str.c_str()); std::string connect_with_pass = connection_str;
if (password && !password->empty())
{
connect_with_pass += " password=" + *password;
}
conn_ = PQconnectdb(connect_with_pass.c_str());
if (PQstatus(conn_) != CONNECTION_OK) if (PQstatus(conn_) != CONNECTION_OK)
{ {
std::string err_msg = "Postgis Plugin: "; std::string err_msg = "Postgis Plugin: ";
err_msg += status(); err_msg += status();
err_msg += "\nConnection string:'"; err_msg += "\nConnection string: '";
err_msg += connection_str; err_msg += connection_str;
err_msg += "'\n"; err_msg += "'\n";
throw mapnik::datasource_exception(err_msg); throw mapnik::datasource_exception(err_msg);

View file

@ -66,7 +66,7 @@ public:
T* operator()() const T* operator()() const
{ {
return new T(connection_string()); return new T(connection_string_safe(),pass_);
} }
inline std::string id() const inline std::string id() const
@ -75,14 +75,20 @@ public:
} }
inline std::string connection_string() const inline std::string connection_string() const
{
std::string connect_str = connection_string_safe();
if (pass_ && !pass_->empty()) connect_str += " password=" + *pass_;
return connect_str;
}
inline std::string connection_string_safe() const
{ {
std::string connect_str; std::string connect_str;
if (host_ && (*host_).size()) connect_str += "host=" + *host_; if (host_ && !host_->empty()) connect_str += "host=" + *host_;
if (port_ && (*port_).size()) connect_str += " port=" + *port_; if (port_ && !port_->empty()) connect_str += " port=" + *port_;
if (dbname_ && (*dbname_).size()) connect_str += " dbname=" + *dbname_; if (dbname_ && !dbname_->empty()) connect_str += " dbname=" + *dbname_;
if (user_ && (*user_).size()) connect_str += " user=" + *user_; if (user_ && !user_->empty()) connect_str += " user=" + *user_;
if (pass_ && (*pass_).size()) connect_str += " password=" + *pass_; if (connect_timeout_ && !connect_timeout_->empty())
if (connect_timeout_ && (*connect_timeout_).size())
connect_str +=" connect_timeout=" + *connect_timeout_; connect_str +=" connect_timeout=" + *connect_timeout_;
return connect_str; return connect_str;
} }