ATLAS Offline Software
Loading...
Searching...
No Matches
SqliteRecord Class Referencefinal

SqliteRecord is one record in the SqliteRecordset object. More...

#include <SqliteRecord.h>

Inheritance diagram for SqliteRecord:
Collaboration diagram for SqliteRecord:

Public Member Functions

 SqliteRecord (SqliteInpDef_ptr def)
 SqliteRecord ()=delete
 SqliteRecord (const SqliteRecord &)=delete
SqliteRecordoperator= (const SqliteRecord &)=delete
 ~SqliteRecord () override
 Destructor.
bool isFieldNull (std::string_view field) const override
 Check if the field value is NULL.
int getInt (std::string_view field) const override
 Get int field value.
long getLong (std::string_view field) const override
 Get long field value.
double getDouble (std::string_view field) const override
 Get double field value.
float getFloat (std::string_view field) const override
 Get float field value.
virtual const std::string & getString (std::string_view field) const override
 Get string field value.
int getInt (std::string_view field, unsigned int index) const override
 Get array int field value.
long getLong (std::string_view field, unsigned int index) const override
 Get array long field value.
double getDouble (std::string_view field, unsigned int index) const override
 Get array double field value.
float getFloat (std::string_view field, unsigned int index) const override
 Get array float field value.
virtual const std::string & getString (std::string_view field, unsigned int index) const override
 Get array string field value.
void dump () const
 Dump to cout.
void addValue (std::string_view field, SqliteInp value)

Private Types

enum  FieldCheckCode { FIELD_CHECK_OK , FIELD_CHECK_BAD_NAME , FIELD_CHECK_BAD_TYPE , FIELD_CHECK_NULL_VAL }
typedef std::map< std::string, SqliteInp, std::less<> > Record
typedef Record::const_iterator RecordCIterator
typedef std::tuple< RecordCIterator, FieldCheckCodeFieldCheckResult

Private Member Functions

FieldCheckResult checkField (std::string_view field, SqliteInpType fieldType) const
void handleError (std::string_view field, FieldCheckCode checkCode) const

Private Attributes

SqliteInpDef_ptr m_def
Record m_record

Detailed Description

SqliteRecord is one record in the SqliteRecordset object.

Definition at line 48 of file SqliteRecord.h.

Member Typedef Documentation

◆ FieldCheckResult

Definition at line 137 of file SqliteRecord.h.

◆ Record

typedef std::map<std::string,SqliteInp, std::less<> > SqliteRecord::Record
private

Definition at line 128 of file SqliteRecord.h.

◆ RecordCIterator

typedef Record::const_iterator SqliteRecord::RecordCIterator
private

Definition at line 129 of file SqliteRecord.h.

Member Enumeration Documentation

◆ FieldCheckCode

Enumerator
FIELD_CHECK_OK 
FIELD_CHECK_BAD_NAME 
FIELD_CHECK_BAD_TYPE 
FIELD_CHECK_NULL_VAL 

Definition at line 130 of file SqliteRecord.h.

Constructor & Destructor Documentation

◆ SqliteRecord() [1/3]

SqliteRecord::SqliteRecord ( SqliteInpDef_ptr def)

Definition at line 21 of file SqliteRecord.cxx.

22 : m_def(std::move(def))
23{
24}
SqliteInpDef_ptr m_def

◆ SqliteRecord() [2/3]

SqliteRecord::SqliteRecord ( )
delete

◆ SqliteRecord() [3/3]

SqliteRecord::SqliteRecord ( const SqliteRecord & )
delete

◆ ~SqliteRecord()

SqliteRecord::~SqliteRecord ( )
override

Destructor.

Definition at line 26 of file SqliteRecord.cxx.

27{
28}

Member Function Documentation

◆ addValue()

void SqliteRecord::addValue ( std::string_view field,
SqliteInp value )

Definition at line 104 of file SqliteRecord.cxx.

105{
106 auto [it,result] = m_record.emplace(std::string{field}, std::move(value));
107 if(!result) throw std::runtime_error(std::format("Unexpected error when adding new value for the field {}. Duplicate field name?", field));
108}

◆ checkField()

SqliteRecord::FieldCheckResult SqliteRecord::checkField ( std::string_view field,
SqliteInpType fieldType ) const
inlineprivate

Definition at line 146 of file SqliteRecord.h.

