Merge pull request #3984 from lightmare/pg-app-name
accept explicit parameter "application_name" in postgis and pgraster datasources
This commit is contained in:
commit
a0ea7db1a7
5 changed files with 63 additions and 57 deletions
|
@ -78,12 +78,7 @@ pgraster_datasource::pgraster_datasource(parameters const& params)
|
|||
use_overviews_(*params.get<mapnik::boolean_type>("use_overviews", false)),
|
||||
clip_rasters_(*params.get<mapnik::boolean_type>("clip_rasters", false)),
|
||||
desc_(*params.get<std::string>("type"), "utf-8"),
|
||||
creator_(params.get<std::string>("host"),
|
||||
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")),
|
||||
creator_(params),
|
||||
re_tokens_("!(@?\\w+)!"), // matches !mapnik_var! or !@user_var!
|
||||
pool_max_size_(*params_.get<value_integer>("max_size", 10)),
|
||||
persist_connection_(*params.get<mapnik::boolean_type>("persist_connection", true)),
|
||||
|
|
|
@ -26,16 +26,15 @@
|
|||
#include "connection.hpp"
|
||||
|
||||
// mapnik
|
||||
#include <mapnik/params.hpp>
|
||||
#include <mapnik/pool.hpp>
|
||||
#include <mapnik/util/singleton.hpp>
|
||||
|
||||
// boost
|
||||
#include <memory>
|
||||
#include <boost/optional.hpp>
|
||||
|
||||
// stl
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <memory>
|
||||
|
||||
using mapnik::Pool;
|
||||
|
@ -47,22 +46,19 @@ class ConnectionCreator
|
|||
{
|
||||
|
||||
public:
|
||||
ConnectionCreator(boost::optional<std::string> const& host,
|
||||
boost::optional<std::string> const& port,
|
||||
boost::optional<std::string> const& dbname,
|
||||
boost::optional<std::string> const& user,
|
||||
boost::optional<std::string> const& pass,
|
||||
boost::optional<std::string> const& connect_timeout)
|
||||
: host_(host),
|
||||
port_(port),
|
||||
dbname_(dbname),
|
||||
user_(user),
|
||||
pass_(pass),
|
||||
connect_timeout_(connect_timeout) {}
|
||||
ConnectionCreator(mapnik::parameters const& params)
|
||||
: host_{params.get<std::string>("host")},
|
||||
port_{params.get<std::string>("port")},
|
||||
dbname_{params.get<std::string>("dbname")},
|
||||
user_{params.get<std::string>("user")},
|
||||
password_{params.get<std::string>("password")},
|
||||
connect_timeout_{params.get<std::string>("connect_timeout", "4")},
|
||||
application_name_{params.get<std::string>("application_name")}
|
||||
{}
|
||||
|
||||
T* operator()() const
|
||||
{
|
||||
return new T(connection_string_safe(),pass_);
|
||||
return new T(connection_string_safe(), password_);
|
||||
}
|
||||
|
||||
inline std::string id() const
|
||||
|
@ -73,29 +69,52 @@ public:
|
|||
inline std::string connection_string() const
|
||||
{
|
||||
std::string connect_str = connection_string_safe();
|
||||
if (pass_ && !pass_->empty()) connect_str += " password=" + *pass_;
|
||||
append_param(connect_str, "password=", password_);
|
||||
return connect_str;
|
||||
}
|
||||
|
||||
inline std::string connection_string_safe() const
|
||||
{
|
||||
std::string connect_str;
|
||||
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_;
|
||||
append_param(connect_str, "host=", host_);
|
||||
append_param(connect_str, "port=", port_);
|
||||
append_param(connect_str, "dbname=", dbname_);
|
||||
append_param(connect_str, "user=", user_);
|
||||
append_param(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;
|
||||
}
|
||||
|
||||
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> port_;
|
||||
boost::optional<std::string> dbname_;
|
||||
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> application_name_;
|
||||
};
|
||||
|
||||
class ConnectionManager : public singleton <ConnectionManager,CreateStatic>
|
||||
|
|
|
@ -71,12 +71,7 @@ postgis_datasource::postgis_datasource(parameters const& params)
|
|||
extent_initialized_(false),
|
||||
simplify_geometries_(false),
|
||||
desc_(postgis_datasource::name(), "utf-8"),
|
||||
creator_(params.get<std::string>("host"),
|
||||
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")),
|
||||
creator_(params),
|
||||
pool_max_size_(*params_.get<mapnik::value_integer>("max_size", 10)),
|
||||
persist_connection_(*params.get<mapnik::boolean_type>("persist_connection", true)),
|
||||
extent_from_subquery_(*params.get<mapnik::boolean_type>("extent_from_subquery", false)),
|
||||
|
|
|
@ -433,14 +433,16 @@ TEST_CASE("ConnectionCreator") {
|
|||
|
||||
SECTION("ConnectionCreator::id() should not expose password")
|
||||
{
|
||||
ConnectionCreator<Connection> creator(boost::optional<std::string>("host"),
|
||||
boost::optional<std::string>("12345"),
|
||||
boost::optional<std::string>("dbname"),
|
||||
boost::optional<std::string>("user"),
|
||||
boost::optional<std::string>("pass"),
|
||||
boost::optional<std::string>("111"));
|
||||
|
||||
CHECK(creator.id() == "host=host port=12345 dbname=dbname user=user connect_timeout=111");
|
||||
mapnik::parameters params;
|
||||
params["host"] = "H";
|
||||
params["port"] = "1234";
|
||||
params["dbname"] = "D";
|
||||
params["user"] = "U";
|
||||
params["password"] = "SECRET";
|
||||
params["connect_timeout"] = "5";
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -83,20 +83,15 @@ int main ( int argc, char** argv)
|
|||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
boost::optional<std::string> host;
|
||||
boost::optional<std::string> port ;
|
||||
boost::optional<std::string> dbname;
|
||||
boost::optional<std::string> user;
|
||||
boost::optional<std::string> password;
|
||||
boost::optional<std::string> connect_timeout("4");
|
||||
mapnik::parameters conn_params;
|
||||
conn_params["application_name"] = "pgsql2sqlite";
|
||||
for (auto k : {"host", "port", "dbname", "user", "password"})
|
||||
{
|
||||
if (!vm[k].empty())
|
||||
conn_params[k] = vm[k].as<std::string>();
|
||||
}
|
||||
|
||||
if (vm.count("host")) host = vm["host"].as<std::string>();
|
||||
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);
|
||||
ConnectionCreator<Connection> creator(conn_params);
|
||||
try
|
||||
{
|
||||
std::shared_ptr<Connection> conn(creator());
|
||||
|
@ -112,8 +107,8 @@ int main ( int argc, char** argv)
|
|||
catch (mapnik::datasource_exception & ex)
|
||||
{
|
||||
std::cerr << ex.what() << "\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
}
|
||||
catch(std::exception& e) {
|
||||
std::cerr << desc << "\n";
|
||||
|
|
Loading…
Reference in a new issue