ATLAS Offline Software
Public Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
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
 
coral::IQuery * m_query
 
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_query(nullptr)
32  , m_queryCount(nullptr)
33  , m_accessSvc(accessSvc)
34  , m_nodeName(nodeName)
35  , m_tagId(tagId)
36  , m_connName(connName)
37  , m_size(0)
38  , m_cursor(nullptr)
39  , m_executed(false)
40 {
41 }

◆ ~RDBQuery()

RDBQuery::~RDBQuery ( )
overridevirtual

Definition at line 43 of file RDBQuery.cxx.

44 {
45  delete m_query;
46  delete m_queryCount;
47 }

◆ RDBQuery() [2/2]

RDBQuery::RDBQuery ( )
privatedelete

Member Function Documentation

◆ addToOutput()

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

Implements IRDBQuery.

Definition at line 171 of file RDBQuery.cxx.

172 {
173  m_fields.push_back(field);
174 }

◆ 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 }

◆ 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 49 of file RDBQuery.cxx.

50 {
51  if(m_executed) {
52  m_accessSvc->msg() << MSG::WARNING << "RDBQuery cannot be executed more than once! Query: "
53  << m_nodeName << ", "
54  << m_tagId << ", "
55  << m_orderField << ", "
56  << m_fields.size() << endmsg;
57  return;
58  }
59 
60  if(m_accessSvc->msg().level() <= MSG::DEBUG) {
61  m_accessSvc->msg() << MSG::DEBUG << "Query execute " << m_nodeName << ", "
62  << m_tagId << ", "
63  << m_orderField << ", "
64  << m_fields.size() << endmsg;
65  }
66 
68  m_accessSvc->msg() << MSG::ERROR << "Cannot connect to the database: "
69  << m_connName << endmsg;
70  throw std::runtime_error( "Cannot connect to the database " + m_connName);
71  }
72 
73  m_executed=true;
74  try {
75  // ... Get the node name and change to to Upper Case
76  std::string upperName = m_nodeName;
77  for (char& ch : upperName) {
78  ch = std::toupper (static_cast<unsigned int>(ch));
79  }
80 
81  // ... Create query objects
82  m_query = m_accessSvc->getSession(m_connName)->nominalSchema().newQuery();
83  m_queryCount = m_accessSvc->getSession(m_connName)->nominalSchema().newQuery();
84 
85  // Add fields
86  if(m_fields.size()>0) {
87  // Custom fields
88  for(unsigned int i=0; i<m_fields.size(); ++i) {
89  m_query->addToOutputList(upperName+"_DATA."+m_fields[i]);
90  }
91  }
92  else {
93  // All fields from the table
94  const coral::ITableDescription& dataTableDesc = m_accessSvc->getSession(m_connName)->nominalSchema().tableHandle(upperName + "_DATA").description();
95  for(int i=0; i<dataTableDesc.numberOfColumns(); ++i) {
96  m_query->addToOutputList(upperName+"_DATA."+dataTableDesc.columnDescription(i).name());
97  }
98  }
99 
100  m_queryCount->addToOutputList("COUNT("+upperName+"_DATA_ID)","SUMREC");
101 
102  // ... Define table list
103  m_query->addToTableList(upperName + "_DATA");
104  m_query->addToTableList(upperName + "_DATA2TAG");
105 
106  m_queryCount->addToTableList(upperName + "_DATA2TAG");
107 
108  // ... Define order
109  if(m_orderField.empty()) {
110  m_query->addToOrderList(upperName + "_DATA." + upperName + "_DATA_ID");
111  }
112  else {
113  m_query->addToOrderList(upperName + "_DATA." + m_orderField);
114  }
115 
116  // ... Define conditions
118  bindsData.extend<std::string>("tagID");
119 
120  std::string queryStructCondition = upperName +"_DATA2TAG." + upperName + "_TAG_ID =:tagID";
121  m_queryCount->setCondition(queryStructCondition , bindsData);
122 
123  queryStructCondition += " AND " + upperName +"_DATA." + upperName + "_DATA_ID=" + upperName + "_DATA2TAG." + upperName + "_DATA_ID";
124  m_query->setCondition(queryStructCondition , bindsData);
125 
126  bindsData[0].data<std::string>() = m_tagId;
127 
128  m_queryCount->defineOutputType("SUMREC","long");
129  m_query->setMemoryCacheSize(1);
130 
131  // ... Get size of the result set
132  coral::ICursor& cursorCount = m_queryCount->execute();
133  while(cursorCount.next()) {
134  // Just one iteration
135  m_size = cursorCount.currentRow()["SUMREC"].data<long>();
136  }
137 
138  // ... Get the data cursor
139  m_cursor = &(m_query->execute());
140  return;
141  }
142  catch(coral::SchemaException& se) {
143  m_accessSvc->msg() << MSG::WARNING << "QUERY: Schema Exception : " + std::string(se.what()) << endmsg;
144  }
145  catch(std::exception& e) {
146  m_accessSvc->msg() << MSG::WARNING << "QUERY: Exception : " + std::string(e.what()) << endmsg;
147  }
148  catch(...) {
149  m_accessSvc->msg() << MSG::WARNING << "QUERY: Exception caught... " << endmsg;
150  }
151 
152  m_size = 0; // This indicates that there was a problem with query execution
153 }

