ATLAS Offline Software
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 
13 #include "SqliteRecord.h"
14 #include "boost/io/ios_state.hpp"
15 #include <iostream>
16 #include <iomanip>
17 #include <stdexcept>
18 #include <sstream>
19 
21  : m_def(std::move(def))
22 {
23 }
24 
26 {
27 }
28 
29 bool 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 
36 int 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 
46 long 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 
52 double 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 
62 float SqliteRecord::getFloat(const std::string& field) const
63 {
64  // SQLite stores REAL as double
65  return (float)getDouble(field);
66 }
67 
68 const 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 
78 int SqliteRecord::getInt(const std::string& field, unsigned int index) const
79 {
80  return getInt(field + "_" + std::to_string(index));
81 }
82 
83 long SqliteRecord::getLong(const std::string& field, unsigned int index) const
84 {
85  return getLong(field + "_" + std::to_string(index));
86 }
87 
88 double SqliteRecord::getDouble(const std::string& field, unsigned int index) const
89 {
90  return getDouble(field + "_" + std::to_string(index));
91 }
92 
93 float SqliteRecord::getFloat(const std::string& field, unsigned int index) const
94 {
95  return getFloat(field + "_" + std::to_string(index));
96 }
97 
98 const std::string& SqliteRecord::getString(const std::string& field, unsigned int index) const
99 {
100  return getString(field + "_" + std::to_string(index));
101 }
102 
103 void 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 
111 void SqliteRecord::dump() const
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 }
SqliteInp
std::variant< int, long, float, double, std::string > SqliteInp
Definition: SqliteRecord.h:37
SqliteRecord::~SqliteRecord
~SqliteRecord() override
Destructor.
Definition: SqliteRecord.cxx:25
get_generator_info.result
result
Definition: get_generator_info.py:21
SqliteRecord.h
Declaration of the SqliteRecord class.
index
Definition: index.py:1
SqliteRecord::SqliteRecord
SqliteRecord()=delete
skel.it
it
Definition: skel.GENtoEVGEN.py:407
athena.value
value
Definition: athena.py:124
ReadOfcFromCool.field
field
Definition: ReadOfcFromCool.py:48
SQLITEINP_LONG
@ SQLITEINP_LONG
Definition: SqliteRecord.h:26
SQLITEINP_INT
@ SQLITEINP_INT
Definition: SqliteRecord.h:25
SqliteRecord::getFloat
float getFloat(const std::string &field) const override
Get float field value.
Definition: SqliteRecord.cxx:62
SQLITEINP_DOUBLE
@ SQLITEINP_DOUBLE
Definition: SqliteRecord.h:28
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:36
SQLITEINP_STRING
@ SQLITEINP_STRING
Definition: SqliteRecord.h:29
SqliteRecord::addValue
void addValue(const std::string &field, SqliteInp value)
Definition: SqliteRecord.cxx:103
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:111
SQLITEINP_FLOAT
@ SQLITEINP_FLOAT
Definition: SqliteRecord.h:27
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
SqliteRecord::getLong
long getLong(const std::string &field) const override
Get long field value.
Definition: SqliteRecord.cxx:46
DeMoScan.first
bool first
Definition: DeMoScan.py:534
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::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:68
SqliteRecord::isFieldNull
bool isFieldNull(const std::string &field) const override
Check if the field value is NULL.
Definition: SqliteRecord.cxx:29
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:52