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 
43  class OMDSReader : public DataManager
44  {
45 
46  public:
47  // std::vector< std::string > is the list of attribute names.
48  // We need to store the column names because there is no way to ask
49  // AttributeList for this information. We have a vector of AttributeLists
50  // because the query may return more than one row, each of which is
51  // encapsulated in an AttributeList.
53  {
54  public:
56  QueryResults( const std::vector< std::string >& columnNames,
57  const std::vector< coral::AttributeList >& attLists )
58  : m_columnNames( columnNames), m_attributeLists( attLists ) {}
59 
60  virtual ~QueryResults() {}
61 
62  const std::vector< std::string >& columnNames() const
63  { return m_columnNames ; }
64  const std::vector< coral::AttributeList >& attributeLists() const
65  { return m_attributeLists ; }
66  bool queryFailed() const { return m_attributeLists.empty() ; }
67  int numberRows() const { return m_attributeLists.size() ; }
68 
69  // Return value is false if variable is null.
70  template< class T >
71  bool fillVariable( const std::string& columnName,
72  T& outputVariable ) const ;
73 
74  template< class T >
75  bool fillVariableFromRow( const std::string& columnName,
76  int rowNumber,
77  T& outputVariable ) const ;
78 
79  // If there is only one column, no need to give column name
80  template< class T >
81  bool fillVariable( T& outputVariable ) const ;
82 
83  template< class T >
84  bool fillVariableFromRow( int rowNumber,
85  T& outputVariable ) const ;
86 
87  private:
88  std::vector< std::string > m_columnNames ;
89  std::vector< coral::AttributeList > m_attributeLists ;
90  } ;
91 
92  OMDSReader() ;
93 
94  OMDSReader( const std::string& connectString,
96 
97  ~OMDSReader() override;
98 
99  // ---------- const member functions ---------------------
100 
101  // These functions encapsulate basic SQL queries of the form
102  //
103  // SELECT <columns> FROM <schema.table> WHERE <conditionLHS> = <conditionRHS>
104  //
105  // where
106  //
107  // <columns> can be one or many column names
108  // <conditionRHS> can be a string or the result of another query
109 
110  // Assume data type of condition RHS is std::string
111  const QueryResults basicQuery(
112  const std::vector< std::string >& columnNames,
113  const std::string& schemaName, // for nominal schema, use ""
114  const std::string& tableName,
115  const std::string& conditionLHS = "",
116  const QueryResults conditionRHS = QueryResults(),
117  // must have only one row
118  const std::string& conditionRHSName = ""
119  // if empty, conditionRHS must have only one column
120  );
121 
122  // Assume data type of condition RHS is std::string
123  const QueryResults basicQuery(
124  const std::string& columnName,
125  const std::string& schemaName, // for nominal schema, use ""
126  const std::string& tableName,
127  const std::string& conditionLHS = "",
128  const QueryResults conditionRHS = QueryResults(),
129  // must have only one row
130  const std::string& conditionRHSName = ""
131  // if empty, conditionRHS must have only one column
132  );
133 
134 
135  // Assume data type of condition RHS is std::string
137  const std::vector< std::string >& columnNames,
138  const std::string& schemaName, // for nominal schema, use ""
139  const std::string& viewName,
140  const std::string& conditionLHS = "",
141  const QueryResults conditionRHS = QueryResults(),
142  // must have only one row
143  const std::string& conditionRHSName = ""
144  // if empty, conditionRHS must have only one column
145  );
146 
147  // Assume data type of condition RHS is std::string
149  const std::string& columnName,
150  const std::string& schemaName, // for nominal schema, use ""
151  const std::string& viewName,
152  const std::string& conditionLHS = "",
153  const QueryResults conditionRHS = QueryResults(),
154  // must have only one row
155  const std::string& conditionRHSName = ""
156  // if empty, conditionRHS must have only one column
157  );
158 
159  // For any data type of condition RHS.
160  // Example usage, for an int key:
161  // results = omdsReader.basicQueryGenericKey<int>(...) ;
162  template< class T >
164  const std::vector< std::string >& columnNames,
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  // For any data type of condition RHS.
175  // Example usage, for an int key:
176  // results = omdsReader.basicQueryGenericKey<int>(...) ;
177  template< class T >
179  const std::string& columnName,
180  const std::string& schemaName, // for nominal schema, use ""
181  const std::string& tableName,
182  const std::string& conditionLHS = "",
183  const QueryResults conditionRHS = QueryResults(),
184  // must have only one row
185  const std::string& conditionRHSName = ""
186  // if empty, conditionRHS must have only one column
187  );
188 
189  template< class T >
190  const QueryResults singleAttribute( const T& data ) const ;
191 
192  std::vector< std::string > columnNames(
193  const std::string& schemaName, // for nominal schema, use ""
194  const std::string& tableName );
195 
196  std::vector< std::string > columnNamesView(
197  const std::string& schemaName, // for nominal schema, use ""
198  const std::string& viewName );
199 
200  // ---------- static member functions --------------------
201 
202  // ---------- member functions ---------------------------
203 
204  void connect( const std::string& connectString,
206 
207  private:
208  OMDSReader(const OMDSReader&) = delete; // stop default
209 
210  const OMDSReader& operator=(const OMDSReader&) = delete; // stop default
211 
212  // ---------- member data --------------------------------
213 };
214 
215  template< class T > const OMDSReader::QueryResults
217  const std::vector< std::string >& columnNames,
218  const std::string& schemaName,
219  const std::string& tableName,
220  const std::string& conditionLHS,
221  const QueryResults conditionRHS,
222  const std::string& conditionRHSName )
223  {
224  coral::ISessionProxy& coralSession = session.coralSession();
225  coral::ISchema& schema = schemaName.empty() ?
226  coralSession.nominalSchema() :
227  coralSession.schema( schemaName ) ;
228 
229  coral::ITable& table = schema.tableHandle( tableName ) ;
230 
231  // Pointer is deleted automatically at end of function.
232  boost::shared_ptr< coral::IQuery > query( table.newQuery() ) ;
233 
234  // Construct query
235  std::vector< std::string >::const_iterator it = columnNames.begin() ;
236  std::vector< std::string >::const_iterator end = columnNames.end() ;
237  for( ; it != end ; ++it )
238  {
239  query->addToOutputList( *it ) ;
240  }
241 
242  // Only apply condition if RHS has one row.
243  if( !conditionLHS.empty() && conditionRHS.numberRows() == 1 )
244  {
245  if( !conditionRHSName.empty() )
246  {
247  // Use type of dummyVariable to determine type of condition RHS
248  coral::AttributeList attList ;
249  attList.extend( conditionRHSName, typeid( T ) ) ;
250  T tmp = T();
251  conditionRHS.fillVariable( conditionRHSName, tmp ) ;
252  attList[ conditionRHSName ].data< T >() = tmp ;
253 
254  query->setCondition( conditionLHS + " = :" + conditionRHSName,
255  attList ) ;
256  }
257  else if( conditionRHS.columnNames().size() == 1 )
258  // check for only one column
259  {
260  query->setCondition( conditionLHS + " = :" +
261  conditionRHS.columnNames().front(),
262  conditionRHS.attributeLists().front() ) ;
263  }
264  }
265 
266  coral::ICursor& cursor = query->execute() ;
267 
268  // Copy AttributeLists for external use because the cursor is deleted
269  // when the query goes out of scope.
270  std::vector< coral::AttributeList > atts ;
271  while( cursor.next() )
272  {
273  atts.push_back( cursor.currentRow() ) ;
274  } ;
275 
276  return QueryResults( columnNames, atts ) ;
277  }
278 
279  template< class T > const OMDSReader::QueryResults
281  const std::string& columnName,
282  const std::string& schemaName,
283  const std::string& tableName,
284  const std::string& conditionLHS,
285  const QueryResults conditionRHS,
286  const std::string& conditionRHSName )
287  {
288  std::vector< std::string > columnNames ;
289  columnNames.push_back( columnName ) ;
290  return basicQueryGenericKey< T >( columnNames, schemaName, tableName,
291  conditionLHS, conditionRHS, conditionRHSName);
292  }
293 
294  template< class T > const OMDSReader::QueryResults
296  {
297  std::vector< std::string > names ;
298  names.push_back( "dummy" ) ;
299 
300  coral::AttributeList attList ;
301  attList.extend( "dummy", typeid( T ) ) ;
302  attList[ "dummy" ].data< T >() = data ;
303 
304  std::vector< coral::AttributeList > atts ;
305  atts.push_back( attList ) ;
306 
307  return QueryResults( names, atts ) ;
308  }
309 
310  template< class T > bool
312  const std::string& columnName,
313  T& outputVariable ) const
314  {
315  return fillVariableFromRow( columnName, 0, outputVariable ) ;
316  }
317 
318  template< class T > bool
320  const std::string& columnName,
321  int rowNumber,
322  T& outputVariable ) const
323  {
324  // Check index in bounds
325  if( rowNumber < 0 || rowNumber >= numberRows() ) return false ;
326  const coral::AttributeList& row = m_attributeLists[ rowNumber ] ;
327  if( row[ columnName ].isNull() ) return false ;
328  outputVariable = row[ columnName ].template data< T >() ;
329  return true ;
330  }
331 
332  template< class T > bool
333  OMDSReader::QueryResults::fillVariable( T& outputVariable ) const
334  {
335  return fillVariableFromRow( 0, outputVariable ) ;
336  }
337 
338  template< class T > bool
340  T& outputVariable ) const
341  {
342  // Check index in bounds and only one column
343  if( rowNumber < 0 || rowNumber >= numberRows() ||
344  m_columnNames.size() != 1 ) return false ;
345  const coral::AttributeList& row = m_attributeLists[ rowNumber ] ;
346  if( row[ m_columnNames.front() ].isNull() ) return false ;
347  outputVariable = row[ m_columnNames.front() ].template data< T >() ;
348  return true ;
349  }
350 
351 }
352 #endif
std::vector< std::string > m_columnNames
Definition: OMDSReader.h:88
bool fillVariable(const std::string &columnName, T &outputVariable) const
Definition: OMDSReader.h:311
static const HistoName names[]
const QueryResults singleAttribute(const T &data) const
Definition: OMDSReader.h:295
delete x;
Definition: CaloConfig.h:22
std::vector< coral::AttributeList > m_attributeLists
Definition: OMDSReader.h:89
~OMDSReader() override
Definition: OMDSReader.cc:61
def query(query_str, verbose=False)
Definition: das.py:5
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:216
Definition: query.py:1
QueryResults(const std::vector< std::string > &columnNames, const std::vector< coral::AttributeList > &attLists)
Definition: OMDSReader.h:56
bool fillVariableFromRow(const std::string &columnName, int rowNumber, T &outputVariable) const
Definition: OMDSReader.h:319
const std::vector< coral::AttributeList > & attributeLists() const
Definition: OMDSReader.h:64
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:86
#define end
Definition: vmac.h:39
const std::vector< std::string > & columnNames() const
Definition: OMDSReader.h:62
void connect(const std::string &connectString, const std::string &authenticationPath)
Definition: OMDSReader.cc:49
std::vector< std::string > columnNamesView(const std::string &schemaName, const std::string &viewName)
Definition: OMDSReader.cc:280
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:192
coral::ISessionProxy & coralSession()
Definition: Session.cc:228
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
const OMDSReader & operator=(const OMDSReader &)=delete
long double T
cond::persistency::Session session
Definition: DataManager.h:40