ATLAS Offline Software
Loading...
Searching...
No Matches
RDBRecord.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
14
15
16#include "RDBRecord.h"
17#include "RelationalAccess/ICursor.h"
18
19#include "CoralBase/Attribute.h"
20#include "CoralBase/AttributeList.h"
21#include "CoralBase/AttributeSpecification.h"
22
23#include <stdexcept>
24#include <sstream>
25#include <string>
26
27RDBRecord::RDBRecord(const coral::AttributeList& attList
28 , const std::string& tableName)
29 : m_values(0)
30 , m_tableName(tableName)
31{
32 // Copy attList. Try to avoid sharing, for thread-safety.
33 m_values = new coral::AttributeList(attList.specification(), false);
34 m_values->fastCopyData (attList);
35
36 for(unsigned int i=0; i<m_values->size(); i++) {
37 std::string key = (*m_values)[i].specification().name();
38 m_name2Index[key] = i;
39 }
40}
41
43{
44 delete m_values;
45}
46
47bool RDBRecord::isFieldNull(const std::string& fieldName) const
48{
49 FieldName2ListIndex::const_iterator it = m_name2Index.find(m_tableName+"."+fieldName);
50 if(it==m_name2Index.end()) {
51 throw std::runtime_error( "Wrong name for the field " + m_tableName+"."+fieldName);
52 }
53
54 const coral::AttributeList& values = *m_values;
55 return values[it->second].isNull();
56}
57
58int RDBRecord::getInt(const std::string& fieldName) const
59{
60 FieldName2ListIndex::const_iterator it = m_name2Index.find(m_tableName+"."+fieldName);
61 if(it==m_name2Index.end()) {
62 throw std::runtime_error( "Wrong name for the field " + m_tableName+"."+fieldName);
63 }
64
65 const coral::AttributeList& values = *m_values;
66 if(values[it->second].specification().type()==typeid(int)) {
67 return values[it->second].data<int>();
68 }
69 else if(values[it->second].specification().type()==typeid(long)) {
70 return (int)values[it->second].data<long>();
71 }
72 else {
73 throw std::runtime_error( "Field " + fieldName + " is NOT of integer type\n");
74 }
75}
76
77long RDBRecord::getLong(const std::string& fieldName) const
78{
79 FieldName2ListIndex::const_iterator it = m_name2Index.find(m_tableName+"."+fieldName);
80 if(it==m_name2Index.end()) {
81 throw std::runtime_error( "Wrong name for the field " + m_tableName+"."+fieldName);
82 }
83
84 const coral::AttributeList& values = *m_values;
85 if(values[it->second].specification().type()==typeid(long)) {
86 return values[it->second].data<long>();
87 }
88 else if(values[it->second].specification().type()==typeid(int)) {
89 return (long)values[it->second].data<int>();
90 }
91 else if(values[it->second].specification().type()==typeid(long long)) {
92 return (long)values[it->second].data<long long>();
93 }
94 else {
95 throw std::runtime_error( "Field " + fieldName + " is NOT of long type");
96 }
97}
98
99double RDBRecord::getDouble(const std::string& fieldName) const
100{
101 FieldName2ListIndex::const_iterator it = m_name2Index.find(m_tableName+"."+fieldName);
102 if(it==m_name2Index.end()) {
103 throw std::runtime_error( "Wrong name for the field " + m_tableName+"."+fieldName);
104 }
105
106 const coral::AttributeList& values = *m_values;
107 if(values[it->second].specification().type()==typeid(double)) {
108 return values[it->second].data<double>();
109 }
110 else {
111 throw std::runtime_error( "Field " + fieldName + " is NOT of double type");
112 }
113}
114
115float RDBRecord::getFloat(const std::string& fieldName) const
116{
117 FieldName2ListIndex::const_iterator it = m_name2Index.find(m_tableName+"."+fieldName);
118 if(it==m_name2Index.end()) {
119 throw std::runtime_error( "Wrong name for the field " + m_tableName+"."+fieldName);
120 }
121
122 const coral::AttributeList& values = *m_values;
123 if(values[it->second].specification().type()==typeid(float)) {
124 return values[it->second].data<float>();
125 }
126 else {
127 throw std::runtime_error( "Field " + fieldName + " is NOT of float type");
128 }
129}
130
131const std::string& RDBRecord::getString(const std::string& fieldName) const
132{
133 FieldName2ListIndex::const_iterator it = m_name2Index.find(m_tableName+"."+fieldName);
134 if(it==m_name2Index.end()) {
135 throw std::runtime_error( "Wrong name for the field " + m_tableName+"."+fieldName);
136 }
137
138 const coral::AttributeList& values = *m_values;
139 if(values[it->second].specification().type()==typeid(std::string)) {
140 return values[it->second].data<std::string>();
141 }
142 else {
143 throw std::runtime_error( "Field " + fieldName + " is NOT of string type");
144 }
145}
146
147int RDBRecord::getInt(const std::string& fieldName, unsigned int index) const
148{
149 FieldName2ListIndex::const_iterator it = m_name2Index.find(m_tableName+"."+fieldName + "_" + std::to_string(index));
150 if(it==m_name2Index.end()) {
151 throw std::runtime_error("Wrong name for the array field " + m_tableName+"."+fieldName + " or index=" + std::to_string(index) + " is out of range");
152 }
153
154 const coral::AttributeList& values = *m_values;
155 if(values[it->second].specification().type()==typeid(int)) {
156 return values[it->second].data<int>();
157 }
158 else if(values[it->second].specification().type()==typeid(long)) {
159 return (int)values[it->second].data<long>();
160 }
161 else {
162 throw std::runtime_error( "Field " + fieldName + " is NOT of integer type\n");
163 }
164}
165
166long RDBRecord::getLong(const std::string& fieldName, unsigned int index) const
167{
168 FieldName2ListIndex::const_iterator it = m_name2Index.find(m_tableName+"."+fieldName + "_" + std::to_string(index));
169 if(it==m_name2Index.end()) {
170 throw std::runtime_error("Wrong name for the array field " + m_tableName+"."+fieldName + " or index=" + std::to_string(index) + " is out of range");
171 }
172
173 const coral::AttributeList& values = *m_values;
174 if(values[it->second].specification().type()==typeid(long)) {
175 return values[it->second].data<long>();
176 }
177 else if(values[it->second].specification().type()==typeid(int)) {
178 return (long)values[it->second].data<int>();
179 }
180 else {
181 throw std::runtime_error( "Field " + fieldName + " is NOT of long type");
182 }
183}
184
185double RDBRecord::getDouble(const std::string& fieldName, unsigned int index) const
186{
187 FieldName2ListIndex::const_iterator it = m_name2Index.find(m_tableName+"."+fieldName + "_" + std::to_string(index));
188 if(it==m_name2Index.end()) {
189 throw std::runtime_error("Wrong name for the array field " + m_tableName+"."+fieldName + " or index=" + std::to_string(index) + " is out of range");
190 }
191
192 const coral::AttributeList& values = *m_values;
193 if(values[it->second].specification().type()==typeid(double)) {
194 return values[it->second].data<double>();
195 }
196 else {
197 throw std::runtime_error( "Field " + fieldName + " is NOT of double type");
198 }
199}
200
201float RDBRecord::getFloat(const std::string& fieldName, unsigned int index) const
202{
203 FieldName2ListIndex::const_iterator it = m_name2Index.find(m_tableName+"."+fieldName + "_" + std::to_string(index));
204 if(it==m_name2Index.end()) {
205 throw std::runtime_error("Wrong name for the array field " + m_tableName+"."+fieldName + " or index=" + std::to_string(index) + " is out of range");
206 }
207
208 const coral::AttributeList& values = *m_values;
209 if(values[it->second].specification().type()==typeid(float)) {
210 return values[it->second].data<float>();
211 }
212 else {
213 throw std::runtime_error( "Field " + fieldName + " is NOT of float type");
214 }
215}
216
217const std::string& RDBRecord::getString(const std::string& fieldName, unsigned int index) const
218{
219 FieldName2ListIndex::const_iterator it = m_name2Index.find(m_tableName+"."+fieldName + "_" + std::to_string(index));
220 if(it==m_name2Index.end()) {
221 throw std::runtime_error("Wrong name for the array field " + m_tableName+"."+fieldName + " or index=" + std::to_string(index) + " is out of range");
222 }
223
224 const coral::AttributeList& values = *m_values;
225 if(values[it->second].specification().type()==typeid(std::string)) {
226 return values[it->second].data<std::string>();
227 }
228 else {
229 throw std::runtime_error( "Field " + fieldName + " is NOT of string type");
230 }
231}
232
233bool RDBRecord::operator!=(const RDBRecord& rhs) const
234{
235 const coral::AttributeList& myAttList = *m_values;
236 const coral::AttributeList& rhsAttList = *rhs.m_values;
237
238 if(myAttList.size()!=rhsAttList.size()) return true;
239
240 for(size_t i(0); i<myAttList.size(); ++i) {
241 const coral::Attribute& myAtt = myAttList[i];
242 const std::string name = myAtt.specification().name();
243 bool exists(false);
244 for(size_t j(0); j<rhsAttList.size(); ++j) {
245 const coral::Attribute& rhsAtt = rhsAttList[j];
246 if(rhsAtt.specification().name()==name) {
247 if(myAtt!=rhsAtt) {
248 return true;
249 }
250 exists = true;
251 break;
252 }
253 }// Go through the attributes in the RHS list
254 if(!exists)
255 return true;
256 }
257 return false;
258}
259
260std::ostream& RDBRecord::toOutputStream(std::ostream& os) const
261{
262 m_values->toOutputStream(os);
263 return os;
264}
Definition of RDBRecord class.
bool isFieldNull(const std::string &fieldName) const override
Check if the field value is NULL.
Definition RDBRecord.cxx:47
bool operator!=(const RDBRecord &rhs) const
RDBRecord(const RDBRecord &)=delete
std::ostream & toOutputStream(std::ostream &os) const
RDBRecord()
Empty private constructor.
Definition RDBRecord.h:126
virtual const std::string & getString(const std::string &fieldName) const override
Get string field value.
~RDBRecord() override
Destructor.
Definition RDBRecord.cxx:42
std::string m_tableName
Definition RDBRecord.h:131
double getDouble(const std::string &fieldName) const override
Get double field value.
Definition RDBRecord.cxx:99
long getLong(const std::string &fieldName) const override
Get long field value.
Definition RDBRecord.cxx:77
float getFloat(const std::string &fieldName) const override
Get float field value.
int getInt(const std::string &fieldName) const override
Get int field value.
Definition RDBRecord.cxx:58
FieldName2ListIndex m_name2Index
Definition RDBRecord.h:128
bool exists(const std::string &filename)
does a file exist
Definition index.py:1