mapnik/plugins/input/postgis/connection.hpp

281 lines
8.2 KiB
C++
Raw Normal View History

2006-03-31 12:32:02 +02: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>
#include <mapnik/datasource.hpp>
2012-04-08 02:20:56 +02:00
#include <mapnik/timer.hpp>
// std
2013-09-25 07:57:01 +02:00
#include <memory>
#include <sstream>
#include <iostream>
2011-10-23 21:23:04 +02:00
extern "C" {
#include "libpq-fe.h"
}
2005-06-14 17:06:59 +02:00
#include "resultset.hpp"
class Connection
{
public:
Connection(std::string const& connection_str, std::optional<std::string> const& password)
: cursorId(0)
, closed_(false)
, pending_(false)
{
std::string connect_with_pass = connection_str;
if (password && !password->empty())
{
connect_with_pass += " password=" + *password;
}
conn_ = PQconnectdb(connect_with_pass.c_str());
MAPNIK_LOG_DEBUG(postgis) << "postgis_connection: postgresql connection create - " << this;
if (PQstatus(conn_) != CONNECTION_OK)
{
std::string err_msg = "Postgis Plugin: ";
err_msg += status();
err_msg += "Connection string: '";
err_msg += connection_str;
err_msg += "'\n";
MAPNIK_LOG_DEBUG(postgis) << "postgis_connection: creation failed, closing connection - " << this;
close();
throw mapnik::datasource_exception(err_msg);
2012-04-08 02:20:56 +02:00
}
PGresult* result =
PQexec(conn_, "SET DEFAULT_TRANSACTION_READ_ONLY = TRUE; SET CLIENT_MIN_MESSAGES = WARNING;");
bool ok = (result && (PQresultStatus(result) == PGRES_COMMAND_OK));
if (result)
PQclear(result);
if (!ok)
{
std::string err_msg = "Postgis Plugin: ";
err_msg += status();
err_msg += "Connection string: '";
err_msg += connection_str;
err_msg += "'\n";
close();
throw mapnik::datasource_exception(err_msg);
}
2012-04-08 02:20:56 +02:00
}
~Connection()
{
if (!closed_)
2012-04-08 02:20:56 +02:00
{
PQfinish(conn_);
MAPNIK_LOG_DEBUG(postgis) << "postgis_connection: postgresql connection closed - " << this;
2012-04-08 02:20:56 +02:00
closed_ = true;
}
}
bool execute(std::string const& sql)
{
2012-04-08 02:20:56 +02:00
#ifdef MAPNIK_STATS
mapnik::progress_timer __stats__(std::clog, std::string("postgis_connection::execute ") + sql);
#endif
if (!executeAsyncQuery(sql))
return false;
PGresult* result = 0;
// fetch multiple times until NULL is returned,
// to handle multi-statement queries
while (PGresult* tmp = getResult())
{
if (result)
PQclear(result);
result = tmp;
}
2012-04-08 02:20:56 +02:00
bool ok = (result && (PQresultStatus(result) == PGRES_COMMAND_OK));
if (result)
PQclear(result);
return ok;
}
Merge branch '2.3.x' of github.com:mapnik/mapnik Conflicts: .travis.yml CHANGELOG.md bindings/python/build.py include/mapnik/feature_style_processor_context.hpp include/mapnik/feature_style_processor_impl.hpp include/mapnik/json/feature_collection_grammar.hpp include/mapnik/json/feature_collection_parser.hpp include/mapnik/json/feature_generator_grammar.hpp include/mapnik/json/feature_parser.hpp include/mapnik/json/geojson_generator.hpp include/mapnik/json/geometry_generator_grammar.hpp include/mapnik/json/geometry_parser.hpp plugins/input/gdal/gdal_featureset.cpp plugins/input/geojson/geojson_datasource.cpp plugins/input/occi/occi_featureset.cpp plugins/input/osm/osm_featureset.cpp plugins/input/postgis/build.py plugins/input/postgis/connection.hpp src/agg/agg_renderer.cpp src/build.py src/cairo_context.cpp src/datasource_cache.cpp src/grid/process_line_symbolizer.cpp src/grid/process_polygon_pattern_symbolizer.cpp src/grid/process_polygon_symbolizer.cpp src/grid/process_text_symbolizer.cpp src/json/feature_grammar.cpp tests/cpp_tests/fontset_runtime_test.cpp tests/visual_tests/images/collision-600-400-1.0-agg-reference.png tests/visual_tests/images/image-filters-multi-blur-512-512-1.0-agg-reference.png tests/visual_tests/images/image-filters-multi-blur-512-512-2.0-agg-reference.png tests/visual_tests/images/image-filters-multi-blur-inflate-512-512-1.0-agg-reference.png tests/visual_tests/images/image-filters-multi-blur-inflate-512-512-2.0-agg-reference.png tests/visual_tests/images/lines-1-400-400-2.0-agg-reference.png tests/visual_tests/images/lines-1-600-600-1.0-agg-reference.png tests/visual_tests/images/lines-1-600-600-2.0-agg-reference.png tests/visual_tests/images/lines-2-200-200-1.0-agg-reference.png tests/visual_tests/images/lines-2-400-400-1.0-cairo-reference.png tests/visual_tests/images/lines-2-400-400-2.0-agg-reference.png tests/visual_tests/images/lines-2-600-600-2.0-agg-reference.png tests/visual_tests/images/lines-2-800-800-2.0-agg-reference.png tests/visual_tests/images/lines-3-400-400-2.0-agg-reference.png tests/visual_tests/images/lines-3-600-600-1.0-agg-reference.png tests/visual_tests/images/lines-3-600-600-2.0-agg-reference.png tests/visual_tests/images/lines-5-200-200-1.0-agg-reference.png tests/visual_tests/images/lines-5-200-200-2.0-agg-reference.png tests/visual_tests/images/lines-5-400-400-1.0-agg-reference.png tests/visual_tests/images/lines-5-400-400-2.0-agg-reference.png tests/visual_tests/images/lines-5-600-600-1.0-agg-reference.png tests/visual_tests/images/lines-5-600-600-2.0-agg-reference.png tests/visual_tests/images/lines-5-800-800-1.0-agg-reference.png tests/visual_tests/images/lines-5-800-800-2.0-agg-reference.png tests/visual_tests/images/lines-6-200-200-1.0-agg-reference.png tests/visual_tests/images/lines-6-200-200-2.0-agg-reference.png tests/visual_tests/images/lines-6-400-400-1.0-agg-reference.png tests/visual_tests/images/lines-6-600-600-1.0-agg-reference.png tests/visual_tests/images/lines-6-600-600-2.0-agg-reference.png tests/visual_tests/images/lines-6-800-800-1.0-agg-reference.png tests/visual_tests/images/lines-6-800-800-2.0-agg-reference.png tests/visual_tests/images/lines-shield-600-600-2.0-agg-reference.png tests/visual_tests/images/lines-shield-600-600-2.0-cairo-reference.png tests/visual_tests/images/shield-on-polygon-600-400-1.0-agg-reference.png tests/visual_tests/images/shield-on-polygon-600-400-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-1-490-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-1-495-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-1-495-100-2.0-cairo-reference.png tests/visual_tests/images/shieldsymbolizer-1-497-100-1.0-cairo-reference.png tests/visual_tests/images/shieldsymbolizer-1-497-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-1-497-100-2.0-cairo-reference.png tests/visual_tests/images/shieldsymbolizer-1-498-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-1-499-100-1.0-cairo-reference.png tests/visual_tests/images/shieldsymbolizer-1-499-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-1-499-100-2.0-cairo-reference.png tests/visual_tests/images/shieldsymbolizer-1-500-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-1-501-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-1-502-100-1.0-cairo-reference.png tests/visual_tests/images/shieldsymbolizer-1-502-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-1-505-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-1-505-100-2.0-cairo-reference.png tests/visual_tests/images/shieldsymbolizer-1-510-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-3-490-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-3-495-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-3-497-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-3-498-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-3-499-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-3-500-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-3-501-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-3-502-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-3-505-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-3-510-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-4-490-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-4-495-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-4-497-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-4-498-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-4-499-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-4-500-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-4-501-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-4-502-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-4-505-100-2.0-agg-reference.png tests/visual_tests/images/shieldsymbolizer-4-510-100-2.0-agg-reference.png tests/visual_tests/test.py
2014-04-24 23:50:55 +02:00
std::shared_ptr<ResultSet> executeQuery(std::string const& sql, int type = 0)
{
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;
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;
}
}
2012-04-08 02:20:56 +02:00
if (!result || (PQresultStatus(result) != PGRES_TUPLES_OK))
{
2013-09-23 23:57:23 +02:00
std::string err_msg = "Postgis Plugin: ";
err_msg += status();
err_msg += "in executeQuery Full sql was: '";
err_msg += sql;
err_msg += "'\n";
if (result)
PQclear(result);
throw mapnik::datasource_exception(err_msg);
}
return std::make_shared<ResultSet>(result);
}
std::string status() const
{
std::string status;
if (conn_)
{
char* err_msg = PQerrorMessage(conn_);
if (err_msg == nullptr)
{
status = "Bad connection\n";
}
else
{
status = std::string(err_msg);
}
}
else
{
status = "Uninitialized connection\n";
}
return status;
}
bool executeAsyncQuery(std::string const& sql, int type = 0)
{
int result = 0;
if (type == 1)
{
result = PQsendQueryParams(conn_, sql.c_str(), 0, 0, 0, 0, 0, 1);
}
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();
err_msg += "in executeAsyncQuery Full sql was: '";
err_msg += sql;
err_msg += "'\n";
clearAsyncResult(PQgetResult(conn_));
close();
throw mapnik::datasource_exception(err_msg);
}
pending_ = true;
return result;
}
PGresult* getResult()
{
PGresult* result = PQgetResult(conn_);
return result;
}
2013-09-25 07:57:01 +02:00
std::shared_ptr<ResultSet> getNextAsyncResult()
{
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();
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);
}
return std::make_shared<ResultSet>(result);
}
2013-09-25 07:57:01 +02:00
std::shared_ptr<ResultSet> getAsyncResult()
{
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();
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);
}
2024-04-19 20:56:21 +02:00
std::string client_encoding() const { return PQparameterStatus(conn_, "client_encoding"); }
2024-04-19 20:56:21 +02:00
bool isOK() const { return (!closed_) && (PQstatus(conn_) != CONNECTION_BAD); }
2024-04-19 20:56:21 +02:00
bool isPending() const { return pending_; }
void close()
{
if (!closed_)
{
PQfinish(conn_);
MAPNIK_LOG_DEBUG(postgis) << "postgis_connection: closing connection (close)- " << this;
closed_ = true;
}
}
std::string new_cursor_name()
{
std::ostringstream s;
s << "mapnik_" << (cursorId++);
return s.str();
}
private:
PGconn* conn_;
2012-04-08 02:20:56 +02:00
int cursorId;
bool closed_;
bool pending_;
void clearAsyncResult(PGresult* result)
{
// Clear all pending results
while (result)
{
PQclear(result);
result = PQgetResult(conn_);
}
pending_ = false;
}
2005-06-14 17:06:59 +02:00
};
#endif // CONNECTION_HPP