Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // Package:     L1Trigger
0004 // Class  :     OMDSReader
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:
0010 //         Created:  Sun Mar  2 01:46:46 CET 2008
0011 // $Id: OMDSReader.cc,v 1.12 2010/02/16 21:56:37 wsun Exp $
0012 //
0013 
0014 // system include files
0015 #include <set>
0016 #include <iostream>
0017 
0018 // user include files
0019 #include "CondTools/L1Trigger/interface/OMDSReader.h"
0020 #include "RelationalAccess/ITableDescription.h"
0021 #include "RelationalAccess/IColumn.h"
0022 
0023 //
0024 // constants, enums and typedefs
0025 //
0026 
0027 //
0028 // static data member definitions
0029 //
0030 
0031 namespace l1t {
0032 
0033   //
0034   // constructors and destructor
0035   //
0036   OMDSReader::OMDSReader() : DataManager() {}
0037 
0038   OMDSReader::OMDSReader(const std::string& connectString, const std::string& authenticationPath)
0039       : DataManager(connectString, authenticationPath, true) {
0040     session.transaction().start(true);
0041   }
0042 
0043   void OMDSReader::connect(const std::string& connectString, const std::string& authenticationPath) {
0044     DataManager::connect(connectString, authenticationPath, true);
0045     session.transaction().start(true);
0046   }
0047 
0048   // OMDSReader::OMDSReader(const OMDSReader& rhs)
0049   // {
0050   //    // do actual copying here;
0051   // }
0052 
0053   OMDSReader::~OMDSReader() {}
0054 
0055   //
0056   // assignment operators
0057   //
0058   // const OMDSReader& OMDSReader::operator=(const OMDSReader& rhs)
0059   // {
0060   //   //An exception safe implementation is
0061   //   OMDSReader temp(rhs);
0062   //   swap(rhs);
0063   //
0064   //   return *this;
0065   // }
0066 
0067   //
0068   // member functions
0069   //
0070 
0071   //
0072   // const member functions
0073   //
0074 
0075   const OMDSReader::QueryResults OMDSReader::basicQuery(const std::vector<std::string>& columnNames,
0076                                                         const std::string& schemaName,
0077                                                         const std::string& tableName,
0078                                                         const std::string& conditionLHS,
0079                                                         const QueryResults conditionRHS,
0080                                                         const std::string& conditionRHSName) {
0081     coral::ISessionProxy& coralSession = session.coralSession();
0082     coral::ISchema& schema = schemaName.empty() ? coralSession.nominalSchema() : coralSession.schema(schemaName);
0083 
0084     coral::ITable& table = schema.tableHandle(tableName);
0085 
0086     // Pointer is deleted automatically at end of function.
0087     std::shared_ptr<coral::IQuery> query(table.newQuery());
0088 
0089     // Construct query
0090     std::vector<std::string>::const_iterator it = columnNames.begin();
0091     std::vector<std::string>::const_iterator end = columnNames.end();
0092     for (; it != end; ++it) {
0093       query->addToOutputList(*it);
0094     }
0095 
0096     // Only apply condition if RHS has one row.
0097     if (!conditionLHS.empty() && conditionRHS.numberRows() == 1) {
0098       if (!conditionRHSName.empty()) {
0099         // Assume all RHS types are strings.
0100         coral::AttributeList attList;
0101         attList.extend(conditionRHSName, typeid(std::string));
0102         std::string tmp;
0103         conditionRHS.fillVariable(conditionRHSName, tmp);
0104         attList[conditionRHSName].data<std::string>() = tmp;
0105 
0106         query->setCondition(conditionLHS + " = :" + conditionRHSName, attList);
0107       } else if (conditionRHS.columnNames().size() == 1)
0108       // check for only one column
0109       {
0110         query->setCondition(conditionLHS + " = :" + conditionRHS.columnNames().front(),
0111                             conditionRHS.attributeLists().front());
0112       }
0113     }
0114 
0115     coral::ICursor& cursor = query->execute();
0116 
0117     // Copy AttributeLists for external use because the cursor is deleted
0118     // when the query goes out of scope.
0119     std::vector<coral::AttributeList> atts;
0120     while (cursor.next()) {
0121       atts.push_back(cursor.currentRow());
0122     };
0123 
0124     return QueryResults(columnNames, atts);
0125   }
0126 
0127   const OMDSReader::QueryResults OMDSReader::basicQuery(const std::string& columnName,
0128                                                         const std::string& schemaName,
0129                                                         const std::string& tableName,
0130                                                         const std::string& conditionLHS,
0131                                                         const QueryResults conditionRHS,
0132                                                         const std::string& conditionRHSName) {
0133     std::vector<std::string> columnNames;
0134     columnNames.push_back(columnName);
0135     return basicQuery(columnNames, schemaName, tableName, conditionLHS, conditionRHS, conditionRHSName);
0136   }
0137 
0138   std::vector<std::string> OMDSReader::columnNames(const std::string& schemaName, const std::string& tableName) {
0139     coral::ISessionProxy& coralSession = session.coralSession();
0140     coral::ISchema& schema = schemaName.empty() ? coralSession.nominalSchema() : coralSession.schema(schemaName);
0141 
0142     coral::ITable& table = schema.tableHandle(tableName);
0143     const coral::ITableDescription& tableDesc = table.description();
0144 
0145     std::vector<std::string> names;
0146     int nCols = tableDesc.numberOfColumns();
0147 
0148     for (int i = 0; i < nCols; ++i) {
0149       const coral::IColumn& column = tableDesc.columnDescription(i);
0150       names.push_back(column.name());
0151     }
0152 
0153     return names;
0154   }
0155 
0156   // VIEW
0157 
0158   const OMDSReader::QueryResults OMDSReader::basicQueryView(const std::vector<std::string>& columnNames,
0159                                                             const std::string& schemaName,
0160                                                             const std::string& viewName,
0161                                                             const std::string& conditionLHS,
0162                                                             const QueryResults conditionRHS,
0163                                                             const std::string& conditionRHSName) {
0164     coral::ISessionProxy& coralSession = session.coralSession();
0165     coral::ISchema& schema = schemaName.empty() ? coralSession.nominalSchema() : coralSession.schema(schemaName);
0166 
0167     //    coral::IView& view = schema.viewHandle( viewName ) ;
0168 
0169     // Pointer is deleted automatically at end of function.
0170     coral::IQuery* query = schema.newQuery();
0171     ;
0172 
0173     // Construct query
0174     for (std::vector<std::string>::const_iterator constIt = columnNames.begin(); constIt != columnNames.end();
0175          ++constIt) {
0176       query->addToOutputList(*constIt);
0177     }
0178 
0179     query->addToTableList(viewName);
0180 
0181     // Only apply condition if RHS has one row.
0182     if (!conditionLHS.empty() && conditionRHS.numberRows() == 1) {
0183       if (!conditionRHSName.empty()) {
0184         // Assume all RHS types are strings.
0185         coral::AttributeList attList;
0186         attList.extend(conditionRHSName, typeid(std::string));
0187         std::string tmp;
0188         conditionRHS.fillVariable(conditionRHSName, tmp);
0189         attList[conditionRHSName].data<std::string>() = tmp;
0190 
0191         query->setCondition(conditionLHS + " = :" + conditionRHSName, attList);
0192       } else if (conditionRHS.columnNames().size() == 1)
0193       // check for only one column
0194       {
0195         query->setCondition(conditionLHS + " = :" + conditionRHS.columnNames().front(),
0196                             conditionRHS.attributeLists().front());
0197       }
0198     }
0199 
0200     coral::ICursor& cursor = query->execute();
0201 
0202     // Copy AttributeLists for external use because the cursor is deleted
0203     // when the query goes out of scope.
0204     std::vector<coral::AttributeList> atts;
0205     while (cursor.next()) {
0206       atts.push_back(cursor.currentRow());
0207     };
0208 
0209     delete query;
0210 
0211     //    // Run a wildcard query on the view
0212     //    coral::IQuery* query2 = workingSchema.newQuery();
0213     //    query2->addToTableList(V0);
0214     //    coral::ICursor& cursor2 = query2->execute();
0215     //    while ( cursor2.next() ) {
0216     //      cursor2.currentRow().toOutputStream( std::cout ) << std::endl;
0217     //    }
0218     //    delete query2;
0219 
0220     return QueryResults(columnNames, atts);
0221   }
0222 
0223   const OMDSReader::QueryResults OMDSReader::basicQueryView(const std::string& columnName,
0224                                                             const std::string& schemaName,
0225                                                             const std::string& viewName,
0226                                                             const std::string& conditionLHS,
0227                                                             const QueryResults conditionRHS,
0228                                                             const std::string& conditionRHSName) {
0229     std::vector<std::string> columnNames;
0230     columnNames.push_back(columnName);
0231     return basicQuery(columnNames, schemaName, viewName, conditionLHS, conditionRHS, conditionRHSName);
0232   }
0233 
0234   std::vector<std::string> OMDSReader::columnNamesView(const std::string& schemaName, const std::string& viewName) {
0235     coral::ISessionProxy& coralSession = session.coralSession();
0236     coral::ISchema& schema = schemaName.empty() ? coralSession.nominalSchema() : coralSession.schema(schemaName);
0237 
0238     std::set<std::string> views = schema.listViews();
0239     std::vector<std::string> names;
0240 
0241     if (schema.existsView(viewName)) {
0242       coral::IView& view = schema.viewHandle(viewName);
0243 
0244       int nCols = view.numberOfColumns();
0245 
0246       for (int i = 0; i < nCols; ++i) {
0247         const coral::IColumn& column = view.column(i);
0248         names.push_back(column.name());
0249       }
0250 
0251       return names;
0252     }
0253 
0254     return names;
0255   }
0256 
0257   //
0258   // static member functions
0259   //
0260 }  // namespace l1t