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