PostgreSQLConnection.h
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 
3 /****************************************************************************
4 ** Copyright (c) 2001-2014
5 **
6 ** This file is part of the QuickFIX FIX Engine
7 **
8 ** This file may be distributed under the terms of the quickfixengine.org
9 ** license as defined by quickfixengine.org and appearing in the file
10 ** LICENSE included in the packaging of this file.
11 **
12 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
13 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
14 **
15 ** See http://www.quickfixengine.org/LICENSE for licensing information.
16 **
17 ** Contact ask@quickfixengine.org if any conditions of this licensing are
18 ** not clear to you.
19 **
20 ****************************************************************************/
21 
22 #ifndef HAVE_POSTGRESQL
23 #error PostgreSQLConnection.h included, but HAVE_POSTGRESQL not defined
24 #endif
25 
26 #ifdef HAVE_POSTGRESQL
27 #ifndef FIX_POSTGRESQLCONNECTION_H
28 #define FIX_POSTGRESQLCONNECTION_H
29 
30 #ifdef _MSC_VER
31 #pragma warning( disable : 4503 4355 4786 4290 )
32 #pragma comment( lib, "libpq" )
33 #endif
34 
35 #include <libpq-fe.h>
36 #include "DatabaseConnectionID.h"
37 #include "DatabaseConnectionPool.h"
38 #include "Mutex.h"
39 
40 namespace FIX
41 {
42 class PostgreSQLQuery
43 {
44 public:
45  PostgreSQLQuery( const std::string& query )
46  : m_result( 0 ), m_query( query )
47  {}
48 
49  ~PostgreSQLQuery()
50  {
51  if( m_result )
52  PQclear( m_result );
53  }
54 
55  bool execute( PGconn* pConnection )
56  {
57  int retry = 0;
58 
59  do
60  {
61  if( m_result ) PQclear( m_result );
62  m_result = PQexec( pConnection, m_query.c_str() );
63  m_status = PQresultStatus( m_result );
64  if( success() ) return true;
65  PQreset( pConnection );
66  retry++;
67  } while( retry <= 1 );
68  return success();
69  }
70 
71  bool success()
72  {
73  return m_status == PGRES_TUPLES_OK
74  || m_status == PGRES_COMMAND_OK;
75  }
76 
77  int rows()
78  {
79  return PQntuples( m_result );
80  }
81 
82  char* reason()
83  {
84  return PQresultErrorMessage( m_result );
85  }
86 
87  char* getValue( int row, int column )
88  {
89  return PQgetvalue( m_result, row, column );
90  }
91 
92  void throwException() throw( IOException )
93  {
94  if( !success() )
95  throw IOException( "Query failed [" + m_query + "] " );
96  }
97 
98 private:
99  PGresult* m_result;
100  ExecStatusType m_status;
101  std::string m_query;
102 };
103 
104 class PostgreSQLConnection
105 {
106 public:
107  PostgreSQLConnection
108  ( const DatabaseConnectionID& id )
109  : m_connectionID( id )
110  {
111  connect();
112  }
113 
114  PostgreSQLConnection
115  ( const std::string& database, const std::string& user,
116  const std::string& password, const std::string& host, short port )
117  : m_connectionID( database, user, password, host, port )
118  {
119  connect();
120  }
121 
122  ~PostgreSQLConnection()
123  {
124  if( m_pConnection )
125  PQfinish( m_pConnection );
126  }
127 
128  const DatabaseConnectionID& connectionID()
129  {
130  return m_connectionID;
131  }
132 
133  bool connected()
134  {
135  Locker locker( m_mutex );
136  return PQstatus( m_pConnection ) == CONNECTION_OK;
137  }
138 
139  bool reconnect()
140  {
141  Locker locker( m_mutex );
142  PQreset( m_pConnection );
143  return connected();
144  }
145 
146  bool execute( PostgreSQLQuery& pQuery )
147  {
148  Locker locker( m_mutex );
149  return pQuery.execute( m_pConnection );
150  }
151 
152 private:
153  void connect()
154  {
155  short port = m_connectionID.getPort();
156  m_pConnection = PQsetdbLogin
157  ( m_connectionID.getHost().c_str(), port == 0 ? "" : IntConvertor::convert( port ).c_str(),
158  "", "", m_connectionID.getDatabase().c_str(), m_connectionID.getUser().c_str(), m_connectionID.getPassword().c_str() );
159 
160  if( !connected() )
161  throw ConfigError( "Unable to connect to database" );
162  }
163 
164  PGconn* m_pConnection;
165  DatabaseConnectionID m_connectionID;
166  Mutex m_mutex;
167 };
168 
169 typedef DatabaseConnectionPool<PostgreSQLConnection>
170  PostgreSQLConnectionPool;
171 typedef SmartPtr< PostgreSQLConnectionPool >
172  PostgreSQLConnectionPoolPtr;
173 }
174 
175 #endif //FIX_POSTGRESQLCONNECTION_H
176 #endif //HAVE_POSTGRESQL
DatabaseConnectionID.h
Mutex.h
FIX::IntConvertor::convert
static std::string convert(signed_int value)
Definition: FieldConvertors.h:170
FIX
Definition: Acceptor.cpp:34
DatabaseConnectionPool.h

Generated on Wed Apr 29 2020 19:41:30 for QuickFIX by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2001