ATLAS Offline Software
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
SqliteRecordset Class Referencefinal

SqliteRecordset implements IRDBRecordset interface. It is a container of records read from an SQLite table. 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. More...
 
void getData (sqlite3 *db, const std::string &nodeName)
 Constructs SQL query and retrieves data from the DB. More...
 
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. More...
 
MsgStream & msg () const
 The standard message stream. More...
 
MsgStream & msg (const MSG::Level lvl) const
 The standard message stream. More...
 
void setLevel (MSG::Level lvl)
 Change the current logging level. More...
 

Private Member Functions

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

Private Attributes

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

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 }

Member Function Documentation

◆ begin()

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

Implements IRDBRecordset.

Definition at line 143 of file SqliteRecordset.cxx.

144 {
145  return m_records.begin();
146 }

◆ end()

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

Implements IRDBRecordset.

Definition at line 148 of file SqliteRecordset.cxx.

149 {
150  return m_records.end();
151 }

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

◆ 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  m_lvl = m_imsg ?
43  static_cast<MSG::Level>( m_imsg.load()->outputLevel(m_nm) ) :
44  MSG::INFO;
45 }

◆ 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 164 of file AthMessaging.h.

165 {
166  MsgStream* ms = m_msg_tls.get();
167  if (!ms) {
168  if (!m_initialized.test_and_set()) initMessaging();
169  ms = new MsgStream(m_imsg,m_nm);
170  m_msg_tls.reset( ms );
171  }
172 
173  ms->setLevel (m_lvl);
174  return *ms;
175 }

◆ 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 179 of file AthMessaging.h.

180 { return msg() << lvl; }

◆ 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_initialized.test_and_set()) initMessaging();
154  if (m_lvl <= lvl) {
155  msg() << lvl;
156  return true;
157  } else {
158  return false;
159  }
160 }

◆ nodeName()

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

Implements IRDBRecordset.

Definition at line 134 of file SqliteRecordset.cxx.

135 {
136  return m_nodeName;
137 }

◆ 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 139 of file SqliteRecordset.cxx.

140 {
141  return m_records[index].get();
142 }

◆ 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 129 of file SqliteRecordset.cxx.

130 {
131  return m_records.size();
132 }

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

◆ m_lvl

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

Current logging level.

Definition at line 138 of file AthMessaging.h.

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

◆ 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:
AthMessaging::m_lvl
std::atomic< MSG::Level > m_lvl
Current logging level.
Definition: AthMessaging.h:138
SqliteInp
std::variant< int, long, float, double, std::string > SqliteInp
Definition: SqliteRecord.h:37
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
CaloCondBlobAlgs_fillNoiseFromASCII.db
db
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:43
SqliteRecordset::m_records
RecordsVector m_records
Definition: SqliteRecordset.h:62
SqliteRecordset::m_nodeName
std::string m_nodeName
Definition: SqliteRecordset.h:60
SQLITEINP_UNDEF
@ SQLITEINP_UNDEF
Definition: SqliteRecord.h:30
SQLITEINP_INT
@ SQLITEINP_INT
Definition: SqliteRecord.h:25
AthMessaging::m_imsg
std::atomic< IMessageSvc * > m_imsg
MessageSvc pointer.
Definition: AthMessaging.h:135
python.SystemOfUnits.ms
int ms
Definition: SystemOfUnits.py:132
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
SqliteRecordset::nodeName
std::string nodeName() const override
Definition: SqliteRecordset.cxx:134
beamspotman.sql
sql
Definition: beamspotman.py:674
AthMessaging::AthMessaging
AthMessaging()
Default constructor:
TrigConf::MSGTC::Level
Level
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStream.h:21
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
lumiFormat.i
int i
Definition: lumiFormat.py:85
SQLITEINP_DOUBLE
@ SQLITEINP_DOUBLE
Definition: SqliteRecord.h:28
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
SQLITEINP_STRING
@ SQLITEINP_STRING
Definition: SqliteRecord.h:29
SqliteRecord::addValue
void addValue(const std::string &field, SqliteInp value)
Definition: SqliteRecord.cxx:102
SqliteRecord
SqliteRecord is one record in the SqliteRecordset object.
Definition: SqliteRecord.h:49
AthMessaging::msg
MsgStream & msg() const
The standard message stream.
Definition: AthMessaging.h:164
SqliteInpType
SqliteInpType
Definition: SqliteRecord.h:24
DeMoScan.index
string index
Definition: DeMoScan.py:364
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
AthMessaging::m_nm
std::string m_nm
Message source name.
Definition: AthMessaging.h:129
SqliteRecordset::m_def
SqliteInpDef_ptr m_def
Definition: SqliteRecordset.h:61
IRDBRecord_ptr
std::unique_ptr< IRDBRecord > IRDBRecord_ptr
Definition: IRDBRecordset.h:23
AthMessaging::initMessaging
void initMessaging() const
Initialize our message level and MessageSvc.
Definition: AthMessaging.cxx:39
AthMessaging::m_msg_tls
boost::thread_specific_ptr< MsgStream > m_msg_tls
MsgStream instance (a std::cout like with print-out levels)
Definition: AthMessaging.h:132
python.trfValidateRootFile.rc
rc
Definition: trfValidateRootFile.py:349