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 void disconnect();
00060
00062 bool isConnected();
00063
00065 const std::string& connectionString();
00066
00068 Transaction& transaction();
00069
00071 bool exists();
00072
00074 bool create( std::string userSchemaVersion = std::string("") );
00075
00077 bool drop();
00078
00080 void setAccessPermission( const std::string& principal, bool forWrite );
00081
00083 std::set< std::string > containers();
00084
00086 template <typename T> Container createContainer( const std::string& name );
00087
00089 template <typename T> Container createContainer();
00090
00092 Container createContainer( const std::string& className, std::string name="" );
00093
00095 bool dropContainer( const std::string& name );
00096
00098 bool lockContainer( const std::string& name );
00099
00101 Container containerHandle( const std::string& name );
00102
00104 Container containerHandle( int contId );
00105
00107 Object fetchItem(const OId& oid);
00108
00110 template <typename T> boost::shared_ptr<T> fetch( const OId& oid);
00111
00113 OId insertItem( const std::string& containerName, const Object& data );
00114
00116 template <typename T> OId insert( const std::string& containerName, const T& data);
00117
00119 template <typename T> OId insert( const T& data);
00120
00122 void updateItem(const OId& oid, const Object& data );
00123
00125 template <typename T> void update( const OId& oid, const T& data );
00126
00128 void erase(const OId& oid);
00129
00131 void flush();
00132
00134 void setObjectName( const std::string& name, const OId& oid );
00135
00137 bool eraseObjectName( const std::string& name );
00138
00140 bool eraseAllNames();
00141
00143 bool getItemId( const std::string& name, OId& destination );
00144
00146 Object fetchItemByName( const std::string& name );
00147
00149 template <typename T> boost::shared_ptr<T> fetchByName( const std::string& name );
00150
00152 bool getNamesForObject( const OId& oid, std::vector<std::string>& destination );
00153
00155 bool listObjectNames( std::vector<std::string>& destination );
00156
00158 DatabaseUtility utility();
00159
00160 public:
00161
00163 SharedSession& storageAccessSession();
00164
00165 private:
00166
00168 void open( bool writingAccess=false );
00169
00171 void checkTransaction();
00172
00174 Container createContainer( const std::string& name, const std::type_info& typeInfo );
00175
00177 Container createContainer( const std::type_info& typeInfo );
00178
00180 Container getContainer( const std::string& name, const std::type_info& typeInfo );
00181
00183 Container getContainer( const std::type_info& typeInfo );
00184
00186 boost::shared_ptr<void> getTypedObjectByName( const std::string& name, const std::type_info& typeInfo );
00187
00188 private:
00189
00190 boost::shared_ptr<DatabaseImpl> m_impl;
00191
00192 };
00193
00194 }
00195
00196 template <typename T>
00197 inline
00198 ora::Container ora::Database::createContainer( const std::string& name ){
00199 return createContainer( name, typeid(T) );
00200 }
00201
00202 template <typename T>
00203 inline
00204 ora::Container ora::Database::createContainer(){
00205 return createContainer( typeid(T) );
00206 }
00207
00208 template <typename T>
00209 inline
00210 boost::shared_ptr<T> ora::Database::fetch( const ora::OId& oid){
00211 Container cont = containerHandle( oid.containerId() );
00212 return cont.fetch<T>( oid.itemId() );
00213 }
00214
00215 template <typename T>
00216 inline
00217 ora::OId ora::Database::insert(const std::string& containerName,
00218 const T& data){
00219 Container cont = getContainer( containerName, typeid(data) );
00220 int itemId = cont.insert( data );
00221 return OId( cont.id(), itemId );
00222 }
00223
00224 template <typename T>
00225 inline
00226 ora::OId ora::Database::insert( const T& data){
00227 Container cont = getContainer( typeid(data) );
00228 int itemId = cont.insert( data );
00229 return OId( cont.id(), itemId );
00230 }
00231
00232 template <typename T>
00233 inline
00234 void ora::Database::update( const ora::OId& oid,
00235 const T& data ){
00236 Container cont = containerHandle( oid.containerId() );
00237 cont.update( oid.itemId(), data );
00238 }
00239
00240 template <typename T>
00241 inline
00242 boost::shared_ptr<T> ora::Database::fetchByName( const std::string& name ){
00243 return boost::static_pointer_cast<T>( getTypedObjectByName( name, typeid(T) ) );
00244 }
00245
00246
00247 #endif