PostgreSQLLog.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_POSTGRESQL
00027 
00028 #include "PostgreSQLLog.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 PostgreSQLLogFactory::DEFAULT_DATABASE = "quickfix";
00039 const std::string PostgreSQLLogFactory::DEFAULT_USER = "postgres";
00040 const std::string PostgreSQLLogFactory::DEFAULT_PASSWORD = "";
00041 const std::string PostgreSQLLogFactory::DEFAULT_HOST = "localhost";
00042 const short PostgreSQLLogFactory::DEFAULT_PORT = 0;
00043 
00044 PostgreSQLLog::PostgreSQLLog
00045 ( const SessionID& s, const DatabaseConnectionID& d, PostgreSQLConnectionPool* p )
00046 : m_pConnectionPool( p )
00047 {
00048   init();
00049   m_pSessionID = new SessionID( s );
00050   m_pConnection = m_pConnectionPool->create( d );
00051 }
00052 
00053 PostgreSQLLog::PostgreSQLLog
00054 ( const DatabaseConnectionID& d, PostgreSQLConnectionPool* p )
00055 : m_pConnectionPool( p ), m_pSessionID( 0 )
00056 {
00057   init();
00058   m_pConnection = m_pConnectionPool->create( d );
00059 }
00060 
00061 PostgreSQLLog::PostgreSQLLog
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 PostgreSQLConnection( database, user, password, host, port );
00069 }
00070 
00071 PostgreSQLLog::PostgreSQLLog
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   init();
00077   m_pConnection = new PostgreSQLConnection( database, user, password, host, port );
00078 }
00079 
00080 void PostgreSQLLog::init()
00081 {
00082   setIncomingTable( "messages_log" );
00083   setOutgoingTable( "messages_log" );
00084   setEventTable( "event_log" );
00085 }
00086 
00087 PostgreSQLLog::~PostgreSQLLog()
00088 {
00089   if( m_pConnectionPool )
00090     m_pConnectionPool->destroy( m_pConnection );
00091   else
00092     delete m_pConnection;
00093   delete m_pSessionID;
00094 }
00095 
00096 Log* PostgreSQLLogFactory::create()
00097 {
00098   std::string database;
00099   std::string user;
00100   std::string password;
00101   std::string host;
00102   short port;
00103 
00104   init( m_settings.get(), database, user, password, host, port );
00105   DatabaseConnectionID id( database, user, password, host, port );
00106   PostgreSQLLog* result = new PostgreSQLLog( id, m_connectionPoolPtr.get() );
00107   initLog( m_settings.get(), *result );
00108   return result;
00109 }
00110 
00111 Log* PostgreSQLLogFactory::create( const SessionID& s )
00112 {
00113   std::string database;
00114   std::string user;
00115   std::string password;
00116   std::string host;
00117   short port;
00118 
00119   Dictionary settings;
00120   if( m_settings.has(s) ) 
00121     settings = m_settings.get( s );
00122 
00123   init( settings, database, user, password, host, port );
00124   DatabaseConnectionID id( database, user, password, host, port );
00125   PostgreSQLLog* result = new PostgreSQLLog( s, id, m_connectionPoolPtr.get() );
00126   initLog( settings, *result );
00127   return result;
00128 }
00129 
00130 void PostgreSQLLogFactory::init( const Dictionary& settings, 
00131            std::string& database, 
00132            std::string& user,
00133            std::string& password,
00134            std::string& host,
00135            short &port )
00136 {
00137   database = DEFAULT_DATABASE;
00138   user = DEFAULT_USER;
00139   password = DEFAULT_PASSWORD;
00140   host = DEFAULT_HOST;
00141   port = DEFAULT_PORT;
00142 
00143   if( m_useSettings )
00144   {
00145     try { database = settings.getString( POSTGRESQL_LOG_DATABASE ); }
00146     catch( ConfigError& ) {}
00147 
00148     try { user = settings.getString( POSTGRESQL_LOG_USER ); }
00149     catch( ConfigError& ) {}
00150 
00151     try { password = settings.getString( POSTGRESQL_LOG_PASSWORD ); }
00152     catch( ConfigError& ) {}
00153 
00154     try { host = settings.getString( POSTGRESQL_LOG_HOST ); }
00155     catch( ConfigError& ) {}
00156 
00157     try { port = ( short ) settings.getInt( POSTGRESQL_LOG_PORT ); }
00158     catch( ConfigError& ) {}
00159   }
00160   else
00161   {
00162     database = m_database;
00163     user = m_user;
00164     password = m_password;
00165     host = m_host;
00166     port = m_port;
00167   }
00168 }
00169 
00170 void PostgreSQLLogFactory::initLog( const Dictionary& settings, PostgreSQLLog& log )
00171 {
00172   try { log.setIncomingTable( settings.getString( POSTGRESQL_LOG_INCOMING_TABLE ) ); }
00173   catch( ConfigError& ) {}
00174 
00175   try { log.setOutgoingTable( settings.getString( POSTGRESQL_LOG_OUTGOING_TABLE ) ); }
00176   catch( ConfigError& ) {}
00177 
00178   try { log.setEventTable( settings.getString( POSTGRESQL_LOG_EVENT_TABLE ) ); }
00179   catch( ConfigError& ) {}
00180 }
00181 
00182 void PostgreSQLLogFactory::destroy( Log* pLog )
00183 {
00184   delete pLog;
00185 }
00186 
00187 void PostgreSQLLog::clear()
00188 {
00189   std::stringstream whereClause;
00190 
00191   whereClause << "WHERE ";
00192 
00193   if( m_pSessionID )
00194   {
00195     whereClause
00196     << "BeginString = '" << m_pSessionID->getBeginString().getValue() << "' "
00197     << "AND SenderCompID = '" << m_pSessionID->getSenderCompID().getValue() << "' "
00198     << "AND TargetCompID = '" << m_pSessionID->getTargetCompID().getValue() << "' ";
00199 
00200     if( m_pSessionID->getSessionQualifier().size() )
00201       whereClause << "AND SessionQualifier = '" << m_pSessionID->getSessionQualifier() << "'";
00202   }
00203   else
00204   {
00205     whereClause << "BeginString = NULL AND SenderCompID = NULL && TargetCompID = NULL";
00206   }
00207 
00208   std::stringstream incomingQuery;
00209   std::stringstream outgoingQuery;
00210   std::stringstream eventQuery;
00211 
00212   incomingQuery 
00213     << "DELETE FROM " << m_incomingTable << " " << whereClause.str();
00214   outgoingQuery 
00215     << "DELETE FROM " << m_outgoingTable << " " << whereClause.str();
00216   eventQuery 
00217     << "DELETE FROM " << m_eventTable << " " << whereClause.str();
00218 
00219   PostgreSQLQuery incoming( incomingQuery.str() );
00220   PostgreSQLQuery outgoing( outgoingQuery.str() );
00221   PostgreSQLQuery event( eventQuery.str() );
00222   m_pConnection->execute( incoming );
00223   m_pConnection->execute( outgoing );
00224   m_pConnection->execute( event );
00225 }
00226 
00227 void PostgreSQLLog::backup()
00228 {
00229 }
00230 
00231 void PostgreSQLLog::insert( const std::string& table, const std::string value )
00232 {
00233   UtcTimeStamp time;
00234   int year, month, day, hour, minute, second, millis;
00235   time.getYMD( year, month, day );
00236   time.getHMS( hour, minute, second, millis );
00237 
00238   char sqlTime[ 24 ];
00239   STRING_SPRINTF( sqlTime, "%d-%02d-%02d %02d:%02d:%02d.%003d",
00240            year, month, day, hour, minute, second, millis );
00241   
00242   char* valueCopy = new char[ (value.size() * 2) + 1 ];
00243   PQescapeString( valueCopy, value.c_str(), value.size() );
00244 
00245   std::stringstream queryString;
00246   queryString << "INSERT INTO " << table << " "
00247   << "(time, beginstring, sendercompid, targetcompid, session_qualifier, text) "
00248   << "VALUES ("
00249   << "'" << sqlTime << "',";
00250 
00251   if( m_pSessionID )
00252   {
00253     queryString
00254     << "'" << m_pSessionID->getBeginString().getValue() << "',"
00255     << "'" << m_pSessionID->getSenderCompID().getValue() << "',"
00256     << "'" << m_pSessionID->getTargetCompID().getValue() << "',";
00257     if( m_pSessionID->getSessionQualifier() == "" )
00258       queryString << "NULL" << ",";
00259     else
00260       queryString << "'" << m_pSessionID->getSessionQualifier() << "',";
00261   }
00262   else
00263   {
00264     queryString << "NULL, NULL, NULL, NULL, ";
00265   }
00266 
00267   queryString << "'" << valueCopy << "')";
00268   delete [] valueCopy;
00269 
00270   PostgreSQLQuery query( queryString.str() );
00271   m_pConnection->execute( query );
00272 }
00273 
00274 } // namespace FIX
00275 
00276 #endif //HAVE_POSTGRESQL

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