postgis/Connection.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/postgis/Connection.cpp
22 
23  \brief A class that implements a connection to a PostgreSQL database.
24 */
25 
26 // TerraLib
27 #include "../core/translator/Translator.h"
28 #include "Connection.h"
29 #include "Exception.h"
30 
31 // STL
32 #include <cassert>
33 #include <string>
34 
35 // libpq
36 #include <libpq-fe.h>
37 
38 // Boost
39 #include <boost/format.hpp>
40 
41 /*! \brief This will just close the PostgreSQL notice processor. */
42 static void PostGISNoticeProcessor(void* /*arg*/, const char* /*pszMessage*/);
43 
44 PGresult* te::pgis::Connection::query(const std::string& query)
45 {
46  PGresult* result = PQexecParams(m_pgconn, query.c_str(), 0, nullptr, nullptr, nullptr, nullptr, 1);
47 
48  if(PQresultStatus(result) != PGRES_TUPLES_OK)
49  {
50  boost::format errmsg(TE_TR("Could not retrieve the dataset due to the following error: %1%."));
51  errmsg = errmsg % PQerrorMessage(m_pgconn);
52 
53  PQclear(result);
54 
55  throw Exception(errmsg.str());
56  }
57 
58  return result;
59 }
60 
61 void te::pgis::Connection::execute(const std::string& command)
62 {
63  PGresult* result = PQexec(m_pgconn, command.c_str());
64 
65  if((PQresultStatus(result) != PGRES_COMMAND_OK) &&
66  (PQresultStatus(result) != PGRES_TUPLES_OK))
67  {
68  boost::format errmsg(TE_TR("Could not execute the sql statement due to the following error: %1%."));
69 
70  errmsg = errmsg % PQerrorMessage(m_pgconn);
71 
72  std::string msg = errmsg.str();
73 
74  std::size_t idx = msg.find("invalid SRID");
75 
76  if(idx != std::string::npos)
77  msg+= " PostGIS doens't recognize this projection. "
78  "Please, reproject your data to a correct one. "
79  "Or save your result as Vector File, and afterwards use Exchange to put the result "
80  "in PGIS changing the USER SRID to a valid one.";
81 
82  PQclear(result);
83 
84  throw Exception(msg);
85  }
86 
87  PQclear(result);
88 }
89 
91 {
92  if(m_pgconn)
93  PQfinish(m_pgconn);
94 }
95 
96 te::pgis::Connection::Connection(ConnectionPool* pool, const std::string& conninfo, const std::string& cencoding, bool inuse) :
97  m_id(0),
98  m_pool(pool),
99  m_pgconn(nullptr),
100  m_inuse(inuse),
101  m_lastuse(boost::posix_time::second_clock::local_time())
102 {
103  static int newId = 0;
104  m_id = ++newId;
105 
106  // it tries to create the connection object
107  m_pgconn = PQconnectdb(conninfo.c_str());
108 
109  ConnStatusType status = PQstatus(m_pgconn);
110 
111  if(status != CONNECTION_OK)
112  {
113  boost::format errmsg(TE_TR("It was not possible to create a connection to the given data source due to the following error: %1%."));
114 
115  errmsg = errmsg % PQerrorMessage(m_pgconn);
116 
117  PQfinish(m_pgconn);
118 
119  m_pgconn = nullptr;
120 
121  throw Exception(errmsg.str());
122  }
123 
124  PQsetNoticeProcessor(m_pgconn, PostGISNoticeProcessor, this);
125 
126  if(cencoding.empty())
127  return;
128 
129  if(PQsetClientEncoding(m_pgconn, cencoding.c_str()) == -1)
130  {
131  boost::format errmsg(TE_TR("It was not possible to set the client encoding for the PostGIS data source due to the following error: %1%."));
132 
133  errmsg = errmsg % PQerrorMessage(m_pgconn);
134 
135  PQfinish(m_pgconn);
136 
137  m_pgconn = nullptr;
138 
139  throw Exception(errmsg.str());
140  }
141 }
142 
143 static void PostGISNoticeProcessor(void* /*arg*/, const char* /*pszMessage*/)
144 {
145 }
Base exception class for plugin module.
Connection(ConnectionPool *pool, const std::string &conninfo, const std::string &cencoding, bool inuse=false)
Constructor.
struct pg_result PGresult
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
void execute(const std::string &command)
It executes the given SQL command and throws away the result.
An exception class for the PostGIS driver.
boost::posix_time::ptime m_lastuse
It marks the last time this connection was used.
PGconn * m_pgconn
The PostgreSQL real connection handle.
A class that implements a connection to a PostgreSQL database.
static void PostGISNoticeProcessor(void *, const char *)
This will just close the PostgreSQL notice processor.
int m_id
The connection Id.
This class implements a connection pool for the PostGIS driver.
PGresult * query(const std::string &query)
It queries the database.
ConnectionPool * m_pool
The connection pool associated to the connection.
bool m_inuse
Tells if the connection is in use or not.