Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:03:12

0001 #ifndef CondTools_L1Trigger_OMDSReader_h
0002 #define CondTools_L1Trigger_OMDSReader_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     L1Trigger
0006 // Class  :     OMDSReader
0007 //
0008 /**\class OMDSReader OMDSReader.h CondTools/L1Trigger/interface/OMDSReader.h
0009 
0010  Description: <one line class summary>
0011 
0012  Usage:
0013     <usage>
0014 
0015 */
0016 //
0017 // Original Author:  Werner Sun
0018 //         Created:  Sun Mar  2 01:36:06 CET 2008
0019 // $Id: OMDSReader.h,v 1.14 2010/04/12 20:29:23 wsun Exp $
0020 //
0021 
0022 // system include files
0023 #include <memory>
0024 
0025 // user include files
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 // forward declarations
0039 
0040 namespace l1t {
0041 
0042   class OMDSReader : public DataManager {
0043   public:
0044     // std::vector< std::string > is the list of attribute names.
0045     // We need to store the column names because there is no way to ask
0046     // AttributeList for this information.  We have a vector of AttributeLists
0047     // because the query may return more than one row, each of which is
0048     // encapsulated in an AttributeList.
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       // Return value is false if variable is null.
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       // If there is only one column, no need to give column name
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;  // stop default
0086 
0087     const OMDSReader& operator=(const OMDSReader&) = delete;  // stop default
0088 
0089     ~OMDSReader() override;
0090 
0091     // ---------- const member functions ---------------------
0092 
0093     // These functions encapsulate basic SQL queries of the form
0094     //
0095     // SELECT <columns> FROM <schema.table> WHERE <conditionLHS> = <conditionRHS>
0096     //
0097     // where
0098     //
0099     // <columns> can be one or many column names
0100     // <conditionRHS> can be a string or the result of another query
0101 
0102     // Assume data type of condition RHS is std::string
0103     const QueryResults basicQuery(const std::vector<std::string>& columnNames,
0104                                   const std::string& schemaName,  // for nominal schema, use ""
0105                                   const std::string& tableName,
0106                                   const std::string& conditionLHS = "",
0107                                   const QueryResults conditionRHS = QueryResults(),
0108                                   // must have only one row
0109                                   const std::string& conditionRHSName = ""
0110                                   // if empty, conditionRHS must have only one column
0111     );
0112 
0113     // Assume data type of condition RHS is std::string
0114     const QueryResults basicQuery(const std::string& columnName,
0115                                   const std::string& schemaName,  // for nominal schema, use ""
0116                                   const std::string& tableName,
0117                                   const std::string& conditionLHS = "",
0118                                   const QueryResults conditionRHS = QueryResults(),
0119                                   // must have only one row
0120                                   const std::string& conditionRHSName = ""
0121                                   // if empty, conditionRHS must have only one column
0122     );
0123 
0124     // Assume data type of condition RHS is std::string
0125     const QueryResults basicQueryView(const std::vector<std::string>& columnNames,
0126                                       const std::string& schemaName,  // for nominal schema, use ""
0127                                       const std::string& viewName,
0128                                       const std::string& conditionLHS = "",
0129                                       const QueryResults conditionRHS = QueryResults(),
0130                                       // must have only one row
0131                                       const std::string& conditionRHSName = ""
0132                                       // if empty, conditionRHS must have only one column
0133     );
0134 
0135     // Assume data type of condition RHS is std::string
0136     const QueryResults basicQueryView(const std::string& columnName,
0137                                       const std::string& schemaName,  // for nominal schema, use ""
0138                                       const std::string& viewName,
0139                                       const std::string& conditionLHS = "",
0140                                       const QueryResults conditionRHS = QueryResults(),
0141                                       // must have only one row
0142                                       const std::string& conditionRHSName = ""
0143                                       // if empty, conditionRHS must have only one column
0144     );
0145 
0146     // For any data type of condition RHS.
0147     // Example usage, for an int key:
0148     //   results = omdsReader.basicQueryGenericKey<int>(...) ;
0149     template <class T>
0150     const QueryResults basicQueryGenericKey(const std::vector<std::string>& columnNames,
0151                                             const std::string& schemaName,  // for nominal schema, use ""
0152                                             const std::string& tableName,
0153                                             const std::string& conditionLHS = "",
0154                                             const QueryResults conditionRHS = QueryResults(),
0155                                             // must have only one row
0156                                             const std::string& conditionRHSName = ""
0157                                             // if empty, conditionRHS must have only one column
0158     );
0159 
0160     // For any data type of condition RHS.
0161     // Example usage, for an int key:
0162     //   results = omdsReader.basicQueryGenericKey<int>(...) ;
0163     template <class T>
0164     const QueryResults basicQueryGenericKey(const std::string& columnName,
0165                                             const std::string& schemaName,  // for nominal schema, use ""
0166                                             const std::string& tableName,
0167                                             const std::string& conditionLHS = "",
0168                                             const QueryResults conditionRHS = QueryResults(),
0169                                             // must have only one row
0170                                             const std::string& conditionRHSName = ""
0171                                             // if empty, conditionRHS must have only one column
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,  // for nominal schema, use ""
0178                                          const std::string& tableName);
0179 
0180     std::vector<std::string> columnNamesView(const std::string& schemaName,  // for nominal schema, use ""
0181                                              const std::string& viewName);
0182 
0183     // ---------- static member functions --------------------
0184 
0185     // ---------- member functions ---------------------------
0186 
0187     void connect(const std::string& connectString, const std::string& authenticationPath);
0188 
0189   private:
0190     // ---------- member data --------------------------------
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     // Pointer is deleted automatically at end of function.
0206     std::shared_ptr<coral::IQuery> query(table.newQuery());
0207 
0208     // Construct query
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     // Only apply condition if RHS has one row.
0216     if (!conditionLHS.empty() && conditionRHS.numberRows() == 1) {
0217       if (!conditionRHSName.empty()) {
0218         // Use type of dummyVariable to determine type of condition RHS
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       // check for only one column
0228       {
0229         query->setCondition(conditionLHS + " = :" + conditionRHS.columnNames().front(),
0230                             conditionRHS.attributeLists().front());
0231       }
0232     }
0233 
0234     coral::ICursor& cursor = query->execute();
0235 
0236     // Copy AttributeLists for external use because the cursor is deleted
0237     // when the query goes out of scope.
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     // Check index in bounds
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     // Check index in bounds and only one column
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 }  // namespace l1t
0310 #endif