Connection.h
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.h
22 
23  \brief A class that implements a connection to a PostgreSQL database.
24 */
25 
26 #ifndef __TERRALIB_POSTGIS_INTERNAL_CONNECTION_H
27 #define __TERRALIB_POSTGIS_INTERNAL_CONNECTION_H
28 
29 // TerraLib
30 //#include "../dataaccess/datasource/Connection.h"
31 #include "Config.h"
32 
33 // STL
34 #include <cstddef>
35 #include <string>
36 
37 // Boost
38 #include <boost/date_time/posix_time/posix_time_types.hpp>
39 #include <boost/noncopyable.hpp>
40 
41 // Forward declarations for libpq
42 extern "C"
43 {
44  struct pg_conn;
45  typedef struct pg_conn PGconn;
46 
47  struct pg_result;
48  typedef struct pg_result PGresult;
49 }
50 
51 namespace te
52 {
53  namespace pgis
54  {
55  // Forward declarations
56  class ConnectionPool;
57 
58  /*!
59  \class Connection
60 
61  \brief A class that implements a connection to a PostgreSQL database
62 
63  This class models a physical connection to a PostgreSQL data source.
64  It is designed to work with the connection pool.
65 
66  \sa ConnectionPool
67  */
68  class TEPGISEXPORT Connection : public boost::noncopyable
69  {
70  public:
71 
72  /*!
73  \brief It queries the database.
74 
75  \param query A SQL Select command.
76 
77  \return A resultset. The caller of this method will take its ownership.
78 
79  \exception Exception It throws an exception if the query execution fails.
80  */
81  PGresult* query(const std::string& query);
82 
83  /*!
84  \brief It executes the given SQL command and throws away the result.
85 
86  \param command Any SQL command.
87 
88  \exception Exception It throws an exception if the query execution fails.
89  */
90  void execute(const std::string& command);
91 
92  /*!
93  \brief It gets the underlying PGconn object.
94 
95  \return The underlying PGconn object.
96  */
97  PGconn* getConn() const
98  {
99  return m_pgconn;
100  }
101 
102  /*!
103  \brief It gets the associated connection pool.
104 
105  \return The associated connection pool.
106  */
108  {
109  return m_pool;
110  }
111 
112  /*! \brief Destructor. */
113  ~Connection();
114 
115  private:
116 
117  /*!
118  \brief Constructor.
119 
120  \param pool The associated connection pool.
121  \param conninfo A coonection string as undertood by libpq.
122  \param cecoding The character encoding used by application.
123  \param inuse A marker that tells if the connection is in use or not.
124  */
125  Connection(ConnectionPool* pool, const std::string& conninfo, const std::string& cencoding, bool inuse = false);
126 
127  public:
128 
129  ConnectionPool* m_pool; //!< The connection pool associated to the connection.
130  PGconn* m_pgconn; //!< The PostgreSQL real connection handle.
131  bool m_inuse; //!< Tells if the connection is in use or not.
132  boost::posix_time::ptime m_lastuse; //!< It marks the last time this connection was used.
133 
134  friend class ConnectionPool;
135  };
136 
137  } // end namespace pgis
138 } // end namespace te
139 
140 
141 #endif // __TERRALIB_POSTGIS_INTERNAL_CONNECTION_H
ConnectionPool * getPool() const
It gets the associated connection pool.
Definition: Connection.h:107
struct pg_result PGresult
Definition: Connection.h:48
A class that implements a connection to a PostgreSQL database.
Definition: Connection.h:68
#define TEPGISEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:195
struct pg_conn PGconn
Definition: Connection.h:45
PGconn * getConn() const
It gets the underlying PGconn object.
Definition: Connection.h:97
Configuration flags for the PostGIS Driver Implementation of TerraLib.
URI C++ Library.
boost::posix_time::ptime m_lastuse
It marks the last time this connection was used.
Definition: Connection.h:132
PGconn * m_pgconn
The PostgreSQL real connection handle.
Definition: Connection.h:130
This class implements a connection pool for the PostGIS driver.
ConnectionPool * m_pool
The connection pool associated to the connection.
Definition: Connection.h:129
bool m_inuse
Tells if the connection is in use or not.
Definition: Connection.h:131