Merge pull request #3984 from lightmare/pg-app-name

accept explicit parameter "application_name" in postgis and pgraster datasources
This commit is contained in:
lightmare 2019-08-12 12:52:34 +02:00 committed by GitHub
commit a0ea7db1a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 57 deletions

View file

@ -78,12 +78,7 @@ pgraster_datasource::pgraster_datasource(parameters const& params)
use_overviews_(*params.get<mapnik::boolean_type>("use_overviews", false)), use_overviews_(*params.get<mapnik::boolean_type>("use_overviews", false)),
clip_rasters_(*params.get<mapnik::boolean_type>("clip_rasters", false)), clip_rasters_(*params.get<mapnik::boolean_type>("clip_rasters", false)),
desc_(*params.get<std::string>("type"), "utf-8"), desc_(*params.get<std::string>("type"), "utf-8"),
creator_(params.get<std::string>("host"), creator_(params),
params.get<std::string>("port"),
params.get<std::string>("dbname"),
params.get<std::string>("user"),
params.get<std::string>("password"),
params.get<std::string>("connect_timeout", "4")),
re_tokens_("!(@?\\w+)!"), // matches !mapnik_var! or !@user_var! re_tokens_("!(@?\\w+)!"), // matches !mapnik_var! or !@user_var!
pool_max_size_(*params_.get<value_integer>("max_size", 10)), pool_max_size_(*params_.get<value_integer>("max_size", 10)),
persist_connection_(*params.get<mapnik::boolean_type>("persist_connection", true)), persist_connection_(*params.get<mapnik::boolean_type>("persist_connection", true)),

View file

@ -26,16 +26,15 @@
#include "connection.hpp" #include "connection.hpp"
// mapnik // mapnik
#include <mapnik/params.hpp>
#include <mapnik/pool.hpp> #include <mapnik/pool.hpp>
#include <mapnik/util/singleton.hpp> #include <mapnik/util/singleton.hpp>
// boost // boost
#include <memory>
#include <boost/optional.hpp> #include <boost/optional.hpp>
// stl // stl
#include <string> #include <string>
#include <sstream>
#include <memory> #include <memory>
using mapnik::Pool; using mapnik::Pool;
@ -47,22 +46,19 @@ class ConnectionCreator
{ {
public: public:
ConnectionCreator(boost::optional<std::string> const& host, ConnectionCreator(mapnik::parameters const& params)
boost::optional<std::string> const& port, : host_{params.get<std::string>("host")},
boost::optional<std::string> const& dbname, port_{params.get<std::string>("port")},
boost::optional<std::string> const& user, dbname_{params.get<std::string>("dbname")},
boost::optional<std::string> const& pass, user_{params.get<std::string>("user")},
boost::optional<std::string> const& connect_timeout) password_{params.get<std::string>("password")},
: host_(host), connect_timeout_{params.get<std::string>("connect_timeout", "4")},
port_(port), application_name_{params.get<std::string>("application_name")}
dbname_(dbname), {}
user_(user),
pass_(pass),
connect_timeout_(connect_timeout) {}
T* operator()() const T* operator()() const
{ {
return new T(connection_string_safe(),pass_); return new T(connection_string_safe(), password_);
} }
inline std::string id() const inline std::string id() const
@ -73,29 +69,52 @@ public:
inline std::string connection_string() const inline std::string connection_string() const
{ {
std::string connect_str = connection_string_safe(); std::string connect_str = connection_string_safe();
if (pass_ && !pass_->empty()) connect_str += " password=" + *pass_; append_param(connect_str, "password=", password_);
return connect_str; return connect_str;
} }
inline std::string connection_string_safe() const inline std::string connection_string_safe() const
{ {
std::string connect_str; std::string connect_str;
if (host_ && !host_->empty()) connect_str += "host=" + *host_; append_param(connect_str, "host=", host_);
if (port_ && !port_->empty()) connect_str += " port=" + *port_; append_param(connect_str, "port=", port_);
if (dbname_ && !dbname_->empty()) connect_str += " dbname=" + *dbname_; append_param(connect_str, "dbname=", dbname_);
if (user_ && !user_->empty()) connect_str += " user=" + *user_; append_param(connect_str, "user=", user_);
if (connect_timeout_ && !connect_timeout_->empty()) append_param(connect_str, "connect_timeout=", connect_timeout_);
connect_str +=" connect_timeout=" + *connect_timeout_; if (!append_param(connect_str, "application_name=", application_name_))
{
// only set fallback_application_name, so that application_name
// can still be overriden with PGAPPNAME environment variable
append_param(connect_str, "fallback_application_name=", "mapnik");
}
return connect_str; return connect_str;
} }
private: private:
static bool append_param(std::string & dest, char const* key,
std::string const& val)
{
if (val.empty()) return false;
if (!dest.empty()) dest += ' ';
dest += key;
dest += val;
return true;
}
static bool append_param(std::string & dest, char const* key,
boost::optional<std::string> const& opt)
{
return opt && append_param(dest, key, *opt);
}
boost::optional<std::string> host_; boost::optional<std::string> host_;
boost::optional<std::string> port_; boost::optional<std::string> port_;
boost::optional<std::string> dbname_; boost::optional<std::string> dbname_;
boost::optional<std::string> user_; boost::optional<std::string> user_;
boost::optional<std::string> pass_; boost::optional<std::string> password_;
boost::optional<std::string> connect_timeout_; boost::optional<std::string> connect_timeout_;
boost::optional<std::string> application_name_;
}; };
class ConnectionManager : public singleton <ConnectionManager,CreateStatic> class ConnectionManager : public singleton <ConnectionManager,CreateStatic>

