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