mirror of
https://github.com/mapnik/mapnik.git
synced 2025-12-08 20:13:09 +00:00
added port parameter support
This commit is contained in:
parent
d7b94f5d8c
commit
6c04c5f745
@ -27,7 +27,7 @@
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "libpq-fe.h"
|
||||
#include "libpq-fe.h"
|
||||
}
|
||||
|
||||
#include "resultset.hpp"
|
||||
@ -41,50 +41,58 @@ class Connection
|
||||
private:
|
||||
PGconn *conn_;
|
||||
public:
|
||||
Connection(const std::string& uri,const std::string& dbname,
|
||||
const std::string& username,const std::string& password)
|
||||
Connection(std::string const& uri,
|
||||
std::string const& port,
|
||||
std::string const& dbname,
|
||||
std::string const& username,
|
||||
std::string const& password)
|
||||
{
|
||||
std::string connStr="host="+uri+" dbname="+dbname+" user="+username+" password="+password;
|
||||
conn_=PQconnectdb(connStr.c_str());
|
||||
if (PQstatus(conn_) == CONNECTION_BAD)
|
||||
{
|
||||
std::clog << "connection to "<< connStr << " failed\n"
|
||||
<< PQerrorMessage(conn_)<< std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::clog <<"connected ok "<<std::endl;
|
||||
}
|
||||
std::string connStr="host="+uri;
|
||||
if (port.length()) connStr+=" port="+port;
|
||||
connStr+=" dbname="+dbname;
|
||||
connStr+=" user="+username;
|
||||
connStr+=" password="+password;
|
||||
|
||||
conn_=PQconnectdb(connStr.c_str());
|
||||
if (PQstatus(conn_) == CONNECTION_BAD)
|
||||
{
|
||||
std::clog << "connection to "<< connStr << " failed\n"
|
||||
<< PQerrorMessage(conn_)<< std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::clog <<"connected ok "<<std::endl;
|
||||
}
|
||||
}
|
||||
bool execute(const std::string& sql) const
|
||||
{
|
||||
PGresult *result=PQexec(conn_,sql.c_str());
|
||||
bool ok=(result && PQresultStatus(result)==PGRES_COMMAND_OK);
|
||||
PQclear(result);
|
||||
return ok;
|
||||
PGresult *result=PQexec(conn_,sql.c_str());
|
||||
bool ok=(result && PQresultStatus(result)==PGRES_COMMAND_OK);
|
||||
PQclear(result);
|
||||
return ok;
|
||||
}
|
||||
boost::shared_ptr<ResultSet> executeQuery(const std::string& sql,int type=0) const
|
||||
{
|
||||
PGresult *result=0;
|
||||
if (type==1)
|
||||
{
|
||||
result=PQexecParams(conn_,sql.c_str(),0,0,0,0,0,1);
|
||||
return boost::shared_ptr<ResultSet>(new ResultSet(result));
|
||||
}
|
||||
result=PQexec(conn_,sql.c_str());
|
||||
return boost::shared_ptr<ResultSet>(new ResultSet(result));
|
||||
PGresult *result=0;
|
||||
if (type==1)
|
||||
{
|
||||
result=PQexecParams(conn_,sql.c_str(),0,0,0,0,0,1);
|
||||
return boost::shared_ptr<ResultSet>(new ResultSet(result));
|
||||
}
|
||||
result=PQexec(conn_,sql.c_str());
|
||||
return boost::shared_ptr<ResultSet>(new ResultSet(result));
|
||||
}
|
||||
bool isOK() const
|
||||
{
|
||||
return (PQstatus(conn_)!=CONNECTION_BAD);
|
||||
return (PQstatus(conn_)!=CONNECTION_BAD);
|
||||
}
|
||||
void close()
|
||||
{
|
||||
PQfinish(conn_);
|
||||
PQfinish(conn_);
|
||||
}
|
||||
~Connection()
|
||||
{
|
||||
PQfinish(conn_);
|
||||
PQfinish(conn_);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -39,24 +39,29 @@ template <typename T>
|
||||
class ConnectionCreator
|
||||
{
|
||||
string url_;
|
||||
string port_;
|
||||
string dbname_;
|
||||
string user_;
|
||||
string pass_;
|
||||
public:
|
||||
ConnectionCreator(const string& url,const string& dbname,
|
||||
const string& user,const string& pass)
|
||||
: url_(url),
|
||||
dbname_(dbname),
|
||||
user_(user),
|
||||
pass_(pass) {}
|
||||
ConnectionCreator(string const& url,
|
||||
string const& port,
|
||||
string const& dbname,
|
||||
string const& user,
|
||||
string const& pass)
|
||||
: url_(url),
|
||||
port_(port),
|
||||
dbname_(dbname),
|
||||
user_(user),
|
||||
pass_(pass) {}
|
||||
|
||||
T* operator()() const
|
||||
{
|
||||
return new T(url_,dbname_,user_,pass_);
|
||||
return new T(url_,port_,dbname_,user_,pass_);
|
||||
}
|
||||
std::string id() const
|
||||
{
|
||||
return url_+":"+dbname_+":"+user_+":"+pass_;
|
||||
return url_ + ":" + dbname_;
|
||||
}
|
||||
};
|
||||
|
||||
@ -73,39 +78,40 @@ public:
|
||||
|
||||
bool registerPool(const ConnectionCreator<Connection>& creator,int initialSize,int maxSize)
|
||||
{
|
||||
mutex::scoped_lock lock(mutex_);
|
||||
if (pools_.find(creator.id())==pools_.end())
|
||||
{
|
||||
return pools_.insert(std::make_pair(creator.id(),
|
||||
boost::shared_ptr<PoolType>(new PoolType(creator,initialSize,maxSize)))).second;
|
||||
}
|
||||
mutex::scoped_lock lock(mutex_);
|
||||
if (pools_.find(creator.id())==pools_.end())
|
||||
{
|
||||
return pools_.insert(std::make_pair(creator.id(),
|
||||
boost::shared_ptr<PoolType>(new PoolType(creator,initialSize,maxSize)))).second;
|
||||
}
|
||||
|
||||
return false;
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
const boost::shared_ptr<PoolType>& getPool(const std::string& key)
|
||||
{
|
||||
mutex::scoped_lock lock(mutex_);
|
||||
ContType::const_iterator itr=pools_.find(key);
|
||||
if (itr!=pools_.end())
|
||||
{
|
||||
return itr->second;
|
||||
}
|
||||
static const boost::shared_ptr<PoolType> emptyPool;
|
||||
return emptyPool;
|
||||
mutex::scoped_lock lock(mutex_);
|
||||
ContType::const_iterator itr=pools_.find(key);
|
||||
if (itr!=pools_.end())
|
||||
{
|
||||
return itr->second;
|
||||
}
|
||||
static const boost::shared_ptr<PoolType> emptyPool;
|
||||
return emptyPool;
|
||||
}
|
||||
|
||||
const HolderType& get(const std::string& key)
|
||||
{
|
||||
mutex::scoped_lock lock(mutex_);
|
||||
ContType::const_iterator itr=pools_.find(key);
|
||||
if (itr!=pools_.end())
|
||||
{
|
||||
boost::shared_ptr<PoolType> pool=itr->second;
|
||||
return pool->borrowObject();
|
||||
}
|
||||
static const HolderType EmptyConn;
|
||||
return EmptyConn;
|
||||
mutex::scoped_lock lock(mutex_);
|
||||
ContType::const_iterator itr=pools_.find(key);
|
||||
if (itr!=pools_.end())
|
||||
{
|
||||
boost::shared_ptr<PoolType> pool=itr->second;
|
||||
return pool->borrowObject();
|
||||
}
|
||||
static const HolderType EmptyConn;
|
||||
return EmptyConn;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@ -43,20 +43,20 @@ using boost::lexical_cast;
|
||||
using boost::bad_lexical_cast;
|
||||
using boost::shared_ptr;
|
||||
|
||||
postgis_datasource::postgis_datasource(const parameters& params)
|
||||
postgis_datasource::postgis_datasource(parameters const& params)
|
||||
: datasource (params),
|
||||
table_(params.get("table")),
|
||||
type_(datasource::Vector),
|
||||
desc_(params.get("name")),
|
||||
desc_(params.get("type")),
|
||||
creator_(params.get("host"),
|
||||
params.get("port"),
|
||||
params.get("dbname"),
|
||||
params.get("user"),
|
||||
params.get("password"))
|
||||
|
||||
params.get("password"))
|
||||
{
|
||||
ConnectionManager *mgr=ConnectionManager::instance();
|
||||
mgr->registerPool(creator_,10,20);
|
||||
|
||||
|
||||
shared_ptr<Pool<Connection,ConnectionCreator> > pool=mgr->getPool(creator_.id());
|
||||
if (pool)
|
||||
{
|
||||
@ -64,9 +64,7 @@ postgis_datasource::postgis_datasource(const parameters& params)
|
||||
if (conn && conn->isOK())
|
||||
{
|
||||
PoolGuard<shared_ptr<Connection>,shared_ptr<Pool<Connection,ConnectionCreator> > > guard(conn,pool);
|
||||
|
||||
std::string table_name=table_from_sql(table_);
|
||||
|
||||
std::ostringstream s;
|
||||
s << "select f_geometry_column,srid,type from ";
|
||||
s << GEOMETRY_COLUMNS <<" where f_table_name='" << table_name<<"'";
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user