test
CMS 3D CMS Logo

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