ATLAS Offline Software
Loading...
Searching...
No Matches
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
32namespace 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 //
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 //
152 std::set<coral::IQuery*>::iterator it = m_queries.find(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
#define endmsg
if(febId1==febId2)
std::map< std::string, double > instance
void DestroyQuery(coral::IQuery *query)
coral::ISessionProxy * m_session
CalibDbConnection(const std::string &ConnectionString, const std::string &WorkingSchema)
coral::ITableDataEditor & GetTableEditor(const std::string &table_name)
std::set< coral::IQuery * > m_queries
coral::IRelationalDomain & domain(const std::string &connectionString)
singleton-like access to IMessageSvc via open function and helper
IMessageSvc * getMessageSvc(bool quiet=false)
CscCalcPed - algorithm that finds the Cathode Strip Chamber pedestals from an RDO.
Definition query.py:1