forked from quickfix/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQLStore.cpp
More file actions
331 lines (282 loc) · 11.4 KB
/
Copy pathMySQLStore.cpp
File metadata and controls
331 lines (282 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/****************************************************************************
** Copyright (c) 2001-2014
**
** This file is part of the QuickFIX FIX Engine
**
** This file may be distributed under the terms of the quickfixengine.org
** license as defined by quickfixengine.org and appearing in the file
** LICENSE included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.quickfixengine.org/LICENSE for licensing information.
**
** Contact ask@quickfixengine.org if any conditions of this licensing are
** not clear to you.
**
****************************************************************************/
#ifdef _MSC_VER
#include "stdafx.h"
#else
#include "config.h"
#endif
#ifdef HAVE_MYSQL
#include "MySQLStore.h"
#include "SessionID.h"
#include "SessionSettings.h"
#include "FieldConvertors.h"
#include "Parser.h"
#include "Utility.h"
#include "strptime.h"
#include <fstream>
namespace FIX
{
const std::string MySQLStoreFactory::DEFAULT_DATABASE = "quickfix";
const std::string MySQLStoreFactory::DEFAULT_USER = "";
const std::string MySQLStoreFactory::DEFAULT_PASSWORD = "";
const std::string MySQLStoreFactory::DEFAULT_HOST = "localhost";
const short MySQLStoreFactory::DEFAULT_PORT = 3306;
MySQLStore::MySQLStore
( const SessionID& s, const DatabaseConnectionID& d, MySQLConnectionPool* p )
: m_pConnectionPool( p ), m_sessionID( s )
{
m_pConnection = m_pConnectionPool->create( d );
populateCache();
}
MySQLStore::MySQLStore
( const SessionID& s, const std::string& database, const std::string& user,
const std::string& password, const std::string& host, short port )
: m_pConnectionPool( 0 ), m_sessionID( s )
{
m_pConnection = new MySQLConnection( database, user, password, host, port );
populateCache();
}
MySQLStore::~MySQLStore()
{
if( m_pConnectionPool )
m_pConnectionPool->destroy( m_pConnection );
else
delete m_pConnection;
}
void MySQLStore::populateCache()
{
std::stringstream queryString;
queryString << "SELECT creation_time, incoming_seqnum, outgoing_seqnum FROM sessions WHERE "
<< "beginstring=" << "\"" << m_sessionID.getBeginString().getValue() << "\" and "
<< "sendercompid=" << "\"" << m_sessionID.getSenderCompID().getValue() << "\" and "
<< "targetcompid=" << "\"" << m_sessionID.getTargetCompID().getValue() << "\" and "
<< "session_qualifier=" << "\"" << m_sessionID.getSessionQualifier() << "\"";
MySQLQuery query( queryString.str() );
if( !m_pConnection->execute(query) )
throw ConfigError( "No entries found for session in database" );
int rows = query.rows();
if( rows > 1 )
throw ConfigError( "Multiple entries found for session in database" );
if( rows == 1 )
{
struct tm time;
std::string sqlTime = query.getValue( 0, 0 );
strptime( sqlTime.c_str(), "%Y-%m-%d %H:%M:%S", &time );
m_cache.setCreationTime (UtcTimeStamp (&time));
m_cache.setNextTargetMsgSeqNum( atol( query.getValue( 0, 1 ) ) );
m_cache.setNextSenderMsgSeqNum( atol( query.getValue( 0, 2 ) ) );
}
else
{
UtcTimeStamp time = m_cache.getCreationTime();
char sqlTime[ 20 ];
int year, month, day, hour, minute, second, millis;
time.getYMD (year, month, day);
time.getHMS (hour, minute, second, millis);
STRING_SPRINTF( sqlTime, "%d-%02d-%02d %02d:%02d:%02d",
year, month, day, hour, minute, second );
std::stringstream queryString2;
queryString2 << "INSERT INTO sessions (beginstring, sendercompid, targetcompid, session_qualifier,"
<< "creation_time, incoming_seqnum, outgoing_seqnum) VALUES("
<< "\"" << m_sessionID.getBeginString().getValue() << "\","
<< "\"" << m_sessionID.getSenderCompID().getValue() << "\","
<< "\"" << m_sessionID.getTargetCompID().getValue() << "\","
<< "\"" << m_sessionID.getSessionQualifier() << "\","
<< "'" << sqlTime << "',"
<< m_cache.getNextTargetMsgSeqNum() << ","
<< m_cache.getNextSenderMsgSeqNum() << ")";
MySQLQuery query2( queryString2.str() );
if( !m_pConnection->execute(query2) )
throw ConfigError( "Unable to create session in database" );
}
}
MessageStore* MySQLStoreFactory::create( const SessionID& s )
{
if( m_useSettings )
return create( s, m_settings.get(s) );
else if( m_useDictionary )
return create( s, m_dictionary );
else
{
DatabaseConnectionID id( m_database, m_user, m_password, m_host, m_port );
return new MySQLStore( s, id, m_connectionPoolPtr.get() );
}
}
MessageStore* MySQLStoreFactory::create( const SessionID& s, const Dictionary& settings )
{
std::string database = DEFAULT_DATABASE;
std::string user = DEFAULT_USER;
std::string password = DEFAULT_PASSWORD;
std::string host = DEFAULT_HOST;
short port = DEFAULT_PORT;
try { database = settings.getString( MYSQL_STORE_DATABASE ); }
catch( ConfigError& ) {}
try { user = settings.getString( MYSQL_STORE_USER ); }
catch( ConfigError& ) {}
try { password = settings.getString( MYSQL_STORE_PASSWORD ); }
catch( ConfigError& ) {}
try { host = settings.getString( MYSQL_STORE_HOST ); }
catch( ConfigError& ) {}
try { port = ( short ) settings.getInt( MYSQL_STORE_PORT ); }
catch( ConfigError& ) {}
DatabaseConnectionID id( database, user, password, host, port );
return new MySQLStore( s, id, m_connectionPoolPtr.get() );
}
void MySQLStoreFactory::destroy( MessageStore* pStore )
{
delete pStore;
}
bool MySQLStore::set( int msgSeqNum, const std::string& msg )
EXCEPT ( IOException )
{
char* msgCopy = new char[ (msg.size() * 2) + 1 ];
mysql_escape_string( msgCopy, msg.c_str(), msg.size() );
std::stringstream queryString;
queryString << "INSERT INTO messages "
<< "(beginstring, sendercompid, targetcompid, session_qualifier, msgseqnum, message) "
<< "VALUES ("
<< "\"" << m_sessionID.getBeginString().getValue() << "\","
<< "\"" << m_sessionID.getSenderCompID().getValue() << "\","
<< "\"" << m_sessionID.getTargetCompID().getValue() << "\","
<< "\"" << m_sessionID.getSessionQualifier() << "\","
<< msgSeqNum << ","
<< "\"" << msgCopy << "\")";
delete [] msgCopy;
MySQLQuery query( queryString.str() );
if( !m_pConnection->execute(query) )
{
std::stringstream queryString2;
queryString2 << "UPDATE messages SET message=\"" << msg << "\" WHERE "
<< "beginstring=" << "\"" << m_sessionID.getBeginString().getValue() << "\" and "
<< "sendercompid=" << "\"" << m_sessionID.getSenderCompID().getValue() << "\" and "
<< "targetcompid=" << "\"" << m_sessionID.getTargetCompID().getValue() << "\" and "
<< "session_qualifier=" << "\"" << m_sessionID.getSessionQualifier() << "\" and "
<< "msgseqnum=" << msgSeqNum;
MySQLQuery query2( queryString2.str() );
if( !m_pConnection->execute(query2) )
query2.throwException();
}
return true;
}
void MySQLStore::get( int begin, int end,
std::vector < std::string > & result ) const
EXCEPT ( IOException )
{
result.clear();
std::stringstream queryString;
queryString << "SELECT message FROM messages WHERE "
<< "beginstring=" << "\"" << m_sessionID.getBeginString().getValue() << "\" and "
<< "sendercompid=" << "\"" << m_sessionID.getSenderCompID().getValue() << "\" and "
<< "targetcompid=" << "\"" << m_sessionID.getTargetCompID().getValue() << "\" and "
<< "session_qualifier=" << "\"" << m_sessionID.getSessionQualifier() << "\" and "
<< "msgseqnum>=" << begin << " and " << "msgseqnum<=" << end << " "
<< "ORDER BY msgseqnum";
MySQLQuery query( queryString.str() );
if( !m_pConnection->execute(query) )
query.throwException();
int rows = query.rows();
for( int row = 0; row < rows; row++ )
result.push_back( query.getValue( row, 0 ) );
}
int MySQLStore::getNextSenderMsgSeqNum() const EXCEPT ( IOException )
{
return m_cache.getNextSenderMsgSeqNum();
}
int MySQLStore::getNextTargetMsgSeqNum() const EXCEPT ( IOException )
{
return m_cache.getNextTargetMsgSeqNum();
}
void MySQLStore::setNextSenderMsgSeqNum( int value ) EXCEPT ( IOException )
{
std::stringstream queryString;
queryString << "UPDATE sessions SET outgoing_seqnum=" << value << " WHERE "
<< "beginstring=" << "\"" << m_sessionID.getBeginString().getValue() << "\" and "
<< "sendercompid=" << "\"" << m_sessionID.getSenderCompID().getValue() << "\" and "
<< "targetcompid=" << "\"" << m_sessionID.getTargetCompID().getValue() << "\" and "
<< "session_qualifier=" << "\"" << m_sessionID.getSessionQualifier() << "\"";
MySQLQuery query( queryString.str() );
if( !m_pConnection->execute(query) )
query.throwException();
m_cache.setNextSenderMsgSeqNum( value );
}
void MySQLStore::setNextTargetMsgSeqNum( int value ) EXCEPT ( IOException )
{
std::stringstream queryString;
queryString << "UPDATE sessions SET incoming_seqnum=" << value << " WHERE "
<< "beginstring=" << "\"" << m_sessionID.getBeginString().getValue() << "\" and "
<< "sendercompid=" << "\"" << m_sessionID.getSenderCompID().getValue() << "\" and "
<< "targetcompid=" << "\"" << m_sessionID.getTargetCompID().getValue() << "\" and "
<< "session_qualifier=" << "\"" << m_sessionID.getSessionQualifier() << "\"";
MySQLQuery query( queryString.str() );
if( !m_pConnection->execute(query) )
query.throwException();
m_cache.setNextTargetMsgSeqNum( value );
}
void MySQLStore::incrNextSenderMsgSeqNum() EXCEPT ( IOException )
{
m_cache.incrNextSenderMsgSeqNum();
setNextSenderMsgSeqNum( m_cache.getNextSenderMsgSeqNum() );
}
void MySQLStore::incrNextTargetMsgSeqNum() EXCEPT ( IOException )
{
m_cache.incrNextTargetMsgSeqNum();
setNextTargetMsgSeqNum( m_cache.getNextTargetMsgSeqNum() );
}
UtcTimeStamp MySQLStore::getCreationTime() const EXCEPT ( IOException )
{
return m_cache.getCreationTime();
}
void MySQLStore::reset() EXCEPT ( IOException )
{
std::stringstream queryString;
queryString << "DELETE FROM messages WHERE "
<< "beginstring=" << "\"" << m_sessionID.getBeginString().getValue() << "\" and "
<< "sendercompid=" << "\"" << m_sessionID.getSenderCompID().getValue() << "\" and "
<< "targetcompid=" << "\"" << m_sessionID.getTargetCompID().getValue() << "\" and "
<< "session_qualifier=" << "\"" << m_sessionID.getSessionQualifier() << "\"";
MySQLQuery query( queryString.str() );
if( !m_pConnection->execute(query) )
query.throwException();
m_cache.reset();
UtcTimeStamp time = m_cache.getCreationTime();
int year, month, day, hour, minute, second, millis;
time.getYMD( year, month, day );
time.getHMS( hour, minute, second, millis );
char sqlTime[ 20 ];
STRING_SPRINTF( sqlTime, "%d-%02d-%02d %02d:%02d:%02d",
year, month, day, hour, minute, second );
std::stringstream queryString2;
queryString2 << "UPDATE sessions SET creation_time='" << sqlTime << "', "
<< "incoming_seqnum=" << m_cache.getNextTargetMsgSeqNum() << ", "
<< "outgoing_seqnum=" << m_cache.getNextSenderMsgSeqNum() << " WHERE "
<< "beginstring=" << "\"" << m_sessionID.getBeginString().getValue() << "\" and "
<< "sendercompid=" << "\"" << m_sessionID.getSenderCompID().getValue() << "\" and "
<< "targetcompid=" << "\"" << m_sessionID.getTargetCompID().getValue() << "\" and "
<< "session_qualifier=" << "\"" << m_sessionID.getSessionQualifier() << "\"";
MySQLQuery query2( queryString2.str() );
if( !m_pConnection->execute(query2) )
query2.throwException();
}
void MySQLStore::refresh() EXCEPT ( IOException )
{
m_cache.reset();
populateCache();
}
}
#endif