ATLAS Offline Software
Loading...
Searching...
No Matches
RDBQuery Class Referencefinal

RDBQuery is an implementation of IRDBQuery interface. More...

#include <RDBQuery.h>

Inheritance diagram for RDBQuery:
Collaboration diagram for RDBQuery:

Public Member Functions

 RDBQuery (const Athena::DBLock &dblock, RDBAccessSvc *accessSvc, const std::string &nodeName, const std::string &tagId, const std::string &connName)
virtual ~RDBQuery () override
virtual void execute () override
virtual long size () override
virtual void finalize () override
virtual void setOrder (const std::string &) override
virtual void addToOutput (const std::string &) override
virtual bool next () override
template<typename T>
const T & data (const std::string &)
template<typename T>
const T & data (unsigned int)
bool isNull (const std::string &)
bool isNull (unsigned int)

Protected Attributes

const coral::AttributeList * m_attrList {nullptr}

Private Member Functions

 RDBQuery ()=delete

Private Attributes

Athena::DBLock m_dblock
std::unique_ptr< coral::IQuery > m_query
std::unique_ptr< coral::IQuery > m_queryCount
RDBAccessSvcm_accessSvc
std::string m_nodeName
std::string m_tagId
std::string m_connName
long m_size
std::vector< std::string > m_fields
coral::ICursor * m_cursor
std::string m_orderField
bool m_executed

Detailed Description

RDBQuery is an implementation of IRDBQuery interface.

Definition at line 29 of file RDBQuery.h.

Constructor & Destructor Documentation

◆ RDBQuery() [1/2]

RDBQuery::RDBQuery ( const Athena::DBLock & dblock,
RDBAccessSvc * accessSvc,
const std::string & nodeName,
const std::string & tagId,
const std::string & connName )

Definition at line 24 of file RDBQuery.cxx.

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}
IRDBQuery()=default
std::string m_nodeName
Definition RDBQuery.h:55
bool m_executed
Definition RDBQuery.h:62
RDBAccessSvc * m_accessSvc
Definition RDBQuery.h:54
long m_size
Definition RDBQuery.h:58
std::string m_connName
Definition RDBQuery.h:57
std::string m_tagId
Definition RDBQuery.h:56
Athena::DBLock m_dblock
Definition RDBQuery.h:51
coral::ICursor * m_cursor
Definition RDBQuery.h:60

◆ ~RDBQuery()

RDBQuery::~RDBQuery ( )
overridevirtual

Definition at line 41 of file RDBQuery.cxx.

42{
43}

◆ RDBQuery() [2/2]

RDBQuery::RDBQuery ( )
privatedelete

Member Function Documentation

◆ addToOutput()

void RDBQuery::addToOutput ( const std::string & field)
overridevirtual

Implements IRDBQuery.

Definition at line 167 of file RDBQuery.cxx.

168{
169 m_fields.push_back(field);
170}
std::vector< std::string > m_fields
Definition RDBQuery.h:59

◆ data() [1/2]

template<typename T>
const T & IRDBQuery::data ( const std::string & field)
inherited

Definition at line 35 of file IRDBQuery.h.

36{
37 return (*m_attrList)[field].data<T>();
38}
const coral::AttributeList * m_attrList
Definition IRDBQuery.h:32
unsigned long long T

◆ data() [2/2]

template<typename T>
const T & IRDBQuery::data ( unsigned int fieldInd)
inherited

Definition at line 40 of file IRDBQuery.h.

41{
42 return (*m_attrList)[fieldInd].data<T>();
43}

◆ execute()

void RDBQuery::execute ( )
overridevirtual

Implements IRDBQuery.

Definition at line 45 of file RDBQuery.cxx.

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}
#define endmsg
#define ATLAS_THREAD_SAFE
std::unique_ptr< coral::IQuery > m_query
Definition RDBQuery.h:52
std::unique_ptr< coral::IQuery > m_queryCount
Definition RDBQuery.h:53
std::string m_orderField
Definition RDBQuery.h:61

◆ finalize()

void RDBQuery::finalize ( )
overridevirtual

Implements IRDBQuery.

Definition at line 156 of file RDBQuery.cxx.

157{
158 if(m_cursor) m_cursor->close();
159 m_accessSvc->disconnect(m_connName);
160}

◆ isNull() [1/2]

bool IRDBQuery::isNull ( const std::string & field)
inlineinherited

Definition at line 45 of file IRDBQuery.h.

46{
47 return (*m_attrList)[field].isNull();
48}

◆ isNull() [2/2]

bool IRDBQuery::isNull ( unsigned int fieldInd)
inlineinherited

Definition at line 50 of file IRDBQuery.h.

51{
52 return (*m_attrList)[fieldInd].isNull();
53}

◆ next()

bool RDBQuery::next ( )
overridevirtual

Implements IRDBQuery.

Definition at line 172 of file RDBQuery.cxx.

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}

◆ setOrder()

void RDBQuery::setOrder ( const std::string & field)
overridevirtual

Implements IRDBQuery.

Definition at line 162 of file RDBQuery.cxx.

163{
165}

◆ size()

long RDBQuery::size ( )
overridevirtual

Implements IRDBQuery.

Definition at line 151 of file RDBQuery.cxx.

152{
153 return m_size;
154}

Member Data Documentation

◆ m_accessSvc

RDBAccessSvc* RDBQuery::m_accessSvc
private

Definition at line 54 of file RDBQuery.h.

◆ m_attrList

const coral::AttributeList* IRDBQuery::m_attrList {nullptr}
protectedinherited

Definition at line 32 of file IRDBQuery.h.

32{nullptr};

◆ m_connName

std::string RDBQuery::m_connName
private

Definition at line 57 of file RDBQuery.h.

◆ m_cursor

coral::ICursor* RDBQuery::m_cursor
private

Definition at line 60 of file RDBQuery.h.

◆ m_dblock

Athena::DBLock RDBQuery::m_dblock
private

Definition at line 51 of file RDBQuery.h.

◆ m_executed

bool RDBQuery::m_executed
private

Definition at line 62 of file RDBQuery.h.

◆ m_fields

std::vector<std::string> RDBQuery::m_fields
private

Definition at line 59 of file RDBQuery.h.

◆ m_nodeName

std::string RDBQuery::m_nodeName
private

Definition at line 55 of file RDBQuery.h.

◆ m_orderField

std::string RDBQuery::m_orderField
private

Definition at line 61 of file RDBQuery.h.

◆ m_query

std::unique_ptr<coral::IQuery> RDBQuery::m_query
private

Definition at line 52 of file RDBQuery.h.

◆ m_queryCount

std::unique_ptr<coral::IQuery> RDBQuery::m_queryCount
private

Definition at line 53 of file RDBQuery.h.

◆ m_size

long RDBQuery::m_size
private

Definition at line 58 of file RDBQuery.h.

◆ m_tagId

std::string RDBQuery::m_tagId
private

Definition at line 56 of file RDBQuery.h.


The documentation for this class was generated from the following files: