CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Database.cc
Go to the documentation of this file.
5 #include "DatabaseSession.h"
6 #include "DatabaseContainer.h"
7 #include "TransactionCache.h"
8 #include "ContainerSchema.h"
9 #include "IDatabaseSchema.h"
10 #include "ClassUtils.h"
11 
12 namespace ora {
13 
14  class DatabaseImpl {
15  public:
17  m_session(0),
18  m_transaction(0){
19  m_session.reset( new DatabaseSession );
20  m_transaction.reset( new Transaction( *m_session ));
21  }
22 
23  DatabaseImpl(boost::shared_ptr<ConnectionPool>& connectionPool):
24  m_session(0),
25  m_transaction(0){
26  m_session.reset( new DatabaseSession( connectionPool ) );
27  m_transaction.reset( new Transaction( *m_session )) ;
28  }
29 
31  }
32 
33  std::auto_ptr<DatabaseSession> m_session;
34  std::auto_ptr<Transaction> m_transaction;
35  };
36 
37  std::string nameFromClass( const Reflex::Type& contType ){
38  return contType.Name( Reflex::SCOPED );
39  }
40 
41  Container getContainerFromSession( const std::string& name, const Reflex::Type& contType, DatabaseSession& session ){
42  Handle<DatabaseContainer> contHandle = session.containerHandle( name );
43  if( !contHandle ){
46  contHandle = session.createContainer( name, contType );
47  } else {
48  throwException("Container \""+name+"\" does not exist in the database.",
49  "Database::insertItem");
50  }
51  }
52 
53  return Container( contHandle );
54  }
55 }
56 
57 std::string ora::Database::nameForContainer( const std::type_info& typeInfo ){
58  Reflex::Type contType = ClassUtils::lookupDictionary( typeInfo );
59  return nameFromClass( contType );
60 }
61 
62 std::string ora::Database::nameForContainer( const std::string& className ){
63  return className;
64 }
65 
67  m_impl( new DatabaseImpl ){
68 }
69 
71  m_impl( rhs.m_impl ){
72 }
73 
74 ora::Database::Database(boost::shared_ptr<ConnectionPool>& connectionPool):
75  m_impl( new DatabaseImpl( connectionPool) ){
76 }
77 
79  disconnect();
80 }
81 
83  if( this != &rhs ) m_impl = rhs.m_impl;
84  return *this;
85 }
86 
88  return m_impl->m_session->configuration();
89 }
90 
91 bool ora::Database::connect( const std::string& connectionString,
92  bool readOnly){
93  return m_impl->m_session->connect( connectionString, readOnly );
94 }
95 
97  m_impl->m_session->disconnect();
98 }
99 
101  return m_impl->m_session->isConnected();
102 }
103 
104 const std::string& ora::Database::connectionString() {
105  return m_impl->m_session->connectionString();
106 }
107 
109  if(!m_impl->m_session->isConnected()) {
110  throwException("No database storage connected.","Database::transaction");
111  }
112  return *m_impl->m_transaction;
113 }
114 
116  if(!m_impl->m_session->isConnected()) {
117  throwException("No database storage connected.","Database::checkTransaction");
118  }
119  if(!m_impl->m_transaction->isActive()) {
120  throwException("Transaction is not active.","Database::checkTransaction");
121  }
122 }
123 
125  checkTransaction();
126  return m_impl->m_session->exists();
127 }
128 
130  bool created = false;
131  if( !exists()){
132  m_impl->m_session->create();
133  created = true;
134  }
135  return created;
136 }
137 
139  bool dropped = false;
140  if( exists()){
141  open();
142  const std::map<int, Handle<DatabaseContainer> >& conts = m_impl->m_session->containers();
143  for(std::map<int, Handle<DatabaseContainer> >::const_iterator iC = conts.begin();
144  iC != conts.end(); iC++ ){
145  iC->second->drop();
146  }
147  m_impl->m_session->drop();
148  dropped = true;
149  }
150  return dropped;
151 }
152 
153 void ora::Database::open( bool writingAccess /*=false*/){
154  checkTransaction();
155  if( !m_impl->m_session->exists() ){
156  if( writingAccess && m_impl->m_session->configuration().properties().getFlag( Configuration::automaticDatabaseCreation() ) ){
157  m_impl->m_session->create();
158  } else {
159  throwException("Database does not exists in \""+m_impl->m_session->connectionString()+"\"","Database::open");
160  }
161  }
162  m_impl->m_session->open();
163 }
164 
165 std::set< std::string > ora::Database::containers() {
166  open();
167  std::set< std::string > contList;
168  const std::map<int, Handle<DatabaseContainer> >& conts = m_impl->m_session->containers();
169  for(std::map<int, Handle<DatabaseContainer> >::const_iterator iC = conts.begin();
170  iC != conts.end(); iC++ ){
171  contList.insert( iC->second->name() );
172  }
173  return contList;
174 }
175 
177  const std::type_info& typeInfo ){
178  open( true );
179  if( m_impl->m_session->containerHandle( name ) ){
180  throwException("Container with name \""+name+"\" already exists in the database.",
181  "Database::createContainer");
182  }
183  Reflex::Type contType = ClassUtils::lookupDictionary( typeInfo );
184  Handle<DatabaseContainer> cont = m_impl->m_session->createContainer( name, contType );
185  return Container( cont );
186 }
187 
188 ora::Container ora::Database::createContainer( const std::type_info& typeInfo ){
189  open( true );
190  Reflex::Type contType = ClassUtils::lookupDictionary( typeInfo );
191  std::string name = nameFromClass( contType );
192  if( m_impl->m_session->containerHandle( name ) ){
193  throwException("Container with name \""+name+"\" already exists in the database.",
194  "Database::createContainer");
195  }
196  Handle<DatabaseContainer> cont = m_impl->m_session->createContainer( name, contType );
197  return Container( cont );
198 }
199 
201  std::string name ){
202  open( true );
203  Reflex::Type contType = ClassUtils::lookupDictionary( className );
204  if( name.empty() ) name = nameForContainer( className );
205  if( m_impl->m_session->containerHandle( name ) ){
206  throwException("Container with name \""+name+"\" already exists in the database.",
207  "Database::createContainer");
208  }
209  Handle<DatabaseContainer> cont = m_impl->m_session->createContainer( name, contType );
210  return Container( cont );
211 }
212 
213 ora::Container ora::Database::getContainer( const std::string& containerName,
214  const std::type_info& typeInfo){
215  open( true );
216  Reflex::Type objType = ClassUtils::lookupDictionary( typeInfo );
217  return getContainerFromSession( containerName, objType, *m_impl->m_session );
218 }
219 
220 ora::Container ora::Database::getContainer( const std::type_info& typeInfo ){
221  open( true );
222  Reflex::Type objType = ClassUtils::lookupDictionary( typeInfo );
223  std::string contName = nameFromClass( objType );
224  return getContainerFromSession( contName, objType, *m_impl->m_session);
225 }
226 
227 bool ora::Database::dropContainer( const std::string& name ){
228  open();
229  if( !m_impl->m_session->containerHandle( name ) ){
230  return false;
231  }
232  m_impl->m_session->dropContainer( name );
233  return true;
234 }
235 
237  open();
238  Handle<DatabaseContainer> cont = m_impl->m_session->containerHandle( name );
239  if( !cont ){
240  throwException("Container \""+name+"\" does not exist in the database.",
241  "Database::containerHandle");
242  }
243  return Container( cont );
244 }
245 
247  open();
248  Handle<DatabaseContainer> cont = m_impl->m_session->containerHandle( contId );
249  if( !cont ){
250  std::stringstream messg;
251  messg << "Container with id=" << contId << " not found in the database.";
252  throwException(messg.str(),
253  "Database::containerHandle");
254  }
255  return Container( cont );
256 }
257 
259  Container cont = containerHandle( oid.containerId() );
260  return cont.fetchItem( oid.itemId() );
261 }
262 
263 ora::OId ora::Database::insertItem(const std::string& containerName,
264  const Object& dataObject ){
265  open( true );
266  Container cont = getContainerFromSession( containerName, dataObject.type(), *m_impl->m_session );
267  int itemId = cont.insertItem( dataObject );
268  return OId( cont.id(), itemId );
269 }
270 
272  const Object& dataObject ){
273  open();
274  Container cont = containerHandle( oid.containerId() );
275  cont.updateItem( oid.itemId(), dataObject );
276 }
277 
278 void ora::Database::erase(const OId& oid){
279  open();
280  Container cont = containerHandle( oid.containerId() );
281  cont.erase( oid.itemId() );
282 }
283 
285  open();
286  const std::map<int,Handle<DatabaseContainer> >& containers = m_impl->m_session->containers();
287  for( std::map<int,Handle<DatabaseContainer> >::const_iterator iCont = containers.begin();
288  iCont != containers.end(); ++iCont ){
289  iCont->second->flush();
290  }
291 }
292 
293 void ora::Database::setObjectName( const std::string& name, const OId& oid ){
294  open( true );
295  m_impl->m_session->setObjectName( name, oid.containerId(), oid.itemId() );
296 }
297 
298 bool ora::Database::eraseObjectName( const std::string& name ){
299  open();
300  return m_impl->m_session->eraseObjectName( name );
301 }
302 
304  open();
305  return m_impl->m_session->eraseAllNames();
306 }
307 
308 bool ora::Database::getItemId( const std::string& name, ora::OId& destination ){
309  open();
310  return m_impl->m_session->getItemId( name, destination );
311 }
312 
313 boost::shared_ptr<void> ora::Database::getTypedObjectByName( const std::string& name, const std::type_info& typeInfo ){
314  open();
315  Reflex::Type objType = ClassUtils::lookupDictionary( typeInfo );
316  return m_impl->m_session->fetchTypedObjectByName( name, objType );
317 }
318 
320  open();
321  return m_impl->m_session->fetchObjectByName( name );
322 }
323 
325  std::vector<std::string>& destination ){
326  open();
327  return m_impl->m_session->getNamesForObject( oid.containerId(), oid.itemId(), destination );
328 }
329 
330 bool ora::Database::listObjectNames( std::vector<std::string>& destination ){
331  open();
332  return m_impl->m_session->listObjectNames( destination );
333 }
334 
336  checkTransaction();
337  Handle<DatabaseUtilitySession> utilSession = m_impl->m_session->utility();
338  return DatabaseUtility( utilSession );
339 }
340 
342  return m_impl->m_session->storageAccessSession();
343 }
344 
Handle< DatabaseContainer > containerHandle(const std::string &name)
std::auto_ptr< DatabaseSession > m_session
Definition: Database.cc:33
void flush()
Definition: Database.cc:284
bool getFlag(const std::string &flagName) const
Definition: Properties.cc:36
void updateItem(int itemId, const Object &data)
Definition: Container.cc:116
Object fetchItemByName(const std::string &name)
Definition: Database.cc:319
EcalChannelStatus Container
std::string nameFromClass(const Reflex::Type &contType)
Definition: Database.cc:37
Properties & properties()
const std::string & connectionString()
Definition: Database.cc:104
int insertItem(const Object &data)
Definition: Container.cc:101
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
std::auto_ptr< Transaction > m_transaction
Definition: Database.cc:34
static std::string automaticContainerCreation()
void open(bool writingAccess=false)
Definition: Database.cc:153
Object fetchItem(int itemId)
Definition: Container.cc:89
static std::string automaticDatabaseCreation()
Definition: Configuration.cc:5
dictionary map
Definition: Association.py:160
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
bool exists()
Definition: Database.cc:124
std::set< std::string > containers()
Definition: Database.cc:165
int containerId() const
Definition: OId.cc:40
ProductStatus dropped()
Definition: ProductStatus.h:24
bool dropContainer(const std::string &name)
Definition: Database.cc:227
DatabaseImpl(boost::shared_ptr< ConnectionPool > &connectionPool)
Definition: Database.cc:23
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
Handle< DatabaseContainer > createContainer(const std::string &containerName, const Reflex::Type &type)
bool connect(const std::string &connectionString, bool readOnly=false)
Definition: Database.cc:91
void erase(int itemId)
Definition: Container.cc:133
bool eraseObjectName(const std::string &name)
Definition: Database.cc:298
int cont
bool drop()
Definition: Database.cc:138
Configuration & configuration()
static std::string nameForContainer(const std::type_info &typeInfo)
Definition: Database.cc:57
bool isConnected()
Definition: Database.cc:100
const Reflex::Type & type() const
Definition: Object.cc:45
bool eraseAllNames()
Definition: Database.cc:303
bool listObjectNames(std::vector< std::string > &destination)
Definition: Database.cc:330
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:10
Container getContainerFromSession(const std::string &name, const Reflex::Type &contType, DatabaseSession &session)
Definition: Database.cc:41
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
Reflex::Type lookupDictionary(const std::type_info &typeInfo, bool throwFlag=true)
Definition: ClassUtils.cc:84
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