CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
OMDSReader.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: L1Trigger
4 // Class : OMDSReader
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author:
10 // Created: Sun Mar 2 01:46:46 CET 2008
11 // $Id: OMDSReader.cc,v 1.12 2010/02/16 21:56:37 wsun Exp $
12 //
13 
14 // system include files
15 #include <set>
16 #include <iostream>
17 
18 // user include files
20 #include "RelationalAccess/ITableDescription.h"
21 #include "RelationalAccess/IColumn.h"
23 
24 //
25 // constants, enums and typedefs
26 //
27 
28 //
29 // static data member definitions
30 //
31 
32 namespace l1t
33 {
34 
35 //
36 // constructors and destructor
37 //
39  : DataManager()
40  {}
41 
42  OMDSReader::OMDSReader( const std::string& connectString,
43  const std::string& authenticationPath )
44  : DataManager( connectString, authenticationPath, true )
45  {
46  session->transaction().start( true ) ;
47  }
48 
49  void
50  OMDSReader::connect( const std::string& connectString,
51  const std::string& authenticationPath )
52  {
53  DataManager::connect( connectString, authenticationPath, true ) ;
54  session->transaction().start( true ) ;
55  }
56 
57 // OMDSReader::OMDSReader(const OMDSReader& rhs)
58 // {
59 // // do actual copying here;
60 // }
61 
63 {
64 }
65 
66 //
67 // assignment operators
68 //
69 // const OMDSReader& OMDSReader::operator=(const OMDSReader& rhs)
70 // {
71 // //An exception safe implementation is
72 // OMDSReader temp(rhs);
73 // swap(rhs);
74 //
75 // return *this;
76 // }
77 
78 //
79 // member functions
80 //
81 
82 //
83 // const member functions
84 //
85 
88  const std::vector< std::string >& columnNames,
89  const std::string& schemaName,
90  const std::string& tableName,
91  const std::string& conditionLHS,
92  const QueryResults conditionRHS,
93  const std::string& conditionRHSName ) const
94  {
95  coral::ISchema& schema = schemaName.empty() ?
97  session->schema( schemaName ) ;
98 
99  coral::ITable& table = schema.tableHandle( tableName ) ;
100 
101  // Pointer is deleted automatically at end of function.
102  boost::shared_ptr< coral::IQuery > query( table.newQuery() ) ;
103 
104  // Construct query
105  std::vector< std::string >::const_iterator it = columnNames.begin() ;
106  std::vector< std::string >::const_iterator end = columnNames.end() ;
107  for( ; it != end ; ++it )
108  {
109  query->addToOutputList( *it ) ;
110  }
111 
112  // Only apply condition if RHS has one row.
113  if( !conditionLHS.empty() && conditionRHS.numberRows() == 1 )
114  {
115  if( !conditionRHSName.empty() )
116  {
117  // Assume all RHS types are strings.
118  coral::AttributeList attList ;
119  attList.extend( conditionRHSName, typeid( std::string ) ) ;
120  std::string tmp ;
121  conditionRHS.fillVariable( conditionRHSName, tmp ) ;
122  attList[ conditionRHSName ].data< std::string >() = tmp ;
123 
124  query->setCondition( conditionLHS + " = :" + conditionRHSName,
125  attList ) ;
126  }
127  else if( conditionRHS.columnNames().size() == 1 )
128  // check for only one column
129  {
130  query->setCondition( conditionLHS + " = :" +
131  conditionRHS.columnNames().front(),
132  conditionRHS.attributeLists().front() ) ;
133  }
134  }
135 
136  coral::ICursor& cursor = query->execute() ;
137 
138  // Copy AttributeLists for external use because the cursor is deleted
139  // when the query goes out of scope.
140  std::vector< coral::AttributeList > atts ;
141  while( cursor.next() )
142  {
143  atts.push_back( cursor.currentRow() ) ;
144  } ;
145 
146  return QueryResults( columnNames, atts ) ;
147  }
148 
151  const std::string& columnName,
152  const std::string& schemaName,
153  const std::string& tableName,
154  const std::string& conditionLHS,
155  const QueryResults conditionRHS,
156  const std::string& conditionRHSName ) const
157  {
158  std::vector< std::string > columnNames ;
159  columnNames.push_back( columnName ) ;
160  return basicQuery( columnNames, schemaName, tableName,
161  conditionLHS, conditionRHS, conditionRHSName ) ;
162  }
163 
164  std::vector< std::string >
166  const std::string& schemaName,
167  const std::string& tableName ) const
168  {
169  coral::ISchema& schema = schemaName.empty() ?
171  session->schema( schemaName ) ;
172 
173  coral::ITable& table = schema.tableHandle( tableName ) ;
174  const coral::ITableDescription& tableDesc = table.description() ;
175 
176  std::vector< std::string > names ;
177  int nCols = tableDesc.numberOfColumns() ;
178 
179  for( int i = 0 ; i < nCols ; ++i )
180  {
181  const coral::IColumn& column = tableDesc.columnDescription( i ) ;
182  names.push_back( column.name() ) ;
183  }
184 
185  return names ;
186  }
187 
188  // VIEW
189 
192  const std::vector< std::string >& columnNames,
193  const std::string& schemaName,
194  const std::string& viewName,
195  const std::string& conditionLHS,
196  const QueryResults conditionRHS,
197  const std::string& conditionRHSName ) const
198  {
199  coral::ISchema& schema = schemaName.empty() ?
201  session->schema( schemaName ) ;
202 
203  // coral::IView& view = schema.viewHandle( viewName ) ;
204 
205  // Pointer is deleted automatically at end of function.
206  coral::IQuery* query = schema.newQuery(); ;
207 
208  // Construct query
209  for (std::vector<std::string>::const_iterator constIt = columnNames.begin(); constIt
210  != columnNames.end(); ++constIt) {
211  query->addToOutputList(*constIt);
212  }
213 
214  query->addToTableList(viewName);
215 
216  // Only apply condition if RHS has one row.
217  if (!conditionLHS.empty() && conditionRHS.numberRows() == 1) {
218 
219  if (!conditionRHSName.empty()) {
220  // Assume all RHS types are strings.
221  coral::AttributeList attList;
222  attList.extend(conditionRHSName, typeid(std::string));
224  conditionRHS.fillVariable(conditionRHSName, tmp);
225  attList[conditionRHSName].data<std::string> () = tmp;
226 
227  query->setCondition(conditionLHS + " = :" + conditionRHSName, attList);
228  } else if (conditionRHS.columnNames().size() == 1)
229  // check for only one column
230  {
231  query->setCondition(
232  conditionLHS + " = :" + conditionRHS.columnNames().front(),
233  conditionRHS.attributeLists().front());
234  }
235  }
236 
237  coral::ICursor& cursor = query->execute();
238 
239  // Copy AttributeLists for external use because the cursor is deleted
240  // when the query goes out of scope.
241  std::vector<coral::AttributeList> atts;
242  while (cursor.next()) {
243  atts.push_back(cursor.currentRow());
244  };
245 
246  delete query;
247 
248 // // Run a wildcard query on the view
249 // coral::IQuery* query2 = workingSchema.newQuery();
250 // query2->addToTableList(V0);
251 // coral::ICursor& cursor2 = query2->execute();
252 // while ( cursor2.next() ) {
253 // cursor2.currentRow().toOutputStream( std::cout ) << std::endl;
254 // }
255 // delete query2;
256 
257 
258 
259  return QueryResults(columnNames, atts);
260  }
261 
264  const std::string& columnName,
265  const std::string& schemaName,
266  const std::string& viewName,
267  const std::string& conditionLHS,
268  const QueryResults conditionRHS,
269  const std::string& conditionRHSName ) const
270  {
271  std::vector< std::string > columnNames ;
272  columnNames.push_back( columnName ) ;
273  return basicQuery( columnNames, schemaName, viewName,
274  conditionLHS, conditionRHS, conditionRHSName ) ;
275  }
276 
277  std::vector< std::string >
279  const std::string& schemaName,
280  const std::string& viewName ) const
281  {
282  coral::ISchema& schema = schemaName.empty() ?
284  session->schema( schemaName ) ;
285 
286  std::set< std::string > views = schema.listViews ();
287  std::vector< std::string > names ;
288 
289  if (schema.existsView (viewName)) {
290 
291  coral::IView& view = schema.viewHandle( viewName ) ;
292 
293  int nCols = view.numberOfColumns() ;
294 
295  for (int i = 0; i < nCols; ++i) {
296  const coral::IColumn& column = view.column(i);
297  names.push_back(column.name());
298  }
299 
300  return names ;
301 
302  }
303 
304  return names ;
305 
306  }
307 
308  //
309 // static member functions
310 //
311 }
int i
Definition: DBlmapReader.cc:9
bool fillVariable(const std::string &columnName, T &outputVariable) const
Definition: OMDSReader.h:311
static const HistoName names[]
DbTransaction & transaction()
Definition: DbSession.cc:208
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:238
void connect(const std::string &connectString, const std::string &authenticationPath, bool isOMDS=false)
Definition: DataManager.cc:24
int start(bool readOnly=false)
start transaction
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: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:50
#define table(NAME)
Definition: DbCore.h:49
#define column(...)
Definition: DbCore.h:74
coral::ISchema & nominalSchema()
Definition: DbSession.cc:243
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
tuple query
Definition: o2o.py:269
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