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 "Container.h"
7 #include "Configuration.h"
8 #include "DatabaseUtility.h"
9 #include "Exception.h"
10 //
11 #include <set>
12 #include <vector>
13 #include <memory>
14 #include <boost/shared_ptr.hpp>
15 
16 namespace ora {
17 
18  class ConnectionPool;
19  class Transaction;
20  class DatabaseImpl;
21  class SharedSession;
22 
23  class Database {
24  public:
25 
26  //
27  static std::string nameForContainer( const std::type_info& typeInfo );
28  //
29  static std::string nameForContainer( const std::string& className );
30 
31  public:
32 
33  //
34  Database();
35 
36  //
37  Database( const Database& rhs );
38 
39  //
40  Database(boost::shared_ptr<ConnectionPool>& connectionPool);
41 
43  virtual ~Database();
44 
46  Database& operator=( const Database& rhs );
47 
50 
52  bool connect( const std::string& connectionString, bool readOnly=false );
53 
55  void disconnect();
56 
58  bool isConnected();
59 
61  const std::string& connectionString();
62 
65 
67  bool exists();
68 
70  bool create();
71 
73  bool drop();
74 
76  std::set< std::string > containers();
77 
79  template <typename T> Container createContainer( const std::string& name );
80 
82  template <typename T> Container createContainer();
83 
85  Container createContainer( const std::string& className, std::string name="" );
86 
88  bool dropContainer( const std::string& name );
89 
91  Container containerHandle( const std::string& name );
92 
94  Container containerHandle( int contId );
95 
97  Object fetchItem(const OId& oid);
98 
100  template <typename T> boost::shared_ptr<T> fetch( const OId& oid);
101 
103  OId insertItem( const std::string& containerName, const Object& data );
104 
106  template <typename T> OId insert( const std::string& containerName, const T& data);
107 
109  template <typename T> OId insert( const T& data);
110 
112  void updateItem(const OId& oid, const Object& data );
113 
115  template <typename T> void update( const OId& oid, const T& data );
116 
118  void erase(const OId& oid);
119 
121  void flush();
122 
124  void setObjectName( const std::string& name, const OId& oid );
125 
127  bool eraseObjectName( const std::string& name );
128 
130  bool eraseAllNames();
131 
133  bool getItemId( const std::string& name, OId& destination );
134 
136  Object fetchItemByName( const std::string& name );
137 
139  template <typename T> boost::shared_ptr<T> fetchByName( const std::string& name );
140 
142  bool getNamesForObject( const OId& oid, std::vector<std::string>& destination );
143 
145  bool listObjectNames( std::vector<std::string>& destination );
146 
149 
150  public:
151 
154 
155  private:
156 
158  void open( bool writingAccess=false );
159 
161  void checkTransaction();
162 
164  Container createContainer( const std::string& name, const std::type_info& typeInfo );
165 
167  Container createContainer( const std::type_info& typeInfo );
168 
170  Container getContainer( const std::string& name, const std::type_info& typeInfo );
171 
173  Container getContainer( const std::type_info& typeInfo );
174 
176  boost::shared_ptr<void> getTypedObjectByName( const std::string& name, const std::type_info& typeInfo );
177 
178  private:
179 
180  boost::shared_ptr<DatabaseImpl> m_impl;
181 
182  };
183 
184 }
185 
186 template <typename T>
187 inline
189  return createContainer( name, typeid(T) );
190 }
191 
192 template <typename T>
193 inline
195  return createContainer( typeid(T) );
196 }
197 
198 template <typename T>
199 inline
200 boost::shared_ptr<T> ora::Database::fetch( const ora::OId& oid){
201  Container cont = containerHandle( oid.containerId() );
202  return cont.fetch<T>( oid.itemId() );
203 }
204 
205 template <typename T>
206 inline
207 ora::OId ora::Database::insert(const std::string& containerName,
208  const T& data){
209  Container cont = getContainer( containerName, typeid(data) );
210  int itemId = cont.insert( data );
211  return OId( cont.id(), itemId );
212 }
213 
214 template <typename T>
215 inline
217  Container cont = getContainer( typeid(data) );
218  int itemId = cont.insert( data );
219  return OId( cont.id(), itemId );
220 }
221 
222 template <typename T>
223 inline
225  const T& data ){
226  Container cont = containerHandle( oid.containerId() );
227  cont.update( oid.itemId(), data );
228 }
229 
230 template <typename T>
231 inline
232 boost::shared_ptr<T> ora::Database::fetchByName( const std::string& name ){
233  return boost::static_pointer_cast<T>( getTypedObjectByName( name, typeid(T) ) );
234 }
235 
236 
237 #endif
int insert(const T &data)
Definition: Container.h:123
void flush()
Definition: Database.cc:284
Object fetchItemByName(const std::string &name)
Definition: Database.cc:319
OId insert(const std::string &containerName, const T &data)
const std::string & connectionString()
Definition: Database.cc:104
void checkTransaction()
Definition: Database.cc:115
bool getItemId(const std::string &name, OId &destination)
Definition: Database.cc:308
Definition: OId.h:8
virtual ~Database()
Definition: Database.cc:78
void open(bool writingAccess=false)
Definition: Database.cc:153
Transaction & transaction()
Definition: Database.cc:108
SharedSession & storageAccessSession()
Definition: Database.cc:341
void disconnect()
Definition: Database.cc:96
void erase(const OId &oid)
Definition: Database.cc:278
Object fetchItem(const OId &oid)
Definition: Database.cc:258
Container containerHandle(const std::string &name)
Definition: Database.cc:236
boost::shared_ptr< T > fetchByName(const std::string &name)
Definition: Database.h:232
bool exists()
Definition: Database.cc:124
std::set< std::string > containers()
Definition: Database.cc:165
int containerId() const
Definition: OId.cc:40
void update(int itemId, const T &data)
Definition: Container.h:129
boost::shared_ptr< T > fetch(const OId &oid)
Definition: Database.h:200
bool dropContainer(const std::string &name)
Definition: Database.cc:227
Container createContainer()
boost::shared_ptr< DatabaseImpl > m_impl
Definition: Database.h:180
DatabaseUtility utility()
Definition: Database.cc:335
bool getNamesForObject(const OId &oid, std::vector< std::string > &destination)
Definition: Database.cc:324
OId insertItem(const std::string &containerName, const Object &data)
Definition: Database.cc:263
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:298
int cont
bool drop()
Definition: Database.cc:138
static std::string nameForContainer(const std::type_info &typeInfo)
Definition: Database.cc:57
void update(const OId &oid, const T &data)
Definition: Database.h:224
boost::shared_ptr< T > fetch(int itemId)
Definition: Container.h:117
bool isConnected()
Definition: Database.cc:100
bool eraseAllNames()
Definition: Database.cc:303
bool listObjectNames(std::vector< std::string > &destination)
Definition: Database.cc:330
boost::shared_ptr< void > getTypedObjectByName(const std::string &name, const std::type_info &typeInfo)
Definition: Database.cc:313
bool create()
Definition: Database.cc:129
Configuration & configuration()
Definition: Database.cc:87
void updateItem(const OId &oid, const Object &data)
Definition: Database.cc:271
void setObjectName(const std::string &name, const OId &oid)
Definition: Database.cc:293
std::string className(const T &t)
Definition: ClassName.h:30
Container getContainer(const std::string &name, const std::type_info &typeInfo)
Definition: Database.cc:213
int itemId() const
Definition: OId.cc:44