MySQLLog.cpp
Go to the documentation of this file.
1/****************************************************************************
2** Copyright (c) 2001-2014
3**
4** This file is part of the QuickFIX FIX Engine
5**
6** This file may be distributed under the terms of the quickfixengine.org
7** license as defined by quickfixengine.org and appearing in the file
8** LICENSE included in the packaging of this file.
9**
10** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
11** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
12**
13** See http://www.quickfixengine.org/LICENSE for licensing information.
14**
15** Contact ask@quickfixengine.org if any conditions of this licensing are
16** not clear to you.
17**
18****************************************************************************/
19
20#ifdef _MSC_VER
21#include "stdafx.h"
22#else
23#include "config.h"
24#endif
25
26#ifdef HAVE_MYSQL
27
28#include "MySQLLog.h"
29#include "SessionID.h"
30#include "SessionSettings.h"
31#include "Utility.h"
32#include "strptime.h"
33#include <fstream>
34
35namespace FIX
36{
37
38const std::string MySQLLogFactory::DEFAULT_DATABASE = "quickfix";
39const std::string MySQLLogFactory::DEFAULT_USER = "root";
40const std::string MySQLLogFactory::DEFAULT_PASSWORD = "";
41const std::string MySQLLogFactory::DEFAULT_HOST = "localhost";
42const short MySQLLogFactory::DEFAULT_PORT = 0;
43
44MySQLLog::MySQLLog
45( const SessionID& s, const DatabaseConnectionID& d, MySQLConnectionPool* p )
46: m_pConnectionPool( p )
47{
48 init();
49 m_pSessionID = new SessionID( s );
50 m_pConnection = m_pConnectionPool->create( d );
51}
52
53MySQLLog::MySQLLog
54( const DatabaseConnectionID& d, MySQLConnectionPool* p )
55: m_pConnectionPool( p ), m_pSessionID( 0 )
56{
57 init();
58 m_pConnection = m_pConnectionPool->create( d );
59}
60
61MySQLLog::MySQLLog
62( const SessionID& s, const std::string& database, const std::string& user,
63 const std::string& password, const std::string& host, short port )
64 : m_pConnectionPool( 0 )
65{
66 init();
67 m_pSessionID = new SessionID( s );
68 m_pConnection = new MySQLConnection( database, user, password, host, port );
69}
70
71MySQLLog::MySQLLog
72( const std::string& database, const std::string& user,
73 const std::string& password, const std::string& host, short port )
74 : m_pConnectionPool( 0 ), m_pSessionID( 0 )
75{
76 m_pConnection = new MySQLConnection( database, user, password, host, port );
77}
78
79void MySQLLog::init()
80{
81 setIncomingTable( "messages_log" );
82 setOutgoingTable( "messages_log" );
83 setEventTable( "event_log" );
84}
85
86MySQLLog::~MySQLLog()
87{
88 if( m_pConnectionPool )
89 m_pConnectionPool->destroy( m_pConnection );
90 else
91 delete m_pConnection;
92 delete m_pSessionID;
93}
94
95Log* MySQLLogFactory::create()
96{
97 std::string database;
98 std::string user;
99 std::string password;
100 std::string host;
101 short port;
102
103 init( m_settings.get(), database, user, password, host, port );
104 DatabaseConnectionID id( database, user, password, host, port );
105 MySQLLog* result = new MySQLLog( id, m_connectionPoolPtr.get() );
106 initLog( m_settings.get(), *result );
107 return result;
108}
109
110Log* MySQLLogFactory::create( const SessionID& s )
111{
112 std::string database;
113 std::string user;
114 std::string password;
115 std::string host;
116 short port;
117
118 Dictionary settings;
119 if( m_settings.has(s) )
120 settings = m_settings.get( s );
121
122 init( settings, database, user, password, host, port );
123 DatabaseConnectionID id( database, user, password, host, port );
124 MySQLLog* result = new MySQLLog( s, id, m_connectionPoolPtr.get() );
125 initLog( settings, *result );
126 return result;
127}
128
129void MySQLLogFactory::init( const Dictionary& settings,
130 std::string& database,
131 std::string& user,
132 std::string& password,
133 std::string& host,
134 short &port )
135{
136 database = DEFAULT_DATABASE;
137 user = DEFAULT_USER;
138 password = DEFAULT_PASSWORD;
139 host = DEFAULT_HOST;
140 port = DEFAULT_PORT;
141
142 if( m_useSettings )
143 {
144 try { database = settings.getString( MYSQL_LOG_DATABASE ); }
145 catch( ConfigError& ) {}
146
147 try { user = settings.getString( MYSQL_LOG_USER ); }
148 catch( ConfigError& ) {}
149
150 try { password = settings.getString( MYSQL_LOG_PASSWORD ); }
151 catch( ConfigError& ) {}
152
153 try { host = settings.getString( MYSQL_LOG_HOST ); }
154 catch( ConfigError& ) {}
155
156 try { port = ( short ) settings.getInt( MYSQL_LOG_PORT ); }
157 catch( ConfigError& ) {}
158 }
159 else
160 {
161 database = m_database;
162 user = m_user;
163 password = m_password;
164 host = m_host;
165 port = m_port;
166 }
167}
168
169void MySQLLogFactory::initLog( const Dictionary& settings, MySQLLog& log )
170{
171 try { log.setIncomingTable( settings.getString( MYSQL_LOG_INCOMING_TABLE ) ); }
172 catch( ConfigError& ) {}
173
174 try { log.setOutgoingTable( settings.getString( MYSQL_LOG_OUTGOING_TABLE ) ); }
175 catch( ConfigError& ) {}
176
177 try { log.setEventTable( settings.getString( MYSQL_LOG_EVENT_TABLE ) ); }
178 catch( ConfigError& ) {}
179}
180
181void MySQLLogFactory::destroy( Log* pLog )
182{
183 delete pLog;
184}
185
186void MySQLLog::clear()
187{
188 std::stringstream whereClause;
189
190 whereClause << "WHERE ";
191
192 if( m_pSessionID )
193 {
194 whereClause
195 << "BeginString = \"" << m_pSessionID->getBeginString().getValue() << "\" "
196 << "AND SenderCompID = \"" << m_pSessionID->getSenderCompID().getValue() << "\" "
197 << "AND TargetCompID = \"" << m_pSessionID->getTargetCompID().getValue() << "\" ";
198
199 if( m_pSessionID->getSessionQualifier().size() )
200 whereClause << "AND SessionQualifier = \"" << m_pSessionID->getSessionQualifier() << "\"";
201 }
202 else
203 {
204 whereClause << "BeginString = NULL AND SenderCompID = NULL && TargetCompID = NULL";
205 }
206
207 std::stringstream incomingQuery;
208 std::stringstream outgoingQuery;
209 std::stringstream eventQuery;
210
211 incomingQuery
212 << "DELETE FROM " << m_incomingTable << " " << whereClause.str();
213 outgoingQuery
214 << "DELETE FROM " << m_outgoingTable << " " << whereClause.str();
215 eventQuery
216 << "DELETE FROM " << m_eventTable << " " << whereClause.str();
217
218 MySQLQuery incoming( incomingQuery.str() );
219 MySQLQuery outgoing( outgoingQuery.str() );
220 MySQLQuery event( eventQuery.str() );
221 m_pConnection->execute( incoming );
222 m_pConnection->execute( outgoing );
223 m_pConnection->execute( event );
224}
225
226void MySQLLog::backup()
227{
228}
229
230void MySQLLog::insert( const std::string& table, const std::string value )
231{
232 UtcTimeStamp time;
233 int year, month, day, hour, minute, second, millis;
234 time.getYMD( year, month, day );
235 time.getHMS( hour, minute, second, millis );
236
237 char sqlTime[ 20 ];
238 STRING_SPRINTF( sqlTime, "%d-%02d-%02d %02d:%02d:%02d",
239 year, month, day, hour, minute, second );
240
241 char* valueCopy = new char[ (value.size() * 2) + 1 ];
242 mysql_escape_string( valueCopy, value.c_str(), value.size() );
243
244 std::stringstream queryString;
245 queryString << "INSERT INTO " << table << " "
246 << "(time, time_milliseconds, beginstring, sendercompid, targetcompid, session_qualifier, text) "
247 << "VALUES ("
248 << "'" << sqlTime << "','" << millis << "',";
249
250 if( m_pSessionID )
251 {
252 queryString
253 << "\"" << m_pSessionID->getBeginString().getValue() << "\","
254 << "\"" << m_pSessionID->getSenderCompID().getValue() << "\","
255 << "\"" << m_pSessionID->getTargetCompID().getValue() << "\",";
256 if( m_pSessionID->getSessionQualifier() == "" )
257 queryString << "NULL" << ",";
258 else
259 queryString << "\"" << m_pSessionID->getSessionQualifier() << "\",";
260 }
261 else
262 {
263 queryString << "NULL, NULL, NULL, NULL, ";
264 }
265
266 queryString << "\"" << valueCopy << "\")";
267 delete [] valueCopy;
268
269 MySQLQuery query( queryString.str() );
270 m_pConnection->execute( query );
271}
272
273} //namespace FIX
274
275#endif //HAVE_MYSQL
#define STRING_SPRINTF
Definition Utility.h:222

Generated on Mon Oct 14 2024 06:04:44 for QuickFIX by doxygen 1.9.8 written by Dimitri van Heesch, © 1997-2001