File indexing completed on 2024-04-06 12:03:12
0001 #ifndef CondTools_L1Trigger_OMDSReader_h
0002 #define CondTools_L1Trigger_OMDSReader_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 #include <memory>
0024
0025
0026 #include "CondTools/L1Trigger/interface/DataManager.h"
0027 #include "RelationalAccess/IQuery.h"
0028 #include "CoralBase/AttributeList.h"
0029 #include "CoralBase/AttributeSpecification.h"
0030 #include "CoralBase/Attribute.h"
0031
0032 #include "RelationalAccess/ITable.h"
0033 #include "RelationalAccess/IView.h"
0034 #include "RelationalAccess/ISchema.h"
0035 #include "RelationalAccess/ISessionProxy.h"
0036 #include "RelationalAccess/ICursor.h"
0037
0038
0039
0040 namespace l1t {
0041
0042 class OMDSReader : public DataManager {
0043 public:
0044
0045
0046
0047
0048
0049 class QueryResults {
0050 public:
0051 QueryResults() {}
0052 QueryResults(const std::vector<std::string>& columnNames, const std::vector<coral::AttributeList>& attLists)
0053 : m_columnNames(columnNames), m_attributeLists(attLists) {}
0054
0055 virtual ~QueryResults() {}
0056
0057 const std::vector<std::string>& columnNames() const { return m_columnNames; }
0058 const std::vector<coral::AttributeList>& attributeLists() const { return m_attributeLists; }
0059 bool queryFailed() const { return m_attributeLists.empty(); }
0060 int numberRows() const { return m_attributeLists.size(); }
0061
0062
0063 template <class T>
0064 bool fillVariable(const std::string& columnName, T& outputVariable) const;
0065
0066 template <class T>
0067 bool fillVariableFromRow(const std::string& columnName, int rowNumber, T& outputVariable) const;
0068
0069
0070 template <class T>
0071 bool fillVariable(T& outputVariable) const;
0072
0073 template <class T>
0074 bool fillVariableFromRow(int rowNumber, T& outputVariable) const;
0075
0076 private:
0077 std::vector<std::string> m_columnNames;
0078 std::vector<coral::AttributeList> m_attributeLists;
0079 };
0080
0081 OMDSReader();
0082
0083 OMDSReader(const std::string& connectString, const std::string& authenticationPath);
0084
0085 OMDSReader(const OMDSReader&) = delete;
0086
0087 const OMDSReader& operator=(const OMDSReader&) = delete;
0088
0089 ~OMDSReader() override;
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103 const QueryResults basicQuery(const std::vector<std::string>& columnNames,
0104 const std::string& schemaName,
0105 const std::string& tableName,
0106 const std::string& conditionLHS = "",
0107 const QueryResults conditionRHS = QueryResults(),
0108
0109 const std::string& conditionRHSName = ""
0110
0111 );
0112
0113
0114 const QueryResults basicQuery(const std::string& columnName,
0115 const std::string& schemaName,
0116 const std::string& tableName,
0117 const std::string& conditionLHS = "",
0118 const QueryResults conditionRHS = QueryResults(),
0119
0120 const std::string& conditionRHSName = ""
0121
0122 );
0123
0124
0125 const QueryResults basicQueryView(const std::vector<std::string>& columnNames,
0126 const std::string& schemaName,
0127 const std::string& viewName,
0128 const std::string& conditionLHS = "",
0129 const QueryResults conditionRHS = QueryResults(),
0130
0131 const std::string& conditionRHSName = ""
0132
0133 );
0134
0135
0136 const QueryResults basicQueryView(const std::string& columnName,
0137 const std::string& schemaName,
0138 const std::string& viewName,
0139 const std::string& conditionLHS = "",
0140 const QueryResults conditionRHS = QueryResults(),
0141
0142 const std::string& conditionRHSName = ""
0143
0144 );
0145
0146
0147
0148
0149 template <class T>
0150 const QueryResults basicQueryGenericKey(const std::vector<std::string>& columnNames,
0151 const std::string& schemaName,
0152 const std::string& tableName,
0153 const std::string& conditionLHS = "",
0154 const QueryResults conditionRHS = QueryResults(),
0155
0156 const std::string& conditionRHSName = ""
0157
0158 );
0159
0160
0161
0162
0163 template <class T>
0164 const QueryResults basicQueryGenericKey(const std::string& columnName,
0165 const std::string& schemaName,
0166 const std::string& tableName,
0167 const std::string& conditionLHS = "",
0168 const QueryResults conditionRHS = QueryResults(),
0169
0170 const std::string& conditionRHSName = ""
0171
0172 );
0173
0174 template <class T>
0175 const QueryResults singleAttribute(const T& data) const;
0176
0177 std::vector<std::string> columnNames(const std::string& schemaName,
0178 const std::string& tableName);
0179
0180 std::vector<std::string> columnNamesView(const std::string& schemaName,
0181 const std::string& viewName);
0182
0183
0184
0185
0186
0187 void connect(const std::string& connectString, const std::string& authenticationPath);
0188
0189 private:
0190
0191 };
0192
0193 template <class T>
0194 const OMDSReader::QueryResults OMDSReader::basicQueryGenericKey(const std::vector<std::string>& columnNames,
0195 const std::string& schemaName,
0196 const std::string& tableName,
0197 const std::string& conditionLHS,
0198 const QueryResults conditionRHS,
0199 const std::string& conditionRHSName) {
0200 coral::ISessionProxy& coralSession = session.coralSession();
0201 coral::ISchema& schema = schemaName.empty() ? coralSession.nominalSchema() : coralSession.schema(schemaName);
0202
0203 coral::ITable& table = schema.tableHandle(tableName);
0204
0205
0206 std::shared_ptr<coral::IQuery> query(table.newQuery());
0207
0208
0209 std::vector<std::string>::const_iterator it = columnNames.begin();
0210 std::vector<std::string>::const_iterator end = columnNames.end();
0211 for (; it != end; ++it) {
0212 query->addToOutputList(*it);
0213 }
0214
0215
0216 if (!conditionLHS.empty() && conditionRHS.numberRows() == 1) {
0217 if (!conditionRHSName.empty()) {
0218
0219 coral::AttributeList attList;
0220 attList.extend(conditionRHSName, typeid(T));
0221 T tmp = T();
0222 conditionRHS.fillVariable(conditionRHSName, tmp);
0223 attList[conditionRHSName].data<T>() = tmp;
0224
0225 query->setCondition(conditionLHS + " = :" + conditionRHSName, attList);
0226 } else if (conditionRHS.columnNames().size() == 1)
0227
0228 {
0229 query->setCondition(conditionLHS + " = :" + conditionRHS.columnNames().front(),
0230 conditionRHS.attributeLists().front());
0231 }
0232 }
0233
0234 coral::ICursor& cursor = query->execute();
0235
0236
0237
0238 std::vector<coral::AttributeList> atts;
0239 while (cursor.next()) {
0240 atts.push_back(cursor.currentRow());
0241 };
0242
0243 return QueryResults(columnNames, atts);
0244 }
0245
0246 template <class T>
0247 const OMDSReader::QueryResults OMDSReader::basicQueryGenericKey(const std::string& columnName,
0248 const std::string& schemaName,
0249 const std::string& tableName,
0250 const std::string& conditionLHS,
0251 const QueryResults conditionRHS,
0252 const std::string& conditionRHSName) {
0253 std::vector<std::string> columnNames;
0254 columnNames.push_back(columnName);
0255 return basicQueryGenericKey<T>(columnNames, schemaName, tableName, conditionLHS, conditionRHS, conditionRHSName);
0256 }
0257
0258 template <class T>
0259 const OMDSReader::QueryResults OMDSReader::singleAttribute(const T& data) const {
0260 std::vector<std::string> names;
0261 names.push_back("dummy");
0262
0263 coral::AttributeList attList;
0264 attList.extend("dummy", typeid(T));
0265 attList["dummy"].data<T>() = data;
0266
0267 std::vector<coral::AttributeList> atts;
0268 atts.push_back(attList);
0269
0270 return QueryResults(names, atts);
0271 }
0272
0273 template <class T>
0274 bool OMDSReader::QueryResults::fillVariable(const std::string& columnName, T& outputVariable) const {
0275 return fillVariableFromRow(columnName, 0, outputVariable);
0276 }
0277
0278 template <class T>
0279 bool OMDSReader::QueryResults::fillVariableFromRow(const std::string& columnName,
0280 int rowNumber,
0281 T& outputVariable) const {
0282
0283 if (rowNumber < 0 || rowNumber >= numberRows())
0284 return false;
0285 const coral::AttributeList& row = m_attributeLists[rowNumber];
0286 if (row[columnName].isNull())
0287 return false;
0288 outputVariable = row[columnName].template data<T>();
0289 return true;
0290 }
0291
0292 template <class T>
0293 bool OMDSReader::QueryResults::fillVariable(T& outputVariable) const {
0294 return fillVariableFromRow(0, outputVariable);
0295 }
0296
0297 template <class T>
0298 bool OMDSReader::QueryResults::fillVariableFromRow(int rowNumber, T& outputVariable) const {
0299
0300 if (rowNumber < 0 || rowNumber >= numberRows() || m_columnNames.size() != 1)
0301 return false;
0302 const coral::AttributeList& row = m_attributeLists[rowNumber];
0303 if (row[m_columnNames.front()].isNull())
0304 return false;
0305 outputVariable = row[m_columnNames.front()].template data<T>();
0306 return true;
0307 }
0308
0309 }
0310 #endif