2006-03-31 10:32:02 +00:00
|
|
|
/*****************************************************************************
|
2011-11-14 03:37:50 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
2005-06-14 15:06:59 +00:00
|
|
|
*
|
2011-10-23 19:23:04 +00:00
|
|
|
* Copyright (C) 2011 Artem Pavlenko
|
2005-06-14 15:06:59 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00: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 15:06:59 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2006-03-31 10:32:02 +00: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 15:06:59 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00:00
|
|
|
*****************************************************************************/
|
2005-06-14 15:06:59 +00:00
|
|
|
|
2012-04-08 00:20:56 +00:00
|
|
|
#ifndef POSTGIS_CONNECTION_HPP
|
|
|
|
#define POSTGIS_CONNECTION_HPP
|
2005-06-14 15:06:59 +00:00
|
|
|
|
2012-04-08 00:20:56 +00:00
|
|
|
// mapnik
|
|
|
|
#include <mapnik/debug.hpp>
|
2007-09-25 18:47:12 +00:00
|
|
|
#include <mapnik/datasource.hpp>
|
2012-04-08 00:20:56 +00:00
|
|
|
#include <mapnik/timer.hpp>
|
|
|
|
|
|
|
|
// boost
|
2011-10-17 23:41:12 +00:00
|
|
|
#include <boost/make_shared.hpp>
|
2007-09-25 18:47:12 +00:00
|
|
|
|
2011-11-16 19:27:48 +00:00
|
|
|
// std
|
|
|
|
#include <sstream>
|
|
|
|
#include <iostream>
|
|
|
|
|
2011-10-23 19:23:04 +00:00
|
|
|
extern "C" {
|
2011-11-14 03:37:50 +00:00
|
|
|
#include "libpq-fe.h"
|
2006-08-31 21:32:07 +00:00
|
|
|
}
|
2005-06-14 15:06:59 +00:00
|
|
|
|
|
|
|
#include "resultset.hpp"
|
|
|
|
|
|
|
|
class Connection
|
|
|
|
{
|
2011-11-14 03:37:50 +00:00
|
|
|
public:
|
|
|
|
Connection(std::string const& connection_str)
|
2012-04-08 00:20:56 +00:00
|
|
|
: cursorId(0),
|
|
|
|
closed_(false)
|
2011-11-14 03:37:50 +00:00
|
|
|
{
|
2011-11-16 19:27:48 +00:00
|
|
|
conn_ = PQconnectdb(connection_str.c_str());
|
2011-11-14 03:37:50 +00:00
|
|
|
if (PQstatus(conn_) != CONNECTION_OK)
|
|
|
|
{
|
2011-11-16 19:27:48 +00:00
|
|
|
std::ostringstream s;
|
|
|
|
s << "Postgis Plugin: ";
|
2011-11-14 03:37:50 +00:00
|
|
|
if (conn_ )
|
|
|
|
{
|
2012-04-08 00:20:56 +00:00
|
|
|
std::string msg = PQerrorMessage(conn_);
|
|
|
|
if (! msg.empty())
|
2011-11-14 03:37:50 +00:00
|
|
|
{
|
2012-04-08 00:20:56 +00:00
|
|
|
s << msg.substr(0, msg.size() - 1);
|
2011-11-16 19:27:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s << "unable to connect to postgres server";
|
2011-11-14 03:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-16 19:27:48 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
s << "unable to connect to postgres server";
|
|
|
|
}
|
2012-01-24 12:36:33 +00:00
|
|
|
s << "\n" << connection_str;
|
2012-04-08 00:20:56 +00:00
|
|
|
|
|
|
|
throw mapnik::datasource_exception(s.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~Connection()
|
|
|
|
{
|
|
|
|
if (! closed_)
|
|
|
|
{
|
|
|
|
PQfinish(conn_);
|
|
|
|
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_DEBUG(postgis) << "postgis_connection: postgresql connection closed - " << conn_;
|
2012-04-09 10:05:49 +00:00
|
|
|
|
2012-04-08 00:20:56 +00:00
|
|
|
closed_ = true;
|
2011-11-14 03:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-03 17:27:48 +00:00
|
|
|
bool execute(std::string const& sql) const
|
2011-11-14 03:37:50 +00:00
|
|
|
{
|
2012-04-08 00:20:56 +00:00
|
|
|
#ifdef MAPNIK_STATS
|
|
|
|
mapnik::progress_timer __stats__(std::clog, std::string("postgis_connection::execute ") + sql);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
PGresult *result = PQexec(conn_, sql.c_str());
|
|
|
|
bool ok = (result && (PQresultStatus(result) == PGRES_COMMAND_OK));
|
2011-11-14 03:37:50 +00:00
|
|
|
PQclear(result);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2012-09-03 17:27:48 +00:00
|
|
|
boost::shared_ptr<ResultSet> executeQuery(std::string const& sql, int type = 0) const
|
2011-11-14 03:37:50 +00:00
|
|
|
{
|
2012-04-08 00:20:56 +00:00
|
|
|
#ifdef MAPNIK_STATS
|
|
|
|
mapnik::progress_timer __stats__(std::clog, std::string("postgis_connection::execute_query ") + sql);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
PGresult* result = 0;
|
|
|
|
if (type == 1)
|
2011-11-14 03:37:50 +00:00
|
|
|
{
|
2012-04-08 00:20:56 +00:00
|
|
|
result = PQexecParams(conn_,sql.c_str(), 0, 0, 0, 0, 0, 1);
|
2011-11-14 03:37:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-08 00:20:56 +00:00
|
|
|
result = PQexec(conn_, sql.c_str());
|
2011-11-14 03:37:50 +00:00
|
|
|
}
|
2012-04-08 00:20:56 +00:00
|
|
|
|
|
|
|
if (! result || (PQresultStatus(result) != PGRES_TUPLES_OK))
|
2011-11-14 03:37:50 +00:00
|
|
|
{
|
2011-11-17 19:36:16 +00:00
|
|
|
std::ostringstream s;
|
|
|
|
s << "Postgis Plugin: PSQL error";
|
2012-04-08 00:20:56 +00:00
|
|
|
if (conn_)
|
2011-11-14 03:37:50 +00:00
|
|
|
{
|
2012-04-08 00:20:56 +00:00
|
|
|
std::string msg = PQerrorMessage(conn_);
|
|
|
|
if (! msg.empty())
|
2011-11-14 03:37:50 +00:00
|
|
|
{
|
2012-04-08 00:20:56 +00:00
|
|
|
s << ":\n" << msg.substr(0, msg.size() - 1);
|
2011-11-14 03:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s << "\nFull sql was: '" << sql << "'\n";
|
|
|
|
}
|
2011-11-17 19:36:16 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
s << "unable to connect to database";
|
|
|
|
}
|
2012-04-08 00:20:56 +00:00
|
|
|
|
2011-11-14 03:37:50 +00:00
|
|
|
if (result)
|
2012-04-08 00:20:56 +00:00
|
|
|
{
|
2011-11-14 03:37:50 +00:00
|
|
|
PQclear(result);
|
2012-04-08 00:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw mapnik::datasource_exception(s.str());
|
2011-11-14 03:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return boost::make_shared<ResultSet>(result);
|
|
|
|
}
|
2009-08-27 03:13:42 +00:00
|
|
|
|
2011-11-14 03:37:50 +00:00
|
|
|
std::string client_encoding() const
|
|
|
|
{
|
2012-04-08 00:20:56 +00:00
|
|
|
return PQparameterStatus(conn_, "client_encoding");
|
2011-11-14 03:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isOK() const
|
|
|
|
{
|
2011-11-16 19:27:48 +00:00
|
|
|
return (PQstatus(conn_) != CONNECTION_BAD);
|
2011-11-14 03:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void close()
|
|
|
|
{
|
2012-04-08 00:20:56 +00:00
|
|
|
if (! closed_)
|
2011-11-14 03:37:50 +00:00
|
|
|
{
|
|
|
|
PQfinish(conn_);
|
2012-04-08 00:20:56 +00:00
|
|
|
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_DEBUG(postgis) << "postgis_connection: datasource closed, also closing connection - " << conn_;
|
2012-04-09 10:05:49 +00:00
|
|
|
|
2011-11-14 03:37:50 +00:00
|
|
|
closed_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string new_cursor_name()
|
|
|
|
{
|
|
|
|
std::ostringstream s;
|
|
|
|
s << "mapnik_" << (cursorId++);
|
|
|
|
return s.str();
|
|
|
|
}
|
|
|
|
|
2012-04-08 00:20:56 +00:00
|
|
|
private:
|
|
|
|
PGconn *conn_;
|
|
|
|
int cursorId;
|
|
|
|
bool closed_;
|
2005-06-14 15:06:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif //CONNECTION_HPP
|