CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DatabaseSession.cc
Go to the documentation of this file.
3 #include "DatabaseSession.h"
4 #include "IDatabaseSchema.h"
5 #include "Sequences.h"
6 #include "MappingDatabase.h"
7 #include "TransactionCache.h"
8 #include "DatabaseContainer.h"
9 #include "ClassUtils.h"
10 #include "MappingRules.h"
11 #include "DatabaseUtilitySession.h"
12 // externals
13 #include "RelationalAccess/IConnectionServiceConfiguration.h"
14 #include "RelationalAccess/ISessionProxy.h"
15 #include "RelationalAccess/ITransaction.h"
16 
18  m_table(){
19 }
20 
22 }
23 
25  unsigned int size ){
26  std::map<int, unsigned int>::iterator iC = m_table.find( contId );
27  if( iC == m_table.end() ){
28  iC = m_table.insert( std::make_pair( contId, 0 ) ).first;
29  }
30  iC->second = size;
31 }
32 
34  m_table.erase( contId );
35 }
36 
37 const std::map<int, unsigned int>& ora::ContainerUpdateTable::table(){
38  return m_table;
39 }
40 
42  m_table.clear();
43 }
44 
46  m_connectionPool( new ConnectionPool ),
47  m_dbSession(),
48  m_connectionString( "" ),
49  m_schemaName(""),
50  m_schema(),
51  m_contIdSequence(),
52  m_mappingDb(),
53  m_transactionCache(),
54  m_containerUpdateTable(),
55  m_configuration(),
56  m_monitoring(0){
57  // for the private connection pool does not make sense to have a real pool...
58  m_connectionPool->configuration().setConnectionTimeOut(0);
59 }
60 
61 ora::DatabaseSession::DatabaseSession(boost::shared_ptr<ConnectionPool>& connectionPool ):
62  m_connectionPool( connectionPool ),
63  m_dbSession(),
64  m_connectionString( "" ),
65  m_schemaName(""),
66  m_schema(),
67  m_contIdSequence(),
68  m_mappingDb(),
69  m_transactionCache(),
70  m_containerUpdateTable(),
71  m_configuration(),
72  m_monitoring(0){
73 }
74 
76  disconnect();
77 }
78 
79 bool ora::DatabaseSession::connect( const std::string& connectionString,
80  bool readOnly ){
81  m_dbSession = m_connectionPool->connect( connectionString, readOnly?coral::ReadOnly:coral::Update );
82  if(m_dbSession.isValid()) {
83  m_connectionString = connectionString;
85  m_monitoring = ora::Monitoring::get().startSession( connectionString );
86  }
87  }
88  return isConnected();
89 }
90 
91 bool ora::DatabaseSession::connect( const std::string& connectionString,
92  const std::string& asRole,
93  bool readOnly ){
94  m_dbSession = m_connectionPool->connect( connectionString, asRole, readOnly?coral::ReadOnly:coral::Update );
95  if(m_dbSession.isValid()) {
96  m_connectionString = connectionString;
98  m_monitoring = ora::Monitoring::get().startSession( connectionString );
99  }
100  }
101  return isConnected();
102 }
103 
104 bool ora::DatabaseSession::connect( boost::shared_ptr<coral::ISessionProxy>& coralSession,
105  const std::string& connectionString,
106  const std::string& schemaName ){
107  m_ownedTransaction = false;
108  m_dbSession = SharedSession( coralSession );
109  if(m_dbSession.isValid()) {
110  m_connectionString = connectionString;
111  m_schemaName = schemaName;
113  m_monitoring = ora::Monitoring::get().startSession( connectionString );
114  }
115  }
116  return isConnected();
117 }
118 
120  m_transactionCache.reset();
121  m_mappingDb.reset();
122  m_contIdSequence.reset();
123  m_schema.reset();
124  m_containerUpdateTable.clear();
125 }
126 
128  if( isConnected() && m_ownedTransaction ){
129  if( isTransactionActive()) rollbackTransaction();
130  }
131  clearTransaction();
132  m_dbSession.close();
133  m_connectionString.clear();
134  if(m_monitoring) m_monitoring->stop();
135 }
136 
138  return m_dbSession.isValid();
139 }
140 
142  return m_connectionString;
143 }
144 
146  if( !m_transactionCache.get() ){
147  m_dbSession.get().transaction().start( readOnly );
148  coral::ISchema* targetSchema = &m_dbSession.get().nominalSchema();
149  if( !m_schemaName.empty() ) targetSchema = &m_dbSession.get().schema( m_schemaName );
150  m_schema.reset( IDatabaseSchema::createSchemaHandle( *targetSchema ));
151  m_contIdSequence.reset( new NamedSequence( MappingRules::sequenceNameForContainerId(), *m_schema ));
152  m_mappingDb.reset( new MappingDatabase( *m_schema ));
153  m_transactionCache.reset( new TransactionCache );
154  if(m_monitoring) {
155  m_monitoring->newTransaction();
156  }
157  }
158 }
159 
161  if( m_transactionCache.get() ){
162  m_schema->containerHeaderTable().updateNumberOfObjects( m_containerUpdateTable.table() );
163  m_dbSession.get().transaction().commit();
164  clearTransaction();
165  if(m_monitoring) {
166  m_monitoring->stopTransaction();
167  }
168  }
169 }
170 
172  if( m_transactionCache.get() ){
173  m_dbSession.get().transaction().rollback();
174  clearTransaction();
175  if(m_monitoring) {
176  m_monitoring->stopTransaction(false);
177  }
178  }
179 }
180 
181 bool ora::DatabaseSession::isTransactionActive( bool checkIfReadOnly ){
182  bool ret = false;
183  if( m_dbSession.get().transaction().isActive() ){
184  if( checkIfReadOnly ){
185  if( m_dbSession.get().transaction().isReadOnly() ) ret = true;
186  } else {
187  ret = true;
188  }
189  }
190  return ret;
191 }
192 
194  if(!m_transactionCache->dbExistsLoaded()){
195  m_transactionCache->setDbExists( m_schema->exists() );
196  }
197  return m_transactionCache->dbExists();
198 }
199 
200 void ora::DatabaseSession::create( const std::string& userSchemaVersion ){
201  m_schema->create( userSchemaVersion );
202  m_contIdSequence->create();
203  m_mappingDb->setUp();
204  m_transactionCache->setDbExists( true );
205 }
206 
208  if(!testDropPermission()){
209  throwException("Drop permission has been denied for the current user.",
210  "DatabaseSession::drop");
211  }
212  m_schema->drop();
213  m_transactionCache->dropDatabase();
214 }
215 
217  bool forWrite ){
218  m_schema->setAccessPermission( principal, forWrite );
219 }
220 
222  if(!m_transactionCache->dropPermissionLoaded()){
223  m_transactionCache->setDropPermission( m_schema->testDropPermission() );
224  }
225  return m_transactionCache->dropPermission();
226 }
227 
229  if(!m_transactionCache->isLoaded()){
230  std::map<std::string, ContainerHeaderData> containersData;
231  m_schema->containerHeaderTable().getContainerData( containersData );
232  for(std::map<std::string, ContainerHeaderData>::iterator iC = containersData.begin();
233  iC != containersData.end(); ++iC){
234  Handle<DatabaseContainer> container( new DatabaseContainer( iC->second.id, iC->first,
235  iC->second.className,
236  iC->second.numberOfObjects, *this ) );
237  m_transactionCache->addContainer( iC->second.id, iC->first, container );
238  }
239  m_schema->mainTable().getParameters( m_transactionCache->dbParams() );
240  m_transactionCache->setLoaded();
241  }
242 }
243 
245  std::map<std::string,std::string>& params = m_transactionCache->dbParams();
246  std::string version("");
248  if(userSchema ) paramName = IMainTable::userSchemaVersionParameterName();
249  std::map<std::string,std::string>::const_iterator iPar = params.find( paramName );
250  if( iPar != params.end() ){
251  version = iPar->second;
252  }
253  return version;
254 }
255 
257  const std::string& className ){
258  int newContId = m_contIdSequence->getNextId( true );
259  m_schema->containerHeaderTable().addContainer( newContId, containerName, className );
260  Handle<DatabaseContainer> container( new DatabaseContainer( newContId, containerName,
261  className, 0, *this ) );
262  m_transactionCache->addContainer( newContId, containerName, container );
263  return container;
264 }
265 
266 
268  const Reflex::Type& type ){
269  // create the container
270  int newContId = m_contIdSequence->getNextId( true );
271  Handle<DatabaseContainer> newCont ( new DatabaseContainer( newContId, containerName, type, *this ) );
272  m_transactionCache->addContainer( newContId, containerName, newCont );
273  newCont->create();
274  return newCont;
275 }
276 
278  Handle<DatabaseContainer> cont = m_transactionCache->getContainer( name );
279  m_transactionCache->eraseContainer( cont->id(), name );
280  cont->drop();
281 }
282 
284  return m_transactionCache->getContainer( name );
285 }
286 
288  return m_transactionCache->getContainer( contId );
289 }
290 
291 const std::map<int, ora::Handle<ora::DatabaseContainer> >& ora::DatabaseSession::containers(){
292  return m_transactionCache->containers();
293 }
294 
296  int containerId,
297  int itemId ){
298  m_schema->namingServiceTable().setObjectName( name, containerId, itemId );
299 }
300 
302  return m_schema->namingServiceTable().eraseObjectName( name );
303 }
304 
306  return m_schema->namingServiceTable().eraseAllNames();
307 }
308 
311  std::pair<int,int> oid;
312  if( m_schema->namingServiceTable().getObjectByName( name, oid ) ){
313  ora::Handle<ora::DatabaseContainer> cont = containerHandle( oid.first );
314  if( cont ) ret = Object( cont->fetchItem( oid.second ), cont->type() );
315  }
316  return ret;
317 }
318 
320  std::pair<int,int> oidData;
321  if( m_schema->namingServiceTable().getObjectByName( name, oidData ) ){
322  destination = OId( oidData.first, oidData.second );
323  return true;
324  }
325  return false;
326 }
327 
329  const Reflex::Type& asType ){
330  boost::shared_ptr<void> ret = m_transactionCache->getNamedReference( name );
331  if( !ret.get() ){
332  std::pair<int,int> oid;
333  if( m_schema->namingServiceTable().getObjectByName( name, oid ) ){
334  ora::Handle<ora::DatabaseContainer> cont = containerHandle( oid.first );
335  void* ptr = 0;
336  if( cont ) {
337  ptr = cont->fetchItemAsType( oid.second, asType );
338  if( ptr) ret = boost::shared_ptr<void>( ptr, RflxDeleter( cont->type() ) );
339  }
340  }
341  if( ret.get() ) m_transactionCache->setNamedReference( name, ret );
342  }
343  return ret;
344 }
345 
347  std::vector<std::string>& destination ){
348  return m_schema->namingServiceTable().getNamesForContainer( containerId, destination );
349 }
350 
352  int itemId,
353  std::vector<std::string>& destination ){
354  return m_schema->namingServiceTable().getNamesForObject( containerId, itemId, destination );
355 }
356 
357 bool ora::DatabaseSession::listObjectNames( std::vector<std::string>& destination ){
358 
359  return m_schema->namingServiceTable().getAllNames( destination );
360 }
361 
363  if( !m_transactionCache->utility() ){
365  m_transactionCache->setUtility( util );
366  }
367  return m_transactionCache->utility();
368 }
369 
371  return *m_schema;
372 }
373 
375  return *m_contIdSequence;
376 }
377 
379  return *m_mappingDb;
380 }
381 
383  return m_configuration;
384 }
385 
387  return m_dbSession;
388 }
389 
390 boost::shared_ptr<ora::ConnectionPool>& ora::DatabaseSession::connectionPool(){
391  return m_connectionPool;
392 }
393 
395  return m_containerUpdateTable;
396 }
static std::string versionParameterName()
Handle< DatabaseContainer > containerHandle(const std::string &name)
const std::string & connectionString()
type
Definition: HCALResponse.h:21
To be moved in DBCommon, has to serve also the pure coral use case.
static std::string userSchemaVersionParameterName()
const Reflex::Type & type()
Object fetchObjectByName(const std::string &name)
bool listObjectNames(std::vector< std::string > &destination)
void takeNote(int contId, unsigned int size)
SharedSession & storageAccessSession()
boost::shared_ptr< ConnectionPool > & connectionPool()
bool getItemId(const std::string &name, OId &destination)
void dropContainer(const std::string &name)
boost::shared_ptr< void > fetchTypedObjectByName(const std::string &name, const Reflex::Type &asType)
Definition: OId.h:8
const std::map< int, unsigned int > & table()
static Monitoring & get()
Definition: Monitoring.cc:79
std::string schemaVersion(bool userSchema)
bool eraseObjectName(const std::string &name)
void setAccessPermission(const std::string &principal, bool forWrite)
SessionMonitoringData * startSession(const std::string &connectionString)
Definition: Monitoring.cc:128
bool connect(const std::string &connectionString, bool readOnly)
IDatabaseSchema & schema()
bool getNamesForContainer(int containerId, std::vector< std::string > &destination)
Handle< DatabaseUtilitySession > utility()
ContainerUpdateTable & containerUpdateTable()
void setObjectName(const std::string &name, int containerId, int itemId)
bool getNamesForObject(int containerId, int itemId, std::vector< std::string > &destination)
const std::map< int, Handle< DatabaseContainer > > & containers()
static IDatabaseSchema * createSchemaHandle(coral::ISchema &schema)
void startTransaction(bool readOnly)
NamedSequence & containerIdSequence()
MappingDatabase & mappingDatabase()
void create(const std::string &userSchemaVersion=std::string(""))
bool isTransactionActive(bool checkIfReadOnly=false)
Handle< DatabaseContainer > createContainer(const std::string &containerName, const Reflex::Type &type)
void * fetchItem(int itemId)
static bool isEnabled()
Definition: Monitoring.cc:84
int cont
void throwException(const std::string &message, const std::string &methodName) __attribute__((noreturn))
Definition: Exception.cc:10
Configuration & configuration()
void * fetchItemAsType(int itemId, const Reflex::Type &asType)
static std::string sequenceNameForContainerId()
sequence names
Definition: MappingRules.cc:9
tuple size
Write out results.
std::string className(const T &t)
Definition: ClassName.h:30
boost::shared_ptr< ConnectionPool > m_connectionPool
Handle< ora::DatabaseContainer > addContainer(const std::string &containerName, const std::string &className)