MySQLLog.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 MySQLLog.h included, but HAVE_MYSQL not defined
  22. #endif
  23. #ifdef HAVE_MYSQL
  24. #ifndef FIX_MYSQLLOG_H
  25. #define FIX_MYSQLLOG_H
  26. #ifdef _MSC_VER
  27. #pragma warning( disable : 4503 4355 4786 4290 )
  28. #pragma comment( lib, "libMySQL" )
  29. #endif
  30. #include "Log.h"
  31. #include "SessionSettings.h"
  32. #include "MySQLConnection.h"
  33. #include <fstream>
  34. #include <string>
  35. namespace FIX
  36. {
  37. /// MySQL based implementation of Log.
  38. class MySQLLog : public Log
  39. {
  40. public:
  41. MySQLLog( const SessionID& s, const DatabaseConnectionID& d, MySQLConnectionPool* p );
  42. MySQLLog( const DatabaseConnectionID& d, MySQLConnectionPool* p );
  43. MySQLLog( const SessionID& s, const std::string& database, const std::string& user,
  44. const std::string& password, const std::string& host, short port );
  45. MySQLLog( const std::string& database, const std::string& user,
  46. const std::string& password, const std::string& host, short port );
  47. ~MySQLLog();
  48. void clear();
  49. void backup();
  50. void setIncomingTable( const std::string& incomingTable )
  51. { m_incomingTable = incomingTable; }
  52. void setOutgoingTable( const std::string& outgoingTable )
  53. { m_outgoingTable = outgoingTable; }
  54. void setEventTable( const std::string& eventTable )
  55. { m_eventTable = eventTable; }
  56. void onIncoming( const std::string& value )
  57. { insert( m_incomingTable, value ); }
  58. void onOutgoing( const std::string& value )
  59. { insert( m_outgoingTable, value ); }
  60. void onEvent( const std::string& value )
  61. { insert( m_eventTable, value ); }
  62. private:
  63. void init();
  64. void insert( const std::string& table, const std::string value );
  65. std::string m_incomingTable;
  66. std::string m_outgoingTable;
  67. std::string m_eventTable;
  68. MySQLConnection* m_pConnection;
  69. MySQLConnectionPool* m_pConnectionPool;
  70. SessionID* m_pSessionID;
  71. };
  72. /// Creates a MySQL based implementation of Log.
  73. class MySQLLogFactory : public LogFactory
  74. {
  75. public:
  76. static const std::string DEFAULT_DATABASE;
  77. static const std::string DEFAULT_USER;
  78. static const std::string DEFAULT_PASSWORD;
  79. static const std::string DEFAULT_HOST;
  80. static const short DEFAULT_PORT;
  81. MySQLLogFactory( const SessionSettings& settings )
  82. : m_settings( settings ), m_useSettings( true )
  83. {
  84. bool poolConnections = false;
  85. try { poolConnections = settings.get().getBool(MYSQL_LOG_USECONNECTIONPOOL); }
  86. catch( ConfigError& ) {}
  87. m_connectionPoolPtr = MySQLConnectionPoolPtr
  88. ( new MySQLConnectionPool(poolConnections) );
  89. }
  90. MySQLLogFactory( const std::string& database, const std::string& user,
  91. const std::string& password, const std::string& host,
  92. short port )
  93. : m_database( database ), m_user( user ), m_password( password ), m_host( host ), m_port( port ),
  94. m_useSettings( false )
  95. {
  96. m_connectionPoolPtr = MySQLConnectionPoolPtr
  97. ( new MySQLConnectionPool(false) );
  98. }
  99. MySQLLogFactory()
  100. : m_database( DEFAULT_DATABASE ), m_user( DEFAULT_USER ), m_password( DEFAULT_PASSWORD ),
  101. m_host( DEFAULT_HOST ), m_port( DEFAULT_PORT ), m_useSettings( false )
  102. {
  103. m_connectionPoolPtr = MySQLConnectionPoolPtr
  104. ( new MySQLConnectionPool(false) );
  105. }
  106. Log* create();
  107. Log* create( const SessionID& );
  108. void destroy( Log* );
  109. private:
  110. void init( const Dictionary& settings, std::string& database,
  111. std::string& user, std::string& password,
  112. std::string& host, short& port );
  113. void initLog( const Dictionary& settings, MySQLLog& log );
  114. MySQLConnectionPoolPtr m_connectionPoolPtr;
  115. SessionSettings m_settings;
  116. std::string m_database;
  117. std::string m_user;
  118. std::string m_password;
  119. std::string m_host;
  120. short m_port;
  121. bool m_useSettings;
  122. };
  123. }
  124. #endif //FIX_MYSQLLOG_H
  125. #endif //HAVE_MYSQL