From 9afaf091b16d3230aa8df731aed78583a5863bdb Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Tue, 27 Nov 2012 18:37:22 -0800 Subject: [PATCH] postgis plugin: avoid printing the password if connection fails - amends 19deb86591502ad63e390a --- plugins/input/postgis/connection.hpp | 11 ++++++++--- plugins/input/postgis/connection_manager.hpp | 20 +++++++++++++------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/plugins/input/postgis/connection.hpp b/plugins/input/postgis/connection.hpp index 77e919fde..2f4bc5532 100644 --- a/plugins/input/postgis/connection.hpp +++ b/plugins/input/postgis/connection.hpp @@ -44,16 +44,21 @@ extern "C" { class Connection { public: - Connection(std::string const& connection_str) + Connection(std::string const& connection_str,boost::optional const& password) : cursorId(0), 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) { std::string err_msg = "Postgis Plugin: "; err_msg += status(); - err_msg += "\nConnection string:'"; + err_msg += "\nConnection string: '"; err_msg += connection_str; err_msg += "'\n"; throw mapnik::datasource_exception(err_msg); diff --git a/plugins/input/postgis/connection_manager.hpp b/plugins/input/postgis/connection_manager.hpp index 8cb9e58cf..cbf62f4a3 100644 --- a/plugins/input/postgis/connection_manager.hpp +++ b/plugins/input/postgis/connection_manager.hpp @@ -66,7 +66,7 @@ public: T* operator()() const { - return new T(connection_string()); + return new T(connection_string_safe(),pass_); } inline std::string id() const @@ -75,14 +75,20 @@ public: } 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; - if (host_ && (*host_).size()) connect_str += "host=" + *host_; - if (port_ && (*port_).size()) connect_str += " port=" + *port_; - if (dbname_ && (*dbname_).size()) connect_str += " dbname=" + *dbname_; - if (user_ && (*user_).size()) connect_str += " user=" + *user_; - if (pass_ && (*pass_).size()) connect_str += " password=" + *pass_; - if (connect_timeout_ && (*connect_timeout_).size()) + if (host_ && !host_->empty()) connect_str += "host=" + *host_; + if (port_ && !port_->empty()) connect_str += " port=" + *port_; + if (dbname_ && !dbname_->empty()) connect_str += " dbname=" + *dbname_; + if (user_ && !user_->empty()) connect_str += " user=" + *user_; + if (connect_timeout_ && !connect_timeout_->empty()) connect_str +=" connect_timeout=" + *connect_timeout_; return connect_str; }