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

SqliteRecordset implements IRDBRecordset interface. More...

#include <SqliteRecordset.h>

Inheritance diagram for SqliteRecordset:
Collaboration diagram for SqliteRecordset:

Public Types

typedef RecordsVector::const_iterator const_iterator

Public Member Functions

 SqliteRecordset ()
 Construct empty recordset.
void getData (sqlite3 *db, const std::string &nodeName)
 Constructs SQL query and retrieves data from the DB.
unsigned int size () const override
std::string nodeName () const override
std::string tagName () const override
const IRDBRecordoperator[] (unsigned int index) const override
IRDBRecordset::const_iterator begin () const override
IRDBRecordset::const_iterator end () const override
bool msgLvl (const MSG::Level lvl) const
 Test the output level.
MsgStream & msg () const
 The standard message stream.
MsgStream & msg (const MSG::Level lvl) const
 The standard message stream.
void setLevel (MSG::Level lvl)
 Change the current logging level.

Private Member Functions

void initMessaging () const
 Initialize our message level and MessageSvc.

Private Attributes

std::string m_nodeName {}
SqliteInpDef_ptr m_def
RecordsVector m_records
std::string m_nm
 Message source name.
boost::thread_specific_ptr< MsgStream > m_msg_tls
 MsgStream instance (a std::cout like with print-out levels)
std::atomic< IMessageSvc * > m_imsg { nullptr }
 MessageSvc pointer.
std::atomic< MSG::Level > m_lvl { MSG::NIL }
 Current logging level.
std::atomic_flag m_initialized ATLAS_THREAD_SAFE = ATOMIC_FLAG_INIT
 Messaging initialized (initMessaging)

Detailed Description

SqliteRecordset implements IRDBRecordset interface.

It is a container of records read from an SQLite table.

Definition at line 30 of file SqliteRecordset.h.

Member Typedef Documentation

◆ const_iterator

typedef RecordsVector::const_iterator IRDBRecordset::const_iterator
inherited

Definition at line 52 of file IRDBRecordset.h.

Constructor & Destructor Documentation

◆ SqliteRecordset()

SqliteRecordset::SqliteRecordset ( )

Construct empty recordset.

Definition at line 19 of file SqliteRecordset.cxx.

20 : AthMessaging("SqliteRecordset")
21 , m_def(std::make_shared<SqliteInpDef>())
22{
23}
AthMessaging()
Default constructor:
SqliteInpDef_ptr m_def

Member Function Documentation

◆ begin()

IRDBRecordset::const_iterator SqliteRecordset::begin ( ) const
overridevirtual
Returns
begin iterator

Implements IRDBRecordset.

Definition at line 150 of file SqliteRecordset.cxx.

151{
152 return m_records.begin();
153}
RecordsVector m_records

◆ end()

IRDBRecordset::const_iterator SqliteRecordset::end ( ) const
overridevirtual
Returns
end iterator

Implements IRDBRecordset.

Definition at line 155 of file SqliteRecordset.cxx.

156{
157 return m_records.end();
158}

◆ getData()

void SqliteRecordset::getData ( sqlite3 * db,
const std::string & nodeName )

Constructs SQL query and retrieves data from the DB.

Parameters
taginfo[IN] object holding information about the node tag

Definition at line 25 of file SqliteRecordset.cxx.