◆ finalize()

void RDBQuery::finalize ( )
overridevirtual

Implements IRDBQuery.

Definition at line 160 of file RDBQuery.cxx.

161 {
162  if(m_cursor) m_cursor->close();
164 }

◆ 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 176 of file RDBQuery.cxx.

177 {
178  if(m_cursor->next()) {
179  m_attrList = &(m_cursor->currentRow());
180  return true;
181  }
182  else {
183  m_attrList = 0;
184  return false;
185  }
186 }

◆ setOrder()

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

Implements IRDBQuery.

Definition at line 166 of file RDBQuery.cxx.

167 {
169 }

◆ size()

long RDBQuery::size ( )
overridevirtual

Implements IRDBQuery.

Definition at line 155 of file RDBQuery.cxx.

156 {
157  return m_size;
158 }

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.

◆ 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

coral::IQuery* RDBQuery::m_query
private

Definition at line 52 of file RDBQuery.h.

◆ m_queryCount

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:
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
RDBQuery::m_orderField
std::string m_orderField
Definition: RDBQuery.h:61
sendEI_SPB.ch
ch
Definition: sendEI_SPB.py:35
RDBQuery::m_tagId
std::string m_tagId
Definition: RDBQuery.h:56
RDBAccessSvc::getSession
coral::ISessionProxy * getSession(const std::string &connName="ATLASDD")
Definition: RDBAccessSvc.cxx:495
RDBQuery::m_size
long m_size
Definition: RDBQuery.h:58
RDBAccessSvc::connect
bool connect(const std::string &connName) override
Connect to the relational DB.
Definition: RDBAccessSvc.cxx:41
RDBQuery::m_fields
std::vector< std::string > m_fields
Definition: RDBQuery.h:59
IRDBQuery::m_attrList
const coral::AttributeList * m_attrList
Definition: IRDBQuery.h:32
ReadOfcFromCool.field
field
Definition: ReadOfcFromCool.py:48
python.PyKernel.AttributeList
AttributeList
Definition: PyKernel.py:36
keylayer_zslicemap.se
se
Definition: keylayer_zslicemap.py:194
RDBQuery::m_executed
bool m_executed
Definition: RDBQuery.h:62
RDBQuery::m_connName
std::string m_connName
Definition: RDBQuery.h:57
RDBQuery::m_accessSvc
RDBAccessSvc * m_accessSvc
Definition: RDBQuery.h:54
lumiFormat.i
int i
Definition: lumiFormat.py:85
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
calibdata.exception
exception
Definition: calibdata.py:496
RDBQuery::m_nodeName
std::string m_nodeName
Definition: RDBQuery.h:55
RDBQuery::m_cursor
coral::ICursor * m_cursor
Definition: RDBQuery.h:60
RDBQuery::m_queryCount
coral::IQuery * m_queryCount
Definition: RDBQuery.h:53
RDBQuery::m_dblock
Athena::DBLock m_dblock
Definition: RDBQuery.h:51
DEBUG
#define DEBUG
Definition: page_access.h:11
IRDBQuery::IRDBQuery
IRDBQuery()=default
ATLAS_THREAD_SAFE
#define ATLAS_THREAD_SAFE
Definition: checker_macros.h:211
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
RDBAccessSvc::disconnect
bool disconnect(const std::string &connName) override
If the counnection counter==1 closes the connection.
Definition: RDBAccessSvc.cxx:81
RDBQuery::m_query
coral::IQuery * m_query
Definition: RDBQuery.h:52