CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/CondCore/DBCommon/interface/DbSession.h

Go to the documentation of this file.
00001 #ifndef COND_DBCommon_DbSession_h
00002 #define COND_DBCommon_DbSession_h
00003 
00004 #include "CondCore/ORA/interface/Database.h"
00005 #include "CondCore/ORA/interface/PoolToken.h"
00006 #include <string>
00007 #include <boost/shared_ptr.hpp>
00008 
00009 //
00010 // Package:    CondCore/DBCommon
00011 // Class:      DbSession
00012 //
00017 namespace coral {
00018   class IConnectionService;
00019   class ISchema;
00020   class ISessionProxy;
00021 }
00022 
00023 namespace cond{
00024 
00025   class DbTransaction;
00026   class DbConnection;
00027   class SessionImpl;
00028 
00029   /*
00030   **/
00031   class DbSession{
00032   public:
00033   static const char* COND_SCHEMA_VERSION;   
00034   static const char* CHANGE_SCHEMA_VERSION;
00035 
00036   public:
00037     DbSession();
00038 
00039     explicit DbSession( const DbConnection& connection );
00040 
00041     DbSession( const DbSession& rhs );
00042 
00043     virtual ~DbSession();
00044 
00045     DbSession& operator=( const DbSession& rhs );
00046 
00047     const std::string& connectionString() const;
00048 
00049     const DbConnection& connection() const;
00050 
00051     bool isTransactional() const;
00052 
00053     const std::string& blobStreamingService() const;
00054 
00055     void open( const std::string& connectionString, bool readOnly=false );
00056     void open( const std::string& connectionString, const std::string&  role, bool readOnly=false );
00057 
00058     void close();
00059 
00060     bool isOpen() const;
00061 
00062     DbTransaction& transaction();
00063 
00064     bool createDatabase();
00065 
00066     // TEMPORARY, for the IOV schema changeover.
00067     bool isOldSchema();
00068 
00069     coral::ISchema& schema(const std::string& schemaName);
00070 
00071     coral::ISchema& nominalSchema();
00072 
00073     bool deleteMapping( const std::string& mappingVersion );
00074 
00075     bool importMapping( const std::string& sourceConnectionString,
00076                         const std::string& contName );
00077 
00078     ora::Object getObject( const std::string& objectId );
00079 
00080     template <typename T> boost::shared_ptr<T> getTypedObject( const std::string& objectId );
00081 
00082     template <typename T> std::string storeObject( const T* object, const std::string& containerName );
00083 
00084     template <typename T> bool updateObject( const T* object, const std::string& objectId );
00085 
00086     bool deleteObject( const std::string& objectId );
00087 
00088     std::string importObject( cond::DbSession& fromDatabase, const std::string& objectId );
00089 
00090     std::string classNameForItem( const std::string& objectId );
00091 
00092     void flush();
00093     
00094     ora::Database& storage();
00095     
00096     private:
00097     std::string storeObject( const ora::Object& objectRef, const std::string& containerName  );
00098     private:
00099 
00100     boost::shared_ptr<SessionImpl> m_implementation;
00101   };
00102 
00103   class PoolTokenParser : public ora::ITokenParser {
00104     public:
00105     explicit PoolTokenParser( ora::Database& db );
00106     ~PoolTokenParser(){
00107     }
00108     ora::OId parse( const std::string& poolToken );
00109     std::string className( const std::string& poolToken );
00110 
00111     private:
00112     ora::Database& m_db;
00113   };
00114 
00115   class PoolTokenWriter : public ora::ITokenWriter {
00116     public:
00117     explicit PoolTokenWriter( ora::Database& db );
00118     ~PoolTokenWriter(){
00119     }
00120     std::string write( const ora::OId& oid );
00121     private:
00122     ora::Database& m_db;
00123   };
00124 
00125   template <typename T> inline boost::shared_ptr<T> DbSession::getTypedObject( const std::string& objectId ){
00126     ora::OId oid;
00127     oid.fromString( objectId );
00128     return storage().fetch<T>( oid );
00129   }
00130 
00131   template <typename T> inline std::string DbSession::storeObject( const T* object,
00132                                                                    const std::string& containerName ){
00133     std::string ret("");
00134     if( object ){
00135       ora::OId oid = storage().insert( containerName, *object );
00136       storage().flush();
00137       ret =  oid.toString();
00138     }
00139     return ret;
00140   }
00141 
00142   template <typename T> inline bool DbSession::updateObject( const T* object,
00143                                                              const std::string& objectId ){
00144     bool ret = false;
00145     if( object ){
00146       ora::OId oid;
00147       oid.fromString( objectId );
00148       storage().update( oid, *object );
00149       storage().flush();
00150       ret =  true;
00151     }
00152     return ret;
00153   }
00154  
00155 }
00156 
00157 #endif
00158 // DBSESSION_H