26{
27 //the following should already be checked by the calling code and should never happen
28 if (db == nullptr){
29 throw std::runtime_error("SqliteRecordset::getData : db pointer is null");
30 }
31 if (nodeName.empty()){
32 throw std::runtime_error("SqliteRecordset::getData : nodeName is empty");
33 }
34 ATH_MSG_DEBUG("getData for " << nodeName);
36
37 std::ostringstream sql;
38
39 // First check if the table exists in the database
40 sql << "select * from " << m_nodeName;
41 sqlite3_stmt* stTable{nullptr};
42 int rc = sqlite3_prepare_v2(db, sql.str().c_str(), -1, &stTable, NULL);
43 if(rc!=SQLITE_OK) {
44 ATH_MSG_INFO(m_nodeName << " table is not found in the database");
45 return;
46 }
47
48 sql << " order by " << m_nodeName << "_data_id";
49 sqlite3_stmt* st{nullptr};
50 rc = sqlite3_prepare_v2(db, sql.str().c_str(), -1, &st, NULL);
51 if(rc!=SQLITE_OK) {
52 ATH_MSG_ERROR("Error occurred when preparing to fetch data for " << m_nodeName);
53 ATH_MSG_ERROR("SQLite Error: " << sqlite3_errmsg(db));
54 return;
55 }
56 int ctotal = sqlite3_column_count(st);
57
58 bool all_ok{true};
59
60 while(true) {
61 rc = sqlite3_step(st);
62
63 if(rc == SQLITE_ROW) {
64 SqliteRecord* rec = new SqliteRecord(m_def);
65 IRDBRecord_ptr record{rec};
66
67 // Loop throug the fields of the retrieved record
68 for(int i=0; i<ctotal; ++i) {
69
70 // The feature of SQLite: if in the given record some fields have NULL values,
71 // then the data type of the corresponding columns is reported as NULL.
72 // This means that we need to be able to build the Def gradually, as we read
73 // in new records of the table
74
75 // Do we need to extend Def?
76 std::string columnName = sqlite3_column_name(st,i);
77 bool extendDef = (m_def->find(columnName)==m_def->end());
78
79 auto columnType = sqlite3_column_type(st,i);
82
83 switch(columnType) {
84 case SQLITE_INTEGER:
85 inpType = SQLITEINP_INT;
86 val = sqlite3_column_int(st,i);
87 break;
88 case SQLITE_FLOAT:
89 inpType = SQLITEINP_DOUBLE;
90 val = sqlite3_column_double(st,i);
91 break;
92 case SQLITE_TEXT:
93 inpType = SQLITEINP_STRING;
94 val = std::string((char*)(sqlite3_column_text(st,i)));
95 break;
96 case SQLITE_BLOB:
97 inpType = SQLITEINP_STRING;
98 val = std::string((char*)(sqlite3_column_blob(st,i)));
99 break;
100 case SQLITE_NULL:
101 continue;
102 default:
103 break;
104 }
105
106 if(inpType==SQLITEINP_UNDEF) {
107 all_ok = false;
108 ATH_MSG_ERROR("Unexpected data type in column " << columnName << " of the table " << m_nodeName);
109 break;
110 }
111
112 if(extendDef) {
113 (*m_def)[columnName] = inpType;
114 }
115 rec->addValue(columnName,val);
116 }
117 m_records.push_back(std::move(record));
118 }
119 else if(rc == SQLITE_DONE) {
120 break;
121 }
122 else {
123 ATH_MSG_ERROR("Error occurred when fetching data for " << m_nodeName);
124 ATH_MSG_ERROR("SQLite Error: " << sqlite3_errmsg(db));
125 all_ok = false;
126 break;
127 }
128 }
129
130 if(!all_ok) {
131 // Do memory cleanup
132 m_records.clear();
133 }
134}
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_DEBUG(x)
std::unique_ptr< IRDBRecord > IRDBRecord_ptr
static Double_t rc
std::variant< int, long, float, double, std::string > SqliteInp
SqliteInpType
@ SQLITEINP_UNDEF
@ SQLITEINP_STRING
@ SQLITEINP_DOUBLE
@ SQLITEINP_INT
void addValue(const std::string &field, SqliteInp value)
std::string nodeName() const override
std::string m_nodeName

◆ initMessaging()

void AthMessaging::initMessaging ( ) const
privateinherited

Initialize our message level and MessageSvc.

This method should only be called once.

Definition at line 39 of file AthMessaging.cxx.

40{
42 // If user did not set an explicit level, set a default
43 if (m_lvl == MSG::NIL) {
44 m_lvl = m_imsg ?
45 static_cast<MSG::Level>( m_imsg.load()->outputLevel(m_nm) ) :
46 MSG::INFO;
47 }
48}
std::string m_nm
Message source name.
std::atomic< IMessageSvc * > m_imsg
MessageSvc pointer.
std::atomic< MSG::Level > m_lvl
Current logging level.
IMessageSvc * getMessageSvc(bool quiet=false)

◆ msg() [1/2]

MsgStream & AthMessaging::msg ( ) const
inlineinherited

The standard message stream.

Returns a reference to the default message stream May not be invoked before sysInitialize() has been invoked.

Definition at line 163 of file AthMessaging.h.

