From: <den...@us...> - 2010-02-06 17:52:21
|
Revision: 115 http://stdair.svn.sourceforge.net/stdair/?rev=115&view=rev Author: denis_arnaud Date: 2010-02-06 17:51:39 +0000 (Sat, 06 Feb 2010) Log Message: ----------- [DB] Added support for database management (still a little work to do). Modified Paths: -------------- trunk/stdair/stdair/STDAIR_Service.hpp trunk/stdair/stdair/STDAIR_Types.hpp trunk/stdair/stdair/bom/TravelSolutionStruct.hpp trunk/stdair/stdair/bom/sources.mk trunk/stdair/stdair/factory/FacSupervisor.cpp trunk/stdair/stdair/factory/FacSupervisor.hpp trunk/stdair/stdair/service/Logger.cpp trunk/stdair/stdair/service/Logger.hpp trunk/stdair/stdair/service/Makefile.am trunk/stdair/stdair/service/STDAIR_Service.cpp trunk/stdair/stdair/service/sources.mk Added Paths: ----------- trunk/stdair/stdair/bom/AirlineStruct.cpp trunk/stdair/stdair/bom/AirlineStruct.hpp trunk/stdair/stdair/service/DBSessionManager.cpp trunk/stdair/stdair/service/DBSessionManager.hpp Modified: trunk/stdair/stdair/STDAIR_Service.hpp =================================================================== --- trunk/stdair/stdair/STDAIR_Service.hpp 2010-02-06 13:33:54 UTC (rev 114) +++ trunk/stdair/stdair/STDAIR_Service.hpp 2010-02-06 17:51:39 UTC (rev 115) @@ -8,6 +8,7 @@ #include <boost/shared_ptr.hpp> // StdAir #include <stdair/basic/BasLogParams.hpp> +#include <stdair/basic/BasDBParams.hpp> namespace stdair { @@ -24,10 +25,20 @@ for more details. <br>Moreover, a reference on an output stream is given, so that log outputs can be directed onto that stream. - @param const stdair::BasLogParams& Parameters for the output log - stream. */ + @param const BasLogParams& Parameters for the output log stream. */ STDAIR_Service (const BasLogParams&); + /** Constructor. + <br>The init() method is called; see the corresponding documentation + for more details. + <br>A reference on an output stream is given, so + that log outputs can be directed onto that stream. + <br>Moreover, database connection parameters are given, so + that database requests can use the corresponding access. + @param const BasLogParams& Parameters for the output log stream. + @param const BasDBParams& Parameters for the database session. */ + STDAIR_Service (const BasLogParams&, const BasDBParams&); + /** Destructor. */ ~STDAIR_Service(); @@ -51,11 +62,16 @@ /** Initialise the log. */ void logInit (const BasLogParams&); + /** Initialise the database session. */ + void dbInit (const BasDBParams&); + /** Initialise. <br>The static instance of the log service (Logger object) is created. + <br>The static instance of the database session manager + (DBSessionManager object) is created. <br>The static instance of the FacSupervisor object, itself referencing all the other objects (factories and BOM), is created. - <br>As those two objects are static, there is no need to store them + <br>As those three objects are static, there is no need to store them in any service context. However, some lock mechanism may be needed in order to secure the access to the corresponding resources. */ void init (); Modified: trunk/stdair/stdair/STDAIR_Types.hpp =================================================================== --- trunk/stdair/stdair/STDAIR_Types.hpp 2010-02-06 13:33:54 UTC (rev 114) +++ trunk/stdair/stdair/STDAIR_Types.hpp 2010-02-06 17:51:39 UTC (rev 115) @@ -14,6 +14,12 @@ #include <boost/tuple/tuple.hpp> #include <boost/shared_ptr.hpp> +// Forward declarations +namespace soci { + class session; + class statement; +} + namespace stdair { // Forward declarations @@ -29,6 +35,9 @@ class NonInitialisedLogServiceException : public RootException { }; + class NonInitialisedDBSessionManagerException : public RootException { + }; + class NonInitialisedServiceException : public RootException { }; @@ -38,6 +47,12 @@ class ObjectNotFoundException : public RootException { }; + class SQLDatabaseException : public RootException { + }; + + class SQLDatabaseConnectionImpossibleException : public SQLDatabaseException { + }; + class DocumentNotFoundException : public RootException { }; @@ -59,6 +74,12 @@ } // //////// Type definitions ///////// + /** Database session handler. */ + typedef soci::session DBSession_T; + + /** Database request statement handler. */ + typedef soci::statement DBRequestStatement_T; + /** Define the type for durations (e.g., elapsed in-flight time). */ typedef boost::posix_time::time_duration Duration_T; Added: trunk/stdair/stdair/bom/AirlineStruct.cpp =================================================================== --- trunk/stdair/stdair/bom/AirlineStruct.cpp (rev 0) +++ trunk/stdair/stdair/bom/AirlineStruct.cpp 2010-02-06 17:51:39 UTC (rev 115) @@ -0,0 +1,50 @@ +// ////////////////////////////////////////////////////////////////////// +// Import section +// ////////////////////////////////////////////////////////////////////// +// STL +#include <cassert> +#include <istream> +#include <ostream> +#include <sstream> +// StdAir +#include <stdair/bom/AirlineStruct.hpp> + +namespace stdair { + + // //////////////////////////////////////////////////////////////////// + AirlineStruct::AirlineStruct () { + assert (false); + } + + // //////////////////////////////////////////////////////////////////// + AirlineStruct::AirlineStruct (const AirlineStruct& iAirlineStruct) + : _code (iAirlineStruct._code), _name (iAirlineStruct._name) { + } + + // //////////////////////////////////////////////////////////////////// + AirlineStruct::AirlineStruct (const AirlineCode_T& iAirlineCode, + const std::string& iAirlineName) + : _code (iAirlineCode), _name (iAirlineName) { + } + + // //////////////////////////////////////////////////////////////////// + AirlineStruct::~AirlineStruct () { + } + + // ////////////////////////////////////////////////////////////////////// + void AirlineStruct::toStream (std::ostream& ioOut) const { + ioOut << describe(); + } + + // ////////////////////////////////////////////////////////////////////// + void AirlineStruct::fromStream (std::istream& ioIn) { + } + + // ////////////////////////////////////////////////////////////////////// + const std::string AirlineStruct::describe() const { + std::ostringstream oStr; + oStr << _code << " " << _name; + return oStr.str(); + } + +} Added: trunk/stdair/stdair/bom/AirlineStruct.hpp =================================================================== --- trunk/stdair/stdair/bom/AirlineStruct.hpp (rev 0) +++ trunk/stdair/stdair/bom/AirlineStruct.hpp 2010-02-06 17:51:39 UTC (rev 115) @@ -0,0 +1,78 @@ +#ifndef __STDAIR_BOM_AIRLINESTRUCT_HPP +#define __STDAIR_BOM_AIRLINESTRUCT_HPP + +// ////////////////////////////////////////////////////////////////////// +// Import section +// ////////////////////////////////////////////////////////////////////// +// STL +#include <iosfwd> +#include <string> +#include <vector> +// StdAir +#include <stdair/STDAIR_Types.hpp> +#include <stdair/bom/StructAbstract.hpp> + +namespace stdair { + + /** Structure holding parameters describing an airline. */ + struct AirlineStruct : public StructAbstract { + public: + // /////////// Getters /////////////// + /** Get the airline code. */ + const AirlineCode_T& getAirlineCode() const { + return _code; + } + + /** Get the airline name. */ + const std::string& getAirlineName() const { + return _name; + } + + // /////////// Setters /////////////// + /** Set the airline code. */ + void setAirlineCode (const AirlineCode_T& iAirlineCode) { + _code = iAirlineCode; + } + + /** Set the airline name. */ + void setAirlineName (const std::string& iAirlineName) { + _name = iAirlineName; + } + + + public: + // /////////// Display support method ///////////// + /** Dump a Business Object into an output stream. + @param ostream& the output stream. */ + void toStream (std::ostream& ioOut) const; + + /** Read a Business Object from an input stream. + @param istream& the input stream. */ + void fromStream (std::istream& ioIn); + + /** Display of the structure. */ + const std::string describe() const; + + + public: + // //////////// Constructors & Destructor /////////////// + /** Main constructor. */ + AirlineStruct (const AirlineCode_T&, const std::string& iAirlineName); + /** Destructor. */ + ~AirlineStruct (); + /** Default copy constructor. */ + AirlineStruct (const AirlineStruct&); + /** Default constructor, not to be used. */ + AirlineStruct (); + + private: + // ///////////////////// Attributes ////////////////////// + /** Airline code. */ + AirlineCode_T _code; + + /** Airline name. */ + std::string _name; + }; + +} +#endif // __STDAIR_BOM_AIRLINESTRUCT_HPP Modified: trunk/stdair/stdair/bom/TravelSolutionStruct.hpp =================================================================== --- trunk/stdair/stdair/bom/TravelSolutionStruct.hpp 2010-02-06 13:33:54 UTC (rev 114) +++ trunk/stdair/stdair/bom/TravelSolutionStruct.hpp 2010-02-06 17:51:39 UTC (rev 115) @@ -13,6 +13,7 @@ #include <stdair/bom/BookingClassTypes.hpp> namespace stdair { + // Forward declarations class OutboundPath; @@ -42,8 +43,8 @@ const std::string describe() const; + public: // //////////// Constructors & Destructor /////////////// - public: /** Main constructor. */ TravelSolutionStruct (OutboundPath&, const BookingClassSTLList_T&); /** Destructor. */ @@ -54,7 +55,7 @@ TravelSolutionStruct (); private: - // Attributes + // ///////////////////// Attributes ////////////////////// /** The outbound path associated to this solution.*/ OutboundPath* _outboundPath_ptr; Modified: trunk/stdair/stdair/bom/sources.mk =================================================================== --- trunk/stdair/stdair/bom/sources.mk 2010-02-06 13:33:54 UTC (rev 114) +++ trunk/stdair/stdair/bom/sources.mk 2010-02-06 17:51:39 UTC (rev 115) @@ -85,6 +85,7 @@ $(top_srcdir)/stdair/bom/TravelSolutionStruct.hpp \ $(top_srcdir)/stdair/bom/TravelSolutionTypes.hpp \ $(top_srcdir)/stdair/bom/BookingRequestStruct.hpp \ + $(top_srcdir)/stdair/bom/AirlineStruct.hpp \ $(top_srcdir)/stdair/bom/BomManager.hpp bom_cc_sources = \ $(top_srcdir)/stdair/bom/BomRootKey.cpp \ @@ -133,4 +134,5 @@ $(top_srcdir)/stdair/bom/DoWStruct.cpp \ $(top_srcdir)/stdair/bom/TravelSolutionStruct.cpp \ $(top_srcdir)/stdair/bom/BookingRequestStruct.cpp \ + $(top_srcdir)/stdair/bom/AirlineStruct.cpp \ $(top_srcdir)/stdair/bom/BomManager.cpp Modified: trunk/stdair/stdair/factory/FacSupervisor.cpp =================================================================== --- trunk/stdair/stdair/factory/FacSupervisor.cpp 2010-02-06 13:33:54 UTC (rev 114) +++ trunk/stdair/stdair/factory/FacSupervisor.cpp 2010-02-06 17:51:39 UTC (rev 115) @@ -8,6 +8,7 @@ #include <stdair/factory/FacBomContent.hpp> #include <stdair/factory/FacSupervisor.hpp> #include <stdair/service/Logger.hpp> +#include <stdair/service/DBSessionManager.hpp> namespace stdair { @@ -75,9 +76,18 @@ // Clean the static instance of the log service Logger::clean(); } + + // ////////////////////////////////////////////////////////////////////// + void FacSupervisor::cleanDBSessionManager() { + // Clean the static instance of the database session manager + DBSessionManager::clean(); + } // ////////////////////////////////////////////////////////////////////// void FacSupervisor::cleanAll () { + // Clean the static instance of the database session manager + cleanDBSessionManager(); + // Clean the static instance of the log service cleanLoggerService(); Modified: trunk/stdair/stdair/factory/FacSupervisor.hpp =================================================================== --- trunk/stdair/stdair/factory/FacSupervisor.hpp 2010-02-06 13:33:54 UTC (rev 114) +++ trunk/stdair/stdair/factory/FacSupervisor.hpp 2010-02-06 17:51:39 UTC (rev 115) @@ -56,6 +56,9 @@ /** Delete the static instance of the Logger object. */ static void cleanLoggerService(); + /** Delete the static instance of the DBSessionManager object. */ + static void cleanDBSessionManager(); + /** Clean the static instance. <br>As the static instance (singleton) is deleted, all the other registered objects will be deleted in turn. */ Added: trunk/stdair/stdair/service/DBSessionManager.cpp =================================================================== --- trunk/stdair/stdair/service/DBSessionManager.cpp (rev 0) +++ trunk/stdair/stdair/service/DBSessionManager.cpp 2010-02-06 17:51:39 UTC (rev 115) @@ -0,0 +1,117 @@ +// ////////////////////////////////////////////////////////////////////// +// Import section +// ////////////////////////////////////////////////////////////////////// +// STL +#include <cassert> +#include <string> +#include <sstream> +// SOCI +#include <soci/core/soci.h> +#include <soci/backends/mysql/soci-mysql.h> +// StdAir +#include <stdair/basic/BasDBParams.hpp> +#include <stdair/service/DBSessionManager.hpp> +#include <stdair/service/Logger.hpp> + +namespace stdair { + + DBSessionManager* DBSessionManager::_instance = NULL; + + // ////////////////////////////////////////////////////////////////////// + DBSessionManager::DBSessionManager () : _dbSession (NULL) { + assert (false); + } + + // ////////////////////////////////////////////////////////////////////// + DBSessionManager::DBSessionManager (const DBSessionManager&) + : _dbSession (NULL) { + assert (false); + } + + // ////////////////////////////////////////////////////////////////////// + DBSessionManager::DBSessionManager (const BasDBParams& iDBParams) + : _dbSession (NULL) { + dbInit (iDBParams); + } + + // ////////////////////////////////////////////////////////////////////// + DBSessionManager::~DBSessionManager () { + // std::cout << "In DBSessionManager destructor" << std::endl; + dbFinalise(); + } + + // ////////////////////////////////////////////////////////////////////// + void DBSessionManager::dbInit (const BasDBParams& iDBParams) { + + // Database parameters + std::ostringstream oStr; + oStr << "db=" << iDBParams.getDBName() << " user=" << iDBParams.getUser() + << " password=" << iDBParams.getPassword() + << " port=" << iDBParams.getPort() << " host=" << iDBParams.getHost(); + const std::string lDBSessionConnectionString (oStr.str()); + + // Instanciate the database session: nothing else is performed at that stage + _dbSession = new DBSession_T(); + + try { + + // Open the connection to the database + _dbSession->open (soci::mysql, lDBSessionConnectionString); + + } catch (std::exception const& lException) { + STDAIR_LOG_ERROR ("Error while opening a connection to database: " + << lException.what()); + STDAIR_LOG_ERROR ("Database parameters used:" + << " db=" << iDBParams.getDBName() + << " user=" << iDBParams.getUser() + << " port=" << iDBParams.getPort() + << " host=" << iDBParams.getHost()); + throw SQLDatabaseConnectionImpossibleException(); + } + } + + // ////////////////////////////////////////////////////////////////////// + void DBSessionManager::dbFinalise () { + delete _dbSession; _dbSession = NULL; + } + + // ////////////////////////////////////////////////////////////////////// + void DBSessionManager::init (const BasDBParams& iDBParams) { + // Sanity check + if (_instance != NULL) { + STDAIR_LOG_ERROR ("Error: the DB session has already been initialised"); + assert (false); + } + assert (_instance == NULL); + + _instance = new DBSessionManager (iDBParams); + } + + // ////////////////////////////////////////////////////////////////////// + DBSessionManager& DBSessionManager::instance() { + if (_instance == NULL) { + throw NonInitialisedDBSessionManagerException(); + } + assert (_instance != NULL); + return *_instance; + } + + // ////////////////////////////////////////////////////////////////////// + void DBSessionManager::clean() { + //std::cout<< "In DBSessionManager::clean(),before static instance deletion" + // << std::endl; + delete _instance; _instance = NULL; + //std::cout<< "In DBSessionManager::clean(),after static instance deletion" + // << std::endl; + } + + // ////////////////////////////////////////////////////////////////////// + DBSession_T& DBSessionManager::getDBSession() const { + if (_dbSession == NULL) { + throw NonInitialisedDBSessionManagerException(); + } + assert (_dbSession != NULL); + return *_dbSession; + } + +} Added: trunk/stdair/stdair/service/DBSessionManager.hpp =================================================================== --- trunk/stdair/stdair/service/DBSessionManager.hpp (rev 0) +++ trunk/stdair/stdair/service/DBSessionManager.hpp 2010-02-06 17:51:39 UTC (rev 115) @@ -0,0 +1,69 @@ +#ifndef __STDAIR_SVC_DBSESSIONMANAGER_HPP +#define __STDAIR_SVC_DBSESSIONMANAGER_HPP + +// ////////////////////////////////////////////////////////////////////// +// Import section +// ////////////////////////////////////////////////////////////////////// +// StdAir +#include <stdair/STDAIR_Types.hpp> + +namespace stdair { + + // Forward declarations + struct BasDBParams; + + /** Class holding the database session. + <br>Note that the database access is handled by the SOCI library. */ + class DBSessionManager { + // Friend classes + friend class FacSupervisor; + friend class STDAIR_Service; + + public: + /** Return the static DBSessionManager instance. */ + static DBSessionManager& instance(); + + /** Retrieve the database session handler, held by the static instance + of DBSessionManager. */ + DBSession_T& getDBSession() const; + + + private: + /** Default constructors are private so that only the required + constructor can be used. */ + DBSessionManager (const BasDBParams&); + /** Default constructor. It must not be used. */ + DBSessionManager (); + /** Default copy constructor. It must not be used. */ + DBSessionManager (const DBSessionManager&); + /** Destructor. */ + ~DBSessionManager (); + + /** Initialise the (MySQL) database connection for the static + DBSessionManager instance. */ + void dbInit (const BasDBParams&); + + /** Close the (MySQL) database connection attached to the static + DBSessionManager instance. */ + void dbFinalise (); + + + private: + /** Initialise the static DBSessionManager instance. + <br>The (MySQL) database connection is initialised. */ + static void init (const BasDBParams&); + + /** Delete the static DBSessionManager instance.*/ + static void clean(); + + + private: + /** Instance object.*/ + static DBSessionManager* _instance; + + /** Database session handler. */ + DBSession_T* _dbSession; + }; + +} +#endif // __STDAIR_SVC_DBSESSIONMANAGER_HPP Modified: trunk/stdair/stdair/service/Logger.cpp =================================================================== --- trunk/stdair/stdair/service/Logger.cpp 2010-02-06 13:33:54 UTC (rev 114) +++ trunk/stdair/stdair/service/Logger.cpp 2010-02-06 17:51:39 UTC (rev 115) @@ -1,8 +1,7 @@ // ////////////////////////////////////////////////////////////////////// // Import section // ////////////////////////////////////////////////////////////////////// -// Stdair Logger -//#include <stdair/factory/FacSupervisor.hpp> +// StdAir Logger #include <stdair/service/Logger.hpp> namespace stdair { @@ -20,7 +19,7 @@ } // ////////////////////////////////////////////////////////////////////// - Logger::Logger (const stdair::BasLogParams& iLogParams) + Logger::Logger (const BasLogParams& iLogParams) : _level (iLogParams._logLevel), _logStream (iLogParams._logStream) { } Modified: trunk/stdair/stdair/service/Logger.hpp =================================================================== --- trunk/stdair/stdair/service/Logger.hpp 2010-02-06 13:33:54 UTC (rev 114) +++ trunk/stdair/stdair/service/Logger.hpp 2010-02-06 17:51:39 UTC (rev 115) @@ -46,6 +46,7 @@ class Logger { // Friend classes friend class FacSupervisor; + friend class STDAIR_Service; public: /** Main log entry. */ @@ -58,9 +59,6 @@ } } - /** Initialise the static Logger instance. */ - static void init (const stdair::BasLogParams&); - /** Return the static Logger instance. */ static Logger& instance(); @@ -68,7 +66,7 @@ private: /** Default constructors are private so that only the required constructor can be used. */ - Logger (const stdair::BasLogParams&); + Logger (const BasLogParams&); /** Default constructor. It must not be used. */ Logger (); /** Default copy constructor. It must not be used. */ @@ -76,6 +74,13 @@ /** Destructor. */ ~Logger (); + // TODO: migrate all the XXXXXX_Service to the new way to initialise + // Logger, and get rid of that 'public:' interface + public: + /** Initialise the static Logger instance. */ + static void init (const BasLogParams&); + + private: /** Delete the static Logger instance.*/ static void clean(); Modified: trunk/stdair/stdair/service/Makefile.am =================================================================== --- trunk/stdair/stdair/service/Makefile.am 2010-02-06 13:33:54 UTC (rev 114) +++ trunk/stdair/stdair/service/Makefile.am 2010-02-06 17:51:39 UTC (rev 115) @@ -6,7 +6,8 @@ noinst_LTLIBRARIES= libsvc.la libsvc_la_SOURCES= $(svc_h_sources) $(svc_cc_sources) -libsvc_la_CXXFLAGS = +libsvc_la_CXXFLAGS = $(SOCI_CFLAGS) $(MYSQL_CFLAGS) +libsvc_la_LDFLAGS = $(SOCI_LIBS) $(MYSQL_LIBS) # Header files pkgincludedir = $(includedir)/stdair/service Modified: trunk/stdair/stdair/service/STDAIR_Service.cpp =================================================================== --- trunk/stdair/stdair/service/STDAIR_Service.cpp 2010-02-06 13:33:54 UTC (rev 114) +++ trunk/stdair/stdair/service/STDAIR_Service.cpp 2010-02-06 17:51:39 UTC (rev 115) @@ -12,6 +12,7 @@ #include <stdair/factory/FacSupervisor.hpp> #include <stdair/factory/FacBomContent.hpp> #include <stdair/service/Logger.hpp> +#include <stdair/service/DBSessionManager.hpp> #include <stdair/STDAIR_Service.hpp> namespace stdair { @@ -29,7 +30,7 @@ } // ////////////////////////////////////////////////////////////////////// - STDAIR_Service::STDAIR_Service (const stdair::BasLogParams& iLogParams) + STDAIR_Service::STDAIR_Service (const BasLogParams& iLogParams) : _bomRoot (FacBomContent::instance().create<BomRoot>()) { // The root of the BOM tree, on which all of the other BOM objects // will be attached, is being created with the STDAIR_Service constructor. @@ -42,6 +43,23 @@ } // ////////////////////////////////////////////////////////////////////// + STDAIR_Service::STDAIR_Service (const BasLogParams& iLogParams, + const BasDBParams& iDBParams) + : _bomRoot (FacBomContent::instance().create<BomRoot>()) { + // The root of the BOM tree, on which all of the other BOM objects + // will be attached, is being created with the STDAIR_Service constructor. + + // Set the log file + logInit (iLogParams); + + // Create a database session + dbInit (iDBParams); + + // Initialise the (remaining of the) context + init (); + } + + // ////////////////////////////////////////////////////////////////////// STDAIR_Service::~STDAIR_Service () { // Delete/Clean all the objects from memory finalise(); @@ -53,6 +71,11 @@ } // ////////////////////////////////////////////////////////////////////// + void STDAIR_Service::dbInit (const BasDBParams& iDBParams) { + DBSessionManager::init (iDBParams); + } + + // ////////////////////////////////////////////////////////////////////// void STDAIR_Service::init () { } Modified: trunk/stdair/stdair/service/sources.mk =================================================================== --- trunk/stdair/stdair/service/sources.mk 2010-02-06 13:33:54 UTC (rev 114) +++ trunk/stdair/stdair/service/sources.mk 2010-02-06 17:51:39 UTC (rev 115) @@ -1,3 +1,7 @@ -svc_h_sources = $(top_srcdir)/stdair/service/Logger.hpp -svc_cc_sources = $(top_srcdir)/stdair/service/Logger.cpp \ - $(top_srcdir)/stdair/service/STDAIR_Service.cpp +svc_h_sources = \ + $(top_srcdir)/stdair/service/Logger.hpp \ + $(top_srcdir)/stdair/service/DBSessionManager.hpp +svc_cc_sources = \ + $(top_srcdir)/stdair/service/Logger.cpp \ + $(top_srcdir)/stdair/service/DBSessionManager.cpp \ + $(top_srcdir)/stdair/service/STDAIR_Service.cpp This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |