MySQLStore.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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_MYSQL
  21. #error MySQLStore.h included, but HAVE_MYSQL not defined
  22. #endif
  23. #ifdef HAVE_MYSQL
  24. #ifndef FIX_MYSQLSTORE_H
  25. #define FIX_MYSQLSTORE_H
  26. #ifdef _MSC_VER
  27. #pragma warning( disable : 4503 4355 4786 4290 )
  28. #pragma comment( lib, "libMySQL" )
  29. #endif
  30. #include "MessageStore.h"
  31. #include "SessionSettings.h"
  32. #include "MySQLConnection.h"
  33. #include <fstream>
  34. #include <string>
  35. namespace FIX
  36. {
  37. /// Creates a MySQL based implementation of MessageStore.
  38. class MySQLStoreFactory : public MessageStoreFactory
  39. {
  40. public:
  41. static const std::string DEFAULT_DATABASE;
  42. static const std::string DEFAULT_USER;
  43. static const std::string DEFAULT_PASSWORD;
  44. static const std::string DEFAULT_HOST;
  45. static const short DEFAULT_PORT;
  46. MySQLStoreFactory( const SessionSettings& settings )
  47. : m_settings( settings ), m_useSettings( true ), m_useDictionary( false )
  48. {
  49. bool poolConnections = false;
  50. try { poolConnections = settings.get().getBool(MYSQL_STORE_USECONNECTIONPOOL); }
  51. catch( ConfigError& ) {}
  52. m_connectionPoolPtr = MySQLConnectionPoolPtr
  53. ( new MySQLConnectionPool(poolConnections) );
  54. }
  55. MySQLStoreFactory( const Dictionary& dictionary )
  56. : m_dictionary( dictionary ), m_useSettings( false ), m_useDictionary( true )
  57. {
  58. m_connectionPoolPtr = MySQLConnectionPoolPtr
  59. ( new MySQLConnectionPool(false) );
  60. }
  61. MySQLStoreFactory( const std::string& database, const std::string& user,
  62. const std::string& password, const std::string& host,
  63. short port )
  64. : m_database( database ), m_user( user ), m_password( password ), m_host( host ), m_port( port ),
  65. m_useSettings( false ), m_useDictionary( false )
  66. {
  67. m_connectionPoolPtr = MySQLConnectionPoolPtr
  68. ( new MySQLConnectionPool(false) );
  69. }
  70. MySQLStoreFactory()
  71. : m_database( DEFAULT_DATABASE ), m_user( DEFAULT_USER ), m_password( DEFAULT_PASSWORD ),
  72. m_host( DEFAULT_HOST ), m_port( DEFAULT_PORT ), m_useSettings( false ), m_useDictionary( false )
  73. {
  74. m_connectionPoolPtr = MySQLConnectionPoolPtr
  75. ( new MySQLConnectionPool(false) );
  76. }
  77. MessageStore* create( const SessionID& );
  78. void destroy( MessageStore* );
  79. private:
  80. MessageStore* create( const SessionID& s, const Dictionary& );
  81. MySQLConnectionPoolPtr m_connectionPoolPtr;
  82. SessionSettings m_settings;
  83. Dictionary m_dictionary;
  84. std::string m_database;
  85. std::string m_user;
  86. std::string m_password;
  87. std::string m_host;
  88. short m_port;
  89. bool m_useSettings;
  90. bool m_useDictionary;
  91. };
  92. /*! @} */
  93. /// MySQL based implementation of MessageStore.
  94. class MySQLStore : public MessageStore
  95. {
  96. public:
  97. MySQLStore( const SessionID& s, const DatabaseConnectionID& d, MySQLConnectionPool* p );
  98. MySQLStore( const SessionID& s, const std::string& database, const std::string& user,
  99. const std::string& password, const std::string& host, short port );
  100. ~MySQLStore();
  101. bool set( int, const std::string& ) throw ( IOException );
  102. void get( int, int, std::vector < std::string > & ) const throw ( IOException );
  103. int getNextSenderMsgSeqNum() const throw ( IOException );
  104. int getNextTargetMsgSeqNum() const throw ( IOException );
  105. void setNextSenderMsgSeqNum( int value ) throw ( IOException );
  106. void setNextTargetMsgSeqNum( int value ) throw ( IOException );
  107. void incrNextSenderMsgSeqNum() throw ( IOException );
  108. void incrNextTargetMsgSeqNum() throw ( IOException );
  109. UtcTimeStamp getCreationTime() const throw ( IOException );
  110. void reset() throw ( IOException );
  111. void refresh() throw ( IOException );
  112. private:
  113. void populateCache();
  114. MemoryStore m_cache;
  115. MySQLConnection* m_pConnection;
  116. MySQLConnectionPool* m_pConnectionPool;
  117. SessionID m_sessionID;
  118. };
  119. }
  120. #endif //FIX_MYSQLSTORE_H
  121. #endif //HAVE_MYSQL