148{
150 RecordCIterator checkIt{m_record.end()};
151
152 auto defIt = m_def->find(field);
153 if(defIt==m_def->end()) checkCode = FIELD_CHECK_BAD_NAME;
154 else if(defIt->second!=fieldType) checkCode = FIELD_CHECK_BAD_TYPE;
155
156 if(checkCode==FIELD_CHECK_OK) {
157 checkIt = m_record.find(field);
158 if(checkIt==m_record.end()) {
159 checkCode = FIELD_CHECK_NULL_VAL;
160 }
161 }
162 return std::make_tuple(checkIt,checkCode);
163}
Record::const_iterator RecordCIterator

◆ dump()

void SqliteRecord::dump ( ) const

Dump to cout.

Definition at line 110 of file SqliteRecord.cxx.

111{
112 boost::io::ios_all_saver saver (std::cout);
113 bool first{true};
114 for(const auto& [colName,colType] : *m_def) {
115 if(first) {
116 first = false;
117 }
118 else {
119 std::cout << ", ";
120 }
121 auto recIt = m_record.find(colName);
122 bool fieldNull = (recIt==m_record.end());
123 std::cout << "[" << colName << " (";
124 switch(colType) {
125 case SQLITEINP_INT:
126 std::cout << "int) : " << (fieldNull? "NULL" : std::to_string(std::get<int>(recIt->second))) << "]";
127 break;
128 case SQLITEINP_LONG:
129 std::cout << "long) : " << (fieldNull? "NULL" : std::to_string(std::get<long>(recIt->second))) << "]";
130 break;
131 case SQLITEINP_FLOAT:
132 std::cout << "float) : ";
133 if (fieldNull) {
134 std::cout << "NULL";
135 }
136 else {
137 std::cout << std::setprecision(10) << std::get<float>(recIt->second) << "]";
138 }
139 break;
140 case SQLITEINP_DOUBLE:
141 std::cout << "double) : ";
142 if (fieldNull) {
143 std::cout << "NULL";
144 }
145 else {
146 std::cout << std::setprecision(10) << std::get<double>(recIt->second) << "]";
147 }
148 break;
149 case SQLITEINP_STRING:
150 std::cout << "string) : " << (fieldNull? "NULL" : ("\"" + std::get<std::string>(recIt->second)) + "\"") << "]";
151 break;
152 default:
153 std::cout << "ERROR) : ]";
154 }
155 }
156 std::cout << std::endl;
157}
@ SQLITEINP_FLOAT
@ SQLITEINP_STRING
@ SQLITEINP_DOUBLE
@ SQLITEINP_LONG
@ SQLITEINP_INT
bool first
Definition DeMoScan.py:534

◆ getDouble() [1/2]

double SqliteRecord::getDouble ( std::string_view field) const
overridevirtual

Get double field value.

Parameters
field[IN] field name
Returns
field value

Implements IRDBRecord.

Definition at line 53 of file SqliteRecord.cxx.

54{
55 auto [recIt,checkCode] = checkField(field,SQLITEINP_DOUBLE);
56 if(checkCode==FIELD_CHECK_OK) {
57 return std::get<double>(recIt->second);
58 }
59 handleError(field,checkCode);
60 return 0.;
61}
void handleError(std::string_view field, FieldCheckCode checkCode) const
FieldCheckResult checkField(std::string_view field, SqliteInpType fieldType) const

◆ getDouble() [2/2]

double SqliteRecord::getDouble ( std::string_view field,
unsigned int index ) const
overridevirtual

Get array double field value.

Parameters
field[IN] field name
index[IN] index in the array
Returns
field value

Implements IRDBRecord.

Definition at line 89 of file SqliteRecord.cxx.

90{
91 return getDouble(std::format("{}_{}", field, index));
92}
double getDouble(std::string_view field) const override
Get double field value.

◆ getFloat() [1/2]

float SqliteRecord::getFloat ( std::string_view field) const
overridevirtual

Get float field value.

Parameters
field[IN] field name
Returns
field value

Implements IRDBRecord.

Definition at line 63 of file SqliteRecord.cxx.

64{
65 // SQLite stores REAL as double
66 return (float)getDouble(field);
67}

◆ getFloat() [2/2]

float SqliteRecord::getFloat ( std::string_view field,
unsigned int index ) const
overridevirtual

Get array float field value.

Parameters
field[IN] field name
index[IN] index in the array
Returns
field value

Implements IRDBRecord.

Definition at line 94 of file SqliteRecord.cxx.

