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