PostgreSQLLog.h 4.6 KB

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