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_schema(),
50  m_contIdSequence(),
51  m_mappingDb(),
52  m_transactionCache(),
53  m_containerUpdateTable(),
54  m_configuration(),
55  m_monitoring(0){
56  // for the private connection pool does not make sense to have a real pool...
57  m_connectionPool->configuration().setConnectionTimeOut(0);
58 }
59 
60 ora::DatabaseSession::DatabaseSession(boost::shared_ptr<ConnectionPool>& connectionPool ):
61  m_connectionPool( connectionPool ),
62  m_dbSession(),
63  m_connectionString( "" ),
64  m_schema(),
65  m_contIdSequence(),
66  m_mappingDb(),
67  m_transactionCache(),
68  m_containerUpdateTable(),
69  m_configuration(),
70  m_monitoring(0){
71 }
72 
74  disconnect();
75 }
76 
77 bool ora::DatabaseSession::connect( const std::string& connectionString,
78  bool readOnly ){
79  m_dbSession = m_connectionPool->connect( connectionString, readOnly?coral::ReadOnly:coral::Update );
80  if(m_dbSession.isValid()) {
81  m_connectionString = connectionString;
83  m_monitoring = ora::Monitoring::get().startSession( connectionString );
84  }
85  }
86  return isConnected();
87 }
88 
89 bool ora::DatabaseSession::connect( const std::string& connectionString,
90  const std::string& asRole,
91  bool readOnly ){
92  m_dbSession = m_connectionPool->connect( connectionString, asRole, readOnly?coral::ReadOnly:coral::Update );
93  if(m_dbSession.isValid()) {
94  m_connectionString = connectionString;
96  m_monitoring = ora::Monitoring::get().startSession( connectionString );
97  }
98  }
99  return isConnected();
100 }
101 
103  m_transactionCache.reset();
104  m_mappingDb.reset();
105  m_contIdSequence.reset();
106  m_schema.reset();
107  m_containerUpdateTable.clear();
108 }
109 
111  if( isConnected() ){
112  if( isTransactionActive()) rollbackTransaction();
113  }
114  clearTransaction();
115  m_dbSession.close();
116  m_connectionString.clear();
117  if(m_monitoring) m_monitoring->stop();
118 }
119 
121  return m_dbSession.isValid();
122 }
123 
125  return m_connectionString;
126 }
127 
129  if( !m_transactionCache.get() ){
130  m_dbSession.get().transaction().start( readOnly );
131  m_schema.reset( IDatabaseSchema::createSchemaHandle( m_dbSession.get().nominalSchema() ));
132  m_contIdSequence.reset( new NamedSequence( MappingRules::sequenceNameForContainerId(), *m_schema ));
133  m_mappingDb.reset( new MappingDatabase( *m_schema ));
134  m_transactionCache.reset( new TransactionCache );
135  if(m_monitoring) {
136  m_monitoring->newTransaction();
137  }
138  }
139 }
140 
142  if( m_transactionCache.get() ){
143  m_schema->containerHeaderTable().updateNumberOfObjects( m_containerUpdateTable.table() );
144  m_dbSession.get().transaction().commit();
145  clearTransaction();
146  if(m_monitoring) {
147  m_monitoring->stopTransaction();
148  }
149  }
150 }
151 
153  if( m_transactionCache.get() ){
154  m_dbSession.get().transaction().rollback();
155  clearTransaction();
156  if(m_monitoring) {
157  m_monitoring->stopTransaction(false);
158  }
159  }
160 }
161 
162 bool ora::DatabaseSession::isTransactionActive( bool checkIfReadOnly ){
163  bool ret = false;
164  if( m_dbSession.get().transaction().isActive() ){
165  if( checkIfReadOnly ){
166  if( m_dbSession.get().transaction().isReadOnly() ) ret = true;
167  } else {
168  ret = true;
169  }
170  }
171  return ret;
172 }
173 
175  if(!m_transactionCache->dbExistsLoaded()){
176  m_transactionCache->setDbExists( m_schema->exists() );
177  }
178  return m_transactionCache->dbExists();
179 }
180 
181 void ora::DatabaseSession::create( const std::string& userSchemaVersion ){
182  m_schema->create( userSchemaVersion );
183  m_contIdSequence->create();
184  m_mappingDb->setUp();
185  m_transactionCache->setDbExists( true );
186 }
187 
189  if(!testDropPermission()){
190  throwException("Drop permission has been denied for the current user.",
191  "DatabaseSession::drop");
192  }
193  m_schema->drop();
194  m_transactionCache->dropDatabase();
195 }
196 
198  bool forWrite ){
199  m_schema->setAccessPermission( principal, forWrite );
200 }
201 
203  if(!m_transactionCache->dropPermissionLoaded()){
204  m_transactionCache->setDropPermission( m_schema->testDropPermission() );
205  }
206  return m_transactionCache->dropPermission();
207 }
208 
210  if(!m_transactionCache->isLoaded()){
211  std::map<std::string, ContainerHeaderData> containersData;
212  m_schema->containerHeaderTable().getContainerData( containersData );
213  for(std::map<std::string, ContainerHeaderData>::iterator iC = containersData.begin();
214  iC != containersData.end(); ++iC){
215  Handle<DatabaseContainer> container( new DatabaseContainer( iC->second.id, iC->first,
216  iC->second.className,
217  iC->second.numberOfObjects, *this ) );
218  m_transactionCache->addContainer( iC->second.id, iC->first, container );
219  }
220  m_schema->mainTable().getParameters( m_transactionCache->dbParams() );
221  m_transactionCache->setLoaded();
222  }
223 }
224 
225 std::string ora::DatabaseSession::schemaVersion( bool userSchema ){
226  std::map<std::string,std::string>& params = m_transactionCache->dbParams();
227  std::string version("");
228  std::string paramName = IMainTable::versionParameterName();
229  if(userSchema ) paramName = IMainTable::userSchemaVersionParameterName();
230  std::map<std::string,std::string>::const_iterator iPar = params.find( paramName );
231  if( iPar != params.end() ){
232  version = iPar->second;
233  }
234  return version;
235 }
236 
238  const std::string& className ){
239  int newContId = m_contIdSequence->getNextId( true );
240  m_schema->containerHeaderTable().addContainer( newContId, containerName, className );
241  Handle<DatabaseContainer> container( new DatabaseContainer( newContId, containerName,
242  className, 0, *this ) );
243  m_transactionCache->addContainer( newContId, containerName, container );
244  return container;
245 }
246 
247 
249  const Reflex::Type& type ){
250  // create the container
251  int newContId = m_contIdSequence->getNextId( true );
252  Handle<DatabaseContainer> newCont ( new DatabaseContainer( newContId, containerName, type, *this ) );
253  m_transactionCache->addContainer( newContId, containerName, newCont );
254  newCont->create();
255  return newCont;
256 }
257 
258 void ora::DatabaseSession::dropContainer( const std::string& name ){
259  Handle<DatabaseContainer> cont = m_transactionCache->getContainer( name );
260  m_transactionCache->eraseContainer( cont->id(), name );
261  cont->drop();
262 }
263 
265  return m_transactionCache->getContainer( name );
266 }
267 
269  return m_transactionCache->getContainer( contId );
270 }
271 
272 const std::map<int, ora::Handle<ora::DatabaseContainer> >& ora::DatabaseSession::containers(){
273  return m_transactionCache->containers();
274 }
275 
276 void ora::DatabaseSession::setObjectName( const std::string& name,
277  int containerId,
278  int itemId ){
279  m_schema->namingServiceTable().setObjectName( name, containerId, itemId );
280 }
281 
282 bool ora::DatabaseSession::eraseObjectName( const std::string& name ){
283  return m_schema->namingServiceTable().eraseObjectName( name );
284 }
285 
287  return m_schema->namingServiceTable().eraseAllNames();
288 }
289 
292  std::pair<int,int> oid;
293  if( m_schema->namingServiceTable().getObjectByName( name, oid ) ){
294  ora::Handle<ora::DatabaseContainer> cont = containerHandle( oid.first );
295  if( cont ) ret = Object( cont->fetchItem( oid.second ), cont->type() );
296  }
297  return ret;
298 }
299 
300 bool ora::DatabaseSession::getItemId( const std::string& name, ora::OId& destination ){
301  std::pair<int,int> oidData;
302  if( m_schema->namingServiceTable().getObjectByName( name, oidData ) ){
303  destination = OId( oidData.first, oidData.second );
304  return true;
305  }
306  return false;
307 }
308 
309 boost::shared_ptr<void> ora::DatabaseSession::fetchTypedObjectByName( const std::string& name,
310  const Reflex::Type& asType ){
311  boost::shared_ptr<void> ret = m_transactionCache->getNamedReference( name );
312  if( !ret.get() ){
313  std::pair<int,int> oid;
314  if( m_schema->namingServiceTable().getObjectByName( name, oid ) ){
315  ora::Handle<ora::DatabaseContainer> cont = containerHandle( oid.first );
316  void* ptr = 0;
317  if( cont ) {
318  ptr = cont->fetchItemAsType( oid.second, asType );
319  if( ptr) ret = boost::shared_ptr<void>( ptr, RflxDeleter( cont->type() ) );
320  }
321  }
322  if( ret.get() ) m_transactionCache->setNamedReference( name, ret );
323  }
324  return ret;
325 }
326 
328  std::vector<std::string>& destination ){
329  return m_schema->namingServiceTable().getNamesForContainer( containerId, destination );
330 }
331 
333  int itemId,
334  std::vector<std::string>& destination ){
335  return m_schema->namingServiceTable().getNamesForObject( containerId, itemId, destination );
336 }
337 
338 bool ora::DatabaseSession::listObjectNames( std::vector<std::string>& destination ){
339 
340  return m_schema->namingServiceTable().getAllNames( destination );
341 }
342 
344  if( !m_transactionCache->utility() ){
346  m_transactionCache->setUtility( util );
347  }
348  return m_transactionCache->utility();
349 }
350 
352  return *m_schema;
353 }
354 
356  return *m_contIdSequence;
357 }
358 
360  return *m_mappingDb;
361 }
362 
364  return m_configuration;
365 }
366 
368  return m_dbSession;
369 }
370 
371 boost::shared_ptr<ora::ConnectionPool>& ora::DatabaseSession::connectionPool(){
372  return m_connectionPool;
373 }
374 
376  return m_containerUpdateTable;
377 }
static std::string versionParameterName()
Handle< DatabaseContainer > containerHandle(const std::string &name)
const std::string & connectionString()
type
Definition: HCALResponse.h:22
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:78
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:132
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:83
int cont
Configuration & configuration()
void * fetchItemAsType(int itemId, const Reflex::Type &asType)
static std::string sequenceNameForContainerId()
sequence names
Definition: MappingRules.cc:9
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:10
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)