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(const OMDSReader&) = delete; // stop default
86 
87  const OMDSReader& operator=(const OMDSReader&) = delete; // stop default
88 
89  ~OMDSReader() override;
90 
91  // ---------- const member functions ---------------------
92 
93  // These functions encapsulate basic SQL queries of the form
94  //
95  // SELECT <columns> FROM <schema.table> WHERE <conditionLHS> = <conditionRHS>
96  //
97  // where
98  //
99  // <columns> can be one or many column names
100  // <conditionRHS> can be a string or the result of another query
101 
102  // Assume data type of condition RHS is std::string
103  const QueryResults basicQuery(const std::vector<std::string>& columnNames,
104  const std::string& schemaName, // for nominal schema, use ""
105  const std::string& tableName,
106  const std::string& conditionLHS = "",
107  const QueryResults conditionRHS = QueryResults(),
108  // must have only one row
109  const std::string& conditionRHSName = ""
110  // if empty, conditionRHS must have only one column
111  );
112 
113  // Assume data type of condition RHS is std::string
114  const QueryResults basicQuery(const std::string& columnName,
115  const std::string& schemaName, // for nominal schema, use ""
116  const std::string& tableName,
117  const std::string& conditionLHS = "",
118  const QueryResults conditionRHS = QueryResults(),
119  // must have only one row
120  const std::string& conditionRHSName = ""
121  // if empty, conditionRHS must have only one column
122  );
123 
124  // Assume data type of condition RHS is std::string
125  const QueryResults basicQueryView(const std::vector<std::string>& columnNames,
126  const std::string& schemaName, // for nominal schema, use ""
127  const std::string& viewName,
128  const std::string& conditionLHS = "",
129  const QueryResults conditionRHS = QueryResults(),
130  // must have only one row
131  const std::string& conditionRHSName = ""
132  // if empty, conditionRHS must have only one column
133  );
134 
135  // Assume data type of condition RHS is std::string
136  const QueryResults basicQueryView(const std::string& columnName,
137  const std::string& schemaName, // for nominal schema, use ""
138  const std::string& viewName,
139  const std::string& conditionLHS = "",
140  const QueryResults conditionRHS = QueryResults(),
141  // must have only one row
142  const std::string& conditionRHSName = ""
143  // if empty, conditionRHS must have only one column
144  );
145 
146  // For any data type of condition RHS.
147  // Example usage, for an int key:
148  // results = omdsReader.basicQueryGenericKey<int>(...) ;
149  template <class T>
150  const QueryResults basicQueryGenericKey(const std::vector<std::string>& columnNames,
151  const std::string& schemaName, // for nominal schema, use ""
152  const std::string& tableName,
153  const std::string& conditionLHS = "",
154  const QueryResults conditionRHS = QueryResults(),
155  // must have only one row
156  const std::string& conditionRHSName = ""
157  // if empty, conditionRHS must have only one column
158  );
159 
160  // For any data type of condition RHS.
161  // Example usage, for an int key:
162  // results = omdsReader.basicQueryGenericKey<int>(...) ;
163  template <class T>
164  const QueryResults basicQueryGenericKey(const std::string& columnName,
165  const std::string& schemaName, // for nominal schema, use ""
166  const std::string& tableName,
167  const std::string& conditionLHS = "",
168  const QueryResults conditionRHS = QueryResults(),
169  // must have only one row
170  const std::string& conditionRHSName = ""
171  // if empty, conditionRHS must have only one column
172  );
173 
174  template <class T>
175  const QueryResults singleAttribute(const T& data) const;
176 
177  std::vector<std::string> columnNames(const std::string& schemaName, // for nominal schema, use ""
178  const std::string& tableName);
179 
180  std::vector<std::string> columnNamesView(const std::string& schemaName, // for nominal schema, use ""
181  const std::string& viewName);
182 
183  // ---------- static member functions --------------------
184 
185  // ---------- member functions ---------------------------
186 
187  void connect(const std::string& connectString, const std::string& authenticationPath);
188 
189  private:
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
const QueryResults singleAttribute(const T &data) const
Definition: OMDSReader.h:259
delete x;
Definition: CaloConfig.h:22
~OMDSReader() override
Definition: OMDSReader.cc:53
const std::string names[nVars_]
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
Definition: query.py:1
const std::vector< coral::AttributeList > & attributeLists() const
Definition: OMDSReader.h:58
QueryResults(const std::vector< std::string > &columnNames, const std::vector< coral::AttributeList > &attLists)
Definition: OMDSReader.h:52
std::vector< std::string > columnNames(const std::string &schemaName, const std::string &tableName)
Definition: OMDSReader.cc:138
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
const std::vector< std::string > & columnNames() const
Definition: OMDSReader.h:57
bool fillVariableFromRow(const std::string &columnName, int rowNumber, T &outputVariable) const
Definition: OMDSReader.h:279
bool fillVariable(const std::string &columnName, T &outputVariable) const
Definition: OMDSReader.h:274
void connect(const std::string &connectString, const std::string &authenticationPath)
Definition: OMDSReader.cc:43
std::vector< std::string > columnNamesView(const std::string &schemaName, const std::string &viewName)
Definition: OMDSReader.cc:234
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
coral::ISessionProxy & coralSession()
Definition: Session.cc:218
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
const OMDSReader & operator=(const OMDSReader &)=delete
std::vector< coral::AttributeList > m_attributeLists
Definition: OMDSReader.h:78
tmp
align.sh
Definition: createJobs.py:716
long double T
cond::persistency::Session session
Definition: DataManager.h:33
std::vector< std::string > m_columnNames
Definition: OMDSReader.h:77