ATLAS Offline Software
Loading...
Searching...
No Matches
RDBQuery.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "RDBQuery.h"
6#include "RDBAccessSvc.h"
7#include <stdexcept>
8
9#include "RelationalAccess/ICursor.h"
10#include "RelationalAccess/IQuery.h"
11#include "RelationalAccess/ISessionProxy.h"
12#include "RelationalAccess/ISchema.h"
13#include "RelationalAccess/ITable.h"
14#include "RelationalAccess/ITableDescription.h"
15#include "RelationalAccess/IColumn.h"
16
17#include "RelationalAccess/SchemaException.h"
18#include "CoralBase/Exception.h"
19#include "CoralBase/AttributeList.h"
20#include "CoralBase/Attribute.h"
21
23
25 , RDBAccessSvc* accessSvc
26 , const std::string& nodeName
27 , const std::string& tagId
28 , const std::string& connName)
29 : IRDBQuery()
30 , m_dblock (dblock)
31 , m_accessSvc(accessSvc)
32 , m_nodeName(nodeName)
33 , m_tagId(tagId)
34 , m_connName(connName)
35 , m_size(0)
36 , m_cursor(nullptr)
37 , m_executed(false)
38{
39}
40
44
46{
47 if(m_executed) {
48 m_accessSvc->msg() << MSG::WARNING << "RDBQuery cannot be executed more than once! Query: "
49 << m_nodeName << ", "
50 << m_tagId << ", "
51 << m_orderField << ", "
52 << m_fields.size() << endmsg;
53 return;
54 }
55
56 if(m_accessSvc->msg().level() <= MSG::DEBUG) {
57 m_accessSvc->msg() << MSG::DEBUG << "Query execute " << m_nodeName << ", "
58 << m_tagId << ", "
59 << m_orderField << ", "
60 << m_fields.size() << endmsg;
61 }
62
63 if (!m_accessSvc->connect(m_connName)) {
64 m_accessSvc->msg() << MSG::ERROR << "Cannot connect to the database: "
65 << m_connName << endmsg;
66 throw std::runtime_error( "Cannot connect to the database " + m_connName);
67 }
68
69 m_executed=true;
70 try {
71 // ... Get the node name and change to to Upper Case
72 std::string upperName = m_nodeName;
73 for (char& ch : upperName) {
74 ch = std::toupper (static_cast<unsigned int>(ch));
75 }
76
77 // ... Create query objects
78 m_query.reset (m_accessSvc->getSession(m_connName)->nominalSchema().newQuery());
79 m_queryCount.reset (m_accessSvc->getSession(m_connName)->nominalSchema().newQuery());
80
81 // Add fields
82 if(m_fields.size()>0) {
83 // Custom fields
84 for(unsigned int i=0; i<m_fields.size(); ++i) {
85 m_query->addToOutputList(upperName+"_DATA."+m_fields[i]);
86 }
87 }
88 else {
89 // All fields from the table
90 const coral::ITableDescription& dataTableDesc = m_accessSvc->getSession(m_connName)->nominalSchema().tableHandle(upperName + "_DATA").description();
91 for(int i=0; i<dataTableDesc.numberOfColumns(); ++i) {
92 m_query->addToOutputList(upperName+"_DATA."+dataTableDesc.columnDescription(i).name());
93 }
94 }
95
96 m_queryCount->addToOutputList("COUNT("+upperName+"_DATA_ID)","SUMREC");
97
98 // ... Define table list
99 m_query->addToTableList(upperName + "_DATA");
100 m_query->addToTableList(upperName + "_DATA2TAG");
101
102 m_queryCount->addToTableList(upperName + "_DATA2TAG");
103
104 // ... Define order
105 if(m_orderField.empty()) {
106 m_query->addToOrderList(upperName + "_DATA." + upperName + "_DATA_ID");
107 }
108 else {
109 m_query->addToOrderList(upperName + "_DATA." + m_orderField);
110 }
111
112 // ... Define conditions
113 coral::AttributeList bindsData ATLAS_THREAD_SAFE;
114 bindsData.extend<std::string>("tagID");
115
116 std::string queryStructCondition = upperName +"_DATA2TAG." + upperName + "_TAG_ID =:tagID";
117 m_queryCount->setCondition(queryStructCondition , bindsData);
118
119 queryStructCondition += " AND " + upperName +"_DATA." + upperName + "_DATA_ID=" + upperName + "_DATA2TAG." + upperName + "_DATA_ID";
120 m_query->setCondition(queryStructCondition , bindsData);
121
122 bindsData[0].data<std::string>() = m_tagId;
123
124 m_queryCount->defineOutputType("SUMREC","long");
125 m_query->setMemoryCacheSize(1);
126
127 // ... Get size of the result set
128 coral::ICursor& cursorCount = m_queryCount->execute();
129 while(cursorCount.next()) {
130 // Just one iteration
131 m_size = cursorCount.currentRow()["SUMREC"].data<long>();
132 }
133
134 // ... Get the data cursor
135 m_cursor = &(m_query->execute());
136 return;
137 }
138 catch(coral::SchemaException& se) {
139 m_accessSvc->msg() << MSG::WARNING << "QUERY: Schema Exception : " + std::string(se.what()) << endmsg;
140 }
141 catch(std::exception& e) {
142 m_accessSvc->msg() << MSG::WARNING << "QUERY: Exception : " + std::string(e.what()) << endmsg;
143 }
144 catch(...) {
145 m_accessSvc->msg() << MSG::WARNING << "QUERY: Exception caught... " << endmsg;
146 }
147
148 m_size = 0; // This indicates that there was a problem with query execution
149}
150
152{
153 return m_size;
154}
155
157{
158 if(m_cursor) m_cursor->close();
159 m_accessSvc->disconnect(m_connName);
160}
161
162void RDBQuery::setOrder(const std::string& field)
163{
164 m_orderField = field;
165}
166
167void RDBQuery::addToOutput(const std::string& field)
168{
169 m_fields.push_back(field);
170}
171
173{
174 if(m_cursor->next()) {
175 m_attrList = &(m_cursor->currentRow());
176 return true;
177 }
178 else {
179 m_attrList = 0;
180 return false;
181 }
182}
#define endmsg
Definition of RDBAccessSvc class.
Define macros for attributes used to control the static checker.
#define ATLAS_THREAD_SAFE
Common database lock.
Definition DBLock.h:46
IRDBQuery()=default
const coral::AttributeList * m_attrList
Definition IRDBQuery.h:32
RDBAccessSvc is the implementation of IRDBAccessSvc interface.
std::string m_nodeName
Definition RDBQuery.h:55
bool m_executed
Definition RDBQuery.h:62
std::vector< std::string > m_fields
Definition RDBQuery.h:59
RDBAccessSvc * m_accessSvc
Definition RDBQuery.h:54
long m_size
Definition RDBQuery.h:58
virtual void setOrder(const std::string &) override
Definition RDBQuery.cxx:162
std::string m_connName
Definition RDBQuery.h:57
std::string m_tagId
Definition RDBQuery.h:56
Athena::DBLock m_dblock
Definition RDBQuery.h:51
std::unique_ptr< coral::IQuery > m_query
Definition RDBQuery.h:52
virtual ~RDBQuery() override
Definition RDBQuery.cxx:41
coral::ICursor * m_cursor
Definition RDBQuery.h:60
virtual void finalize() override
Definition RDBQuery.cxx:156
virtual long size() override
Definition RDBQuery.cxx:151
RDBQuery()=delete
virtual void addToOutput(const std::string &) override
Definition RDBQuery.cxx:167
std::unique_ptr< coral::IQuery > m_queryCount
Definition RDBQuery.h:53
std::string m_orderField
Definition RDBQuery.h:61
virtual void execute() override
Definition RDBQuery.cxx:45
virtual bool next() override
Definition RDBQuery.cxx:172