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 (const std::string &field) const override
 Check if the field value is NULL.
int getInt (const std::string &field) const override
 Get int field value.
long getLong (const std::string &field) const override
 Get long field value.
double getDouble (const std::string &field) const override
 Get double field value.
float getFloat (const std::string &field) const override
 Get float field value.
virtual const std::string & getString (const std::string &field) const override
 Get string field value.
int getInt (const std::string &field, unsigned int index) const override
 Get array int field value.
long getLong (const std::string &field, unsigned int index) const override
 Get array long field value.
double getDouble (const std::string &field, unsigned int index) const override
 Get array double field value.
float getFloat (const std::string &field, unsigned int index) const override
 Get array float field value.
virtual const std::string & getString (const std::string &field, unsigned int index) const override
 Get array string field value.
void dump () const
 Dump to cout.
void addValue (const std::string &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, SqliteInpRecord
typedef Record::const_iterator RecordCIterator
typedef std::tuple< RecordCIterator, FieldCheckCodeFieldCheckResult

Private Member Functions

FieldCheckResult checkField (const std::string &field, SqliteInpType fieldType) const
void handleError (const std::string &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 138 of file SqliteRecord.h.

◆ Record

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

Definition at line 129 of file SqliteRecord.h.

◆ RecordCIterator

typedef Record::const_iterator SqliteRecord::RecordCIterator
private

Definition at line 130 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 131 of file SqliteRecord.h.

Constructor & Destructor Documentation

◆ SqliteRecord() [1/3]

SqliteRecord::SqliteRecord ( SqliteInpDef_ptr def)

Definition at line 20 of file SqliteRecord.cxx.

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

26{
27}

Member Function Documentation

◆ addValue()

void SqliteRecord::addValue ( const std::string & field,
SqliteInp value )

Definition at line 103 of file SqliteRecord.cxx.

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}

◆ checkField()

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

Definition at line 149 of file SqliteRecord.h.

151{
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 else 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}
Record::const_iterator RecordCIterator

◆ dump()

void SqliteRecord::dump ( ) const

Dump to cout.

Definition at line 111 of file SqliteRecord.cxx.

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_FLOAT
@ SQLITEINP_STRING
@ SQLITEINP_DOUBLE
@ SQLITEINP_LONG
@ SQLITEINP_INT
bool first
Definition DeMoScan.py:534

◆ getDouble() [1/2]

double SqliteRecord::getDouble ( const std::string & field) const
overridevirtual

Get double field value.

Parameters
field[IN] field name
Returns
field value

Implements IRDBRecord.

Definition at line 52 of file SqliteRecord.cxx.

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}
FieldCheckResult checkField(const std::string &field, SqliteInpType fieldType) const
void handleError(const std::string &field, FieldCheckCode checkCode) const

◆ getDouble() [2/2]

double SqliteRecord::getDouble ( const std::string & 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 88 of file SqliteRecord.cxx.

89{
90 return getDouble(field + "_" + std::to_string(index));
91}
double getDouble(const std::string &field) const override
Get double field value.

◆ getFloat() [1/2]

float SqliteRecord::getFloat ( const std::string & field) const
overridevirtual

Get float field value.

Parameters
field[IN] field name
Returns
field value

Implements IRDBRecord.

Definition at line 62 of file SqliteRecord.cxx.

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

◆ getFloat() [2/2]

float SqliteRecord::getFloat ( const std::string & 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 93 of file SqliteRecord.cxx.

94{
95 return getFloat(field + "_" + std::to_string(index));
96}
float getFloat(const std::string &field) const override
Get float field value.

◆ getInt() [1/2]

int SqliteRecord::getInt ( const std::string & field) const
overridevirtual

Get int field value.

Parameters
field[IN] field name
Returns
field value

Implements IRDBRecord.

Definition at line 36 of file SqliteRecord.cxx.

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}

◆ getInt() [2/2]

int SqliteRecord::getInt ( const std::string & 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 78 of file SqliteRecord.cxx.

79{
80 return getInt(field + "_" + std::to_string(index));
81}
int getInt(const std::string &field) const override
Get int field value.

◆ getLong() [1/2]

long SqliteRecord::getLong ( const std::string & field) const
overridevirtual

Get long field value.

Parameters
field[IN] field name
Returns
field value

Implements IRDBRecord.

Definition at line 46 of file SqliteRecord.cxx.

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

◆ getLong() [2/2]

long SqliteRecord::getLong ( const std::string & 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 83 of file SqliteRecord.cxx.

84{
85 return getLong(field + "_" + std::to_string(index));
86}
long getLong(const std::string &field) const override
Get long field value.

◆ getString() [1/2]

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

Get string field value.

Parameters
field[IN] field name
Returns
field value

Implements IRDBRecord.

Definition at line 68 of file SqliteRecord.cxx.

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}

◆ getString() [2/2]

const std::string & SqliteRecord::getString ( const std::string & 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 98 of file SqliteRecord.cxx.

99{
100 return getString(field + "_" + std::to_string(index));
101}
virtual const std::string & getString(const std::string &field) const override
Get string field value.

◆ handleError()

void SqliteRecord::handleError ( const std::string & field,
FieldCheckCode checkCode ) const
inlineprivate

Definition at line 168 of file SqliteRecord.h.

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}

◆ isFieldNull()

bool SqliteRecord::isFieldNull ( const std::string & 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 29 of file SqliteRecord.cxx.

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}

◆ operator=()

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

Member Data Documentation

◆ m_def

SqliteInpDef_ptr SqliteRecord::m_def
private

Definition at line 140 of file SqliteRecord.h.

◆ m_record

Record SqliteRecord::m_record
private

Definition at line 141 of file SqliteRecord.h.


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