MySQLLog.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (c) 2001-2014
00003 **
00004 ** This file is part of the QuickFIX FIX Engine
00005 **
00006 ** This file may be distributed under the terms of the quickfixengine.org
00007 ** license as defined by quickfixengine.org and appearing in the file
00008 ** LICENSE included in the packaging of this file.
00009 **
00010 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00011 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00012 **
00013 ** See http://www.quickfixengine.org/LICENSE for licensing information.
00014 **
00015 ** Contact ask@quickfixengine.org if any conditions of this licensing are
00016 ** not clear to you.
00017 **
00018 ****************************************************************************/
00019 
00020 #ifdef _MSC_VER
00021 #include "stdafx.h"
00022 #else
00023 #include "config.h"
00024 #endif
00025 
00026 #ifdef HAVE_MYSQL
00027 
00028 #include "MySQLLog.h"
00029 #include "SessionID.h"
00030 #include "SessionSettings.h"
00031 #include "Utility.h"
00032 #include "strptime.h"
00033 #include <fstream>
00034 
00035 namespace FIX
00036 {
00037 
00038 const std::string MySQLLogFactory::DEFAULT_DATABASE = "quickfix";
00039 const std::string MySQLLogFactory::DEFAULT_USER = "root";
00040 const std::string MySQLLogFactory::DEFAULT_PASSWORD = "";
00041 const std::string MySQLLogFactory::DEFAULT_HOST = "localhost";
00042 const short MySQLLogFactory::DEFAULT_PORT = 0;
00043 
00044 MySQLLog::MySQLLog
00045 ( const SessionID& s, const DatabaseConnectionID& d, MySQLConnectionPool* p )
00046 : m_pConnectionPool( p )
00047 {
00048   init();
00049   m_pSessionID = new SessionID( s );
00050   m_pConnection = m_pConnectionPool->create( d );
00051 }
00052 
00053 MySQLLog::MySQLLog
00054 ( const DatabaseConnectionID& d, MySQLConnectionPool* p )
00055 : m_pConnectionPool( p ), m_pSessionID( 0 )
00056 {
00057   init();
00058   m_pConnection = m_pConnectionPool->create( d );
00059 }
00060 
00061 MySQLLog::MySQLLog
00062 ( const SessionID& s, const std::string& database, const std::string& user,
00063   const std::string& password, const std::string& host, short port )
00064   : m_pConnectionPool( 0 )
00065 {
00066   init();
00067   m_pSessionID = new SessionID( s );
00068   m_pConnection = new MySQLConnection( database, user, password, host, port );
00069 }
00070 
00071 MySQLLog::MySQLLog
00072 ( const std::string& database, const std::string& user,
00073   const std::string& password, const std::string& host, short port )
00074   : m_pConnectionPool( 0 ), m_pSessionID( 0 )
00075 {
00076   m_pConnection = new MySQLConnection( database, user, password, host, port );
00077 }
00078 
00079 void MySQLLog::init()
00080 {
00081   setIncomingTable( "messages_log" );
00082   setOutgoingTable( "messages_log" );
00083   setEventTable( "event_log" );
00084 }
00085 
00086 MySQLLog::~MySQLLog()
00087 {
00088   if( m_pConnectionPool )
00089     m_pConnectionPool->destroy( m_pConnection );
00090   else
00091     delete m_pConnection;
00092   delete m_pSessionID;
00093 }
00094 
00095 Log* MySQLLogFactory::create()
00096 {
00097   std::string database;
00098   std::string user;
00099   std::string password;
00100   std::string host;
00101   short port;
00102 
00103   init( m_settings.get(), database, user, password, host, port );
00104   DatabaseConnectionID id( database, user, password, host, port );
00105   MySQLLog* result = new MySQLLog( id, m_connectionPoolPtr.get() );
00106   initLog( m_settings.get(), *result );
00107   return result;
00108 }
00109 
00110 Log* MySQLLogFactory::create( const SessionID& s )
00111 {
00112   std::string database;
00113   std::string user;
00114   std::string password;
00115   std::string host;
00116   short port;
00117 
00118   Dictionary settings;
00119   if( m_settings.has(s) ) 
00120     settings = m_settings.get( s );
00121 
00122   init( settings, database, user, password, host, port );
00123   DatabaseConnectionID id( database, user, password, host, port );
00124   MySQLLog* result = new MySQLLog( s, id, m_connectionPoolPtr.get() );
00125   initLog( settings, *result );
00126   return result;
00127 }
00128 
00129 void MySQLLogFactory::init( const Dictionary& settings, 
00130                             std::string& database, 
00131                             std::string& user,
00132                             std::string& password,
00133                             std::string& host,
00134                             short &port )
00135 {
00136   database = DEFAULT_DATABASE;
00137   user = DEFAULT_USER;
00138   password = DEFAULT_PASSWORD;
00139   host = DEFAULT_HOST;
00140   port = DEFAULT_PORT;
00141 
00142   if( m_useSettings )
00143   {
00144     try { database = settings.getString( MYSQL_LOG_DATABASE ); }
00145     catch( ConfigError& ) {}
00146 
00147     try { user = settings.getString( MYSQL_LOG_USER ); }
00148     catch( ConfigError& ) {}
00149 
00150     try { password = settings.getString( MYSQL_LOG_PASSWORD ); }
00151     catch( ConfigError& ) {}
00152 
00153     try { host = settings.getString( MYSQL_LOG_HOST ); }
00154     catch( ConfigError& ) {}
00155 
00156     try { port = ( short ) settings.getInt( MYSQL_LOG_PORT ); }
00157     catch( ConfigError& ) {}
00158   }
00159   else
00160   {
00161     database = m_database;
00162     user = m_user;
00163     password = m_password;
00164     host = m_host;
00165     port = m_port;
00166   }
00167 }
00168 
00169 void MySQLLogFactory::initLog( const Dictionary& settings, MySQLLog& log )
00170 {
00171   try { log.setIncomingTable( settings.getString( MYSQL_LOG_INCOMING_TABLE ) ); }
00172   catch( ConfigError& ) {}
00173 
00174   try { log.setOutgoingTable( settings.getString( MYSQL_LOG_OUTGOING_TABLE ) ); }
00175   catch( ConfigError& ) {}
00176 
00177   try { log.setEventTable( settings.getString( MYSQL_LOG_EVENT_TABLE ) ); }
00178   catch( ConfigError& ) {}
00179 }
00180 
00181 void MySQLLogFactory::destroy( Log* pLog )
00182 {
00183   delete pLog;
00184 }
00185 
00186 void MySQLLog::clear()
00187 {
00188   std::stringstream whereClause;
00189 
00190   whereClause << "WHERE ";
00191 
00192   if( m_pSessionID )
00193   {
00194     whereClause
00195     << "BeginString = \"" << m_pSessionID->getBeginString().getValue() << "\" " 
00196     << "AND SenderCompID = \"" << m_pSessionID->getSenderCompID().getValue() << "\" "
00197     << "AND TargetCompID = \"" << m_pSessionID->getTargetCompID().getValue() << "\" ";
00198 
00199     if( m_pSessionID->getSessionQualifier().size() )
00200       whereClause << "AND SessionQualifier = \"" << m_pSessionID->getSessionQualifier() << "\"";
00201   }
00202   else
00203   {
00204     whereClause << "BeginString = NULL AND SenderCompID = NULL && TargetCompID = NULL";
00205   }
00206 
00207   std::stringstream incomingQuery;
00208   std::stringstream outgoingQuery;
00209   std::stringstream eventQuery;
00210 
00211   incomingQuery 
00212     << "DELETE FROM " << m_incomingTable << " " << whereClause.str();
00213   outgoingQuery 
00214     << "DELETE FROM " << m_outgoingTable << " " << whereClause.str();
00215   eventQuery 
00216     << "DELETE FROM " << m_eventTable << " " << whereClause.str();
00217 
00218   MySQLQuery incoming( incomingQuery.str() );
00219   MySQLQuery outgoing( outgoingQuery.str() );
00220   MySQLQuery event( eventQuery.str() );
00221   m_pConnection->execute( incoming );
00222   m_pConnection->execute( outgoing );
00223   m_pConnection->execute( event );
00224 }
00225 
00226 void MySQLLog::backup()
00227 {
00228 }
00229 
00230 void MySQLLog::insert( const std::string& table, const std::string value )
00231 {
00232   UtcTimeStamp time;
00233   int year, month, day, hour, minute, second, millis;
00234   time.getYMD( year, month, day );
00235   time.getHMS( hour, minute, second, millis );
00236 
00237   char sqlTime[ 20 ];
00238   STRING_SPRINTF( sqlTime, "%d-%02d-%02d %02d:%02d:%02d",
00239            year, month, day, hour, minute, second );
00240 
00241   char* valueCopy = new char[ (value.size() * 2) + 1 ];
00242   mysql_escape_string( valueCopy, value.c_str(), value.size() );
00243 
00244   std::stringstream queryString;
00245   queryString << "INSERT INTO " << table << " "
00246   << "(time, time_milliseconds, beginstring, sendercompid, targetcompid, session_qualifier, text) "
00247   << "VALUES ("
00248   << "'" << sqlTime << "','" << millis << "',";
00249 
00250   if( m_pSessionID )
00251   {
00252     queryString
00253     << "\"" << m_pSessionID->getBeginString().getValue() << "\","
00254     << "\"" << m_pSessionID->getSenderCompID().getValue() << "\","
00255     << "\"" << m_pSessionID->getTargetCompID().getValue() << "\",";
00256     if( m_pSessionID->getSessionQualifier() == "" )
00257       queryString << "NULL" << ",";
00258     else
00259       queryString << "\"" << m_pSessionID->getSessionQualifier() << "\",";
00260   }
00261   else
00262   {
00263     queryString << "NULL, NULL, NULL, NULL, ";
00264   }
00265 
00266   queryString << "\"" << valueCopy << "\")";
00267   delete [] valueCopy;
00268 
00269   MySQLQuery query( queryString.str() );
00270   m_pConnection->execute( query );
00271 }
00272 
00273 } //namespace FIX
00274 
00275 #endif //HAVE_MYSQL

Generated on Mon Sep 15 2014 01:23:54 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001