2006-03-31 12:32:02 +02:00
|
|
|
/*****************************************************************************
|
2011-11-14 04:37:50 +01:00
|
|
|
*
|
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
|
|
|
*
|
2021-01-05 15:39:07 +01:00
|
|
|
* Copyright (C) 2021 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
|
|
|
|
2012-04-08 02:20:56 +02:00
|
|
|
#ifndef POSTGIS_CONNECTION_HPP
|
|
|
|
#define POSTGIS_CONNECTION_HPP
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2012-04-08 02:20:56 +02:00
|
|
|
// mapnik
|
|
|
|
#include <mapnik/debug.hpp>
|
2007-09-25 20:47:12 +02:00
|
|
|
#include <mapnik/datasource.hpp>
|
2012-04-08 02:20:56 +02:00
|
|
|
#include <mapnik/timer.hpp>
|
|
|
|
|
2011-11-16 20:27:48 +01:00
|
|
|
// std
|
2013-09-25 07:57:01 +02:00
|
|
|
#include <memory>
|
2011-11-16 20:27:48 +01:00
|
|
|
#include <sstream>
|
|
|
|
#include <iostream>
|
|
|
|
|
2011-10-23 21:23:04 +02:00
|
|
|
extern "C" {
|
2011-11-14 04:37:50 +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 Connection
|
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
public:
|
2024-04-19 20:45:24 +02:00
|
|
|
Connection(std::string const& connection_str, std::optional<std::string> const& password)
|
2022-01-26 10:43:31 +01:00
|
|
|
: cursorId(0)
|
|
|
|
, closed_(false)
|
|
|
|
, pending_(false)
|
2011-11-14 04:37:50 +01:00
|
|
|
{
|
2012-11-28 03:37:22 +01:00
|
|
|
std::string connect_with_pass = connection_str;
|
|
|
|
if (password && !password->empty())
|
|
|
|
{
|
|
|
|
connect_with_pass += " password=" + *password;
|
|
|
|
}
|
|
|
|
conn_ = PQconnectdb(connect_with_pass.c_str());
|
2013-11-13 03:38:39 +01:00
|
|
|
MAPNIK_LOG_DEBUG(postgis) << "postgis_connection: postgresql connection create - " << this;
|
2011-11-14 04:37:50 +01:00
|
|
|
if (PQstatus(conn_) != CONNECTION_OK)
|
|
|
|
{
|
2012-11-27 13:48:22 +01:00
|
|
|
std::string err_msg = "Postgis Plugin: ";
|
|
|
|
err_msg += status();
|
2015-03-06 23:01:04 +01:00
|
|
|
err_msg += "Connection string: '";
|
2012-11-27 13:48:22 +01:00
|
|
|
err_msg += connection_str;
|
|
|
|
err_msg += "'\n";
|
2013-11-13 03:38:39 +01:00
|
|
|
MAPNIK_LOG_DEBUG(postgis) << "postgis_connection: creation failed, closing connection - " << this;
|
|
|
|
close();
|
2012-11-27 13:48:22 +01:00
|
|
|
throw mapnik::datasource_exception(err_msg);
|
2012-04-08 02:20:56 +02:00
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
PGresult* result =
|
|
|
|
PQexec(conn_, "SET DEFAULT_TRANSACTION_READ_ONLY = TRUE; SET CLIENT_MIN_MESSAGES = WARNING;");
|
2014-07-16 10:59:11 +02:00
|
|
|
bool ok = (result && (PQresultStatus(result) == PGRES_COMMAND_OK));
|
2022-01-26 10:43:31 +01:00
|
|
|
if (result)
|
|
|
|
PQclear(result);
|
|
|
|
if (!ok)
|
|
|
|
{
|
2014-07-16 10:59:11 +02:00
|
|
|
std::string err_msg = "Postgis Plugin: ";
|
|
|
|
err_msg += status();
|
2015-03-06 23:01:04 +01:00
|
|
|
err_msg += "Connection string: '";
|
2014-07-16 10:59:11 +02:00
|
|
|
err_msg += connection_str;
|
|
|
|
err_msg += "'\n";
|
2014-08-13 05:26:52 +02:00
|
|
|
close();
|
2014-07-16 10:59:11 +02:00
|
|
|
throw mapnik::datasource_exception(err_msg);
|
|
|
|
}
|
2012-04-08 02:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
~Connection()
|
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
if (!closed_)
|
2012-04-08 02:20:56 +02:00
|
|
|
{
|
|
|
|
PQfinish(conn_);
|
2013-11-13 03:38:39 +01:00
|
|
|
MAPNIK_LOG_DEBUG(postgis) << "postgis_connection: postgresql connection closed - " << this;
|
2012-04-08 02:20:56 +02:00
|
|
|
closed_ = true;
|
2011-11-14 04:37:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-11 11:10:41 +01:00
|
|
|
bool execute(std::string const& sql)
|
2011-11-14 04:37:50 +01:00
|
|
|
{
|
2012-04-08 02:20:56 +02:00
|
|
|
#ifdef MAPNIK_STATS
|
|
|
|
mapnik::progress_timer __stats__(std::clog, std::string("postgis_connection::execute ") + sql);
|
|
|
|
#endif
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
if (!executeAsyncQuery(sql))
|
|
|
|
return false;
|
|
|
|
PGresult* result = 0;
|
2014-03-11 11:10:41 +01:00
|
|
|
// fetch multiple times until NULL is returned,
|
|
|
|
// to handle multi-statement queries
|
2022-01-26 10:43:31 +01:00
|
|
|
while (PGresult* tmp = getResult())
|
|
|
|
{
|
|
|
|
if (result)
|
|
|
|
PQclear(result);
|
|
|
|
result = tmp;
|
2014-03-11 11:10:41 +01:00
|
|
|
}
|
2012-04-08 02:20:56 +02:00
|
|
|
bool ok = (result && (PQresultStatus(result) == PGRES_COMMAND_OK));
|
2022-01-26 10:43:31 +01:00
|
|
|
if (result)
|
|
|
|
PQclear(result);
|
2011-11-14 04:37:50 +01:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2014-04-24 23:50:55 +02:00
|
|
|
std::shared_ptr<ResultSet> executeQuery(std::string const& sql, int type = 0)
|
2011-11-14 04:37:50 +01:00
|
|
|
{
|
2012-04-08 02:20:56 +02:00
|
|
|
#ifdef MAPNIK_STATS
|
|
|
|
mapnik::progress_timer __stats__(std::clog, std::string("postgis_connection::execute_query ") + sql);
|
|
|
|
#endif
|
|
|
|
PGresult* result = 0;
|
2022-01-26 10:43:31 +01:00
|
|
|
if (executeAsyncQuery(sql, type))
|
|
|
|
{
|
|
|
|
// fetch multiple times until NULL is returned,
|
|
|
|
// to handle multi-statement queries
|
|
|
|
while (PGresult* tmp = getResult())
|
|
|
|
{
|
|
|
|
if (result)
|
|
|
|
PQclear(result);
|
|
|
|
result = tmp;
|
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
}
|
2012-04-08 02:20:56 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
if (!result || (PQresultStatus(result) != PGRES_TUPLES_OK))
|
2011-11-14 04:37:50 +01:00
|
|
|
{
|
2013-09-23 23:57:23 +02:00
|
|
|
std::string err_msg = "Postgis Plugin: ";
|
|
|
|
err_msg += status();
|
2015-03-06 23:01:04 +01:00
|
|
|
err_msg += "in executeQuery Full sql was: '";
|
2012-11-27 13:48:22 +01:00
|
|
|
err_msg += sql;
|
|
|
|
err_msg += "'\n";
|
2022-01-26 10:43:31 +01:00
|
|
|
if (result)
|
|
|
|
PQclear(result);
|
2012-11-27 13:48:22 +01:00
|
|
|
throw mapnik::datasource_exception(err_msg);
|
2011-11-14 04:37:50 +01:00
|
|
|
}
|
|
|
|
|
2013-09-20 15:00:11 +02:00
|
|
|
return std::make_shared<ResultSet>(result);
|
2011-11-14 04:37:50 +01:00
|
|
|
}
|
2009-08-27 05:13:42 +02:00
|
|
|
|
2012-11-27 13:48:22 +01:00
|
|
|
std::string status() const
|
|
|
|
{
|
|
|
|
std::string status;
|
|
|
|
if (conn_)
|
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
char* err_msg = PQerrorMessage(conn_);
|
2015-03-06 23:01:04 +01:00
|
|
|
if (err_msg == nullptr)
|
|
|
|
{
|
|
|
|
status = "Bad connection\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = std::string(err_msg);
|
|
|
|
}
|
2012-11-27 13:48:22 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-06 23:01:04 +01:00
|
|
|
status = "Uninitialized connection\n";
|
2012-11-27 13:48:22 +01:00
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2013-05-16 17:08:42 +02:00
|
|
|
bool executeAsyncQuery(std::string const& sql, int type = 0)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
if (type == 1)
|
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
result = PQsendQueryParams(conn_, sql.c_str(), 0, 0, 0, 0, 0, 1);
|
2013-05-16 17:08:42 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = PQsendQuery(conn_, sql.c_str());
|
|
|
|
}
|
|
|
|
if (result != 1)
|
|
|
|
{
|
2013-09-23 23:57:23 +02:00
|
|
|
std::string err_msg = "Postgis Plugin: ";
|
|
|
|
err_msg += status();
|
2015-03-06 23:01:04 +01:00
|
|
|
err_msg += "in executeAsyncQuery Full sql was: '";
|
2013-05-16 17:08:42 +02:00
|
|
|
err_msg += sql;
|
|
|
|
err_msg += "'\n";
|
|
|
|
clearAsyncResult(PQgetResult(conn_));
|
|
|
|
close();
|
|
|
|
throw mapnik::datasource_exception(err_msg);
|
|
|
|
}
|
2013-11-13 03:38:39 +01:00
|
|
|
pending_ = true;
|
2013-05-16 17:08:42 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-03-11 11:10:41 +01:00
|
|
|
PGresult* getResult()
|
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
PGresult* result = PQgetResult(conn_);
|
2014-03-11 11:10:41 +01:00
|
|
|
return result;
|
|
|
|
}
|
2013-05-16 17:08:42 +02:00
|
|
|
|
2013-09-25 07:57:01 +02:00
|
|
|
std::shared_ptr<ResultSet> getNextAsyncResult()
|
2013-05-16 17:08:42 +02:00
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
PGresult* result = getResult();
|
|
|
|
if (result && (PQresultStatus(result) != PGRES_TUPLES_OK))
|
2013-09-23 23:57:23 +02:00
|
|
|
{
|
|
|
|
std::string err_msg = "Postgis Plugin: ";
|
|
|
|
err_msg += status();
|
2015-03-06 23:01:04 +01:00
|
|
|
err_msg += "in getNextAsyncResult";
|
2013-09-23 23:57:23 +02:00
|
|
|
clearAsyncResult(result);
|
|
|
|
// We need to guarde against losing the connection
|
|
|
|
// (i.e db restart) so here we invalidate the full connection
|
|
|
|
close();
|
|
|
|
throw mapnik::datasource_exception(err_msg);
|
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
return std::make_shared<ResultSet>(result);
|
2013-05-16 17:08:42 +02:00
|
|
|
}
|
|
|
|
|
2013-09-25 07:57:01 +02:00
|
|
|
std::shared_ptr<ResultSet> getAsyncResult()
|
2013-05-16 17:08:42 +02:00
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
PGresult* result = getResult();
|
|
|
|
if (!result || (PQresultStatus(result) != PGRES_TUPLES_OK))
|
2013-09-23 23:57:23 +02:00
|
|
|
{
|
|
|
|
std::string err_msg = "Postgis Plugin: ";
|
|
|
|
err_msg += status();
|
2015-03-06 23:01:04 +01:00
|
|
|
err_msg += "in getAsyncResult";
|
2013-09-23 23:57:23 +02:00
|
|
|
clearAsyncResult(result);
|
|
|
|
// We need to be guarded against losing the connection
|
|
|
|
// (i.e db restart), we invalidate the full connection
|
|
|
|
close();
|
|
|
|
throw mapnik::datasource_exception(err_msg);
|
|
|
|
}
|
2013-09-25 07:57:01 +02:00
|
|
|
return std::make_shared<ResultSet>(result);
|
2013-05-16 17:08:42 +02:00
|
|
|
}
|
|
|
|
|
2022-08-11 22:54:50 +02:00
|
|
|
std::string client_encoding() const
|
|
|
|
{
|
|
|
|
return PQparameterStatus(conn_, "client_encoding");
|
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2022-08-11 22:54:50 +02:00
|
|
|
bool isOK() const
|
|
|
|
{
|
|
|
|
return (!closed_) && (PQstatus(conn_) != CONNECTION_BAD);
|
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2022-08-11 22:54:50 +02:00
|
|
|
bool isPending() const
|
|
|
|
{
|
|
|
|
return pending_;
|
|
|
|
}
|
2013-11-13 03:38:39 +01:00
|
|
|
|
2011-11-14 04:37:50 +01:00
|
|
|
void close()
|
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
if (!closed_)
|
2011-11-14 04:37:50 +01:00
|
|
|
{
|
|
|
|
PQfinish(conn_);
|
2013-11-13 03:38:39 +01:00
|
|
|
MAPNIK_LOG_DEBUG(postgis) << "postgis_connection: closing connection (close)- " << this;
|
2011-11-14 04:37:50 +01:00
|
|
|
closed_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string new_cursor_name()
|
|
|
|
{
|
|
|
|
std::ostringstream s;
|
|
|
|
s << "mapnik_" << (cursorId++);
|
|
|
|
return s.str();
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
private:
|
|
|
|
PGconn* conn_;
|
2012-04-08 02:20:56 +02:00
|
|
|
int cursorId;
|
|
|
|
bool closed_;
|
2013-11-13 03:38:39 +01:00
|
|
|
bool pending_;
|
2013-05-16 17:08:42 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
void clearAsyncResult(PGresult* result)
|
2013-05-16 17:08:42 +02:00
|
|
|
{
|
|
|
|
// Clear all pending results
|
2022-01-26 10:43:31 +01:00
|
|
|
while (result)
|
2013-05-16 17:08:42 +02:00
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
PQclear(result);
|
|
|
|
result = PQgetResult(conn_);
|
2013-05-16 17:08:42 +02:00
|
|
|
}
|
2013-11-13 03:38:39 +01:00
|
|
|
pending_ = false;
|
2013-05-16 17:08:42 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
};
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
#endif // CONNECTION_HPP
|