ATLAS Offline Software
Loading...
Searching...
No Matches
SqliteRecord.h
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#ifndef RDBACCESSSVC_SQLITERECORD_H
13#define RDBACCESSSVC_SQLITERECORD_H
14
16#include <map>
17#include <memory>
18#include <variant>
19#include <string>
20#include <tuple>
21#include <stdexcept>
22
32
33typedef std::variant<int
34 , long
35 , float
36 , double
37 , std::string> SqliteInp;
38
39typedef std::map<std::string,SqliteInpType> SqliteInpDef;
40typedef std::shared_ptr<SqliteInpDef> SqliteInpDef_ptr;
41
47
48class SqliteRecord final : public IRDBRecord
49{
50 public:
52 SqliteRecord() = delete;
53 SqliteRecord (const SqliteRecord&) = delete;
55
57 ~SqliteRecord() override;
58
62 bool isFieldNull(const std::string& field) const override;
63
67 int getInt(const std::string& field) const override;
68
72 long getLong(const std::string& field) const override;
73
77 double getDouble(const std::string& field) const override;
78
82 float getFloat(const std::string& field) const override;
83
87 virtual const std::string& getString(const std::string& field) const override;
88
89 // Access array values by index
90 // arrays are implemented using the field with names like NAME_0, NAME_1 etc.
91
96 int getInt(const std::string& field, unsigned int index) const override;
97
102 long getLong(const std::string& field, unsigned int index) const override;
103
108 double getDouble(const std::string& field, unsigned int index) const override;
109
114 float getFloat(const std::string& field, unsigned int index) const override;
115
120 virtual const std::string& getString(const std::string& field, unsigned int index) const override;
121
123 void dump() const;
124
125 void addValue(const std::string& field
126 , SqliteInp value);
127
128 private:
129 typedef std::map<std::string,SqliteInp> Record;
130 typedef Record::const_iterator RecordCIterator;
138 typedef std::tuple<RecordCIterator,FieldCheckCode> FieldCheckResult;
139
142
143 FieldCheckResult checkField(const std::string& field
144 , SqliteInpType fieldType) const;
145 void handleError(const std::string& field
146 ,FieldCheckCode checkCode) const;
147};
148
150 , SqliteInpType fieldType) const
151{
153 RecordCIterator checkIt{m_record.end()};
154
155 auto defIt = m_def->find(field);
156 if(defIt==m_def->end()) checkCode = FIELD_CHECK_BAD_NAME;
157 else if(defIt->second!=fieldType) checkCode = FIELD_CHECK_BAD_TYPE;
158
159 if(checkCode==FIELD_CHECK_OK) {
160 checkIt = m_record.find(field);
161 if(checkIt==m_record.end()) {
162 checkCode = FIELD_CHECK_NULL_VAL;
163 }
164 }
165 return std::make_tuple(checkIt,checkCode);
166}
167
168inline void SqliteRecord::handleError(const std::string& field
169 ,FieldCheckCode checkCode) const
170{
171 switch(checkCode) {
173 throw std::runtime_error( "Wrong name for the field " + field);
175 throw std::runtime_error( "Wrong data type requested for the field " + field);
177 throw std::runtime_error( field + " is NULL");
178 default:
179 break;
180 }
181 return;
182}
183
184#endif
185
Definition of the abstract IRDBRecord interface.
std::shared_ptr< SqliteInpDef > SqliteInpDef_ptr
std::variant< int, long, float, double, std::string > SqliteInp
std::map< std::string, SqliteInpType > SqliteInpDef
SqliteInpType
@ SQLITEINP_UNDEF
@ SQLITEINP_FLOAT
@ SQLITEINP_STRING
@ SQLITEINP_DOUBLE
@ SQLITEINP_LONG
@ SQLITEINP_INT
IRDBRecord is one record in the IRDBRecordset object.
Definition IRDBRecord.h:27
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
std::tuple< RecordCIterator, FieldCheckCode > FieldCheckResult
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.
Record::const_iterator RecordCIterator
void dump() const
Dump to cout.
void handleError(const std::string &field, FieldCheckCode checkCode) const
SqliteRecord(const SqliteRecord &)=delete
int getInt(const std::string &field) const override
Get int field value.
std::map< std::string, SqliteInp > Record
SqliteRecord & operator=(const SqliteRecord &)=delete
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(SqliteInpDef_ptr def)
~SqliteRecord() override
Destructor.
Definition index.py:1