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
00011
00012
00013
00018 namespace coral {
00019 class IConnectionService;
00020 class ISchema;
00021 class ISessionProxy;
00022 }
00023
00024 namespace pool {
00025 class IDataSvc;
00026 }
00027
00028 namespace cond{
00029
00030 class DbTransaction;
00031 class DbConnection;
00032 class SessionImpl;
00033
00034
00035
00036 class DbSession{
00037 public:
00038 DbSession();
00039
00040 explicit DbSession( const DbConnection& connection );
00041
00042 DbSession( const DbSession& rhs );
00043
00044 virtual ~DbSession();
00045
00046 DbSession& operator=( const DbSession& rhs );
00047
00048 const std::string& connectionString() const;
00049
00050 const DbConnection& connection() const;
00051
00052 bool isTransactional() const;
00053
00054 const std::string& blobStreamingService() const;
00055
00056 void open( const std::string& connectionString, bool readOnly=false );
00057
00058 void close();
00059
00060 bool isOpen() const;
00061
00062 DbTransaction& transaction();
00063
00064 coral::ISchema& schema(const std::string& schemaName);
00065
00066 coral::ISchema& nominalSchema();
00067
00068
00069
00070 bool deleteMapping( const std::string& mappingVersion );
00071
00072 bool importMapping( const std::string& sourceConnectionString,
00073 const std::string& contName );
00074
00075 ora::Object getObject( const std::string& objectId );
00076
00077 template <typename T> boost::shared_ptr<T> getTypedObject( const std::string& objectId );
00078
00079 template <typename T> std::string storeObject( const T* object, const std::string& containerName );
00080
00081 template <typename T> bool updateObject( const T* object, const std::string& objectId );
00082
00083 bool deleteObject( const std::string& objectId );
00084
00085 std::string importObject( cond::DbSession& fromDatabase, const std::string& objectId );
00086
00087 void flush();
00088
00089 ora::Database& storage();
00090
00091 private:
00092 std::string storeObject( const ora::Object& objectRef, const std::string& containerName );
00093 private:
00094
00095 boost::shared_ptr<SessionImpl> m_implementation;
00096 };
00097
00098 template <typename T> inline boost::shared_ptr<T> DbSession::getTypedObject( const std::string& objectId ){
00099 std::pair<std::string,int> oidData = parseToken( objectId );
00100 ora::Container cont = storage().containerHandle( oidData.first );
00101 return cont.fetch<T>( oidData.second );
00102 }
00103
00104 template <typename T> inline std::string DbSession::storeObject( const T* object,
00105 const std::string& containerName ){
00106 std::string ret("");
00107 if( object ){
00108 ora::OId oid = storage().insert( containerName, *object );
00109 storage().flush();
00110 ora::Container cont = storage().containerHandle( containerName );
00111 int oid0 = cont.id();
00112 ret = writeToken( containerName, oid0, oid.itemId(), cont.className() );
00113 }
00114 return ret;
00115 }
00116
00117 template <typename T> inline bool DbSession::updateObject( const T* object,
00118 const std::string& objectId ){
00119 bool ret = false;
00120 if( object ){
00121 std::pair<std::string,int> oidData = parseToken( objectId );
00122 ora::Container cont = storage().containerHandle( oidData.first );
00123 cont.update( oidData.second, *object );
00124 cont.flush();
00125 ret = true;
00126 }
00127 return ret;
00128 }
00129
00130 }
00131
00132 #endif
00133