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 <iostream>
15#include <stdexcept>
16#include <sstream>
17#include <format>
18
23
27
28bool 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
35int 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
45long 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
51double 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
61float SqliteRecord::getFloat(std::string_view field) const
62{
63 // SQLite stores REAL as double
64 return (float)getDouble(field);
65}
66
67const 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
77int SqliteRecord::getInt(std::string_view field, unsigned int index) const
78{
79 return getInt(std::format("{}_{}", field, index));
80}
81
82long SqliteRecord::getLong(std::string_view field, unsigned int index) const
83{
84 return getLong(std::format("{}_{}", field, index));
85}
86
87double SqliteRecord::getDouble(std::string_view field, unsigned int index) const
88{
89 return getDouble(std::format("{}_{}", field, index));
90}
91
92float SqliteRecord::getFloat(std::string_view field, unsigned int index) const
93{
94 return getFloat(std::format("{}_{}", field, index));
95}
96
97const std::string& SqliteRecord::getString(std::string_view field, unsigned int index) const
98{
99 return getString(std::format("{}_{}", field, index));
100}
101
102void 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
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
156void
157SqliteRecord::handleError(std::string_view field, FieldCheckCode checkCode) const
158{
159 switch(checkCode) {
161 throw std::runtime_error( std::format("handleError: Wrong name for the field {}",field));
163 throw std::runtime_error( std::format("handleError: Wrong data type requested for the field {}",field));
165 throw std::runtime_error( std::format("handleError: {} is NULL", field));
166 default:
167 break;
168 }
169 return;
170}
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.