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
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 static const std::string CONDITIONS_GENERAL_WRITER;
00036 static const std::string CONDITIONS_GENERAL_READER;
00037
00038 public:
00039 DbSession();
00040
00041 explicit DbSession( const DbConnection& connection );
00042
00043 DbSession( const DbSession& rhs );
00044
00045 virtual ~DbSession();
00046
00047 DbSession& operator=( const DbSession& rhs );
00048
00049 const std::string& connectionString() const;
00050
00051 const DbConnection& connection() const;
00052
00053 bool isTransactional() const;
00054
00055 const std::string& blobStreamingService() const;
00056
00057 void open( const std::string& connectionString, bool readOnly=false );
00058
00059 void close();
00060
00061 bool isOpen() const;
00062
00063 DbTransaction& transaction();
00064
00065 bool createDatabase();
00066
00067
00068 bool isOldSchema();
00069
00070 coral::ISchema& schema(const std::string& schemaName);
00071
00072 coral::ISchema& nominalSchema();
00073
00074 bool deleteMapping( const std::string& mappingVersion );
00075
00076 bool importMapping( const std::string& sourceConnectionString,
00077 const std::string& contName );
00078
00079 ora::Object getObject( const std::string& objectId );
00080
00081 template <typename T> boost::shared_ptr<T> getTypedObject( const std::string& objectId );
00082
00083 template <typename T> std::string storeObject( const T* object, const std::string& containerName );
00084
00085 template <typename T> bool updateObject( const T* object, const std::string& objectId );
00086
00087 bool deleteObject( const std::string& objectId );
00088
00089 std::string importObject( cond::DbSession& fromDatabase, const std::string& objectId );
00090
00091 std::string classNameForItem( const std::string& objectId );
00092
00093 void flush();
00094
00095 ora::Database& storage();
00096
00097 private:
00098 std::string storeObject( const ora::Object& objectRef, const std::string& containerName );
00099 private:
00100
00101 boost::shared_ptr<SessionImpl> m_implementation;
00102 };
00103
00104 class PoolTokenParser : public ora::ITokenParser {
00105 public:
00106 explicit PoolTokenParser( ora::Database& db );
00107 ~PoolTokenParser(){
00108 }
00109 ora::OId parse( const std::string& poolToken );
00110 std::string className( const std::string& poolToken );
00111
00112 private:
00113 ora::Database& m_db;
00114 };
00115
00116 class PoolTokenWriter : public ora::ITokenWriter {
00117 public:
00118 explicit PoolTokenWriter( ora::Database& db );
00119 ~PoolTokenWriter(){
00120 }
00121 std::string write( const ora::OId& oid );
00122 private:
00123 ora::Database& m_db;
00124 };
00125
00126 template <typename T> inline boost::shared_ptr<T> DbSession::getTypedObject( const std::string& objectId ){
00127 ora::OId oid;
00128 oid.fromString( objectId );
00129 return storage().fetch<T>( oid );
00130 }
00131
00132 template <typename T> inline std::string DbSession::storeObject( const T* object,
00133 const std::string& containerName ){
00134 std::string ret("");
00135 if( object ){
00136 ora::OId oid = storage().insert( containerName, *object );
00137 storage().flush();
00138 ret = oid.toString();
00139 }
00140 return ret;
00141 }
00142
00143 template <typename T> inline bool DbSession::updateObject( const T* object,
00144 const std::string& objectId ){
00145 bool ret = false;
00146 if( object ){
00147 ora::OId oid;
00148 oid.fromString( objectId );
00149 storage().update( oid, *object );
00150 storage().flush();
00151 ret = true;
00152 }
00153 return ret;
00154 }
00155
00156 }
00157
00158 #endif
00159