OdbcLog.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_ODBC
00027 
00028 #include "OdbcLog.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 OdbcLogFactory::DEFAULT_USER = "sa";
00039 const std::string OdbcLogFactory::DEFAULT_PASSWORD = "";
00040 const std::string OdbcLogFactory::DEFAULT_CONNECTION_STRING
00041   = "DATABASE=quickfix;DRIVER={SQL Server};SERVER=(local);";
00042 
00043 OdbcLog::OdbcLog
00044 ( const SessionID& s, const std::string& user, const std::string& password, 
00045   const std::string& connectionString )
00046 {
00047   init();
00048   m_pSessionID = new SessionID( s );
00049   m_pConnection = new OdbcConnection( user, password, connectionString );
00050 }
00051 
00052 OdbcLog::OdbcLog
00053 ( const std::string& user, const std::string& password, 
00054   const std::string& connectionString )
00055 : m_pSessionID( 0 )
00056 {
00057   init();
00058   m_pConnection = new OdbcConnection( user, password, connectionString );
00059 }
00060 
00061 void OdbcLog::init()
00062 {
00063   setIncomingTable( "messages_log" );
00064   setOutgoingTable( "messages_log" );
00065   setEventTable( "event_log" );
00066 }
00067 
00068 OdbcLog::~OdbcLog()
00069 {
00070   delete m_pSessionID;
00071   delete m_pConnection;
00072 }
00073 
00074 OdbcLogFactory::OdbcLogFactory( const std::string& user, const std::string& password, 
00075                                 const std::string& connectionString )
00076 : m_user( user ), m_password( password ), m_connectionString( connectionString ),
00077   m_useSettings( false )
00078 {
00079 }
00080 
00081 OdbcLogFactory::OdbcLogFactory()
00082 : m_user( DEFAULT_USER ), m_password( DEFAULT_PASSWORD ),
00083   m_connectionString( DEFAULT_CONNECTION_STRING ), m_useSettings( false )
00084 {
00085 }
00086 
00087 OdbcLogFactory::~OdbcLogFactory()
00088 {
00089 }
00090 
00091 Log* OdbcLogFactory::create()
00092 {
00093   std::string database;
00094   std::string user;
00095   std::string connectionString;
00096 
00097   init( m_settings.get(), database, user, connectionString );
00098   OdbcLog* result = new OdbcLog( database, user, connectionString );
00099   initLog( m_settings.get(), *result );
00100   return result;
00101 }
00102 
00103 Log* OdbcLogFactory::create( const SessionID& s )
00104 {
00105   std::string database;
00106   std::string user;
00107   std::string connectionString;
00108 
00109   Dictionary settings;
00110   if( m_settings.has(s) ) 
00111     settings = m_settings.get( s );
00112 
00113   init( settings, database, user, connectionString );
00114   OdbcLog* result = new OdbcLog( s, database, user, connectionString );
00115   initLog( settings, *result );
00116   return result;
00117 }
00118 
00119 void OdbcLogFactory::init( const Dictionary& settings, 
00120                            std::string& user,
00121                            std::string& password, 
00122                            std::string& connectionString )
00123 {
00124   user = DEFAULT_USER;
00125   password = DEFAULT_PASSWORD;
00126   connectionString = DEFAULT_CONNECTION_STRING;
00127 
00128   if( m_useSettings )
00129   {
00130     try { user = settings.getString( ODBC_LOG_USER ); }
00131     catch( ConfigError& ) {}
00132 
00133     try { password = settings.getString( ODBC_LOG_PASSWORD ); }
00134     catch( ConfigError& ) {}
00135 
00136     try { connectionString = settings.getString( ODBC_LOG_CONNECTION_STRING ); }
00137     catch( ConfigError& ) {}
00138   }
00139   else
00140   {
00141     user = m_user;
00142     password = m_password;
00143     connectionString = m_connectionString;
00144   }
00145 }
00146 
00147 void OdbcLogFactory::initLog( const Dictionary& settings, OdbcLog& log )
00148 {
00149   try { log.setIncomingTable( settings.getString( ODBC_LOG_INCOMING_TABLE ) ); }
00150   catch( ConfigError& ) {}
00151 
00152   try { log.setOutgoingTable( settings.getString( ODBC_LOG_OUTGOING_TABLE ) ); }
00153   catch( ConfigError& ) {}
00154 
00155   try { log.setEventTable( settings.getString( ODBC_LOG_EVENT_TABLE ) ); }
00156   catch( ConfigError& ) {}
00157 }
00158 
00159 void OdbcLogFactory::destroy( Log* pLog )
00160 {
00161   delete pLog;
00162 }
00163 
00164 void OdbcLog::clear()
00165 {
00166   std::stringstream whereClause;
00167 
00168   whereClause << "WHERE ";
00169 
00170   if( m_pSessionID )
00171   {
00172     whereClause
00173     << "BeginString = '" << m_pSessionID->getBeginString().getValue() << "' "
00174     << "AND SenderCompID = '" << m_pSessionID->getSenderCompID().getValue() << "' "
00175     << "AND TargetCompID = '" << m_pSessionID->getTargetCompID().getValue() << "' ";
00176 
00177     if( m_pSessionID->getSessionQualifier().size() )
00178       whereClause << "AND SessionQualifier = '" << m_pSessionID->getSessionQualifier() << "'";
00179   }
00180   else
00181   {
00182     whereClause << "BeginString = NULL AND SenderCompID = NULL && TargetCompID = NULL";
00183   }
00184 
00185   std::stringstream incomingQuery;
00186   std::stringstream outgoingQuery;
00187   std::stringstream eventQuery;
00188 
00189   incomingQuery 
00190     << "DELETE FROM " << m_incomingTable << " " << whereClause.str();
00191   outgoingQuery 
00192     << "DELETE FROM " << m_outgoingTable << " " << whereClause.str();
00193   eventQuery 
00194     << "DELETE FROM " << m_eventTable << " " << whereClause.str();
00195 
00196   OdbcQuery incoming( incomingQuery.str() );
00197   OdbcQuery outgoing( outgoingQuery.str() );
00198   OdbcQuery event( eventQuery.str() );
00199   m_pConnection->execute( incoming );
00200   m_pConnection->execute( outgoing );
00201   m_pConnection->execute( event );
00202 }
00203 
00204 void OdbcLog::backup()
00205 {
00206 }
00207 
00208 void OdbcLog::insert( const std::string& table, const std::string value )
00209 {
00210   UtcTimeStamp time;
00211   int year, month, day, hour, minute, second, millis;
00212   time.getYMD( year, month, day );
00213   time.getHMS( hour, minute, second, millis );
00214 
00215   char sqlTime[ 24 ];
00216   STRING_SPRINTF( sqlTime, "%d-%02d-%02d %02d:%02d:%02d.%003d",
00217                   year, month, day, hour, minute, second, millis );
00218 
00219   std::string valueCopy = value;
00220   string_replace( "'", "''", valueCopy );
00221 
00222   std::stringstream queryString;
00223   queryString << "INSERT INTO " << table << " "
00224   << "(time, beginstring, sendercompid, targetcompid, session_qualifier, text) "
00225   << "VALUES ("
00226   << "{ts '" << sqlTime << "'},";
00227 
00228   if( m_pSessionID )
00229   {
00230     queryString
00231     << "'" << m_pSessionID->getBeginString().getValue() << "',"
00232     << "'" << m_pSessionID->getSenderCompID().getValue() << "',"
00233     << "'" << m_pSessionID->getTargetCompID().getValue() << "',";
00234     if( m_pSessionID->getSessionQualifier() == "" )
00235       queryString << "NULL" << ",";
00236     else
00237       queryString << "'" << m_pSessionID->getSessionQualifier() << "',";
00238   }
00239   else
00240   {
00241     queryString << "NULL, NULL, NULL, NULL, ";
00242   }
00243 
00244   queryString << "'" << valueCopy << "')";
00245 
00246   OdbcQuery query( queryString.str() );
00247   m_pConnection->execute( query );
00248 }
00249 
00250 }
00251 
00252 #endif

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