ATLAS Offline Software
SqliteRecord.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
13 #include "SqliteRecord.h"
14 #include <iostream>
15 #include <iomanip>
16 #include <stdexcept>
17 #include <sstream>
18 
20  : m_def(def)
21 {
22 }
23 
25 {
26 }
27 
28 bool SqliteRecord::isFieldNull(const std::string& 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( "Wrong name for the field "+ field);
32  return true;
33 }
34 
35 int SqliteRecord::getInt(const std::string& 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 
45 long SqliteRecord::getLong(const std::string& field) const
46 {
47  // Our database does not support LONG data types at this moment
48  return (long)getInt(field);
49 }
50 
51 double SqliteRecord::getDouble(const std::string& 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 
61 float SqliteRecord::getFloat(const std::string& field) const
62 {
63  // SQLite stores REAL as double
64  return (float)getDouble(field);
65 }
66 
67 const std::string& SqliteRecord::getString(const std::string& 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 
77 int SqliteRecord::getInt(const std::string& field, unsigned int index) const
78 {
79  return getInt(field + "_" + std::to_string(index));
80 }
81 
82 long SqliteRecord::getLong(const std::string& field, unsigned int index) const
83 {
84  return getLong(field + "_" + std::to_string(index));
85 }
86 
87 double SqliteRecord::getDouble(const std::string& field, unsigned int index) const
88 {
89  return getDouble(field + "_" + std::to_string(index));
90 }
91 
92 float SqliteRecord::getFloat(const std::string& field, unsigned int index) const
93 {
94  return getFloat(field + "_" + std::to_string(index));
95 }
96 
97 const std::string& SqliteRecord::getString(const std::string& field, unsigned int index) const
98 {
99  return getString(field + "_" + std::to_string(index));
100 }
101 
102 void SqliteRecord::addValue(const std::string& field
103  , SqliteInp value)
104 {
105  auto [it,result] = m_record.insert(std::pair(field,value));
106  if(!result) throw std::runtime_error("Unexpected error when adding new value for the field " +
107  field + ". Duplicate field name?");
108 }
109 
110 void SqliteRecord::dump() const
111 {
112  bool first{true};
113  for(const auto& [colName,colType] : *m_def) {
114  if(first) {
115  first = false;
116  }
117  else {
118  std::cout << ", ";
119  }
120  auto recIt = m_record.find(colName);
121  bool fieldNull = (recIt==m_record.end());
122  std::cout << "[" << colName << " (";
123  switch(colType) {
124  case SQLITEINP_INT:
125  std::cout << "int) : " << (fieldNull? "NULL" : std::to_string(std::get<int>(recIt->second))) << "]";
126  break;
127  case SQLITEINP_LONG:
128  std::cout << "long) : " << (fieldNull? "NULL" : std::to_string(std::get<long>(recIt->second))) << "]";
129  break;
130  case SQLITEINP_FLOAT:
131  std::cout << "float) : ";
132  if (fieldNull) {
133  std::cout << "NULL";
134  }
135  else {
136  std::cout << std::setprecision(10) << std::get<float>(recIt->second) << "]";
137  }
138  break;
139  case SQLITEINP_DOUBLE:
140  std::cout << "double) : ";
141  if (fieldNull) {
142  std::cout << "NULL";
143  }
144  else {
145  std::cout << std::setprecision(10) << std::get<double>(recIt->second) << "]";
146  }
147  break;
148  case SQLITEINP_STRING:
149  std::cout << "string) : " << (fieldNull? "NULL" : ("\"" + std::get<std::string>(recIt->second)) + "\"") << "]";
150  break;
151  default:
152  std::cout << "ERROR) : ]";
153  }
154  }
155  std::cout << std::endl;
156 }
SqliteInp
std::variant< int, long, float, double, std::string > SqliteInp
Definition: SqliteRecord.h:37
SqliteRecord::~SqliteRecord
~SqliteRecord() override
Destructor.
Definition: SqliteRecord.cxx:24
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:423
athena.value
value
Definition: athena.py:122
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:61
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:35
SQLITEINP_STRING
@ SQLITEINP_STRING
Definition: SqliteRecord.h:29
SqliteRecord::addValue
void addValue(const std::string &field, SqliteInp value)
Definition: SqliteRecord.cxx:102
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
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:45
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: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