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 
38  return contType.cppName();
39  }
40 
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::getContainerFromSession");
50  }
51  }
52 
53  return Container( contHandle );
54  }
55 }
56 
57 std::string ora::Database::nameForContainer( const std::type_info& typeInfo ){
58  edm::TypeWithDict contType = ClassUtils::lookupDictionary( typeInfo );
59  return nameFromClass( contType );
60 }
61 
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 }
80 
82  if( this != &rhs ) m_impl = rhs.m_impl;
83  return *this;
84 }
85 
87  return m_impl->m_session->configuration();
88 }
89 
90 bool ora::Database::connect( const std::string& connectionString,
91  bool readOnly ){
92  return m_impl->m_session->connect( connectionString, readOnly );
93 }
94 
95 bool ora::Database::connect( const std::string& connectionString,
96  const std::string& asRole,
97  bool readOnly ){
98  return m_impl->m_session->connect( connectionString, asRole, readOnly );
99 }
100 
101 bool ora::Database::connect( boost::shared_ptr<coral::ISessionProxy>& coralSession, const std::string& connectionString, const std::string& schemaName ){
102  return m_impl->m_session->connect( coralSession, connectionString, schemaName );
103 }
104 
106  m_impl->m_session->disconnect();
107 }
108 
110  return m_impl->m_session->isConnected();
111 }
112 
114  return m_impl->m_session->connectionString();
115 }
116 
118  if(!m_impl->m_session->isConnected()) {
119  throwException("No database storage connected.","Database::transaction");
120  }
121  return *m_impl->m_transaction;
122 }
123 
125  if(!m_impl->m_session->isConnected()) {
126  throwException("No database storage connected.","Database::checkTransaction");
127  }
128  if(!m_impl->m_transaction->isActive()) {
129  throwException("Transaction is not active.","Database::checkTransaction");
130  }
131 }
132 
134  checkTransaction();
135  return m_impl->m_session->exists();
136 }
137 
138 bool ora::Database::create( std::string userSchemaVersion ){
139  bool created = false;
140  if( !exists()){
141  m_impl->m_session->create( userSchemaVersion );
142  created = true;
143  }
144  return created;
145 }
146 
148  bool dropped = false;
149  if( exists()){
150  open();
151  const std::map<int, Handle<DatabaseContainer> >& conts = m_impl->m_session->containers();
152  for(std::map<int, Handle<DatabaseContainer> >::const_iterator iC = conts.begin();
153  iC != conts.end(); iC++ ){
154  iC->second->drop();
155  }
156  m_impl->m_session->drop();
157  dropped = true;
158  }
159  return dropped;
160 }
161 
163  if( exists()){
164  open();
165  m_impl->m_session->setAccessPermission( principal, forWrite );
166  const std::map<int, Handle<DatabaseContainer> >& conts = m_impl->m_session->containers();
167  for(std::map<int, Handle<DatabaseContainer> >::const_iterator iC = conts.begin();
168  iC != conts.end(); iC++ ){
169  iC->second->setAccessPermission( principal, forWrite );
170  }
171  }
172 }
173 
174 void ora::Database::open( bool writingAccess /*=false*/){
175  checkTransaction();
176  if( !m_impl->m_session->exists() ){
177  if( writingAccess && m_impl->m_session->configuration().properties().getFlag( Configuration::automaticDatabaseCreation() ) ){
178  m_impl->m_session->create();
179  } else {
180  throwException("Database does not exists in \""+m_impl->m_session->connectionString()+"\"","Database::open");
181  }
182  }
183  m_impl->m_session->open();
184 }
185 
187  checkTransaction();
188  if( !m_impl->m_session->exists() ){
189  throwException("Database does not exists in \""+m_impl->m_session->connectionString()+"\"","Database::schemaVersion");
190  }
191  return Version::fromString( m_impl->m_session->schemaVersion( userSchema ) );
192 }
193 
194 std::set< std::string > ora::Database::containers() {
195  open();
196  std::set< std::string > contList;
197  const std::map<int, Handle<DatabaseContainer> >& conts = m_impl->m_session->containers();
198  for(std::map<int, Handle<DatabaseContainer> >::const_iterator iC = conts.begin();
199  iC != conts.end(); iC++ ){
200  contList.insert( iC->second->name() );
201  }
202  return contList;
203 }
204 
206  const std::type_info& typeInfo ){
207  open( true );
208  if( m_impl->m_session->containerHandle( name ) ){
209  throwException("Container with name \""+name+"\" already exists in the database.",
210  "Database::createContainer");
211  }
212  edm::TypeWithDict contType = ClassUtils::lookupDictionary( typeInfo );
213  Handle<DatabaseContainer> cont = m_impl->m_session->createContainer( name, contType );
214  return Container( cont );
215 }
216 
217 ora::Container ora::Database::createContainer( const std::type_info& typeInfo ){
218  open( true );
219  edm::TypeWithDict contType = ClassUtils::lookupDictionary( typeInfo );
220  std::string name = nameFromClass( contType );
221  if( m_impl->m_session->containerHandle( name ) ){
222  throwException("Container with name \""+name+"\" already exists in the database.",
223  "Database::createContainer");
224  }
225  Handle<DatabaseContainer> cont = m_impl->m_session->createContainer( name, contType );
226  return Container( cont );
227 }
228 
230  std::string name ){
231  open( true );
232  edm::TypeWithDict contType = ClassUtils::lookupDictionary( className );
233  if( name.empty() ) name = nameForContainer( className );
234  if( m_impl->m_session->containerHandle( name ) ){
235  throwException("Container with name \""+name+"\" already exists in the database.",
236  "Database::createContainer");
237  }
238  Handle<DatabaseContainer> cont = m_impl->m_session->createContainer( name, contType );
239  return Container( cont );
240 }
241 
243  const std::type_info& typeInfo){
244  open( true );
245  edm::TypeWithDict objType = ClassUtils::lookupDictionary( typeInfo );
246  return getContainerFromSession( containerName, objType, *m_impl->m_session );
247 }
248 
249 ora::Container ora::Database::getContainer( const std::type_info& typeInfo ){
250  open( true );
251  edm::TypeWithDict objType = ClassUtils::lookupDictionary( typeInfo );
252  std::string contName = nameFromClass( objType );
253  return getContainerFromSession( contName, objType, *m_impl->m_session);
254 }
255 
257  open();
258  if( !m_impl->m_session->containerHandle( name ) ){
259  return false;
260  }
261  m_impl->m_session->dropContainer( name );
262  return true;
263 }
264 
266  open();
267  Handle<DatabaseContainer> cont = m_impl->m_session->containerHandle( name );
268  if( !cont ){
269  throwException("Container \""+name+"\" does not exist in the database.",
270  "Database::lockContainer");
271  }
272  return cont->lock();
273 }
274 
276  open();
277  Handle<DatabaseContainer> cont = m_impl->m_session->containerHandle( name );
278  if( !cont ){
279  throwException("Container \""+name+"\" does not exist in the database.",
280  "Database::containerHandle");
281  }
282  return Container( cont );
283 }
284 
286  open();
287  Handle<DatabaseContainer> cont = m_impl->m_session->containerHandle( contId );
288  if( !cont ){
289  std::stringstream messg;
290  messg << "Container with id=" << contId << " not found in the database.";
291  throwException(messg.str(),
292  "Database::containerHandle");
293  }
294  return Container( cont );
295 }
296 
298  Container cont = containerHandle( oid.containerId() );
299  return cont.fetchItem( oid.itemId() );
300 }
301 
303  const Object& dataObject ){
304  open( true );
305  Container cont = getContainerFromSession( containerName, dataObject.type(), *m_impl->m_session );
306  int itemId = cont.insertItem( dataObject );
307  return OId( cont.id(), itemId );
308 }
309 
311  const Object& dataObject ){
312  open();
313  Container cont = containerHandle( oid.containerId() );
314  cont.updateItem( oid.itemId(), dataObject );
315 }
316 
317 void ora::Database::erase(const OId& oid){
318  open();
319  Container cont = containerHandle( oid.containerId() );
320  cont.erase( oid.itemId() );
321 }
322 
324  open();
325  const std::map<int,Handle<DatabaseContainer> >& containers = m_impl->m_session->containers();
326  for( std::map<int,Handle<DatabaseContainer> >::const_iterator iCont = containers.begin();
327  iCont != containers.end(); ++iCont ){
328  iCont->second->flush();
329  }
330 }
331 
333  open( true );
334  m_impl->m_session->setObjectName( name, oid.containerId(), oid.itemId() );
335 }
336 
338  open();
339  return m_impl->m_session->eraseObjectName( name );
340 }
341 
343  open();
344  return m_impl->m_session->eraseAllNames();
345 }
346 
347 bool ora::Database::getItemId( const std::string& name, ora::OId& destination ){
348  open();
349  return m_impl->m_session->getItemId( name, destination );
350 }
351 
352 boost::shared_ptr<void> ora::Database::getTypedObjectByName( const std::string& name, const std::type_info& typeInfo ){
353  open();
354  edm::TypeWithDict objType = ClassUtils::lookupDictionary( typeInfo );
355  return m_impl->m_session->fetchTypedObjectByName( name, objType );
356 }
357 
359  open();
360  return m_impl->m_session->fetchObjectByName( name );
361 }
362 
364  std::vector<std::string>& destination ){
365  checkTransaction();
366  if( !m_impl->m_session->exists() ){
367  throwException("Database does not exists in \""+m_impl->m_session->connectionString()+"\"","Database::getNamesForObject");
368  }
369  return m_impl->m_session->getNamesForObject( oid.containerId(), oid.itemId(), destination );
370 }
371 
372 bool ora::Database::listObjectNames( std::vector<std::string>& destination ){
373  checkTransaction();
374  if( !m_impl->m_session->exists() ){
375  throwException("Database does not exists in \""+m_impl->m_session->connectionString()+"\"","Database::listObjectNames");
376  }
377  return m_impl->m_session->listObjectNames( destination );
378 }
379 
381  checkTransaction();
382  Handle<DatabaseUtilitySession> utilSession = m_impl->m_session->utility();
383  return DatabaseUtility( utilSession );
384 }
385 
387  return m_impl->m_session->storageAccessSession();
388 }
389 
Handle< DatabaseContainer > containerHandle(const std::string &name)
bool create(std::string userSchemaVersion=std::string(""))
Definition: Database.cc:138
std::auto_ptr< DatabaseSession > m_session
Definition: Database.cc:33
void flush()
Definition: Database.cc:323
bool getFlag(const std::string &flagName) const
Definition: Properties.cc:36
void updateItem(int itemId, const Object &data)
Definition: Container.cc:144
Object fetchItemByName(const std::string &name)
Definition: Database.cc:358
tuple cont
load Luminosity info ##
Definition: generateEDF.py:622
EcalChannelStatus Container
static Version fromString(const std::string &versionString)
Definition: Version.cc:21
const edm::TypeWithDict & type() const
Definition: Object.cc:55
Properties & properties()
const std::string & connectionString()
Definition: Database.cc:113
int insertItem(const Object &data)
Definition: Container.cc:129
void checkTransaction()
Definition: Database.cc:124
bool getItemId(const std::string &name, OId &destination)
Definition: Database.cc:347
Definition: OId.h:8
virtual ~Database()
Definition: Database.cc:78
std::auto_ptr< Transaction > m_transaction
Definition: Database.cc:34
ora::Version schemaVersion(bool userSchema=false)
Definition: Database.cc:186
static std::string automaticContainerCreation()
void open(bool writingAccess=false)
Definition: Database.cc:174
Object fetchItem(int itemId)
Definition: Container.cc:109
Container getContainerFromSession(const std::string &name, const edm::TypeWithDict &contType, DatabaseSession &session)
Definition: Database.cc:41
static std::string automaticDatabaseCreation()
bool lockContainer(const std::string &name)
Definition: Database.cc:265
Transaction & transaction()
Definition: Database.cc:117
SharedSession & storageAccessSession()
Definition: Database.cc:386
void disconnect()
Definition: Database.cc:105
void erase(const OId &oid)
Definition: Database.cc:317
Object fetchItem(const OId &oid)
Definition: Database.cc:297
std::string cppName() const
std::string nameFromClass(const edm::TypeWithDict &contType)
Definition: Database.cc:37
Container containerHandle(const std::string &name)
Definition: Database.cc:275
bool exists()
Definition: Database.cc:133
std::set< std::string > containers()
Definition: Database.cc:194
int containerId() const
Definition: OId.cc:50
bool dropContainer(const std::string &name)
Definition: Database.cc:256
DatabaseImpl(boost::shared_ptr< ConnectionPool > &connectionPool)
Definition: Database.cc:23
Container createContainer()
boost::shared_ptr< DatabaseImpl > m_impl
Definition: Database.h:200
edm::TypeWithDict lookupDictionary(const std::type_info &typeInfo, bool throwFlag=true)
Definition: ClassUtils.cc:167
DatabaseUtility utility()
Definition: Database.cc:380
bool getNamesForObject(const OId &oid, std::vector< std::string > &destination)
Definition: Database.cc:363
OId insertItem(const std::string &containerName, const Object &data)
Definition: Database.cc:302
Database & operator=(const Database &rhs)
Definition: Database.cc:81
bool connect(const std::string &connectionString, bool readOnly=false)
Definition: Database.cc:90
void erase(int itemId)
Definition: Container.cc:161
Handle< DatabaseContainer > createContainer(const std::string &containerName, const edm::TypeWithDict &type)
bool eraseObjectName(const std::string &name)
Definition: Database.cc:337
void throwException(const std::string &message, const std::string &methodName) __attribute__((noreturn))
Definition: Exception.cc:10
bool drop()
Definition: Database.cc:147
Configuration & configuration()
static std::string nameForContainer(const std::type_info &typeInfo)
Definition: Database.cc:57
bool isConnected()
Definition: Database.cc:109
bool eraseAllNames()
Definition: Database.cc:342
bool listObjectNames(std::vector< std::string > &destination)
Definition: Database.cc:372
boost::shared_ptr< void > getTypedObjectByName(const std::string &name, const std::type_info &typeInfo)
Definition: Database.cc:352
Configuration & configuration()
Definition: Database.cc:86
void updateItem(const OId &oid, const Object &data)
Definition: Database.cc:310
void setObjectName(const std::string &name, const OId &oid)
Definition: Database.cc:332
std::string className(const T &t)
Definition: ClassName.h:30
Container getContainer(const std::string &name, const std::type_info &typeInfo)
Definition: Database.cc:242
void setAccessPermission(const std::string &principal, bool forWrite)
Definition: Database.cc:162
int itemId() const
Definition: OId.cc:54