OdbcLog.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_ODBC
27 
28 #include "OdbcLog.h"
29 #include "SessionID.h"
30 #include "SessionSettings.h"
31 #include "Utility.h"
32 #include "strptime.h"
33 #include <fstream>
34 
35 namespace FIX
36 {
37 
38 const std::string OdbcLogFactory::DEFAULT_USER = "sa";
39 const std::string OdbcLogFactory::DEFAULT_PASSWORD = "";
40 const std::string OdbcLogFactory::DEFAULT_CONNECTION_STRING
41  = "DATABASE=quickfix;DRIVER={SQL Server};SERVER=(local);";
42 
43 OdbcLog::OdbcLog
44 ( const SessionID& s, const std::string& user, const std::string& password,
45  const std::string& connectionString )
46 {
47  init();
48  m_pSessionID = new SessionID( s );
49  m_pConnection = new OdbcConnection( user, password, connectionString );
50 }
51 
52 OdbcLog::OdbcLog
53 ( const std::string& user, const std::string& password,
54  const std::string& connectionString )
55 : m_pSessionID( 0 )
56 {
57  init();
58  m_pConnection = new OdbcConnection( user, password, connectionString );
59 }
60 
61 void OdbcLog::init()
62 {
63  setIncomingTable( "messages_log" );
64  setOutgoingTable( "messages_log" );
65  setEventTable( "event_log" );
66 }
67 
68 OdbcLog::~OdbcLog()
69 {
70  delete m_pSessionID;
71  delete m_pConnection;
72 }
73 
74 OdbcLogFactory::OdbcLogFactory( const std::string& user, const std::string& password,
75  const std::string& connectionString )
76 : m_user( user ), m_password( password ), m_connectionString( connectionString ),
77  m_useSettings( false )
78 {
79 }
80 
81 OdbcLogFactory::OdbcLogFactory()
82 : m_user( DEFAULT_USER ), m_password( DEFAULT_PASSWORD ),
83  m_connectionString( DEFAULT_CONNECTION_STRING ), m_useSettings( false )
84 {
85 }
86 
87 OdbcLogFactory::~OdbcLogFactory()
88 {
89 }
90 
91 Log* OdbcLogFactory::create()
92 {
93  std::string database;
94  std::string user;
95  std::string connectionString;
96 
97  init( m_settings.get(), database, user, connectionString );
98  OdbcLog* result = new OdbcLog( database, user, connectionString );
99  initLog( m_settings.get(), *result );
100  return result;
101 }
102 
103 Log* OdbcLogFactory::create( const SessionID& s )
104 {
105  std::string database;
106  std::string user;
107  std::string connectionString;
108 
109  Dictionary settings;
110  if( m_settings.has(s) )
111  settings = m_settings.get( s );
112 
113  init( settings, database, user, connectionString );
114  OdbcLog* result = new OdbcLog( s, database, user, connectionString );
115  initLog( settings, *result );
116  return result;
117 }
118 
119 void OdbcLogFactory::init( const Dictionary& settings,
120  std::string& user,
121  std::string& password,
122  std::string& connectionString )
123 {
124  user = DEFAULT_USER;
125  password = DEFAULT_PASSWORD;
126  connectionString = DEFAULT_CONNECTION_STRING;
127 
128  if( m_useSettings )
129  {
130  try { user = settings.getString( ODBC_LOG_USER ); }
131  catch( ConfigError& ) {}
132 
133  try { password = settings.getString( ODBC_LOG_PASSWORD ); }
134  catch( ConfigError& ) {}
135 
136  try { connectionString = settings.getString( ODBC_LOG_CONNECTION_STRING ); }
137  catch( ConfigError& ) {}
138  }
139  else
140  {
141  user = m_user;
142  password = m_password;
143  connectionString = m_connectionString;
144  }
145 }
146 
147 void OdbcLogFactory::initLog( const Dictionary& settings, OdbcLog& log )
148 {
149  try { log.setIncomingTable( settings.getString( ODBC_LOG_INCOMING_TABLE ) ); }
150  catch( ConfigError& ) {}
151 
152  try { log.setOutgoingTable( settings.getString( ODBC_LOG_OUTGOING_TABLE ) ); }
153  catch( ConfigError& ) {}
154 
155  try { log.setEventTable( settings.getString( ODBC_LOG_EVENT_TABLE ) ); }
156  catch( ConfigError& ) {}
157 }
158 
159 void OdbcLogFactory::destroy( Log* pLog )
160 {
161  delete pLog;
162 }
163 
164 void OdbcLog::clear()
165 {
166  std::stringstream whereClause;
167 
168  whereClause << "WHERE ";
169 
170  if( m_pSessionID )
171  {
172  whereClause
173  << "BeginString = '" << m_pSessionID->getBeginString().getValue() << "' "
174  << "AND SenderCompID = '" << m_pSessionID->getSenderCompID().getValue() << "' "
175  << "AND TargetCompID = '" << m_pSessionID->getTargetCompID().getValue() << "' ";
176 
177  if( m_pSessionID->getSessionQualifier().size() )
178  whereClause << "AND SessionQualifier = '" << m_pSessionID->getSessionQualifier() << "'";
179  }
180  else
181  {
182  whereClause << "BeginString = NULL AND SenderCompID = NULL && TargetCompID = NULL";
183  }
184 
185  std::stringstream incomingQuery;
186  std::stringstream outgoingQuery;
187  std::stringstream eventQuery;
188 
189  incomingQuery
190  << "DELETE FROM " << m_incomingTable << " " << whereClause.str();
191  outgoingQuery
192  << "DELETE FROM " << m_outgoingTable << " " << whereClause.str();
193  eventQuery
194  << "DELETE FROM " << m_eventTable << " " << whereClause.str();
195 
196  OdbcQuery incoming( incomingQuery.str() );
197  OdbcQuery outgoing( outgoingQuery.str() );
198  OdbcQuery event( eventQuery.str() );
199  m_pConnection->execute( incoming );
200  m_pConnection->execute( outgoing );
201  m_pConnection->execute( event );
202 }
203 
204 void OdbcLog::backup()
205 {
206 }
207 
208 void OdbcLog::insert( const std::string& table, const std::string value )
209 {
210  UtcTimeStamp time;
211  int year, month, day, hour, minute, second, millis;
212  time.getYMD( year, month, day );
213  time.getHMS( hour, minute, second, millis );
214 
215  char sqlTime[ 24 ];
216  STRING_SPRINTF( sqlTime, "%d-%02d-%02d %02d:%02d:%02d.%003d",
217  year, month, day, hour, minute, second, millis );
218 
219  std::string valueCopy = value;
220  string_replace( "'", "''", valueCopy );
221 
222  std::stringstream queryString;
223  queryString << "INSERT INTO " << table << " "
224  << "(time, beginstring, sendercompid, targetcompid, session_qualifier, text) "
225  << "VALUES ("
226  << "{ts '" << sqlTime << "'},";
227 
228  if( m_pSessionID )
229  {
230  queryString
231  << "'" << m_pSessionID->getBeginString().getValue() << "',"
232  << "'" << m_pSessionID->getSenderCompID().getValue() << "',"
233  << "'" << m_pSessionID->getTargetCompID().getValue() << "',";
234  if( m_pSessionID->getSessionQualifier() == "" )
235  queryString << "NULL" << ",";
236  else
237  queryString << "'" << m_pSessionID->getSessionQualifier() << "',";
238  }
239  else
240  {
241  queryString << "NULL, NULL, NULL, NULL, ";
242  }
243 
244  queryString << "'" << valueCopy << "')";
245 
246  OdbcQuery query( queryString.str() );
247  m_pConnection->execute( query );
248 }
249 
250 }
251 
252 #endif
SessionID.h
SessionSettings.h
FIX::ODBC_LOG_CONNECTION_STRING
const char ODBC_LOG_CONNECTION_STRING[]
Definition: SessionSettings.h:138
FIX::ODBC_LOG_PASSWORD
const char ODBC_LOG_PASSWORD[]
Definition: SessionSettings.h:137
FIX::ODBC_LOG_EVENT_TABLE
const char ODBC_LOG_EVENT_TABLE[]
Definition: SessionSettings.h:141
FIX::ODBC_LOG_INCOMING_TABLE
const char ODBC_LOG_INCOMING_TABLE[]
Definition: SessionSettings.h:139
FIX::TYPE::UtcTimeStamp
@ UtcTimeStamp
Definition: FieldTypes.h:940
FIX::string_replace
void string_replace(const std::string &oldValue, const std::string &newValue, std::string &value)
Definition: Utility.cpp:57
FIX::ODBC_LOG_USER
const char ODBC_LOG_USER[]
Definition: SessionSettings.h:136
STRING_SPRINTF
#define STRING_SPRINTF
Definition: Utility.h:222
FIX
Definition: Acceptor.cpp:34
OdbcLog.h
Utility.h
FIX::ODBC_LOG_OUTGOING_TABLE
const char ODBC_LOG_OUTGOING_TABLE[]
Definition: SessionSettings.h:140

Generated on Wed Apr 29 2020 19:41:30 for QuickFIX by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2001