13 #include "RelationalAccess/IConnectionServiceConfiguration.h"
14 #include "RelationalAccess/ISessionProxy.h"
15 #include "RelationalAccess/ITransaction.h"
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;
34 m_table.erase( contId );
48 m_connectionString(
"" ),
54 m_containerUpdateTable(),
62 m_connectionPool( connectionPool ),
64 m_connectionString(
"" ),
70 m_containerUpdateTable(),
81 m_dbSession = m_connectionPool->connect( connectionString, readOnly?coral::ReadOnly:coral::Update );
82 if(m_dbSession.isValid()) {
83 m_connectionString = connectionString;
94 m_dbSession = m_connectionPool->connect( connectionString, asRole, readOnly?coral::ReadOnly:coral::Update );
95 if(m_dbSession.isValid()) {
96 m_connectionString = connectionString;
101 return isConnected();
107 m_ownedTransaction =
false;
109 if(m_dbSession.isValid()) {
110 m_connectionString = connectionString;
111 m_schemaName = schemaName;
116 return isConnected();
120 m_transactionCache.reset();
122 m_contIdSequence.reset();
124 m_containerUpdateTable.clear();
128 if( isConnected() && m_ownedTransaction ){
129 if( isTransactionActive()) rollbackTransaction();
133 m_connectionString.clear();
134 if(m_monitoring) m_monitoring->stop();
138 return m_dbSession.isValid();
142 return m_connectionString;
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 );
155 m_monitoring->newTransaction();
161 if( m_transactionCache.get() ){
162 m_schema->containerHeaderTable().updateNumberOfObjects( m_containerUpdateTable.table() );
163 m_dbSession.get().transaction().commit();
166 m_monitoring->stopTransaction();
172 if( m_transactionCache.get() ){
173 m_dbSession.get().transaction().rollback();
176 m_monitoring->stopTransaction(
false);
183 if( m_dbSession.get().transaction().isActive() ){
184 if( checkIfReadOnly ){
185 if( m_dbSession.get().transaction().isReadOnly() ) ret =
true;
194 if(!m_transactionCache->dbExistsLoaded()){
195 m_transactionCache->setDbExists( m_schema->exists() );
197 return m_transactionCache->dbExists();
201 m_schema->create( userSchemaVersion );
202 m_contIdSequence->create();
203 m_mappingDb->setUp();
204 m_transactionCache->setDbExists(
true );
208 if(!testDropPermission()){
209 throwException(
"Drop permission has been denied for the current user.",
210 "DatabaseSession::drop");
213 m_transactionCache->dropDatabase();
218 m_schema->setAccessPermission( principal, forWrite );
222 if(!m_transactionCache->dropPermissionLoaded()){
223 m_transactionCache->setDropPermission( m_schema->testDropPermission() );
225 return m_transactionCache->dropPermission();
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){
235 iC->second.className,
236 iC->second.numberOfObjects, *
this ) );
237 m_transactionCache->addContainer( iC->second.id, iC->first, container );
239 m_schema->mainTable().getParameters( m_transactionCache->dbParams() );
240 m_transactionCache->setLoaded();
245 std::map<std::string,std::string>& params = m_transactionCache->dbParams();
249 std::map<std::string,std::string>::const_iterator iPar = params.find( paramName );
250 if( iPar != params.end() ){
251 version = iPar->second;
258 int newContId = m_contIdSequence->getNextId(
true );
259 m_schema->containerHeaderTable().addContainer( newContId, containerName, className );
261 className, 0, *
this ) );
262 m_transactionCache->addContainer( newContId, containerName, container );
270 int newContId = m_contIdSequence->getNextId(
true );
272 m_transactionCache->addContainer( newContId, containerName, newCont );
279 m_transactionCache->eraseContainer( cont->
id(),
name );
284 return m_transactionCache->getContainer( name );
288 return m_transactionCache->getContainer( contId );
292 return m_transactionCache->containers();
298 m_schema->namingServiceTable().setObjectName( name, containerId, itemId );
302 return m_schema->namingServiceTable().eraseObjectName( name );
306 return m_schema->namingServiceTable().eraseAllNames();
311 std::pair<int,int> oid;
312 if( m_schema->namingServiceTable().getObjectByName( name, oid ) ){
320 std::pair<int,int> oidData;
321 if( m_schema->namingServiceTable().getObjectByName( name, oidData ) ){
322 destination =
OId( oidData.first, oidData.second );
330 boost::shared_ptr<void>
ret = m_transactionCache->getNamedReference( name );
332 std::pair<int,int> oid;
333 if( m_schema->namingServiceTable().getObjectByName( name, oid ) ){
338 if( ptr) ret = boost::shared_ptr<void>( ptr,
RflxDeleter( cont->
type() ) );
341 if( ret.get() ) m_transactionCache->setNamedReference( name, ret );
348 return m_schema->namingServiceTable().getNamesForContainer( containerId, destination );
354 return m_schema->namingServiceTable().getNamesForObject( containerId, itemId, destination );
359 return m_schema->namingServiceTable().getAllNames( destination );
363 if( !m_transactionCache->utility() ){
365 m_transactionCache->setUtility( util );
367 return m_transactionCache->utility();
375 return *m_contIdSequence;
383 return m_configuration;
391 return m_connectionPool;
395 return m_containerUpdateTable;
static std::string versionParameterName()
Handle< DatabaseContainer > containerHandle(const std::string &name)
const std::string & connectionString()
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)
const std::map< int, unsigned int > & table()
static Monitoring & get()
std::string schemaVersion(bool userSchema)
bool eraseObjectName(const std::string &name)
void setAccessPermission(const std::string &principal, bool forWrite)
bool testDropPermission()
SessionMonitoringData * startSession(const std::string &connectionString)
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)
virtual ~DatabaseSession()
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)
void throwException(const std::string &message, const std::string &methodName) __attribute__((noreturn))
Configuration & configuration()
void rollbackTransaction()
void * fetchItemAsType(int itemId, const Reflex::Type &asType)
static std::string sequenceNameForContainerId()
sequence names
tuple size
Write out results.
std::string className(const T &t)
boost::shared_ptr< ConnectionPool > m_connectionPool
Handle< ora::DatabaseContainer > addContainer(const std::string &containerName, const std::string &className)