CMS 3D CMS Logo

OMDSReader.h
Go to the documentation of this file.
1 #ifndef CondTools_L1Trigger_OMDSReader_h
2 #define CondTools_L1Trigger_OMDSReader_h
3 // -*- C++ -*-
4 //
5 // Package: L1Trigger
6 // Class : OMDSReader
7 //
16 //
17 // Original Author: Werner Sun
18 // Created: Sun Mar 2 01:36:06 CET 2008
19 // $Id: OMDSReader.h,v 1.14 2010/04/12 20:29:23 wsun Exp $
20 //
21 
22 // system include files
23 #include <memory>
24 
25 // user include files
27 #include "RelationalAccess/IQuery.h"
28 #include "CoralBase/AttributeList.h"
29 #include "CoralBase/AttributeSpecification.h"
30 #include "CoralBase/Attribute.h"
31 
32 #include "RelationalAccess/ITable.h"
33 #include "RelationalAccess/IView.h"
34 #include "RelationalAccess/ISchema.h"
35 #include "RelationalAccess/ISessionProxy.h"
36 #include "RelationalAccess/ICursor.h"
37 
38 // forward declarations
39 
40 namespace l1t {
41 
42  class OMDSReader : public DataManager {
43  public:
44  // std::vector< std::string > is the list of attribute names.
45  // We need to store the column names because there is no way to ask
46  // AttributeList for this information. We have a vector of AttributeLists
47  // because the query may return more than one row, each of which is
48  // encapsulated in an AttributeList.
49  class QueryResults {
50  public:
52  QueryResults(const std::vector<std::string>& columnNames, const std::vector<coral::AttributeList>& attLists)
54 
55  virtual ~QueryResults() {}
56 
57  const std::vector<std::string>& columnNames() const { return m_columnNames; }
58  const std::vector<coral::AttributeList>& attributeLists() const { return m_attributeLists; }
59  bool queryFailed() const { return m_attributeLists.empty(); }
60  int numberRows() const { return m_attributeLists.size(); }
61 
62  // Return value is false if variable is null.
63  template <class T>
64  bool fillVariable(const std::string& columnName, T& outputVariable) const;
65 
66  template <class T>
67  bool fillVariableFromRow(const std::string& columnName, int rowNumber, T& outputVariable) const;
68 
69  // If there is only one column, no need to give column name
70  template <class T>
71  bool fillVariable(T& outputVariable) const;
72 
73  template <class T>
74  bool fillVariableFromRow(int rowNumber, T& outputVariable) const;
75 
76  private:
77  std::vector<std::string> m_columnNames;
78  std::vector<coral::AttributeList> m_attributeLists;
79  };
80 
81  OMDSReader();
82 
83  OMDSReader(const std::string& connectString, const std::string& authenticationPath);
84 
85  ~OMDSReader() override;
86 
87  // ---------- const member functions ---------------------
88 
89  // These functions encapsulate basic SQL queries of the form
90  //
91  // SELECT <columns> FROM <schema.table> WHERE <conditionLHS> = <conditionRHS>
92  //
93  // where
94  //
95  // <columns> can be one or many column names
96  // <conditionRHS> can be a string or the result of another query
97 
98  // Assume data type of condition RHS is std::string
99  const QueryResults basicQuery(const std::vector<std::string>& columnNames,
100  const std::string& schemaName, // for nominal schema, use ""
101  const std::string& tableName,
102  const std::string& conditionLHS = "",
103  const QueryResults conditionRHS = QueryResults(),
104  // must have only one row
105  const std::string& conditionRHSName = ""
106  // if empty, conditionRHS must have only one column
107  );
108 
109  // Assume data type of condition RHS is std::string
110  const QueryResults basicQuery(const std::string& columnName,
111  const std::string& schemaName, // for nominal schema, use ""
112  const std::string& tableName,
113  const std::string& conditionLHS = "",
114  const QueryResults conditionRHS = QueryResults(),
115  // must have only one row
116  const std::string& conditionRHSName = ""
117  // if empty, conditionRHS must have only one column
118  );
119 
120  // Assume data type of condition RHS is std::string
121  const QueryResults basicQueryView(const std::vector<std::string>& columnNames,
122  const std::string& schemaName, // for nominal schema, use ""
123  const std::string& viewName,
124  const std::string& conditionLHS = "",
125  const QueryResults conditionRHS = QueryResults(),
126  // must have only one row
127  const std::string& conditionRHSName = ""
128  // if empty, conditionRHS must have only one column
129  );
130 
131  // Assume data type of condition RHS is std::string
132  const QueryResults basicQueryView(const std::string& columnName,
133  const std::string& schemaName, // for nominal schema, use ""
134  const std::string& viewName,
135  const std::string& conditionLHS = "",
136  const QueryResults conditionRHS = QueryResults(),
137  // must have only one row
138  const std::string& conditionRHSName = ""
139  // if empty, conditionRHS must have only one column
140  );
141 
142  // For any data type of condition RHS.
143  // Example usage, for an int key:
144  // results = omdsReader.basicQueryGenericKey<int>(...) ;
145  template <class T>
146  const QueryResults basicQueryGenericKey(const std::vector<std::string>& columnNames,
147  const std::string& schemaName, // for nominal schema, use ""
148  const std::string& tableName,
149  const std::string& conditionLHS = "",
150  const QueryResults conditionRHS = QueryResults(),
151  // must have only one row
152  const std::string& conditionRHSName = ""
153  // if empty, conditionRHS must have only one column
154  );
155 
156  // For any data type of condition RHS.
157  // Example usage, for an int key:
158  // results = omdsReader.basicQueryGenericKey<int>(...) ;
159  template <class T>
160  const QueryResults basicQueryGenericKey(const std::string& columnName,
161  const std::string& schemaName, // for nominal schema, use ""
162  const std::string& tableName,
163  const std::string& conditionLHS = "",
164  const QueryResults conditionRHS = QueryResults(),
165  // must have only one row
166  const std::string& conditionRHSName = ""
167  // if empty, conditionRHS must have only one column
168  );
169 
170  template <class T>
171  const QueryResults singleAttribute(const T& data) const;
172 
173  std::vector<std::string> columnNames(const std::string& schemaName, // for nominal schema, use ""
174  const std::string& tableName);
175 
176  std::vector<std::string> columnNamesView(const std::string& schemaName, // for nominal schema, use ""
177  const std::string& viewName);
178 
179  // ---------- static member functions --------------------
180 
181  // ---------- member functions ---------------------------
182 
183  void connect(const std::string& connectString, const std::string& authenticationPath);
184 
185  private:
186  OMDSReader(const OMDSReader&) = delete; // stop default
187 
188  const OMDSReader& operator=(const OMDSReader&) = delete; // stop default
189 
190  // ---------- member data --------------------------------
191  };
192 
193  template <class T>
194  const OMDSReader::QueryResults OMDSReader::basicQueryGenericKey(const std::vector<std::string>& columnNames,
195  const std::string& schemaName,
196  const std::string& tableName,
197  const std::string& conditionLHS,
198  const QueryResults conditionRHS,
199  const std::string& conditionRHSName) {
200  coral::ISessionProxy& coralSession = session.coralSession();
201  coral::ISchema& schema = schemaName.empty() ? coralSession.nominalSchema() : coralSession.schema(schemaName);
202 
203  coral::ITable& table = schema.tableHandle(tableName);
204 
205  // Pointer is deleted automatically at end of function.
206  std::shared_ptr<coral::IQuery> query(table.newQuery());
207 
208  // Construct query
209  std::vector<std::string>::const_iterator it = columnNames.begin();
210  std::vector<std::string>::const_iterator end = columnNames.end();
211  for (; it != end; ++it) {
212  query->addToOutputList(*it);
213  }
214 
215  // Only apply condition if RHS has one row.
216  if (!conditionLHS.empty() && conditionRHS.numberRows() == 1) {
217  if (!conditionRHSName.empty()) {
218  // Use type of dummyVariable to determine type of condition RHS
219  coral::AttributeList attList;
220  attList.extend(conditionRHSName, typeid(T));
221  T tmp = T();
222  conditionRHS.fillVariable(conditionRHSName, tmp);
223  attList[conditionRHSName].data<T>() = tmp;
224 
225  query->setCondition(conditionLHS + " = :" + conditionRHSName, attList);
226  } else if (conditionRHS.columnNames().size() == 1)
227  // check for only one column
228  {
229  query->setCondition(conditionLHS + " = :" + conditionRHS.columnNames().front(),
230  conditionRHS.attributeLists().front());
231  }
232  }
233 
234  coral::ICursor& cursor = query->execute();
235 
236  // Copy AttributeLists for external use because the cursor is deleted
237  // when the query goes out of scope.
238  std::vector<coral::AttributeList> atts;
239  while (cursor.next()) {
240  atts.push_back(cursor.currentRow());
241  };
242 
243  return QueryResults(columnNames, atts);
244  }
245 
246  template <class T>
248  const std::string& schemaName,
249  const std::string& tableName,
250  const std::string& conditionLHS,
251  const QueryResults conditionRHS,
252  const std::string& conditionRHSName) {
253  std::vector<std::string> columnNames;
254  columnNames.push_back(columnName);
255  return basicQueryGenericKey<T>(columnNames, schemaName, tableName, conditionLHS, conditionRHS, conditionRHSName);
256  }
257 
258  template <class T>
260  std::vector<std::string> names;
261  names.push_back("dummy");
262 
263  coral::AttributeList attList;
264  attList.extend("dummy", typeid(T));
265  attList["dummy"].data<T>() = data;
266 
267  std::vector<coral::AttributeList> atts;
268  atts.push_back(attList);
269 
270  return QueryResults(names, atts);
271  }
272 
273  template <class T>
274  bool OMDSReader::QueryResults::fillVariable(const std::string& columnName, T& outputVariable) const {
275  return fillVariableFromRow(columnName, 0, outputVariable);
276  }
277 
278  template <class T>
280  int rowNumber,
281  T& outputVariable) const {
282  // Check index in bounds
283  if (rowNumber < 0 || rowNumber >= numberRows())
284  return false;
285  const coral::AttributeList& row = m_attributeLists[rowNumber];
286  if (row[columnName].isNull())
287  return false;
288  outputVariable = row[columnName].template data<T>();
289  return true;
290  }
291 
292  template <class T>
293  bool OMDSReader::QueryResults::fillVariable(T& outputVariable) const {
294  return fillVariableFromRow(0, outputVariable);
295  }
296 
297  template <class T>
298  bool OMDSReader::QueryResults::fillVariableFromRow(int rowNumber, T& outputVariable) const {
299  // Check index in bounds and only one column
300  if (rowNumber < 0 || rowNumber >= numberRows() || m_columnNames.size() != 1)
301  return false;
302  const coral::AttributeList& row = m_attributeLists[rowNumber];
303  if (row[m_columnNames.front()].isNull())
304  return false;
305  outputVariable = row[m_columnNames.front()].template data<T>();
306  return true;
307  }
308 
309 } // namespace l1t
310 #endif
l1t::OMDSReader::QueryResults
Definition: OMDSReader.h:49
l1t::OMDSReader::OMDSReader
OMDSReader()
Definition: OMDSReader.cc:36
l1t::OMDSReader
Definition: OMDSReader.h:42
l1t::OMDSReader::QueryResults::m_attributeLists
std::vector< coral::AttributeList > m_attributeLists
Definition: OMDSReader.h:78
l1t::DataManager::session
cond::persistency::Session session
Definition: DataManager.h:33
l1t::OMDSReader::singleAttribute
const QueryResults singleAttribute(const T &data) const
Definition: OMDSReader.h:259
createJobs.tmp
tmp
align.sh
Definition: createJobs.py:716
end
#define end
Definition: vmac.h:39
l1t::OMDSReader::basicQueryGenericKey
const QueryResults basicQueryGenericKey(const std::vector< std::string > &columnNames, const std::string &schemaName, const std::string &tableName, const std::string &conditionLHS="", const QueryResults conditionRHS=QueryResults(), const std::string &conditionRHSName="")
Definition: OMDSReader.h:194
l1t::OMDSReader::QueryResults::fillVariableFromRow
bool fillVariableFromRow(const std::string &columnName, int rowNumber, T &outputVariable) const
Definition: OMDSReader.h:279
query
Definition: query.py:1
names
const std::string names[nVars_]
Definition: PhotonIDValueMapProducer.cc:122
l1t::OMDSReader::QueryResults::fillVariable
bool fillVariable(const std::string &columnName, T &outputVariable) const
Definition: OMDSReader.h:274
l1t::OMDSReader::QueryResults::columnNames
const std::vector< std::string > & columnNames() const
Definition: OMDSReader.h:57
l1t::OMDSReader::columnNames
std::vector< std::string > columnNames(const std::string &schemaName, const std::string &tableName)
Definition: OMDSReader.cc:138
HLT_2018_cff.tableName
tableName
Definition: HLT_2018_cff.py:10
l1t::OMDSReader::QueryResults::~QueryResults
virtual ~QueryResults()
Definition: OMDSReader.h:55
l1t::OMDSReader::~OMDSReader
~OMDSReader() override
Definition: OMDSReader.cc:53
l1t::OMDSReader::connect
void connect(const std::string &connectString, const std::string &authenticationPath)
Definition: OMDSReader.cc:43
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
l1t::OMDSReader::QueryResults::m_columnNames
std::vector< std::string > m_columnNames
Definition: OMDSReader.h:77
l1t
delete x;
Definition: CaloConfig.h:22
cond::persistency::Session::coralSession
coral::ISessionProxy & coralSession()
Definition: Session.cc:218
l1t::OMDSReader::QueryResults::numberRows
int numberRows() const
Definition: OMDSReader.h:60
l1t::OMDSReader::operator=
const OMDSReader & operator=(const OMDSReader &)=delete
l1t::OMDSReader::basicQuery
const QueryResults basicQuery(const std::vector< std::string > &columnNames, const std::string &schemaName, const std::string &tableName, const std::string &conditionLHS="", const QueryResults conditionRHS=QueryResults(), const std::string &conditionRHSName="")
Definition: OMDSReader.cc:75
writeEcalDQMStatus.authenticationPath
authenticationPath
Definition: writeEcalDQMStatus.py:19
l1t::OMDSReader::columnNamesView
std::vector< std::string > columnNamesView(const std::string &schemaName, const std::string &viewName)
Definition: OMDSReader.cc:234
DataManager.h
l1t::DataManager
Definition: DataManager.h:17
T
long double T
Definition: Basic3DVectorLD.h:48
l1t::OMDSReader::QueryResults::QueryResults
QueryResults(const std::vector< std::string > &columnNames, const std::vector< coral::AttributeList > &attLists)
Definition: OMDSReader.h:52
l1t::OMDSReader::QueryResults::queryFailed
bool queryFailed() const
Definition: OMDSReader.h:59
data
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
contentValuesFiles.query
query
Definition: contentValuesFiles.py:38
l1t::OMDSReader::basicQueryView
const QueryResults basicQueryView(const std::vector< std::string > &columnNames, const std::string &schemaName, const std::string &viewName, const std::string &conditionLHS="", const QueryResults conditionRHS=QueryResults(), const std::string &conditionRHSName="")
Definition: OMDSReader.cc:158
l1t::OMDSReader::QueryResults::QueryResults
QueryResults()
Definition: OMDSReader.h:51
TableParser.table
table
Definition: TableParser.py:111
l1t::OMDSReader::QueryResults::attributeLists
const std::vector< coral::AttributeList > & attributeLists() const
Definition: OMDSReader.h:58