postgis: avoid empty error messages (closes #955) and more verbosity in general if postgres connection fails

This commit is contained in:
Dane Springmeyer 2011-11-16 11:27:48 -08:00
parent d1b53c4305
commit dddd6fa781

View file

@ -26,6 +26,10 @@
#include <mapnik/datasource.hpp>
#include <boost/make_shared.hpp>
// std
#include <sstream>
#include <iostream>
extern "C" {
#include "libpq-fe.h"
}
@ -46,14 +50,25 @@ public:
conn_ = PQconnectdb(connection_str.c_str());
if (PQstatus(conn_) != CONNECTION_OK)
{
std::ostringstream s("Postgis Plugin: PSQL error");
// note: empty ctor is intentional here
// as somehow constructor string can dissapear
std::ostringstream s;
s << "Postgis Plugin: ";
if (conn_ )
{
std::string msg = PQerrorMessage( conn_ );
if ( ! msg.empty() )
{
s << ":\n" << msg.substr( 0, msg.size() - 1 );
s << msg.substr( 0, msg.size() - 1 );
}
else
{
s << "unable to connect to postgres server";
}
}
else
{
s << "unable to connect to postgres server";
}
throw mapnik::datasource_exception( s.str() );
}
@ -62,7 +77,7 @@ public:
bool execute(const std::string& sql) const
{
PGresult *result = PQexec(conn_,sql.c_str());
bool ok=(result && PQresultStatus(result)==PGRES_COMMAND_OK);
bool ok=(result && (PQresultStatus(result) == PGRES_COMMAND_OK));
PQclear(result);
return ok;
}
@ -78,7 +93,7 @@ public:
{
result = PQexec(conn_,sql.c_str());
}
if(!result || PQresultStatus(result) != PGRES_TUPLES_OK)
if(!result || (PQresultStatus(result) != PGRES_TUPLES_OK))
{
std::ostringstream s("Postgis Plugin: PSQL error");
if (conn_ )