ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Database
RDBAccessSvc
src
SqliteRecord.cxx
Go to the documentation of this file.
1
/*
2
Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3
*/
4
11
12
13
#include "
SqliteRecord.h
"
14
#include <iostream>
15
#include <stdexcept>
16
#include <sstream>
17
#include <format>
18
19
SqliteRecord::SqliteRecord
(
SqliteInpDef_ptr
def)
20
:
m_def
(
std
::move(def))
21
{
22
}
23
24
SqliteRecord::~SqliteRecord
()
25
{
26
}
27
28
bool
SqliteRecord::isFieldNull
(std::string_view field)
const
29
{
30
if
(
m_record
.find(field)!=
m_record
.end())
return
false
;
31
if
(
m_def
->find(field)==
m_def
->end())
throw
std::runtime_error( std::format(
"Wrong name for the field {}"
,field));
32
return
true
;
33
}
34
35
int
SqliteRecord::getInt
(std::string_view field)
const
36
{
37
auto
[recIt,checkCode] =
checkField
(field,
SQLITEINP_INT
);
38
if
(checkCode==
FIELD_CHECK_OK
) {
39
return
std::get<int>(recIt->second);
40
}
41
handleError
(field,checkCode);
42
return
0;
43
}
44
45
long
SqliteRecord::getLong
(std::string_view field)
const
46
{
47
// Our database does not support LONG data types at this moment
48
return
(
long
)
getInt
(field);
49
}
50
51
double
SqliteRecord::getDouble
(std::string_view field)
const
52
{
53
auto
[recIt,checkCode] =
checkField
(field,
SQLITEINP_DOUBLE
);
54
if
(checkCode==
FIELD_CHECK_OK
) {
55
return
std::get<double>(recIt->second);
56
}
57
handleError
(field,checkCode);
58
return
0.;
59
}
60
61
float
SqliteRecord::getFloat
(std::string_view field)
const
62
{
63
// SQLite stores REAL as double
64
return
(
float
)
getDouble
(field);
65
}
66
67
const
std::string&
SqliteRecord::getString
(std::string_view field)
const
68
{
69
auto
[recIt,checkCode] =
checkField
(field,
SQLITEINP_STRING
);
70
if
(checkCode==
FIELD_CHECK_OK
) {
71
return
std::get<std::string>(recIt->second);
72
}
73
handleError
(field,checkCode);
74
throw
std::runtime_error(
"Unexpected error in SqliteRecord::getString()"
);
75
}
76
77
int
SqliteRecord::getInt
(std::string_view field,
unsigned
int
index
)
const
78
{
79
return
getInt
(std::format(
"{}_{}"
, field,
index
));
80
}
81
82
long
SqliteRecord::getLong
(std::string_view field,
unsigned
int
index
)
const
83
{
84
return
getLong
(std::format(
"{}_{}"
, field,
index
));
85
}
86
87
double
SqliteRecord::getDouble
(std::string_view field,
unsigned
int
index
)
const
88
{
89
return
getDouble
(std::format(
"{}_{}"
, field,
index
));
90
}
91
92
float
SqliteRecord::getFloat
(std::string_view field,
unsigned
int
index
)
const
93
{
94
return
getFloat
(std::format(
"{}_{}"
, field,
index
));
95
}
96
97
const
std::string&
SqliteRecord::getString
(std::string_view field,
unsigned
int
index
)
const
98
{
99
return
getString
(std::format(
"{}_{}"
, field,
index
));
100
}
101
102
void
SqliteRecord::addValue
(std::string_view field,
SqliteInp
value)
103
{
104
auto
[it,result] =
m_record
.emplace(std::string{field}, std::move(value));
105
if
(!result)
throw
std::runtime_error(std::format(
"Unexpected error when adding new value for the field {}. Duplicate field name?"
, field));
106
}
107
108
void
SqliteRecord::dump
()
const
109
{
110
bool
first{
true
};
111
for
(
const
auto
& [colName,colType] : *
m_def
) {
112
if
(first) {
113
first =
false
;
114
}
115
else
{
116
std::cout <<
", "
;
117
}
118
auto
recIt =
m_record
.find(colName);
119
bool
fieldNull = (recIt==
m_record
.end());
120
std::cout <<
"["
<< colName <<
" ("
;
121
switch
(colType) {
122
case
SQLITEINP_INT
:
123
std::cout <<
"int) : "
<< (fieldNull?
"NULL"
: std::to_string(std::get<int>(recIt->second))) <<
"]"
;
124
break
;
125
case
SQLITEINP_LONG
:
126
std::cout <<
"long) : "
<< (fieldNull?
"NULL"
: std::to_string(std::get<long>(recIt->second))) <<
"]"
;
127
break
;
128
case
SQLITEINP_FLOAT
:
129
std::cout <<
"float) : "
;
130
if
(fieldNull) {
131
std::cout <<
"NULL"
;
132
}
133
else
{
134
std::cout << std::format(
"{:.10g}]"
, std::get<float>(recIt->second));
135
}
136
break
;
137
case
SQLITEINP_DOUBLE
:
138
std::cout <<
"double) : "
;
139
if
(fieldNull) {
140
std::cout <<
"NULL"
;
141
}
142
else
{
143
std::cout << std::format(
"{:.10g}]"
, std::get<double>(recIt->second));
144
}
145
break
;
146
case
SQLITEINP_STRING
:
147
std::cout <<
"string) : "
<< (fieldNull?
"NULL"
: (
"\""
+ std::get<std::string>(recIt->second)) +
"\""
) <<
"]"
;
148
break
;
149
default
:
150
std::cout <<
"ERROR) : ]"
;
151
}
152
}
153
std::cout << std::endl;
154
}
155
156
void
157
SqliteRecord::handleError
(std::string_view field,
FieldCheckCode
checkCode)
const
158
{
159
switch
(checkCode) {
160
case
FIELD_CHECK_BAD_NAME
:
161
throw
std::runtime_error( std::format(
"handleError: Wrong name for the field {}"
,field));
162
case
FIELD_CHECK_BAD_TYPE
:
163
throw
std::runtime_error( std::format(
"handleError: Wrong data type requested for the field {}"
,field));
164
case
FIELD_CHECK_NULL_VAL
:
165
throw
std::runtime_error( std::format(
"handleError: {} is NULL"
, field));
166
default
:
167
break
;
168
}
169
return
;
170
}
SqliteRecord.h
Declaration of the SqliteRecord class.
SqliteInpDef_ptr
std::shared_ptr< SqliteInpDef > SqliteInpDef_ptr
Definition
SqliteRecord.h:40
SqliteInp
std::variant< int, long, float, double, std::string > SqliteInp
Definition
SqliteRecord.h:37
SQLITEINP_FLOAT
@ SQLITEINP_FLOAT
Definition
SqliteRecord.h:27
SQLITEINP_STRING
@ SQLITEINP_STRING
Definition
SqliteRecord.h:29
SQLITEINP_DOUBLE
@ SQLITEINP_DOUBLE
Definition
SqliteRecord.h:28
SQLITEINP_LONG
@ SQLITEINP_LONG
Definition
SqliteRecord.h:26
SQLITEINP_INT
@ SQLITEINP_INT
Definition
SqliteRecord.h:25
SqliteRecord::isFieldNull
bool isFieldNull(std::string_view field) const override
Check if the field value is NULL.
Definition
SqliteRecord.cxx:28
SqliteRecord::handleError
void handleError(std::string_view field, FieldCheckCode checkCode) const
Definition
SqliteRecord.cxx:157
SqliteRecord::getFloat
float getFloat(std::string_view field) const override
Get float field value.
Definition
SqliteRecord.cxx:61
SqliteRecord::checkField
FieldCheckResult checkField(std::string_view field, SqliteInpType fieldType) const
Definition
SqliteRecord.h:146
SqliteRecord::addValue
void addValue(std::string_view field, SqliteInp value)
Definition
SqliteRecord.cxx:102
SqliteRecord::dump
void dump() const
Dump to cout.
Definition
SqliteRecord.cxx:108
SqliteRecord::getInt
int getInt(std::string_view field) const override
Get int field value.
Definition
SqliteRecord.cxx:35
SqliteRecord::getString
virtual const std::string & getString(std::string_view field) const override
Get string field value.
Definition
SqliteRecord.cxx:67
SqliteRecord::getDouble
double getDouble(std::string_view field) const override
Get double field value.
Definition
SqliteRecord.cxx:51
SqliteRecord::m_def
SqliteInpDef_ptr m_def
Definition
SqliteRecord.h:139
SqliteRecord::getLong
long getLong(std::string_view field) const override
Get long field value.
Definition
SqliteRecord.cxx:45
SqliteRecord::SqliteRecord
SqliteRecord()=delete
SqliteRecord::m_record
Record m_record
Definition
SqliteRecord.h:140
SqliteRecord::~SqliteRecord
~SqliteRecord() override
Destructor.
Definition
SqliteRecord.cxx:24
SqliteRecord::FieldCheckCode
FieldCheckCode
Definition
SqliteRecord.h:131
SqliteRecord::FIELD_CHECK_BAD_TYPE
@ FIELD_CHECK_BAD_TYPE
Definition
SqliteRecord.h:134
SqliteRecord::FIELD_CHECK_OK
@ FIELD_CHECK_OK
Definition
SqliteRecord.h:132
SqliteRecord::FIELD_CHECK_BAD_NAME
@ FIELD_CHECK_BAD_NAME
Definition
SqliteRecord.h:133
SqliteRecord::FIELD_CHECK_NULL_VAL
@ FIELD_CHECK_NULL_VAL
Definition
SqliteRecord.h:135
index
Definition
index.py:1
std
STL namespace.
Generated on
for ATLAS Offline Software by
1.17.0