All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "../common/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, 0, 0, 0, 0, 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  PQclear(result);
73 
74  throw Exception(errmsg.str());
75  }
76 
77  PQclear(result);
78 }
79 
81 {
82  if(m_pgconn)
83  PQfinish(m_pgconn);
84 }
85 
86 te::pgis::Connection::Connection(ConnectionPool* pool, const std::string& conninfo, const std::string& cencoding, bool inuse)
87  : m_pool(pool),
88  m_pgconn(0),
89  m_inuse(inuse),
90  m_lastuse(boost::posix_time::second_clock::local_time())
91 {
92  //if(conninfo.empty())
93  // return;
94 
95 // it tries to create the connection object
96  m_pgconn = PQconnectdb(conninfo.c_str());
97 
98  ConnStatusType status = PQstatus(m_pgconn);
99 
100  if(status != CONNECTION_OK)
101  {
102  boost::format errmsg(TE_TR("It was not possible to create a connection to the given data source due to the following error: %1%."));
103 
104  errmsg = errmsg % PQerrorMessage(m_pgconn);
105 
106  PQfinish(m_pgconn);
107 
108  m_pgconn = 0;
109 
110  throw Exception(errmsg.str());
111  }
112 
113  PQsetNoticeProcessor(m_pgconn, PostGISNoticeProcessor, this);
114 
115  if(cencoding.empty())
116  return;
117 
118  if(PQsetClientEncoding(m_pgconn, cencoding.c_str()) == -1)
119  {
120  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%."));
121 
122  errmsg = errmsg % PQerrorMessage(m_pgconn);
123 
124  PQfinish(m_pgconn);
125 
126  m_pgconn = 0;
127 
128  throw Exception(errmsg.str());
129  }
130 }
131 
132 static void PostGISNoticeProcessor(void* /*arg*/, const char* /*pszMessage*/)
133 {
134 }
Connection(ConnectionPool *pool, const std::string &conninfo, const std::string &cencoding, bool inuse=false)
Constructor.
Definition: Connection.cpp:86
struct pg_result PGresult
Definition: Connection.h:48
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
void execute(const std::string &command)
It executes the given SQL command and throws away the result.
Definition: Connection.cpp:61
PGconn * m_pgconn
The PostgreSQL real connection handle.
Definition: Connection.h:130
A class that implements a connection to a PostgreSQL database.
static void PostGISNoticeProcessor(void *, const char *)
This will just close the PostgreSQL notice processor.
Definition: Connection.cpp:132
An exception class for the PostGIS driver.
This class implements a connection pool for the PostGIS driver.
~Connection()
Destructor.
Definition: Connection.cpp:80
PGresult * query(const std::string &query)
It queries the database.
Definition: Connection.cpp:44