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 19 of file SqliteRecord.cxx.

20 : m_def(std::move(def))
21{
22}
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 24 of file SqliteRecord.cxx.

25{
26}

Member Function Documentation

◆ addValue()

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

Definition at line 102 of file SqliteRecord.cxx.

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

◆ 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 108 of file SqliteRecord.cxx.

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

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}
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 87 of file SqliteRecord.cxx.

88{
89 return getDouble(std::format("{}_{}", field, index));
90}
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 61 of file SqliteRecord.cxx.

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

◆ 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 92 of file SqliteRecord.cxx.

93{
94 return getFloat(std::format("{}_{}", field, index));
95}
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 35 of file SqliteRecord.cxx.

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}

◆ 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 77 of file SqliteRecord.cxx.

78{
79 return getInt(std::format("{}_{}", field, index));
80}
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 45 of file SqliteRecord.cxx.

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

◆ 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 82 of file SqliteRecord.cxx.

83{
84 return getLong(std::format("{}_{}", field, index));
85}
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 67 of file SqliteRecord.cxx.

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}

◆ 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 97 of file SqliteRecord.cxx.

98{
99 return getString(std::format("{}_{}", field, index));
100}
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 157 of file SqliteRecord.cxx.

158{
159 switch(checkCode) {
161 throw std::runtime_error( std::format("handleError: Wrong name for the field {}",field));
163 throw std::runtime_error( std::format("handleError: Wrong data type requested for the field {}",field));
165 throw std::runtime_error( std::format("handleError: {} is NULL", field));
166 default:
167 break;
168 }
169 return;
170}

◆ 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 28 of file SqliteRecord.cxx.

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( std::format("Wrong name for the field {}",field));
32 return true;
33}

◆ 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: