CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/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 "Version.h"
00007 #include "Container.h"
00008 #include "Configuration.h"
00009 #include "DatabaseUtility.h"
00010 #include "Exception.h"
00011 //
00012 #include <set>
00013 #include <vector>
00014 #include <memory>
00015 #include <boost/shared_ptr.hpp>
00016 
00017 namespace ora {
00018 
00019   class ConnectionPool;
00020   class Transaction;
00021   class DatabaseImpl;
00022   class SharedSession;
00023 
00024   class Database {
00025     public:
00026 
00027     // 
00028     static std::string nameForContainer( const std::type_info& typeInfo );
00029     //
00030     static std::string nameForContainer( const std::string& className );
00031 
00032     public:
00033 
00034     // 
00035     Database();
00036 
00037     //
00038     Database( const Database& rhs );
00039     
00040     // 
00041     Database(boost::shared_ptr<ConnectionPool>& connectionPool);
00042     
00044     virtual ~Database();
00045 
00047     Database& operator=( const Database& rhs );
00048 
00050     Configuration& configuration();
00051 
00053     ora::Version schemaVersion( bool userSchema=false );
00054 
00056     bool connect( const std::string& connectionString, bool readOnly=false );
00057     
00059     bool connect( const std::string& connectionString, const std::string& asRole, bool readOnly=false );
00060 
00062     void disconnect();
00063 
00065     bool isConnected();
00066 
00068     const std::string& connectionString();
00069 
00071     Transaction& transaction();
00072 
00074     bool exists();
00075 
00077     bool create( std::string userSchemaVersion = std::string("") );
00078 
00080     bool drop();
00081 
00083     void setAccessPermission( const std::string& principal, bool forWrite );
00084     
00086     std::set< std::string > containers();
00087 
00089     template <typename T> Container createContainer( const std::string& name );
00090     
00092     template <typename T> Container createContainer();
00093 
00095     Container createContainer( const std::string& className, std::string name="" );
00096 
00098     bool dropContainer( const std::string& name );
00099 
00101     bool lockContainer( const std::string& name );
00102 
00104     Container containerHandle( const std::string& name );
00105 
00107     Container containerHandle( int contId );
00108 
00110     Object fetchItem(const OId& oid);
00111 
00113     template <typename T> boost::shared_ptr<T> fetch( const OId& oid);
00114 
00116     OId insertItem( const std::string& containerName, const Object& data );
00117 
00119     template <typename T> OId insert( const std::string& containerName, const T& data);
00120 
00122     template <typename T> OId insert( const T& data);
00123 
00125     void updateItem(const OId& oid, const Object& data );
00126 
00128     template <typename T> void update( const OId& oid, const T& data );
00129 
00131     void erase(const OId& oid);
00132 
00134     void flush();
00135 
00137     void setObjectName( const std::string& name, const OId& oid );
00138    
00140     bool eraseObjectName( const std::string& name );
00141 
00143     bool eraseAllNames();
00144 
00146     bool getItemId( const std::string& name, OId& destination );
00147 
00149     Object fetchItemByName( const std::string& name );
00150 
00152     template <typename T> boost::shared_ptr<T> fetchByName( const std::string& name );
00153 
00155     bool getNamesForObject( const OId& oid, std::vector<std::string>& destination );
00156 
00158     bool listObjectNames( std::vector<std::string>& destination );
00159 
00161     DatabaseUtility utility();
00162 
00163     public:
00164 
00166     SharedSession& storageAccessSession();
00167 
00168     private:
00169 
00171     void open( bool writingAccess=false );
00172 
00174     void checkTransaction();
00175 
00177     Container createContainer( const std::string& name, const std::type_info& typeInfo );
00178     
00180     Container createContainer( const std::type_info& typeInfo );
00181 
00183     Container getContainer( const std::string& name, const std::type_info& typeInfo );
00184 
00186     Container getContainer( const std::type_info& typeInfo );
00187 
00189     boost::shared_ptr<void> getTypedObjectByName( const std::string& name, const std::type_info& typeInfo );    
00190 
00191     private:
00192 
00193     boost::shared_ptr<DatabaseImpl> m_impl;
00194     
00195   };
00196 
00197 }
00198 
00199 template <typename T>
00200 inline
00201 ora::Container ora::Database::createContainer( const std::string& name ){
00202   return createContainer( name, typeid(T) );
00203 }
00204 
00205 template <typename T>
00206 inline
00207 ora::Container ora::Database::createContainer(){
00208   return createContainer( typeid(T) );
00209 }
00210 
00211 template <typename T>
00212 inline
00213 boost::shared_ptr<T> ora::Database::fetch( const ora::OId& oid){
00214   Container cont = containerHandle( oid.containerId() );
00215   return cont.fetch<T>( oid.itemId() );
00216 }
00217 
00218 template <typename T>
00219 inline
00220 ora::OId ora::Database::insert(const std::string& containerName,
00221                                const T& data){
00222   Container cont = getContainer( containerName, typeid(data) );
00223   int itemId = cont.insert( data );
00224   return OId( cont.id(), itemId );
00225 }
00226 
00227 template <typename T>
00228 inline
00229 ora::OId ora::Database::insert( const T& data){
00230   Container cont = getContainer( typeid(data) );
00231   int itemId =  cont.insert( data );
00232   return OId( cont.id(), itemId );  
00233 }
00234 
00235 template <typename T>
00236 inline
00237 void ora::Database::update( const ora::OId& oid,
00238                             const T& data ){
00239   Container cont = containerHandle( oid.containerId() );
00240   cont.update( oid.itemId(), data );
00241 }
00242 
00243 template <typename T>
00244 inline
00245 boost::shared_ptr<T> ora::Database::fetchByName( const std::string& name ){
00246   return boost::static_pointer_cast<T>( getTypedObjectByName( name, typeid(T) ) );
00247 }
00248 
00249 
00250 #endif