test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SchemaUtils.cc
Go to the documentation of this file.
2 // externals
3 #include "RelationalAccess/ISessionProxy.h"
4 #include "RelationalAccess/ITransaction.h"
5 #include "RelationalAccess/ConnectionService.h"
6 #include "RelationalAccess/IConnectionServiceConfiguration.h"
7 #include "RelationalAccess/ISchema.h"
8 #include "RelationalAccess/ITable.h"
9 #include "RelationalAccess/TableDescription.h"
10 #include "RelationalAccess/ITableSchemaEditor.h"
11 #include "RelationalAccess/ITablePrivilegeManager.h"
12 #include "RelationalAccess/ITableDataEditor.h"
13 #include "RelationalAccess/IForeignKey.h"
14 #include "RelationalAccess/IQuery.h"
15 #include "RelationalAccess/ICursor.h"
16 #include "CoralBase/AttributeSpecification.h"
17 #include "CoralBase/AttributeList.h"
18 #include "CoralBase/Attribute.h"
19 //
20 #include <memory>
21 
22 void ora::SchemaUtils::cleanUp( const std::string& connectionString, std::set<std::string> exclusionList ){
23  coral::ConnectionService connServ;
24  std::auto_ptr<coral::ISessionProxy> session( connServ.connect( connectionString, coral::Update ));
25  session->transaction().start();
26  try{
27  coral::ISchema& schema = session->nominalSchema();
28  std::set<std::string> tables = schema.listTables();
29  for( std::set<std::string>::const_iterator iEx = exclusionList.begin();
30  iEx != exclusionList.end(); ++iEx ){
31  tables.erase( *iEx );
32  }
33  for( std::set<std::string>::const_iterator iT = tables.begin();
34  iT != tables.end(); ++iT ){
35  coral::ITable& t = schema.tableHandle( *iT );
36  int numFKs = t.description().numberOfForeignKeys();
37  for( int ifk=0; ifk < numFKs; ifk++ ){
38  // workaround: since the dropFK triggers a commit, the fk list is reset. therefore, always drop the fk id=0!!!
39  t.schemaEditor().dropForeignKey( t.description().foreignKey( 0 ).name() );
40  };
41  }
42  for( std::set<std::string>::const_iterator iT = tables.begin();
43  iT != tables.end(); ++iT ){
44  schema.dropIfExistsTable( *iT );
45  }
46  session->transaction().commit();
47  } catch ( ... ){
48  session->transaction().rollback();
49  throw;
50  }
51 }
52 
54  static const std::string s_tableName("ORA_LOCK");
55  return s_tableName;
56 }
57 
59  m_connServ( new coral::ConnectionService ),
60  m_session(),
61  m_lock( false ){
62 }
63 
65  release();
66 }
67 
69  if( !m_lock ){
70  m_connServ->configuration().setConnectionTimeOut(0);
71  m_session.reset( m_connServ->connect( connectionString, coral::Update ) );
72  m_session->transaction().start( false );
73  coral::ISchema& schema = m_session->nominalSchema();
74  if(!schema.existsTable( tableName() )){
75  coral::TableDescription descr( "OraDb" );
76  descr.setName( tableName() );
77  descr.insertColumn( "P_LOCK",
78  coral::AttributeSpecification::typeNameForType<int>() );
79  descr.setNotNullConstraint( "P_LOCK" );
80  descr.setPrimaryKey( std::vector<std::string>( 1, "P_LOCK" ) );
81  coral::ITable& table = schema.createTable( descr );
82  table.privilegeManager().grantToPublic( coral::ITablePrivilegeManager::Select );
83  }
84  coral::ITable& table = schema.tableHandle( tableName() );
85  std::string condition("P_LOCK = 1");
86  std::auto_ptr<coral::IQuery> query( table.newQuery() );
87  query->addToOutputList( "P_LOCK" );
88  query->defineOutputType( "P_LOCK", coral::AttributeSpecification::typeNameForType<int>());
89  query->setCondition( condition, coral::AttributeList() );
90  query->setForUpdate();
91  coral::ICursor& cursor = query->execute();
92  coral::AttributeList data;
93  data.extend<int>( "P_LOCK" );
94  data["P_LOCK"].data<int>() = 1;
95  if( cursor.next() ){
96  // row found. will be locked by the DB if some other session owns the transaction...
97  std::string setCLause = "P_LOCK = :P_LOCK";
98  table.dataEditor().updateRows( setCLause, condition , data );
99  } else {
100  // no row found... no lock!
101  table.dataEditor().insertRow( data );
102  }
103  m_lock = true;
104  }
105 }
106 
107 
109  if( m_lock ){
110  m_lock = false;
111  m_session->transaction().commit();
112  }
113 }
114 
115 
tuple schema
Definition: dataDML.py:2334
tuple tables
Definition: dataDML.py:2338
static const std::string & tableName()
Definition: SchemaUtils.cc:53
string connectionString
Definition: autoCondHLT.py:4
void cleanUp(const std::string &connectionString, std::set< std::string > exclusionList=std::set< std::string >())
Definition: SchemaUtils.cc:22
tuple query
Definition: o2o.py:269
volatile std::atomic< bool > shutdown_flag false
session
Definition: models.py:201
virtual ~Serializer()
Definition: SchemaUtils.cc:64
void lock(const std::string &connectionString)
Definition: SchemaUtils.cc:68