ATLAS Offline Software
Loading...
Searching...
No Matches
RDBVersionAccessor.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
14
15#include "RDBVersionAccessor.h"
16
17#include "RelationalAccess/ISessionProxy.h"
18#include "RelationalAccess/ICursor.h"
19#include "RelationalAccess/ITable.h"
20#include "RelationalAccess/ISchema.h"
21#include "RelationalAccess/IQuery.h"
22#include "RelationalAccess/SchemaException.h"
23
25
26#include "CoralBase/Attribute.h"
27#include "CoralBase/AttributeList.h"
28
29#include "GaudiKernel/MsgStream.h"
30
31#include <stdexcept>
32#include <sstream>
33
34RDBVersionAccessor::RDBVersionAccessor(const std::string& childNode
35 , const std::string& parentNode
36 , const std::string& parentTag
37 , coral::ISessionProxy* session
38 , MsgStream& msgStream)
39 : m_session(session)
40 , m_childNode(childNode)
41 , m_parentNode(parentNode)
42 , m_parentTag(parentTag)
43 , m_tagName("")
44 , m_tagID("")
45 , m_isChildLeaf(false)
46 , m_msgStream(msgStream)
47{
48}
49
51{
52 std::string parentNodeId, childNodeId, parentTagId;
53 int nRows{};
54
55 if(!m_session) {
56 m_msgStream << MSG::ERROR << "VersionAccessor: No connection to database!" << endmsg;
57 return;
58 }
59
60 try {
61 if(m_msgStream.level()==MSG::VERBOSE) {
62 m_msgStream << MSG::VERBOSE << "VersionAccessor: Version accessor for \n ChildNode = " << m_childNode
63 << " ParentNode = " << m_parentNode
64 << " ParentTag = " << m_parentTag << endmsg;
65 }
66
67 coral::ITable& tableTag2Node = m_session->nominalSchema().tableHandle("HVS_TAG2NODE");
68 coral::ITable& tableNode = m_session->nominalSchema().tableHandle("HVS_NODE");
69 coral::ITable& tableLtag2Ltag = m_session->nominalSchema().tableHandle("HVS_LTAG2LTAG");
70
71 //
72 // STEP 1. Get TagId for parentTag
73 //
74 // Query: "SELECT TAG_ID FROM HVS_TAG2NODE WHERE TAG_NAME='m_parentTag'"
75 //
76 coral::IQuery *queryTag2Node = tableTag2Node.newQuery();
77 queryTag2Node->addToOutputList("TAG_ID");
78 queryTag2Node->setMemoryCacheSize(1);
79 coral::AttributeList bindsTag2Node ATLAS_THREAD_SAFE;
80 bindsTag2Node.extend<std::string>("tagN");
81 queryTag2Node->setCondition("TAG_NAME=:tagN", bindsTag2Node);
82 bindsTag2Node[0].data<std::string>() = m_parentTag;
83
84 coral::ICursor& cursorTag2Node = queryTag2Node->execute();
85 nRows = 0;
86 while(cursorTag2Node.next()) {
87 if(++nRows>1) {
88 delete queryTag2Node;
89 throw std::runtime_error( "The tag " + m_parentTag + " is not unique in HVS_TAG2NODE table!");
90 }
91
92 const coral::AttributeList& row = cursorTag2Node.currentRow();
93 parentTagId = attribute2String(row,"TAG_ID");
94
95 }
96 if(nRows==0) {
97 delete queryTag2Node;
98 throw std::runtime_error( "The tag " + m_parentTag + " not found in HVS_TAG2NODE table!");
99 }
100
101 delete queryTag2Node;
102 if(m_msgStream.level()==MSG::VERBOSE) {
103 m_msgStream << MSG::VERBOSE << "VersionAccessor: Parent Tag Id = " << parentTagId << endmsg;
104 }
105
106 //
107 // STEP 2. Get NodeIDs for parentNode and child
108 //
109 // Query: "SELECT NODE_NAME, NODE_ID, BRANCH_FLAG FROM HVS_NODE
110 // WHERE NODE_NAME='m_parentNode' OR NODE_NAME='m_childNode'"
111 //
112
113 coral::IQuery* queryNodeIDs = tableNode.newQuery();
114
115 queryNodeIDs->addToOutputList("NODE_NAME");
116 queryNodeIDs->addToOutputList("NODE_ID");
117 queryNodeIDs->addToOutputList("BRANCH_FLAG");
118 queryNodeIDs->setMemoryCacheSize(2);
119
120 coral::AttributeList bindsNodeIDs ATLAS_THREAD_SAFE;
121 bindsNodeIDs.extend<std::string>("parentN");
122 bindsNodeIDs.extend<std::string>("childN");
123
124 queryNodeIDs->setCondition( "NODE_NAME=:parentN OR NODE_NAME=:childN", bindsNodeIDs);
125
126 bindsNodeIDs[0].data<std::string>() = m_parentNode;
127 bindsNodeIDs[1].data<std::string>() = m_childNode;
128
129 coral::ICursor& cursorNodeIDs = queryNodeIDs->execute();
130
131 nRows = 0;
132 while(cursorNodeIDs.next()) {
133 if(++nRows>2) {
134 break;
135 }
136 const coral::AttributeList& row = cursorNodeIDs.currentRow();
137
138 if(attribute2String(row,"NODE_NAME")==m_childNode) {
139 childNodeId = attribute2String(row,"NODE_ID");
140 m_isChildLeaf = (attribute2String(row,"BRANCH_FLAG")=="0");
141 }
142 else {
143 parentNodeId = attribute2String(row,"NODE_ID");
144 }
145 }
146 if(nRows!=2 && m_childNode!=m_parentNode) {
147 delete queryNodeIDs;
148 throw std::runtime_error("Error processing HVS_NODE, Number of fetched records !=2");
149 }
150
151 if(m_msgStream.level()==MSG::VERBOSE) {
152 m_msgStream << MSG::VERBOSE << "VersionAccessor: Child Node Id = " << childNodeId << endmsg;
153 m_msgStream << MSG::VERBOSE << "VersionAccessor: Parent Node Id = " << parentNodeId << endmsg;
154 }
155
156 delete queryNodeIDs;
157
160 m_tagID = std::move(parentTagId);
161 return;
162 }
163
164 //
165 // STEP 3.
166 //
167 // *********************** THIS IS A TEMPORARY SOLUTION ************************
168 // *** ***
169 // *** 1. First we determine a path from childNode to parentNode querying ***
170 // *** HVS_NODE few times ***
171 // *** 2. Then we query HVS_LTAG2LTAG few times to get tag of childNode ***
172 // *** of childNode ***
173 // *** ***
174 // *********** ************ ********** ************* ************ **************
175
176 std::vector<std::string> path;
177 path.push_back(childNodeId);
178 std::string currentParrent = childNodeId;
179 std::string currentChild = std::move(childNodeId);
180
181 coral::AttributeList bindsNode ATLAS_THREAD_SAFE;
182 bindsNode.extend<std::string>("nodeId");
183 const std::string parentIdStr{"PARENT_ID"};
184 const std::string conditionStr{"NODE_ID=:nodeId"};
185 while(currentParrent != parentNodeId) {
186 //
187 // Query: "SELECT PARENT_ID FROM HVS_NODE WHERE NODE_ID='currentChild'"
188 //
189 coral::IQuery* queryNode(tableNode.newQuery());
190
191 queryNode->addToOutputList(parentIdStr);
192 queryNode->setMemoryCacheSize(1);
193
194 queryNode->setCondition(conditionStr, bindsNode);
195 bindsNode[0].data<std::string>()=currentChild;
196
197 coral::ICursor& cursorNode = queryNode->execute();
198
199 nRows = 0;
200
201 // Process Query results
202 while(cursorNode.next()) {
203 if(++nRows>1) {
204 delete queryNode;
205 throw std::runtime_error("The node " + currentChild + " has more than one parent!");
206 }
207
208 const coral::AttributeList& row = cursorNode.currentRow();
209
210 if(row[0].isNull()) {
211 delete queryNode;
212 throw std::runtime_error("The requested child and parent nodes are not on the same branch!");
213 }
214
215 currentParrent = attribute2String(row,parentIdStr);
216 currentChild = currentParrent;
217 path.push_back(currentParrent);
218 }
219 if(nRows==0) {
220 delete queryNode;
221 throw std::runtime_error("The node " + currentChild + " has no parent!");
222 }
223
224 delete queryNode;
225 if(m_msgStream.level()==MSG::VERBOSE) {
226 m_msgStream << MSG::VERBOSE << "VersionAccessor: Current Child = " << currentChild << endmsg;
227 m_msgStream << MSG::VERBOSE << "VersionAccessor: Current Parrent = " << currentParrent << endmsg;
228 }
229 }
230
231 coral::AttributeList bindsLtag2Ltag ATLAS_THREAD_SAFE;
232 bindsLtag2Ltag.extend<std::string>("childN");
233 bindsLtag2Ltag.extend<std::string>("parentT");
234 bindsLtag2Ltag.extend<std::string>("parentN");
235
236 const std::string childTagString{"CHILD_TAG"};
237 const std::string childConditionStr = "CHILD_NODE =:childN AND PARENT_TAG =:parentT AND PARENT_NODE =:parentN";
238 for(unsigned int ind=1; ind<path.size(); ind++) {
239 // Query: "SELECT CHILD_TAG FROM HVS_LTAG2LTAG WHERE
240 // CHILD_NODE = 'path[path.size()-ind-1]' AND
241 // PARENT_TAG = 'parentTagId' AND
242 // PARENT_NODE = 'path[path.size()-ind]'
243 coral::IQuery* queryLtag2Ltag(tableLtag2Ltag.newQuery());
244
245 queryLtag2Ltag->addToOutputList(childTagString);
246 queryLtag2Ltag->setMemoryCacheSize(1);
247
248 queryLtag2Ltag->setCondition(childConditionStr, bindsLtag2Ltag);
249
250 bindsLtag2Ltag[0].data<std::string>() = path[path.size()-ind-1];
251 bindsLtag2Ltag[1].data<std::string>() = parentTagId;
252 bindsLtag2Ltag[2].data<std::string>() = path[path.size()-ind];
253
254 coral::ICursor& cursorLtag2Ltag = queryLtag2Ltag->execute();
255
256 nRows = 0;
257 // Process Query results
258 while(cursorLtag2Ltag.next()) {
259 if(++nRows>1) {
260 delete queryLtag2Ltag;
261 throw std::runtime_error("Version " + parentTagId +
262 " has more than one child of type " + path[path.size()-ind-1] + "!");
263 }
264
265 const coral::AttributeList& row = cursorLtag2Ltag.currentRow();
266 parentTagId = attribute2String(row,childTagString);
267 }
268 if(nRows==0) {
269 delete queryLtag2Ltag;
270 throw std::runtime_error("Version " + parentTagId + " has no child of type " + path[path.size()-ind-1] + "!");
271 }
272
273 delete queryLtag2Ltag;
274
275 if(m_msgStream.level()==MSG::VERBOSE) {
276 m_msgStream << MSG::VERBOSE << "VersionAccessor: Parent Tag Id = " << parentTagId << endmsg;
277 }
278 }
279
280 //
281 // STEP 4. Get tag name for obtained TAG_ID
282 //
283 // Query: "SELECT TAG_NAME from HVS_TAG2NODE WHERE TAG_ID='parentTagId'"
284 //
285 coral::IQuery* queryTagName(tableTag2Node.newQuery());
286 const std::string tagNameStr{"TAG_NAME"};
287 queryTagName->addToOutputList(tagNameStr);
288 queryTagName->setMemoryCacheSize(1);
289
290 coral::AttributeList bindsTagName ATLAS_THREAD_SAFE;
291 bindsTagName.extend<std::string>("tagId");
292
293 queryTagName->setCondition("TAG_ID=:tagId", bindsTagName);
294 bindsTagName[0].data<std::string>() = parentTagId;
295
296 coral::ICursor& cursorTagName = queryTagName->execute();
297
298 nRows = 0;
299 while(cursorTagName.next()) {
300 if(++nRows>1) {
301 delete queryTagName;
302 throw std::runtime_error("More than one record retrieved when getting tag name for given ID");
303 }
304
305 const coral::AttributeList& row = cursorTagName.currentRow();
306
307 m_tagName =attribute2String(row,tagNameStr);
308 m_tagID = parentTagId;
309
310 if(m_msgStream.level()==MSG::VERBOSE) {
311 m_msgStream << MSG::VERBOSE << "VersionAccessor: Child Tag Name = " << m_tagName << endmsg;
312 }
313 }
314 delete queryTagName;
315
316 }
317 catch(coral::SchemaException& se)
318 {
319 m_msgStream << MSG::ERROR << "VersionAccessor: Schema Exception : " << se.what() << endmsg;
320 }
321 catch(std::exception& e)
322 {
323 if(m_msgStream.level()<=MSG::DEBUG)
324 m_msgStream << MSG::DEBUG << e.what() << endmsg;
325 }
326 catch(...)
327 {
328 m_msgStream << MSG::ERROR << "VersionAccessor: Exception caught(...)" << endmsg;
329 }
330}
331
332std::string RDBVersionAccessor::attribute2String(const coral::AttributeList& attList,
333 const std::string& fieldName)
334{
335 std::ostringstream streamValue;
336 attList[fieldName].toOutputStream(streamValue);
337 std::string stringValue = streamValue.str();
338 unsigned int position = stringValue.find(") :");
339 return stringValue.substr(position+4);
340}
#define endmsg
Definition of RDBVersionAccessor class.
Define macros for attributes used to control the static checker.
#define ATLAS_THREAD_SAFE
RDBVersionAccessor()
Default constructor.
coral::ISessionProxy * m_session
void getChildTagData()
Constructs SQL query and retrieves child tag ID and Name from DB.
std::string attribute2String(const coral::AttributeList &attList, const std::string &fieldName)