164{
165 MsgStream* ms = m_msg_tls.get();
166 if (!ms) {
167 if (!m_initialized.test_and_set()) initMessaging();
168 ms = new MsgStream(m_imsg,m_nm);
169 m_msg_tls.reset( ms );
170 }
171
172 ms->setLevel (m_lvl);
173 return *ms;
174}
boost::thread_specific_ptr< MsgStream > m_msg_tls
MsgStream instance (a std::cout like with print-out levels)
void initMessaging() const
Initialize our message level and MessageSvc.

◆ msg() [2/2]

MsgStream & AthMessaging::msg ( const MSG::Level lvl) const
inlineinherited

The standard message stream.

Returns a reference to the default message stream May not be invoked before sysInitialize() has been invoked.

Definition at line 178 of file AthMessaging.h.

179{ return msg() << lvl; }
MsgStream & msg() const
The standard message stream.

◆ msgLvl()

bool AthMessaging::msgLvl ( const MSG::Level lvl) const
inlineinherited

Test the output level.

Parameters
lvlThe message level to test against
Returns
boolean Indicating if messages at given level will be printed
Return values
trueMessages at level "lvl" will be printed

Definition at line 151 of file AthMessaging.h.

152{
153 if (m_lvl <= lvl) {
154 msg() << lvl;
155 return true;
156 } else {
157 return false;
158 }
159}

◆ nodeName()

std::string SqliteRecordset::nodeName ( ) const
overridevirtual
Returns
node name

Implements IRDBRecordset.

Definition at line 141 of file SqliteRecordset.cxx.

142{
143 return m_nodeName;
144}

◆ operator[]()

const IRDBRecord * SqliteRecordset::operator[] ( unsigned int index) const
overridevirtual
Parameters
index[IN] index of the record
Returns
RDBRecord by index

Implements IRDBRecordset.

Definition at line 146 of file SqliteRecordset.cxx.

147{
148 return m_records[index].get();
149}
str index
Definition DeMoScan.py:362

◆ setLevel()

void AthMessaging::setLevel ( MSG::Level lvl)
inherited

Change the current logging level.

Use this rather than msg().setLevel() for proper operation with MT.

Definition at line 28 of file AthMessaging.cxx.

29{
30 m_lvl = lvl;
31}

◆ size()

unsigned int SqliteRecordset::size ( ) const
overridevirtual
Returns
number of records

Implements IRDBRecordset.

Definition at line 136 of file SqliteRecordset.cxx.

137{
138 return m_records.size();
139}

◆ tagName()

std::string SqliteRecordset::tagName ( ) const
inlineoverridevirtual
Returns
tag name

Implements IRDBRecordset.

Definition at line 47 of file SqliteRecordset.h.

47{return std::string();}

Member Data Documentation

◆ ATLAS_THREAD_SAFE

std::atomic_flag m_initialized AthMessaging::ATLAS_THREAD_SAFE = ATOMIC_FLAG_INIT
mutableprivateinherited

Messaging initialized (initMessaging)

Definition at line 141 of file AthMessaging.h.

◆ m_def

SqliteInpDef_ptr SqliteRecordset::m_def
private

Definition at line 61 of file SqliteRecordset.h.

◆ m_imsg

std::atomic<IMessageSvc*> AthMessaging::m_imsg { nullptr }
mutableprivateinherited

MessageSvc pointer.

Definition at line 135 of file AthMessaging.h.

135{ nullptr };

◆ m_lvl

std::atomic<MSG::Level> AthMessaging::m_lvl { MSG::NIL }
mutableprivateinherited

Current logging level.

Definition at line 138 of file AthMessaging.h.

138{ MSG::NIL };

◆ m_msg_tls

boost::thread_specific_ptr<MsgStream> AthMessaging::m_msg_tls
mutableprivateinherited

MsgStream instance (a std::cout like with print-out levels)

Definition at line 132 of file AthMessaging.h.

◆ m_nm

std::string AthMessaging::m_nm
privateinherited

Message source name.

Definition at line 129 of file AthMessaging.h.

◆ m_nodeName

std::string SqliteRecordset::m_nodeName {}
private

Definition at line 60 of file SqliteRecordset.h.

60{};

◆ m_records

RecordsVector SqliteRecordset::m_records
private

Definition at line 62 of file SqliteRecordset.h.


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