CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Database.h
Go to the documentation of this file.
1 #ifndef INCLUDE_ORA_DATABASE_H
2 #define INCLUDE_ORA_DATABASE_H
3 
4 #include "Object.h"
5 #include "OId.h"
6 #include "Version.h"
7 #include "Container.h"
8 #include "Configuration.h"
9 #include "DatabaseUtility.h"
10 #include "Exception.h"
11 //
12 #include <set>
13 #include <vector>
14 #include <memory>
15 #include <boost/shared_ptr.hpp>
16 
17 namespace ora {
18 
19  class ConnectionPool;
20  class Transaction;
21  class DatabaseImpl;
22  class SharedSession;
23 
24  class Database {
25  public:
26 
27  //
28  static std::string nameForContainer( const std::type_info& typeInfo );
29  //
30  static std::string nameForContainer( const std::string& className );
31 
32  public:
33 
34  //
35  Database();
36 
37  //
38  Database( const Database& rhs );
39 
40  //
41  Database(boost::shared_ptr<ConnectionPool>& connectionPool);
42 
44  virtual ~Database();
45 
47  Database& operator=( const Database& rhs );
48 
51 
53  ora::Version schemaVersion( bool userSchema=false );
54 
56  bool connect( const std::string& connectionString, bool readOnly=false );
57 
59  bool connect( const std::string& connectionString, const std::string& asRole, bool readOnly=false );
60 
62  void disconnect();
63 
65  bool isConnected();
66 
68  const std::string& connectionString();
69 
72 
74  bool exists();
75 
77  bool create( std::string userSchemaVersion = std::string("") );
78 
80  bool drop();
81 
83  void setAccessPermission( const std::string& principal, bool forWrite );
84 
86  std::set< std::string > containers();
87 
89  template <typename T> Container createContainer( const std::string& name );
90 
92  template <typename T> Container createContainer();
93 
95  Container createContainer( const std::string& className, std::string name="" );
96 
98  bool dropContainer( const std::string& name );
99 
101  bool lockContainer( const std::string& name );
102 
104  Container containerHandle( const std::string& name );
105 
107  Container containerHandle( int contId );
108 
110  Object fetchItem(const OId& oid);
111 
113  template <typename T> boost::shared_ptr<T> fetch( const OId& oid);
114 
116  OId insertItem( const std::string& containerName, const Object& data );
117 
119  template <typename T> OId insert( const std::string& containerName, const T& data);
120 
122  template <typename T> OId insert( const T& data);
123 
125  void updateItem(const OId& oid, const Object& data );
126 
128  template <typename T> void update( const OId& oid, const T& data );
129 
131  void erase(const OId& oid);
132 
134  void flush();
135 
137  void setObjectName( const std::string& name, const OId& oid );
138 
140  bool eraseObjectName( const std::string& name );
141 
143  bool eraseAllNames();
144 
146  bool getItemId( const std::string& name, OId& destination );
147 
149  Object fetchItemByName( const std::string& name );
150 
152  template <typename T> boost::shared_ptr<T> fetchByName( const std::string& name );
153 
155  bool getNamesForObject( const OId& oid, std::vector<std::string>& destination );
156 
158  bool listObjectNames( std::vector<std::string>& destination );
159 
162 
163  public:
164 
167 
168  private:
169 
171  void open( bool writingAccess=false );
172 
174  void checkTransaction();
175 
177  Container createContainer( const std::string& name, const std::type_info& typeInfo );
178 
180  Container createContainer( const std::type_info& typeInfo );
181 
183  Container getContainer( const std::string& name, const std::type_info& typeInfo );
184 
186  Container getContainer( const std::type_info& typeInfo );
187 
189  boost::shared_ptr<void> getTypedObjectByName( const std::string& name, const std::type_info& typeInfo );
190 
191  private:
192 
193  boost::shared_ptr<DatabaseImpl> m_impl;
194 
195  };
196 
197 }
198 
199 template <typename T>
200 inline
202  return createContainer( name, typeid(T) );
203 }
204 
205 template <typename T>
206 inline
208  return createContainer( typeid(T) );
209 }
210 
211 template <typename T>
212 inline
213 boost::shared_ptr<T> ora::Database::fetch( const ora::OId& oid){
214  Container cont = containerHandle( oid.containerId() );
215  return cont.fetch<T>( oid.itemId() );
216 }
217 
218 template <typename T>
219 inline
220 ora::OId ora::Database::insert(const std::string& containerName,
221  const T& data){
222  Container cont = getContainer( containerName, typeid(data) );
223  int itemId = cont.insert( data );
224  return OId( cont.id(), itemId );
225 }
226 
227 template <typename T>
228 inline
230  Container cont = getContainer( typeid(data) );
231  int itemId = cont.insert( data );
232  return OId( cont.id(), itemId );
233 }
234 
235 template <typename T>
236 inline
238  const T& data ){
239  Container cont = containerHandle( oid.containerId() );
240  cont.update( oid.itemId(), data );
241 }
242 
243 template <typename T>
244 inline
245 boost::shared_ptr<T> ora::Database::fetchByName( const std::string& name ){
246  return boost::static_pointer_cast<T>( getTypedObjectByName( name, typeid(T) ) );
247 }
248 
249 
250 #endif
bool create(std::string userSchemaVersion=std::string(""))
Definition: Database.cc:135
int insert(const T &data)
Definition: Container.h:133
void flush()
Definition: Database.cc:320
Object fetchItemByName(const std::string &name)
Definition: Database.cc:355
OId insert(const std::string &containerName, const T &data)
const std::string & connectionString()
Definition: Database.cc:110
void checkTransaction()
Definition: Database.cc:121
bool getItemId(const std::string &name, OId &destination)
Definition: Database.cc:344
Definition: OId.h:8
virtual ~Database()
Definition: Database.cc:78
ora::Version schemaVersion(bool userSchema=false)
Definition: Database.cc:183
void open(bool writingAccess=false)
Definition: Database.cc:171
bool lockContainer(const std::string &name)
Definition: Database.cc:262
Transaction & transaction()
Definition: Database.cc:114
SharedSession & storageAccessSession()
Definition: Database.cc:383
void disconnect()
Definition: Database.cc:102
void erase(const OId &oid)
Definition: Database.cc:314
Object fetchItem(const OId &oid)
Definition: Database.cc:294
Container containerHandle(const std::string &name)
Definition: Database.cc:272
boost::shared_ptr< T > fetchByName(const std::string &name)
Definition: Database.h:245
bool exists()
Definition: Database.cc:130
std::set< std::string > containers()
Definition: Database.cc:191
int containerId() const
Definition: OId.cc:50
void update(int itemId, const T &data)
Definition: Container.h:139
boost::shared_ptr< T > fetch(const OId &oid)
Definition: Database.h:213
bool dropContainer(const std::string &name)
Definition: Database.cc:253
Container createContainer()
boost::shared_ptr< DatabaseImpl > m_impl
Definition: Database.h:193
DatabaseUtility utility()
Definition: Database.cc:377
bool getNamesForObject(const OId &oid, std::vector< std::string > &destination)
Definition: Database.cc:360
OId insertItem(const std::string &containerName, const Object &data)
Definition: Database.cc:299
Database & operator=(const Database &rhs)
Definition: Database.cc:82
bool connect(const std::string &connectionString, bool readOnly=false)
Definition: Database.cc:91
bool eraseObjectName(const std::string &name)
Definition: Database.cc:334
int cont
bool drop()
Definition: Database.cc:144
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
static std::string nameForContainer(const std::type_info &typeInfo)
Definition: Database.cc:57
void update(const OId &oid, const T &data)
Definition: Database.h:237
boost::shared_ptr< T > fetch(int itemId)
Definition: Container.h:127
bool isConnected()
Definition: Database.cc:106
bool eraseAllNames()
Definition: Database.cc:339
bool listObjectNames(std::vector< std::string > &destination)
Definition: Database.cc:369
boost::shared_ptr< void > getTypedObjectByName(const std::string &name, const std::type_info &typeInfo)
Definition: Database.cc:349
Configuration & configuration()
Definition: Database.cc:87
long double T
void updateItem(const OId &oid, const Object &data)
Definition: Database.cc:307
void setObjectName(const std::string &name, const OId &oid)
Definition: Database.cc:329
std::string className(const T &t)
Definition: ClassName.h:30
Container getContainer(const std::string &name, const std::type_info &typeInfo)
Definition: Database.cc:239
void setAccessPermission(const std::string &principal, bool forWrite)
Definition: Database.cc:159
int itemId() const
Definition: OId.cc:54