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