CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/CondCore/ORA/interface/Database.h

Go to the documentation of this file.
00001 #ifndef INCLUDE_ORA_DATABASE_H
00002 #define INCLUDE_ORA_DATABASE_H
00003 
00004 #include "Object.h"
00005 #include "OId.h"
00006 #include "Container.h"
00007 #include "Configuration.h"
00008 #include "DatabaseUtility.h"
00009 #include "Exception.h"
00010 //
00011 #include <set>
00012 #include <vector>
00013 #include <memory>
00014 #include <boost/shared_ptr.hpp>
00015 
00016 namespace ora {
00017 
00018   class ConnectionPool;
00019   class Transaction;
00020   class DatabaseImpl;
00021   class SharedSession;
00022 
00023   class Database {
00024     public:
00025 
00026     // 
00027     static std::string nameForContainer( const std::type_info& typeInfo );
00028     //
00029     static std::string nameForContainer( const std::string& className );
00030 
00031     public:
00032 
00033     // 
00034     Database();
00035 
00036     //
00037     Database( const Database& rhs );
00038     
00039     // 
00040     Database(boost::shared_ptr<ConnectionPool>& connectionPool);
00041     
00043     virtual ~Database();
00044 
00046     Database& operator=( const Database& rhs );
00047 
00049     Configuration& configuration();
00050 
00052     bool connect( const std::string& connectionString, bool readOnly=false );
00053     
00055     void disconnect();
00056 
00058     bool isConnected();
00059 
00061     const std::string& connectionString();
00062 
00064     Transaction& transaction();
00065 
00067     bool exists();
00068 
00070     bool create();
00071 
00073     bool drop();
00074     
00076     std::set< std::string > containers();
00077 
00079     template <typename T> Container createContainer( const std::string& name );
00080     
00082     template <typename T> Container createContainer();
00083 
00085     Container createContainer( const std::string& className, std::string name="" );
00086 
00088     bool dropContainer( const std::string& name );
00089 
00091     Container containerHandle( const std::string& name );
00092 
00094     Container containerHandle( int contId );
00095 
00097     Object fetchItem(const OId& oid);
00098 
00100     template <typename T> boost::shared_ptr<T> fetch( const OId& oid);
00101 
00103     OId insertItem( const std::string& containerName, const Object& data );
00104 
00106     template <typename T> OId insert( const std::string& containerName, const T& data);
00107 
00109     template <typename T> OId insert( const T& data);
00110 
00112     void updateItem(const OId& oid, const Object& data );
00113 
00115     template <typename T> void update( const OId& oid, const T& data );
00116 
00118     void erase(const OId& oid);
00119 
00121     void flush();
00122 
00124     void setObjectName( const std::string& name, const OId& oid );
00125    
00127     bool eraseObjectName( const std::string& name );
00128 
00130     bool eraseAllNames();
00131 
00133     bool getItemId( const std::string& name, OId& destination );
00134 
00136     Object fetchItemByName( const std::string& name );
00137 
00139     template <typename T> boost::shared_ptr<T> fetchByName( const std::string& name );
00140 
00142     bool getNamesForObject( const OId& oid, std::vector<std::string>& destination );
00143 
00145     bool listObjectNames( std::vector<std::string>& destination );
00146 
00148     DatabaseUtility utility();
00149 
00150     public:
00151 
00153     SharedSession& storageAccessSession();
00154 
00155     private:
00156 
00158     void open( bool writingAccess=false );
00159 
00161     void checkTransaction();
00162 
00164     Container createContainer( const std::string& name, const std::type_info& typeInfo );
00165     
00167     Container createContainer( const std::type_info& typeInfo );
00168 
00170     Container getContainer( const std::string& name, const std::type_info& typeInfo );
00171 
00173     Container getContainer( const std::type_info& typeInfo );
00174 
00176     boost::shared_ptr<void> getTypedObjectByName( const std::string& name, const std::type_info& typeInfo );    
00177 
00178     private:
00179 
00180     boost::shared_ptr<DatabaseImpl> m_impl;
00181     
00182   };
00183 
00184 }
00185 
00186 template <typename T>
00187 inline
00188 ora::Container ora::Database::createContainer( const std::string& name ){
00189   return createContainer( name, typeid(T) );
00190 }
00191 
00192 template <typename T>
00193 inline
00194 ora::Container ora::Database::createContainer(){
00195   return createContainer( typeid(T) );
00196 }
00197 
00198 template <typename T>
00199 inline
00200 boost::shared_ptr<T> ora::Database::fetch( const ora::OId& oid){
00201   Container cont = containerHandle( oid.containerId() );
00202   return cont.fetch<T>( oid.itemId() );
00203 }
00204 
00205 template <typename T>
00206 inline
00207 ora::OId ora::Database::insert(const std::string& containerName,
00208                                const T& data){
00209   Container cont = getContainer( containerName, typeid(data) );
00210   int itemId = cont.insert( data );
00211   return OId( cont.id(), itemId );
00212 }
00213 
00214 template <typename T>
00215 inline
00216 ora::OId ora::Database::insert( const T& data){
00217   Container cont = getContainer( typeid(data) );
00218   int itemId =  cont.insert( data );
00219   return OId( cont.id(), itemId );  
00220 }
00221 
00222 template <typename T>
00223 inline
00224 void ora::Database::update( const ora::OId& oid,
00225                             const T& data ){
00226   Container cont = containerHandle( oid.containerId() );
00227   cont.update( oid.itemId(), data );
00228 }
00229 
00230 template <typename T>
00231 inline
00232 boost::shared_ptr<T> ora::Database::fetchByName( const std::string& name ){
00233   return boost::static_pointer_cast<T>( getTypedObjectByName( name, typeid(T) ) );
00234 }
00235 
00236 
00237 #endif