2006-03-31 12:32:02 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* Copyright (C) 2006 Artem Pavlenko
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
2005-06-14 17:06:59 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2006-03-31 12:32:02 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
*****************************************************************************/
|
2005-06-14 17:06:59 +02:00
|
|
|
|
|
|
|
//$Id: connection.hpp 17 2005-03-08 23:58:43Z pavlenko $
|
|
|
|
|
|
|
|
#ifndef CONNECTION_HPP
|
|
|
|
#define CONNECTION_HPP
|
|
|
|
|
2007-09-25 20:47:12 +02:00
|
|
|
#include <mapnik/datasource.hpp>
|
|
|
|
|
2006-08-31 23:32:07 +02:00
|
|
|
extern "C"
|
|
|
|
{
|
2006-11-17 22:10:28 +01:00
|
|
|
#include "libpq-fe.h"
|
2006-08-31 23:32:07 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
|
|
|
#include "resultset.hpp"
|
|
|
|
|
|
|
|
class ResultSet;
|
|
|
|
class Connection
|
|
|
|
{
|
2007-02-06 15:27:21 +01:00
|
|
|
private:
|
|
|
|
PGconn *conn_;
|
2009-02-05 18:41:44 +01:00
|
|
|
int cursorId;
|
2009-10-14 06:21:46 +02:00
|
|
|
bool closed_;
|
2007-02-06 15:27:21 +01:00
|
|
|
public:
|
2007-06-12 10:59:54 +02:00
|
|
|
Connection(std::string const& connection_str)
|
2009-10-14 06:21:46 +02:00
|
|
|
:cursorId(0),
|
|
|
|
closed_(false)
|
2007-02-06 15:27:21 +01:00
|
|
|
{
|
2007-06-12 10:59:54 +02:00
|
|
|
conn_=PQconnectdb(connection_str.c_str());
|
2007-09-25 20:47:12 +02:00
|
|
|
if (PQstatus(conn_) != CONNECTION_OK)
|
2007-02-06 15:27:21 +01:00
|
|
|
{
|
2010-11-20 00:03:35 +01:00
|
|
|
std::ostringstream s("Postgis Plugin: PSQL error");
|
2007-09-25 20:47:12 +02:00
|
|
|
if (conn_ )
|
|
|
|
{
|
|
|
|
std::string msg = PQerrorMessage( conn_ );
|
|
|
|
if ( ! msg.empty() )
|
|
|
|
{
|
2010-11-20 00:03:35 +01:00
|
|
|
s << ":\n" << msg.substr( 0, msg.size() - 1 );
|
2007-09-25 20:47:12 +02:00
|
|
|
}
|
|
|
|
}
|
2010-11-20 00:03:35 +01:00
|
|
|
throw mapnik::datasource_exception( s.str() );
|
2007-02-06 15:27:21 +01:00
|
|
|
}
|
|
|
|
}
|
2007-06-12 10:59:54 +02:00
|
|
|
|
2007-02-06 15:27:21 +01:00
|
|
|
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;
|
|
|
|
}
|
2009-10-14 06:21:46 +02:00
|
|
|
|
2007-02-06 15:27:21 +01:00
|
|
|
boost::shared_ptr<ResultSet> executeQuery(const std::string& sql,int type=0) const
|
|
|
|
{
|
|
|
|
PGresult *result=0;
|
|
|
|
if (type==1)
|
|
|
|
{
|
2009-08-27 05:13:42 +02:00
|
|
|
result=PQexecParams(conn_,sql.c_str(),0,0,0,0,0,1);
|
2007-02-06 15:27:21 +01:00
|
|
|
}
|
2009-08-27 05:13:42 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
result=PQexec(conn_,sql.c_str());
|
|
|
|
}
|
|
|
|
if(!result || PQresultStatus(result) != PGRES_TUPLES_OK)
|
|
|
|
{
|
2010-11-20 00:03:35 +01:00
|
|
|
std::ostringstream s("Postgis Plugin: PSQL error");
|
2009-08-27 05:13:42 +02:00
|
|
|
if (conn_ )
|
|
|
|
{
|
|
|
|
std::string msg = PQerrorMessage( conn_ );
|
|
|
|
if ( ! msg.empty() )
|
|
|
|
{
|
2010-11-22 12:25:27 +01:00
|
|
|
s << ":\n" << msg.substr( 0, msg.size() - 1 );
|
2009-08-27 05:13:42 +02:00
|
|
|
}
|
2009-10-02 07:16:15 +02:00
|
|
|
|
2010-11-22 12:25:27 +01:00
|
|
|
s << "\nFull sql was: '" << sql << "'\n";
|
2011-04-12 00:11:14 +02:00
|
|
|
}
|
|
|
|
if (result)
|
|
|
|
PQclear(result);
|
2010-11-20 00:03:35 +01:00
|
|
|
throw mapnik::datasource_exception( s.str() );
|
2009-08-27 05:13:42 +02:00
|
|
|
}
|
|
|
|
|
2007-02-06 15:27:21 +01:00
|
|
|
return boost::shared_ptr<ResultSet>(new ResultSet(result));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string client_encoding() const
|
|
|
|
{
|
|
|
|
return PQparameterStatus(conn_,"client_encoding");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isOK() const
|
|
|
|
{
|
|
|
|
return (PQstatus(conn_)!=CONNECTION_BAD);
|
|
|
|
}
|
|
|
|
|
|
|
|
void close()
|
|
|
|
{
|
2009-10-14 06:21:46 +02:00
|
|
|
if (!closed_)
|
|
|
|
{
|
|
|
|
PQfinish(conn_);
|
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-11-19 00:42:33 +01:00
|
|
|
std::clog << "PostGIS: datasource closed, also closing connection - " << conn_ << std::endl;
|
2009-10-14 06:21:46 +02:00
|
|
|
#endif
|
|
|
|
closed_ = true;
|
|
|
|
}
|
2007-02-06 15:27:21 +01:00
|
|
|
}
|
|
|
|
|
2009-02-05 18:41:44 +01:00
|
|
|
std::string new_cursor_name()
|
|
|
|
{
|
|
|
|
std::ostringstream s;
|
|
|
|
s << "mapnik_" << (cursorId++);
|
|
|
|
return s.str();
|
|
|
|
}
|
|
|
|
|
2007-02-06 15:27:21 +01:00
|
|
|
~Connection()
|
|
|
|
{
|
2009-10-14 06:21:46 +02:00
|
|
|
if (!closed_)
|
|
|
|
{
|
|
|
|
PQfinish(conn_);
|
2006-11-27 22:13:15 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-11-19 00:42:33 +01:00
|
|
|
std::clog << "PostGIS: postgresql connection closed - " << conn_ << std::endl;
|
2009-10-14 06:21:46 +02:00
|
|
|
#endif
|
|
|
|
closed_ = true;
|
|
|
|
}
|
2007-02-06 15:27:21 +01:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif //CONNECTION_HPP
|