CMS 3D CMS Logo

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"
22 
23 //
24 // constants, enums and typedefs
25 //
26 
27 //
28 // static data member definitions
29 //
30 
31 namespace l1t
32 {
33 
34 //
35 // constructors and destructor
36 //
38  : DataManager()
39  {}
40 
41  OMDSReader::OMDSReader( const std::string& connectString,
43  : DataManager( connectString, authenticationPath, true )
44  {
45  session.transaction().start( true ) ;
46  }
47 
48  void
49  OMDSReader::connect( const std::string& connectString,
51  {
52  DataManager::connect( connectString, authenticationPath, true ) ;
53  session.transaction().start( true ) ;
54  }
55 
56 // OMDSReader::OMDSReader(const OMDSReader& rhs)
57 // {
58 // // do actual copying here;
59 // }
60 
62 {
63 }
64 
65 //
66 // assignment operators
67 //
68 // const OMDSReader& OMDSReader::operator=(const OMDSReader& rhs)
69 // {
70 // //An exception safe implementation is
71 // OMDSReader temp(rhs);
72 // swap(rhs);
73 //
74 // return *this;
75 // }
76 
77 //
78 // member functions
79 //
80 
81 //
82 // const member functions
83 //
84 
87  const std::vector< std::string >& columnNames,
88  const std::string& schemaName,
89  const std::string& tableName,
90  const std::string& conditionLHS,
91  const QueryResults conditionRHS,
92  const std::string& conditionRHSName )
93  {
94  coral::ISessionProxy& coralSession = session.coralSession();
95  coral::ISchema& schema = schemaName.empty() ?
96  coralSession.nominalSchema() :
97  coralSession.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 )
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 )
168  {
169  coral::ISessionProxy& coralSession = session.coralSession();
170  coral::ISchema& schema = schemaName.empty() ?
171  coralSession.nominalSchema() :
172  coralSession.schema( schemaName ) ;
173 
174  coral::ITable& table = schema.tableHandle( tableName ) ;
175  const coral::ITableDescription& tableDesc = table.description() ;
176 
177  std::vector< std::string > names ;
178  int nCols = tableDesc.numberOfColumns() ;
179 
180  for( int i = 0 ; i < nCols ; ++i )
181  {
182  const coral::IColumn& column = tableDesc.columnDescription( i ) ;
183  names.push_back( column.name() ) ;
184  }
185 
186  return names ;
187  }
188 
189  // VIEW
190 
193  const std::vector< std::string >& columnNames,
194  const std::string& schemaName,
195  const std::string& viewName,
196  const std::string& conditionLHS,
197  const QueryResults conditionRHS,
198  const std::string& conditionRHSName )
199  {
200  coral::ISessionProxy& coralSession = session.coralSession();
201  coral::ISchema& schema = schemaName.empty() ?
202  coralSession.nominalSchema() :
203  coralSession.schema( schemaName ) ;
204 
205  // coral::IView& view = schema.viewHandle( viewName ) ;
206 
207  // Pointer is deleted automatically at end of function.
208  coral::IQuery* query = schema.newQuery(); ;
209 
210  // Construct query
211  for (std::vector<std::string>::const_iterator constIt = columnNames.begin(); constIt
212  != columnNames.end(); ++constIt) {
213  query->addToOutputList(*constIt);
214  }
215 
216  query->addToTableList(viewName);
217 
218  // Only apply condition if RHS has one row.
219  if (!conditionLHS.empty() && conditionRHS.numberRows() == 1) {
220 
221  if (!conditionRHSName.empty()) {
222  // Assume all RHS types are strings.
223  coral::AttributeList attList;
224  attList.extend(conditionRHSName, typeid(std::string));
226  conditionRHS.fillVariable(conditionRHSName, tmp);
227  attList[conditionRHSName].data<std::string> () = tmp;
228 
229  query->setCondition(conditionLHS + " = :" + conditionRHSName, attList);
230  } else if (conditionRHS.columnNames().size() == 1)
231  // check for only one column
232  {
233  query->setCondition(
234  conditionLHS + " = :" + conditionRHS.columnNames().front(),
235  conditionRHS.attributeLists().front());
236  }
237  }
238 
239  coral::ICursor& cursor = query->execute();
240 
241  // Copy AttributeLists for external use because the cursor is deleted
242  // when the query goes out of scope.
243  std::vector<coral::AttributeList> atts;
244  while (cursor.next()) {
245  atts.push_back(cursor.currentRow());
246  };
247 
248  delete query;
249 
250 // // Run a wildcard query on the view
251 // coral::IQuery* query2 = workingSchema.newQuery();
252 // query2->addToTableList(V0);
253 // coral::ICursor& cursor2 = query2->execute();
254 // while ( cursor2.next() ) {
255 // cursor2.currentRow().toOutputStream( std::cout ) << std::endl;
256 // }
257 // delete query2;
258 
259 
260 
261  return QueryResults(columnNames, atts);
262  }
263 
266  const std::string& columnName,
267  const std::string& schemaName,
268  const std::string& viewName,
269  const std::string& conditionLHS,
270  const QueryResults conditionRHS,
271  const std::string& conditionRHSName )
272  {
273  std::vector< std::string > columnNames ;
274  columnNames.push_back( columnName ) ;
275  return basicQuery( columnNames, schemaName, viewName,
276  conditionLHS, conditionRHS, conditionRHSName ) ;
277  }
278 
279  std::vector< std::string >
281  const std::string& schemaName,
282  const std::string& viewName )
283  {
284  coral::ISessionProxy& coralSession = session.coralSession();
285  coral::ISchema& schema = schemaName.empty() ?
286  coralSession.nominalSchema() :
287  coralSession.schema( schemaName ) ;
288 
289  std::set< std::string > views = schema.listViews ();
290  std::vector< std::string > names ;
291 
292  if (schema.existsView (viewName)) {
293 
294  coral::IView& view = schema.viewHandle( viewName ) ;
295 
296  int nCols = view.numberOfColumns() ;
297 
298  for (int i = 0; i < nCols; ++i) {
299  const coral::IColumn& column = view.column(i);
300  names.push_back(column.name());
301  }
302 
303  return names ;
304 
305  }
306 
307  return names ;
308 
309  }
310 
311  //
312 // static member functions
313 //
314 }
bool fillVariable(const std::string &columnName, T &outputVariable) const
Definition: OMDSReader.h:311
static const HistoName names[]
void start(bool readOnly=true)
Definition: Session.cc:22
delete x;
Definition: CaloConfig.h:22
~OMDSReader() override
Definition: OMDSReader.cc:61
Transaction & transaction()
Definition: Session.cc:66
def query(query_str, verbose=False)
Definition: das.py:5
void connect(const std::string &connectString, const std::string &authenticationPath, bool isOMDS=false)
Definition: DataManager.cc:24
Definition: query.py:1
std::vector< std::string > columnNames(const std::string &schemaName, const std::string &tableName)
Definition: OMDSReader.cc:165
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:37
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
cond::persistency::Session session
Definition: DataManager.h:40