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.15 2013/01/09 14:07:50 davidlt 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  ) const ;
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  ) const ;
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  ) const ;
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  ) const ;
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  ) const ;
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  ) const ;
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 ) const ;
196 
197  std::vector< std::string > columnNamesView(
198  const std::string& schemaName, // for nominal schema, use ""
199  const std::string& viewName ) const ;
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 ) const
224  {
225  coral::ISchema& schema = schemaName.empty() ?
227  session->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 ) const
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
list table
Definition: asciidump.py:386
std::vector< std::string > m_columnNames
Definition: OMDSReader.h:89
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
std::vector< std::string > columnNamesView(const std::string &schemaName, const std::string &viewName) const
Definition: OMDSReader.cc:278
coral::ISchema & schema(const std::string &schemaName)
Definition: DbSession.cc:223
std::vector< coral::AttributeList > m_attributeLists
Definition: OMDSReader.h:90
QueryResults(const std::vector< std::string > &columnNames, const std::vector< coral::AttributeList > &attLists)
Definition: OMDSReader.h:57
bool fillVariableFromRow(const std::string &columnName, int rowNumber, T &outputVariable) const
Definition: OMDSReader.h:319
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="") const
Definition: OMDSReader.cc:87
const std::vector< coral::AttributeList > & attributeLists() const
Definition: OMDSReader.h:65
virtual ~OMDSReader()
Definition: OMDSReader.cc:62
#define end
Definition: vmac.h:38
const std::vector< std::string > & columnNames() const
Definition: OMDSReader.h:63
void connect(const std::string &connectString, const std::string &authenticationPath)
Definition: OMDSReader.cc:50
coral::ISchema & nominalSchema()
Definition: DbSession.cc:228
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
tuple query
Definition: o2o.py:269
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="") const
Definition: OMDSReader.h:217
long double T
std::vector< std::string > columnNames(const std::string &schemaName, const std::string &tableName) const
Definition: OMDSReader.cc:165
cond::DbSession * session
Definition: DataManager.h:44
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="") const
Definition: OMDSReader.cc:191
const OMDSReader & operator=(const OMDSReader &)