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  // constructors and destructor
35  //
37 
39  : DataManager(connectString, authenticationPath, true) {
40  session.transaction().start(true);
41  }
42 
43  void OMDSReader::connect(const std::string& connectString, const std::string& authenticationPath) {
44  DataManager::connect(connectString, authenticationPath, true);
45  session.transaction().start(true);
46  }
47 
48  // OMDSReader::OMDSReader(const OMDSReader& rhs)
49  // {
50  // // do actual copying here;
51  // }
52 
54 
55  //
56  // assignment operators
57  //
58  // const OMDSReader& OMDSReader::operator=(const OMDSReader& rhs)
59  // {
60  // //An exception safe implementation is
61  // OMDSReader temp(rhs);
62  // swap(rhs);
63  //
64  // return *this;
65  // }
66 
67  //
68  // member functions
69  //
70 
71  //
72  // const member functions
73  //
74 
75  const OMDSReader::QueryResults OMDSReader::basicQuery(const std::vector<std::string>& columnNames,
76  const std::string& schemaName,
77  const std::string& tableName,
78  const std::string& conditionLHS,
79  const QueryResults conditionRHS,
80  const std::string& conditionRHSName) {
81  coral::ISessionProxy& coralSession = session.coralSession();
82  coral::ISchema& schema = schemaName.empty() ? coralSession.nominalSchema() : coralSession.schema(schemaName);
83 
84  coral::ITable& table = schema.tableHandle(tableName);
85 
86  // Pointer is deleted automatically at end of function.
87  std::shared_ptr<coral::IQuery> query(table.newQuery());
88 
89  // Construct query
90  std::vector<std::string>::const_iterator it = columnNames.begin();
91  std::vector<std::string>::const_iterator end = columnNames.end();
92  for (; it != end; ++it) {
93  query->addToOutputList(*it);
94  }
95 
96  // Only apply condition if RHS has one row.
97  if (!conditionLHS.empty() && conditionRHS.numberRows() == 1) {
98  if (!conditionRHSName.empty()) {
99  // Assume all RHS types are strings.
100  coral::AttributeList attList;
101  attList.extend(conditionRHSName, typeid(std::string));
103  conditionRHS.fillVariable(conditionRHSName, tmp);
104  attList[conditionRHSName].data<std::string>() = tmp;
105 
106  query->setCondition(conditionLHS + " = :" + conditionRHSName, attList);
107  } else if (conditionRHS.columnNames().size() == 1)
108  // check for only one column
109  {
110  query->setCondition(conditionLHS + " = :" + conditionRHS.columnNames().front(),
111  conditionRHS.attributeLists().front());
112  }
113  }
114 
115  coral::ICursor& cursor = query->execute();
116 
117  // Copy AttributeLists for external use because the cursor is deleted
118  // when the query goes out of scope.
119  std::vector<coral::AttributeList> atts;
120  while (cursor.next()) {
121  atts.push_back(cursor.currentRow());
122  };
123 
124  return QueryResults(columnNames, atts);
125  }
126 
128  const std::string& schemaName,
129  const std::string& tableName,
130  const std::string& conditionLHS,
131  const QueryResults conditionRHS,
132  const std::string& conditionRHSName) {
133  std::vector<std::string> columnNames;
134  columnNames.push_back(columnName);
135  return basicQuery(columnNames, schemaName, tableName, conditionLHS, conditionRHS, conditionRHSName);
136  }
137 
138  std::vector<std::string> OMDSReader::columnNames(const std::string& schemaName, const std::string& tableName) {
139  coral::ISessionProxy& coralSession = session.coralSession();
140  coral::ISchema& schema = schemaName.empty() ? coralSession.nominalSchema() : coralSession.schema(schemaName);
141 
142  coral::ITable& table = schema.tableHandle(tableName);
143  const coral::ITableDescription& tableDesc = table.description();
144 
145  std::vector<std::string> names;
146  int nCols = tableDesc.numberOfColumns();
147 
148  for (int i = 0; i < nCols; ++i) {
149  const coral::IColumn& column = tableDesc.columnDescription(i);
150  names.push_back(column.name());
151  }
152 
153  return names;
154  }
155 
156  // VIEW
157 
158  const OMDSReader::QueryResults OMDSReader::basicQueryView(const std::vector<std::string>& columnNames,
159  const std::string& schemaName,
160  const std::string& viewName,
161  const std::string& conditionLHS,
162  const QueryResults conditionRHS,
163  const std::string& conditionRHSName) {
164  coral::ISessionProxy& coralSession = session.coralSession();
165  coral::ISchema& schema = schemaName.empty() ? coralSession.nominalSchema() : coralSession.schema(schemaName);
166 
167  // coral::IView& view = schema.viewHandle( viewName ) ;
168 
169  // Pointer is deleted automatically at end of function.
170  coral::IQuery* query = schema.newQuery();
171  ;
172 
173  // Construct query
174  for (std::vector<std::string>::const_iterator constIt = columnNames.begin(); constIt != columnNames.end();
175  ++constIt) {
176  query->addToOutputList(*constIt);
177  }
178 
179  query->addToTableList(viewName);
180 
181  // Only apply condition if RHS has one row.
182  if (!conditionLHS.empty() && conditionRHS.numberRows() == 1) {
183  if (!conditionRHSName.empty()) {
184  // Assume all RHS types are strings.
185  coral::AttributeList attList;
186  attList.extend(conditionRHSName, typeid(std::string));
188  conditionRHS.fillVariable(conditionRHSName, tmp);
189  attList[conditionRHSName].data<std::string>() = tmp;
190 
191  query->setCondition(conditionLHS + " = :" + conditionRHSName, attList);
192  } else if (conditionRHS.columnNames().size() == 1)
193  // check for only one column
194  {
195  query->setCondition(conditionLHS + " = :" + conditionRHS.columnNames().front(),
196  conditionRHS.attributeLists().front());
197  }
198  }
199 
200  coral::ICursor& cursor = query->execute();
201 
202  // Copy AttributeLists for external use because the cursor is deleted
203  // when the query goes out of scope.
204  std::vector<coral::AttributeList> atts;
205  while (cursor.next()) {
206  atts.push_back(cursor.currentRow());
207  };
208 
209  delete query;
210 
211  // // Run a wildcard query on the view
212  // coral::IQuery* query2 = workingSchema.newQuery();
213  // query2->addToTableList(V0);
214  // coral::ICursor& cursor2 = query2->execute();
215  // while ( cursor2.next() ) {
216  // cursor2.currentRow().toOutputStream( std::cout ) << std::endl;
217  // }
218  // delete query2;
219 
220  return QueryResults(columnNames, atts);
221  }
222 
224  const std::string& schemaName,
225  const std::string& viewName,
226  const std::string& conditionLHS,
227  const QueryResults conditionRHS,
228  const std::string& conditionRHSName) {
229  std::vector<std::string> columnNames;
230  columnNames.push_back(columnName);
231  return basicQuery(columnNames, schemaName, viewName, conditionLHS, conditionRHS, conditionRHSName);
232  }
233 
234  std::vector<std::string> OMDSReader::columnNamesView(const std::string& schemaName, const std::string& viewName) {
235  coral::ISessionProxy& coralSession = session.coralSession();
236  coral::ISchema& schema = schemaName.empty() ? coralSession.nominalSchema() : coralSession.schema(schemaName);
237 
238  std::set<std::string> views = schema.listViews();
239  std::vector<std::string> names;
240 
241  if (schema.existsView(viewName)) {
242  coral::IView& view = schema.viewHandle(viewName);
243 
244  int nCols = view.numberOfColumns();
245 
246  for (int i = 0; i < nCols; ++i) {
247  const coral::IColumn& column = view.column(i);
248  names.push_back(column.name());
249  }
250 
251  return names;
252  }
253 
254  return names;
255  }
256 
257  //
258  // static member functions
259  //
260 } // namespace l1t
l1t::OMDSReader::QueryResults
Definition: OMDSReader.h:49
l1t::OMDSReader::OMDSReader
OMDSReader()
Definition: OMDSReader.cc:36
mps_fire.i
i
Definition: mps_fire.py:428
OMDSReader.h
l1t::DataManager::session
cond::persistency::Session session
Definition: DataManager.h:33
createJobs.tmp
tmp
align.sh
Definition: createJobs.py:716
HLT_Fake1_cff.tableName
tableName
Definition: HLT_Fake1_cff.py:10
query
Definition: query.py:1
names
const std::string names[nVars_]
Definition: PhotonIDValueMapProducer.cc:124
l1t::OMDSReader::QueryResults::fillVariable
bool fillVariable(const std::string &columnName, T &outputVariable) const
Definition: OMDSReader.h:274
l1t::OMDSReader::QueryResults::columnNames
const std::vector< std::string > & columnNames() const
Definition: OMDSReader.h:57
l1t::OMDSReader::columnNames
std::vector< std::string > columnNames(const std::string &schemaName, const std::string &tableName)
Definition: OMDSReader.cc:138
mps_fire.end
end
Definition: mps_fire.py:242
l1t::OMDSReader::~OMDSReader
~OMDSReader() override
Definition: OMDSReader.cc:53
l1t::OMDSReader::connect
void connect(const std::string &connectString, const std::string &authenticationPath)
Definition: OMDSReader.cc:43
funct::true
true
Definition: Factorize.h:173
l1t
delete x;
Definition: CaloConfig.h:22
cond::persistency::Session::coralSession
coral::ISessionProxy & coralSession()
Definition: Session.cc:218
l1t::OMDSReader::QueryResults::numberRows
int numberRows() const
Definition: OMDSReader.h:60
l1t::OMDSReader::basicQuery
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:75
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
writeEcalDQMStatus.authenticationPath
authenticationPath
Definition: writeEcalDQMStatus.py:19
l1t::DataManager::connect
void connect(const std::string &connectString, const std::string &authenticationPath, bool isOMDS=false)
Definition: DataManager.cc:14
l1t::OMDSReader::columnNamesView
std::vector< std::string > columnNamesView(const std::string &schemaName, const std::string &viewName)
Definition: OMDSReader.cc:234
cond::persistency::Session::transaction
Transaction & transaction()
Definition: Session.cc:52
cond::persistency::Transaction::start
void start(bool readOnly=true)
Definition: Session.cc:18
l1t::DataManager
Definition: DataManager.h:17
contentValuesFiles.query
query
Definition: contentValuesFiles.py:38
l1t::OMDSReader::basicQueryView
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:158
TableParser.table
table
Definition: TableParser.py:111
l1t::OMDSReader::QueryResults::attributeLists
const std::vector< coral::AttributeList > & attributeLists() const
Definition: OMDSReader.h:58