ATLAS Offline Software
SqliteRecord.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
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 
24 {
31 };
32 
33 typedef std::variant<int
34  , long
35  , float
36  , double
37  , std::string> SqliteInp;
38 
39 typedef std::map<std::string,SqliteInpType> SqliteInpDef;
40 typedef std::shared_ptr<SqliteInpDef> SqliteInpDef_ptr;
41 
48 class 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;
132  {
137  };
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 {
152  FieldCheckCode checkCode{FIELD_CHECK_OK};
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  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 
168 inline 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 
SqliteInp
std::variant< int, long, float, double, std::string > SqliteInp
Definition: SqliteRecord.h:37
SqliteRecord::~SqliteRecord
~SqliteRecord() override
Destructor.
Definition: SqliteRecord.cxx:24
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
index
Definition: index.py:1
SqliteRecord::SqliteRecord
SqliteRecord()=delete
athena.value
value
Definition: athena.py:124
SqliteRecord::FieldCheckResult
std::tuple< RecordCIterator, FieldCheckCode > FieldCheckResult
Definition: SqliteRecord.h:138
ReadOfcFromCool.field
field
Definition: ReadOfcFromCool.py:48
SQLITEINP_LONG
@ SQLITEINP_LONG
Definition: SqliteRecord.h:26
SQLITEINP_UNDEF
@ SQLITEINP_UNDEF
Definition: SqliteRecord.h:30
SQLITEINP_INT
@ SQLITEINP_INT
Definition: SqliteRecord.h:25
SqliteInpDef
std::map< std::string, SqliteInpType > SqliteInpDef
Definition: SqliteRecord.h:39
SqliteRecord::getFloat
float getFloat(const std::string &field) const override
Get float field value.
Definition: SqliteRecord.cxx:61
SqliteRecord::FIELD_CHECK_BAD_NAME
@ FIELD_CHECK_BAD_NAME
Definition: SqliteRecord.h:134
SQLITEINP_DOUBLE
@ SQLITEINP_DOUBLE
Definition: SqliteRecord.h:28
SqliteRecord::FIELD_CHECK_BAD_TYPE
@ FIELD_CHECK_BAD_TYPE
Definition: SqliteRecord.h:135
SqliteRecord::FIELD_CHECK_OK
@ FIELD_CHECK_OK
Definition: SqliteRecord.h:133
SqliteRecord::getInt
int getInt(const std::string &field) const override
Get int field value.
Definition: SqliteRecord.cxx:35
SQLITEINP_STRING
@ SQLITEINP_STRING
Definition: SqliteRecord.h:29
SqliteRecord::addValue
void addValue(const std::string &field, SqliteInp value)
Definition: SqliteRecord.cxx:102
SqliteRecord::FieldCheckCode
FieldCheckCode
Definition: SqliteRecord.h:132
SqliteRecord::SqliteRecord
SqliteRecord(const SqliteRecord &)=delete
xAOD::double
double
Definition: CompositeParticle_v1.cxx:159
SqliteRecord
SqliteRecord is one record in the SqliteRecordset object.
Definition: SqliteRecord.h:49
SqliteRecord::Record
std::map< std::string, SqliteInp > Record
Definition: SqliteRecord.h:129
SqliteRecord::handleError
void handleError(const std::string &field, FieldCheckCode checkCode) const
Definition: SqliteRecord.h:168
SqliteRecord::dump
void dump() const
Dump to cout.
Definition: SqliteRecord.cxx:110
SQLITEINP_FLOAT
@ SQLITEINP_FLOAT
Definition: SqliteRecord.h:27
SqliteInpType
SqliteInpType
Definition: SqliteRecord.h:24
SqliteRecord::getLong
long getLong(const std::string &field) const override
Get long field value.
Definition: SqliteRecord.cxx:45
IRDBRecord.h
Definition of the abstract IRDBRecord interface.
IRDBRecord
IRDBRecord is one record in the IRDBRecordset object.
Definition: IRDBRecord.h:27
SqliteRecord::m_def
SqliteInpDef_ptr m_def
Definition: SqliteRecord.h:140
SqliteRecord::checkField
FieldCheckResult checkField(const std::string &field, SqliteInpType fieldType) const
Definition: SqliteRecord.h:149
SqliteRecord::operator=
SqliteRecord & operator=(const SqliteRecord &)=delete
SqliteRecord::m_record
Record m_record
Definition: SqliteRecord.h:141
SqliteRecord::getString
virtual const std::string & getString(const std::string &field) const override
Get string field value.
Definition: SqliteRecord.cxx:67
SqliteRecord::isFieldNull
bool isFieldNull(const std::string &field) const override
Check if the field value is NULL.
Definition: SqliteRecord.cxx:28
SqliteInpDef_ptr
std::shared_ptr< SqliteInpDef > SqliteInpDef_ptr
Definition: SqliteRecord.h:40
SqliteRecord::getDouble
double getDouble(const std::string &field) const override
Get double field value.
Definition: SqliteRecord.cxx:51
SqliteRecord::RecordCIterator
Record::const_iterator RecordCIterator
Definition: SqliteRecord.h:130
readCCLHist.float
float
Definition: readCCLHist.py:83
SqliteRecord::FIELD_CHECK_NULL_VAL
@ FIELD_CHECK_NULL_VAL
Definition: SqliteRecord.h:136