PostgreSQLStore.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* -*- C++ -*- */
  2. /****************************************************************************
  3. ** Copyright (c) 2001-2014
  4. **
  5. ** This file is part of the QuickFIX FIX Engine
  6. **
  7. ** This file may be distributed under the terms of the quickfixengine.org
  8. ** license as defined by quickfixengine.org and appearing in the file
  9. ** LICENSE included in the packaging of this file.
  10. **
  11. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  12. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  13. **
  14. ** See http://www.quickfixengine.org/LICENSE for licensing information.
  15. **
  16. ** Contact ask@quickfixengine.org if any conditions of this licensing are
  17. ** not clear to you.
  18. **
  19. ****************************************************************************/
  20. #ifndef HAVE_POSTGRESQL
  21. #error PostgreSQLStore.h included, but HAVE_POSTGRESQL not defined
  22. #endif
  23. #ifdef HAVE_POSTGRESQL
  24. #ifndef FIX_POSTGRESQLSTORE_H
  25. #define FIX_POSTGRESQLSTORE_H
  26. #ifdef _MSC_VER
  27. #pragma warning( disable : 4503 4355 4786 4290 )
  28. #endif
  29. #include "MessageStore.h"
  30. #include "SessionSettings.h"
  31. #include "PostgreSQLConnection.h"
  32. #include <fstream>
  33. #include <string>
  34. namespace FIX
  35. {
  36. /// Creates a PostgreSQL based implementation of MessageStore.
  37. class PostgreSQLStoreFactory : public MessageStoreFactory
  38. {
  39. public:
  40. static const std::string DEFAULT_DATABASE;
  41. static const std::string DEFAULT_USER;
  42. static const std::string DEFAULT_PASSWORD;
  43. static const std::string DEFAULT_HOST;
  44. static const short DEFAULT_PORT;
  45. PostgreSQLStoreFactory( const SessionSettings& settings )
  46. : m_settings( settings ), m_useSettings( true ), m_useDictionary( false )
  47. {
  48. bool poolConnections = false;
  49. try { poolConnections = settings.get().getBool(POSTGRESQL_STORE_USECONNECTIONPOOL); }
  50. catch( ConfigError& ) {}
  51. m_connectionPoolPtr = PostgreSQLConnectionPoolPtr
  52. ( new PostgreSQLConnectionPool(poolConnections) );
  53. }
  54. PostgreSQLStoreFactory( const Dictionary& dictionary )
  55. : m_dictionary( dictionary ), m_useSettings( false ), m_useDictionary( true )
  56. {
  57. m_connectionPoolPtr = PostgreSQLConnectionPoolPtr
  58. ( new PostgreSQLConnectionPool(false) );
  59. }
  60. PostgreSQLStoreFactory( const std::string& database, const std::string& user,
  61. const std::string& password, const std::string& host,
  62. short port )
  63. : m_database( database ), m_user( user ), m_password( password ), m_host( host ), m_port( port ),
  64. m_useSettings( false ), m_useDictionary( false )
  65. {
  66. m_connectionPoolPtr = PostgreSQLConnectionPoolPtr
  67. ( new PostgreSQLConnectionPool(false) );
  68. }
  69. PostgreSQLStoreFactory()
  70. : m_database( DEFAULT_DATABASE ), m_user( DEFAULT_USER ), m_password( DEFAULT_PASSWORD ),
  71. m_host( DEFAULT_HOST ), m_port( DEFAULT_PORT ), m_useSettings( false ), m_useDictionary( false )
  72. {
  73. m_connectionPoolPtr = PostgreSQLConnectionPoolPtr
  74. ( new PostgreSQLConnectionPool(false) );
  75. }
  76. MessageStore* create( const SessionID& );
  77. void destroy( MessageStore* );
  78. private:
  79. MessageStore* create( const SessionID& s, const Dictionary& );
  80. PostgreSQLConnectionPoolPtr m_connectionPoolPtr;
  81. SessionSettings m_settings;
  82. Dictionary m_dictionary;
  83. std::string m_database;
  84. std::string m_user;
  85. std::string m_password;
  86. std::string m_host;
  87. short m_port;
  88. bool m_useSettings;
  89. bool m_useDictionary;
  90. };
  91. /*! @} */
  92. /// PostgreSQL based implementation of MessageStore.
  93. class PostgreSQLStore : public MessageStore
  94. {
  95. public:
  96. PostgreSQLStore( const SessionID& s, const DatabaseConnectionID& d, PostgreSQLConnectionPool* p );
  97. PostgreSQLStore( const SessionID& s, const std::string& database, const std::string& user,
  98. const std::string& password, const std::string& host, short port );
  99. ~PostgreSQLStore();
  100. bool set( int, const std::string& ) throw ( IOException );
  101. void get( int, int, std::vector < std::string > & ) const throw ( IOException );
  102. int getNextSenderMsgSeqNum() const throw ( IOException );
  103. int getNextTargetMsgSeqNum() const throw ( IOException );
  104. void setNextSenderMsgSeqNum( int value ) throw ( IOException );
  105. void setNextTargetMsgSeqNum( int value ) throw ( IOException );
  106. void incrNextSenderMsgSeqNum() throw ( IOException );
  107. void incrNextTargetMsgSeqNum() throw ( IOException );
  108. UtcTimeStamp getCreationTime() const throw ( IOException );
  109. void reset() throw ( IOException );
  110. void refresh() throw ( IOException );
  111. private:
  112. void populateCache();
  113. MemoryStore m_cache;
  114. PostgreSQLConnection* m_pConnection;
  115. PostgreSQLConnectionPool* m_pConnectionPool;
  116. SessionID m_sessionID;
  117. };
  118. }
  119. #endif //FIX_POSTGRESQLSTORE_H
  120. #endif //HAVE_POSTGRESQL