View file

@ -71,12 +71,7 @@ postgis_datasource::postgis_datasource(parameters const& params)
extent_initialized_(false), extent_initialized_(false),
simplify_geometries_(false), simplify_geometries_(false),
desc_(postgis_datasource::name(), "utf-8"), desc_(postgis_datasource::name(), "utf-8"),
creator_(params.get<std::string>("host"), creator_(params),
params.get<std::string>("port"),
params.get<std::string>("dbname"),
params.get<std::string>("user"),
params.get<std::string>("password"),
params.get<std::string>("connect_timeout", "4")),
pool_max_size_(*params_.get<mapnik::value_integer>("max_size", 10)), pool_max_size_(*params_.get<mapnik::value_integer>("max_size", 10)),
persist_connection_(*params.get<mapnik::boolean_type>("persist_connection", true)), persist_connection_(*params.get<mapnik::boolean_type>("persist_connection", true)),
extent_from_subquery_(*params.get<mapnik::boolean_type>("extent_from_subquery", false)), extent_from_subquery_(*params.get<mapnik::boolean_type>("extent_from_subquery", false)),

View file

@ -433,14 +433,16 @@ TEST_CASE("ConnectionCreator") {
SECTION("ConnectionCreator::id() should not expose password") SECTION("ConnectionCreator::id() should not expose password")
{ {
ConnectionCreator<Connection> creator(boost::optional<std::string>("host"), mapnik::parameters params;
boost::optional<std::string>("12345"), params["host"] = "H";
boost::optional<std::string>("dbname"), params["port"] = "1234";
boost::optional<std::string>("user"), params["dbname"] = "D";
boost::optional<std::string>("pass"), params["user"] = "U";
boost::optional<std::string>("111")); params["password"] = "SECRET";
params["connect_timeout"] = "5";
CHECK(creator.id() == "host=host port=12345 dbname=dbname user=user connect_timeout=111"); params["application_name"] = "A";
ConnectionCreator<Connection> creator(params);
CHECK(creator.id() == "host=H port=1234 dbname=D user=U connect_timeout=5 application_name=A");
} }
} }

View file

@ -83,20 +83,15 @@ int main ( int argc, char** argv)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
boost::optional<std::string> host; mapnik::parameters conn_params;
boost::optional<std::string> port ; conn_params["application_name"] = "pgsql2sqlite";
boost::optional<std::string> dbname; for (auto k : {"host", "port", "dbname", "user", "password"})
boost::optional<std::string> user; {
boost::optional<std::string> password; if (!vm[k].empty())
boost::optional<std::string> connect_timeout("4"); conn_params[k] = vm[k].as<std::string>();
}
if (vm.count("host")) host = vm["host"].as<std::string>(); ConnectionCreator<Connection> creator(conn_params);
if (vm.count("port")) port = vm["port"].as<std::string>();
if (vm.count("dbname")) dbname = vm["dbname"].as<std::string>();
if (vm.count("user")) user = vm["user"].as<std::string>();
if (vm.count("password")) password = vm["password"].as<std::string>();
ConnectionCreator<Connection> creator(host,port,dbname,user,password,connect_timeout);
try try
{ {
std::shared_ptr<Connection> conn(creator()); std::shared_ptr<Connection> conn(creator());
@ -112,8 +107,8 @@ int main ( int argc, char** argv)
catch (mapnik::datasource_exception & ex) catch (mapnik::datasource_exception & ex)
{ {
std::cerr << ex.what() << "\n"; std::cerr << ex.what() << "\n";
return EXIT_FAILURE;
} }
} }
catch(std::exception& e) { catch(std::exception& e) {
std::cerr << desc << "\n"; std::cerr << desc << "\n";