95{
96 return getFloat(std::format("{}_{}", field, index));
97}
float getFloat(std::string_view field) const override
Get float field value.

◆ getInt() [1/2]

int SqliteRecord::getInt ( std::string_view field) const
overridevirtual

Get int field value.

Parameters
field[IN] field name
Returns
field value

Implements IRDBRecord.

Definition at line 37 of file SqliteRecord.cxx.

38{
39 auto [recIt,checkCode] = checkField(field,SQLITEINP_INT);
40 if(checkCode==FIELD_CHECK_OK) {
41 return std::get<int>(recIt->second);
42 }
43 handleError(field,checkCode);
44 return 0;
45}

◆ getInt() [2/2]

int SqliteRecord::getInt ( std::string_view field,
unsigned int index ) const
overridevirtual

Get array int field value.

Parameters
field[IN] field name
index[IN] index in the array
Returns
field value

Implements IRDBRecord.

Definition at line 79 of file SqliteRecord.cxx.

80{
81 return getInt(std::format("{}_{}", field, index));
82}
int getInt(std::string_view field) const override
Get int field value.

◆ getLong() [1/2]

long SqliteRecord::getLong ( std::string_view field) const
overridevirtual

Get long field value.

Parameters
field[IN] field name
Returns
field value

Implements IRDBRecord.

Definition at line 47 of file SqliteRecord.cxx.

48{
49 // Our database does not support LONG data types at this moment
50 return (long)getInt(field);
51}

◆ getLong() [2/2]

long SqliteRecord::getLong ( std::string_view field,
unsigned int index ) const
overridevirtual

Get array long field value.

Parameters
field[IN] field name
index[IN] index in the array
Returns
field value

Implements IRDBRecord.

Definition at line 84 of file SqliteRecord.cxx.

85{
86 return getLong(std::format("{}_{}", field, index));
87}
long getLong(std::string_view field) const override
Get long field value.

◆ getString() [1/2]

const std::string & SqliteRecord::getString ( std::string_view field) const
overridevirtual

Get string field value.

Parameters
field[IN] field name
Returns
field value

Implements IRDBRecord.

Definition at line 69 of file SqliteRecord.cxx.

70{
71 auto [recIt,checkCode] = checkField(field,SQLITEINP_STRING);
72 if(checkCode==FIELD_CHECK_OK) {
73 return std::get<std::string>(recIt->second);
74 }
75 handleError(field,checkCode);
76 throw std::runtime_error("Unexpected error in SqliteRecord::getString()");
77}

◆ getString() [2/2]

const std::string & SqliteRecord::getString ( std::string_view field,
unsigned int index ) const
overridevirtual

Get array string field value.

Parameters
field[IN] field name
index[IN] index in the array
Returns
field value

Implements IRDBRecord.

Definition at line 99 of file SqliteRecord.cxx.

100{
101 return getString(std::format("{}_{}", field, index));
102}
virtual const std::string & getString(std::string_view field) const override
Get string field value.

◆ handleError()

void SqliteRecord::handleError ( std::string_view field,
FieldCheckCode checkCode ) const
private

Definition at line 160 of file SqliteRecord.cxx.

161{
162 switch(checkCode) {
164 throw std::runtime_error( std::format("handleError: Wrong name for the field {}",field));
166 throw std::runtime_error( std::format("handleError: Wrong data type requested for the field {}",field));
168 throw std::runtime_error( std::format("handleError: {} is NULL", field));
169 default:
170 break;
171 }
172 return;
173}

◆ isFieldNull()

bool SqliteRecord::isFieldNull ( std::string_view field) const
overridevirtual

Check if the field value is NULL.

Parameters
field[IN] field name @retun TRUE if the field is NULL, FALSE otherwise

Implements IRDBRecord.

Definition at line 30 of file SqliteRecord.cxx.

31{
32 if(m_record.find(field)!=m_record.end()) return false;
33 if(m_def->find(field)==m_def->end()) throw std::runtime_error( std::format("Wrong name for the field {}",field));
34 return true;
35}

◆ operator=()

SqliteRecord & SqliteRecord::operator= ( const SqliteRecord & )
delete

Member Data Documentation

◆ m_def

SqliteInpDef_ptr SqliteRecord::m_def
private

Definition at line 139 of file SqliteRecord.h.

◆ m_record

Record SqliteRecord::m_record
private

Definition at line 140 of file SqliteRecord.h.


The documentation for this class was generated from the following files: