CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/CondCore/ORA/src/SchemaUtils.cc

Go to the documentation of this file.
00001 #include "CondCore/ORA/interface/SchemaUtils.h"
00002 // externals
00003 #include "RelationalAccess/ISessionProxy.h"
00004 #include "RelationalAccess/ITransaction.h"
00005 #include "RelationalAccess/ConnectionService.h"
00006 #include "RelationalAccess/IConnectionServiceConfiguration.h"
00007 #include "RelationalAccess/ISchema.h"
00008 #include "RelationalAccess/ITable.h"
00009 #include "RelationalAccess/TableDescription.h"
00010 #include "RelationalAccess/ITableSchemaEditor.h"
00011 #include "RelationalAccess/ITablePrivilegeManager.h"
00012 #include "RelationalAccess/ITableDataEditor.h"
00013 #include "RelationalAccess/IForeignKey.h"
00014 #include "RelationalAccess/IQuery.h"
00015 #include "RelationalAccess/ICursor.h"
00016 #include "CoralBase/AttributeSpecification.h"
00017 #include "CoralBase/AttributeList.h"
00018 #include "CoralBase/Attribute.h"
00019 //
00020 #include <memory>
00021 
00022 void ora::SchemaUtils::cleanUp( const std::string& connectionString, std::set<std::string> exclusionList ){
00023   coral::ConnectionService connServ;
00024   std::auto_ptr<coral::ISessionProxy> session( connServ.connect( connectionString, coral::Update ));
00025   session->transaction().start();
00026   try{
00027     coral::ISchema& schema = session->nominalSchema();
00028     std::set<std::string> tables = schema.listTables();
00029     for( std::set<std::string>::const_iterator iEx = exclusionList.begin();
00030          iEx != exclusionList.end(); ++iEx ){
00031       tables.erase( *iEx );
00032     }
00033     for( std::set<std::string>::const_iterator iT = tables.begin();
00034          iT != tables.end(); ++iT ){
00035       coral::ITable& t = schema.tableHandle( *iT );
00036       int numFKs = t.description().numberOfForeignKeys();
00037       for( int ifk=0; ifk < numFKs; ifk++ ){
00038         // workaround: since the dropFK triggers a commit, the fk list is reset. therefore, always drop the fk id=0!!!
00039         t.schemaEditor().dropForeignKey( t.description().foreignKey( 0 ).name() );
00040       };
00041     }
00042     for( std::set<std::string>::const_iterator iT = tables.begin();
00043          iT != tables.end(); ++iT ){
00044       schema.dropIfExistsTable( *iT );
00045     }
00046     session->transaction().commit();
00047   } catch ( ... ){
00048     session->transaction().rollback();
00049     throw;
00050   }
00051 }  
00052 
00053 const std::string& ora::Serializer::tableName(){
00054   static const std::string s_tableName("ORA_LOCK");
00055   return s_tableName;
00056 }
00057 
00058 ora::Serializer::Serializer():
00059   m_connServ( new coral::ConnectionService ),
00060   m_session(),
00061   m_lock( false ){
00062 }
00063       
00064 ora::Serializer::~Serializer(){
00065   release();
00066 }
00067     
00068 void ora::Serializer::lock( const std::string& connectionString ){
00069   if( !m_lock ){
00070     m_connServ->configuration().setConnectionTimeOut(0);
00071     m_session.reset( m_connServ->connect( connectionString, coral::Update ) );
00072     m_session->transaction().start( false );
00073     coral::ISchema& schema = m_session->nominalSchema();
00074     if(!schema.existsTable( tableName() )){
00075       coral::TableDescription descr( "OraDb" );
00076       descr.setName( tableName() );
00077       descr.insertColumn( "P_LOCK",
00078                           coral::AttributeSpecification::typeNameForType<int>() );
00079       descr.setNotNullConstraint( "P_LOCK" );
00080       descr.setPrimaryKey( std::vector<std::string>( 1, "P_LOCK" ) );
00081       coral::ITable& table = schema.createTable( descr );
00082       table.privilegeManager().grantToPublic( coral::ITablePrivilegeManager::Select );
00083     }
00084     coral::ITable& table = schema.tableHandle( tableName() );
00085     std::string condition("P_LOCK = 1");
00086     std::auto_ptr<coral::IQuery> query( table.newQuery() );
00087     query->addToOutputList( "P_LOCK" );
00088     query->defineOutputType( "P_LOCK", coral::AttributeSpecification::typeNameForType<int>());
00089     query->setCondition( condition, coral::AttributeList() );
00090     query->setForUpdate();
00091     coral::ICursor& cursor = query->execute();
00092     coral::AttributeList data;
00093     data.extend<int>( "P_LOCK" );
00094     data["P_LOCK"].data<int>() = 1;
00095     if( cursor.next() ){
00096       // row found. will be locked by the DB if some other session owns the transaction...
00097       std::string setCLause = "P_LOCK = :P_LOCK";
00098       table.dataEditor().updateRows( setCLause, condition , data );
00099     } else {
00100       // no row found... no lock!
00101       table.dataEditor().insertRow( data );
00102     }
00103     m_lock = true;
00104   }
00105 }
00106     
00107 
00108 void ora::Serializer::release(){
00109   if( m_lock ){
00110     m_lock = false;
00111     m_session->transaction().commit();
00112   }
00113 }
00114     
00115