ATLAS Offline Software
CalibDbConnection.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // this
7 
8 #include <TString.h> // for Form
9 
10 #include <stdexcept>
11 
13 #include "CoralKernel/Context.h"
14 #include "GaudiKernel/MsgStream.h"
17 #include "RelationalAccess/IAuthenticationCredentials.h"
18 #include "RelationalAccess/IAuthenticationService.h"
19 #include "RelationalAccess/IConnectionService.h"
20 #include "RelationalAccess/ICursor.h"
21 #include "RelationalAccess/IQuery.h"
22 #include "RelationalAccess/IRelationalDomain.h"
23 #include "RelationalAccess/IRelationalService.h"
24 #include "RelationalAccess/ISchema.h"
25 #include "RelationalAccess/ISessionProxy.h"
26 #include "RelationalAccess/ITable.h"
27 #include "RelationalAccess/ITableDataEditor.h"
28 #include "RelationalAccess/ITransaction.h"
29 #include "RelationalAccess/SchemaException.h"
30 #include "iostream"
31 
32 namespace MuonCalib {
33 
35  // Constructor //
37 
38  CalibDbConnection::CalibDbConnection(const std::string& ConnectionString, const std::string& WorkingSchema) :
39  m_connection_string(ConnectionString),
40  m_working_schema(WorkingSchema),
41  m_comp_loaded(false),
42  m_context(&coral::Context::instance()),
43  m_session(nullptr),
44  m_transaction(false) {
45  coral::IHandle<coral::IConnectionService> lookSvcH = m_context->query<coral::IConnectionService>();
46  if (!lookSvcH.isValid()) {
47  m_context->loadComponent("CORAL/Services/ConnectionService");
48  lookSvcH = m_context->query<coral::IConnectionService>();
49  }
50  if (!lookSvcH.isValid()) { return; }
51  m_context->loadComponent("CORAL/Services/XMLAuthenticationService");
52  m_context->loadComponent("CORAL/Services/RelationalService");
53  m_comp_loaded = true;
54  m_session = nullptr;
55  }
56 
58  for (std::set<coral::IQuery*>::iterator it = m_queries.begin(); it != m_queries.end(); ++it) {
59  if (*it) { delete (*it); }
60  }
61  m_queries.clear();
62  }
63 
65  // OpenConnection //
67 
69  if (!m_comp_loaded) { return false; }
70  if (m_session) { return true; }
71 
72  try {
73  // Load CORAL connection service
74  coral::IHandle<coral::IConnectionService> lookSvcH = m_context->query<coral::IConnectionService>();
75  m_context->loadComponent("CORAL/Services/ConnectionService");
76  lookSvcH = m_context->query<coral::IConnectionService>();
77 
78  if (!lookSvcH.isValid()) {
79  throw std::runtime_error(
80  Form("File: %s, Line: %d\nCalibDbConnection::OpenConnection() - Could not locate the connection service!", __FILE__,
81  __LINE__));
82  }
83  // connection to CORAL
84  MsgStream log(Athena::getMessageSvc(), "CalibDbConnection");
85  log << MSG::INFO << "CalibDbConnection::OpenConnection: " << m_connection_string << " Schema: " << m_working_schema << endmsg;
86  m_session = lookSvcH->connect(m_connection_string, coral::Update);
87  return true;
88  } catch (coral::SchemaException& e) {
89  MsgStream log(Athena::getMessageSvc(), "CalibDbConnection");
90  log << MSG::WARNING << "Schema exception : " << e.what() << endmsg;
91  m_session = nullptr;
92  return false;
93  }
94  }
95 
97  // OpenTransaction //
100  if (!m_transaction) {
101  m_session->transaction().start();
102  m_transaction = true;
103  }
104  }
105 
107  // Commit //
110  if (m_transaction) {
111  m_session->transaction().commit();
112  m_transaction = false;
113  }
114  }
115 
117  // Rollback //
120  if (m_transaction) {
121  m_session->transaction().rollback();
122  m_transaction = false;
123  }
124  }
125 
127  // GetTableEditor //
129  coral::ITableDataEditor& CalibDbConnection::GetTableEditor(const std::string& table_name) {
130  return m_session->schema(m_working_schema).tableHandle(table_name).dataEditor();
131  }
132 
134  // GetQuery //
136  coral::IQuery* CalibDbConnection::GetQuery() {
137  if (!m_session) {
138  MsgStream log(Athena::getMessageSvc(), "CalibDbConnection");
139  log << MSG::WARNING << "No Session open!" << endmsg;
140  return nullptr;
141  }
142  coral::ISchema& workingSchema = m_session->schema(m_working_schema);
143  coral::IQuery* query = workingSchema.newQuery();
144  m_queries.insert(query);
145  return query;
146  }
147 
149  // GetQuery //
151  void CalibDbConnection::DestroyQuery(coral::IQuery* query) {
153  if (it == m_queries.end()) { return; }
154  m_queries.erase(it);
155  delete query;
156  }
157 
158  coral::IRelationalDomain& CalibDbConnection::domain(const std::string& connectionString) {
159  coral::IHandle<coral::IRelationalService> relationalService = m_context->query<coral::IRelationalService>();
160  if (!relationalService.isValid()) {
161  throw std::runtime_error(
162  Form("File: %s, Line: %d\nCalibDbConnection::domain() - Could not locate the relational service!", __FILE__, __LINE__));
163  }
164 
165  coral::IHandle<coral::IAuthenticationService> authenticationService = m_context->query<coral::IAuthenticationService>();
166  if (!authenticationService.isValid()) {
167  throw std::runtime_error(
168  Form("File: %s, Line: %d\nCalibDbConnection::domain() - Could not locate the authentication service!", __FILE__, __LINE__));
169  }
170  if (m_username == "") {
171  const coral::IAuthenticationCredentials& credentials = authenticationService->credentials(connectionString);
172  m_username = credentials.valueForItem("user");
173  m_password = credentials.valueForItem("password");
174  }
175  if (m_target_user != "" && m_target_user != m_username) { m_username = m_username + "[" + m_target_user + "]"; }
176  return relationalService->domainForConnection(connectionString);
177  }
178 
179 } // namespace MuonCalib
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
CalibDbConnection.h
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
MuonCalib::CalibDbConnection::Commit
void Commit()
Definition: CalibDbConnection.cxx:109
getMessageSvc.h
singleton-like access to IMessageSvc via open function and helper
MuonCalib::CalibDbConnection::Rollback
void Rollback()
Definition: CalibDbConnection.cxx:119
MuonCalib::CalibDbConnection::~CalibDbConnection
virtual ~CalibDbConnection()
Definition: CalibDbConnection.cxx:57
MuonCalib::CalibDbConnection::m_working_schema
std::string m_working_schema
Definition: CalibDbConnection.h:54
MuonCalib::CalibDbConnection::m_transaction
bool m_transaction
Definition: CalibDbConnection.h:69
skel.it
it
Definition: skel.GENtoEVGEN.py:396
MuonCalib::CalibDbConnection::m_username
std::string m_username
Definition: CalibDbConnection.h:63
MuonCalib::CalibDbConnection::domain
coral::IRelationalDomain & domain(const std::string &connectionString)
Definition: CalibDbConnection.cxx:158
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
MuonCalib::CalibDbConnection::m_password
std::string m_password
Definition: CalibDbConnection.h:65
coral
Definition: ISecondaryEventSelector.h:19
instance
std::map< std::string, double > instance
Definition: Run_To_Get_Tags.h:8
query
Definition: query.py:1
MuonCalib::CalibDbConnection::DestroyQuery
void DestroyQuery(coral::IQuery *query)
Definition: CalibDbConnection.cxx:151
MuonCalib::CalibDbConnection::GetQuery
coral::IQuery * GetQuery()
Definition: CalibDbConnection.cxx:136
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
MuonCalib::CalibDbConnection::m_context
coral::Context * m_context
Definition: CalibDbConnection.h:59
MuonCalib
CscCalcPed - algorithm that finds the Cathode Strip Chamber pedestals from an RDO.
Definition: CscCalcPed.cxx:22
MuonCalib::CalibDbConnection::m_target_user
std::string m_target_user
Definition: CalibDbConnection.h:67
MuonCalib::CalibDbConnection::OpenTransaction
void OpenTransaction()
Definition: CalibDbConnection.cxx:99
MuonCalib::CalibDbConnection::CalibDbConnection
CalibDbConnection(const std::string &ConnectionString, const std::string &WorkingSchema)
Definition: CalibDbConnection.cxx:38
query_example.query
query
Definition: query_example.py:15
MuonCalib::CalibDbConnection::m_queries
std::set< coral::IQuery * > m_queries
Definition: CalibDbConnection.h:57
MuonCalib::CalibDbConnection::m_connection_string
std::string m_connection_string
Definition: CalibDbConnection.h:54
MuonCalib::CalibDbConnection::m_session
coral::ISessionProxy * m_session
Definition: CalibDbConnection.h:61
RegionSelectorBase.h
MuonCalib::CalibDbConnection::m_comp_loaded
bool m_comp_loaded
Definition: CalibDbConnection.h:55
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
MuonCalib::CalibDbConnection::OpenConnection
bool OpenConnection()
Definition: CalibDbConnection.cxx:68
MuonCalib::CalibDbConnection::GetTableEditor
coral::ITableDataEditor & GetTableEditor(const std::string &table_name)
Definition: CalibDbConnection.cxx:129
RegionSelectionSvc.h