OMDSReader

QueryResults

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
#ifndef CondTools_L1Trigger_OMDSReader_h
#define CondTools_L1Trigger_OMDSReader_h
// -*- C++ -*-
//
// Package:     L1Trigger
// Class  :     OMDSReader
//
/**\class OMDSReader OMDSReader.h CondTools/L1Trigger/interface/OMDSReader.h

 Description: <one line class summary>

 Usage:
    <usage>

*/
//
// Original Author:  Werner Sun
//         Created:  Sun Mar  2 01:36:06 CET 2008
// $Id: OMDSReader.h,v 1.14 2010/04/12 20:29:23 wsun Exp $
//

// system include files
#include <memory>

// user include files
#include "CondTools/L1Trigger/interface/DataManager.h"
#include "RelationalAccess/IQuery.h"
#include "CoralBase/AttributeList.h"
#include "CoralBase/AttributeSpecification.h"
#include "CoralBase/Attribute.h"

#include "RelationalAccess/ITable.h"
#include "RelationalAccess/IView.h"
#include "RelationalAccess/ISchema.h"
#include "RelationalAccess/ISessionProxy.h"
#include "RelationalAccess/ICursor.h"

// forward declarations

namespace l1t {

  class OMDSReader : public DataManager {
  public:
    // std::vector< std::string > is the list of attribute names.
    // We need to store the column names because there is no way to ask
    // AttributeList for this information.  We have a vector of AttributeLists
    // because the query may return more than one row, each of which is
    // encapsulated in an AttributeList.
    class QueryResults {
    public:
      QueryResults() {}
      QueryResults(const std::vector<std::string>& columnNames, const std::vector<coral::AttributeList>& attLists)
          : m_columnNames(columnNames), m_attributeLists(attLists) {}

      virtual ~QueryResults() {}

      const std::vector<std::string>& columnNames() const { return m_columnNames; }
      const std::vector<coral::AttributeList>& attributeLists() const { return m_attributeLists; }
      bool queryFailed() const { return m_attributeLists.empty(); }
      int numberRows() const { return m_attributeLists.size(); }

      // Return value is false if variable is null.
      template <class T>
      bool fillVariable(const std::string& columnName, T& outputVariable) const;

      template <class T>
      bool fillVariableFromRow(const std::string& columnName, int rowNumber, T& outputVariable) const;

      // If there is only one column, no need to give column name
      template <class T>
      bool fillVariable(T& outputVariable) const;

      template <class T>
      bool fillVariableFromRow(int rowNumber, T& outputVariable) const;

    private:
      std::vector<std::string> m_columnNames;
      std::vector<coral::AttributeList> m_attributeLists;
    };

    OMDSReader();

    OMDSReader(const std::string& connectString, const std::string& authenticationPath);

    OMDSReader(const OMDSReader&) = delete;  // stop default

    const OMDSReader& operator=(const OMDSReader&) = delete;  // stop default

    ~OMDSReader() override;

    // ---------- const member functions ---------------------

    // These functions encapsulate basic SQL queries of the form
    //
    // SELECT <columns> FROM <schema.table> WHERE <conditionLHS> = <conditionRHS>
    //
    // where
    //
    // <columns> can be one or many column names
    // <conditionRHS> can be a string or the result of another query

    // Assume data type of condition RHS is std::string
    const QueryResults basicQuery(const std::vector<std::string>& columnNames,
                                  const std::string& schemaName,  // for nominal schema, use ""
                                  const std::string& tableName,
                                  const std::string& conditionLHS = "",
                                  const QueryResults conditionRHS = QueryResults(),
                                  // must have only one row
                                  const std::string& conditionRHSName = ""
                                  // if empty, conditionRHS must have only one column
    );

    // Assume data type of condition RHS is std::string
    const QueryResults basicQuery(const std::string& columnName,
                                  const std::string& schemaName,  // for nominal schema, use ""
                                  const std::string& tableName,
                                  const std::string& conditionLHS = "",
                                  const QueryResults conditionRHS = QueryResults(),
                                  // must have only one row
                                  const std::string& conditionRHSName = ""
                                  // if empty, conditionRHS must have only one column
    );

    // Assume data type of condition RHS is std::string
    const QueryResults basicQueryView(const std::vector<std::string>& columnNames,
                                      const std::string& schemaName,  // for nominal schema, use ""
                                      const std::string& viewName,
                                      const std::string& conditionLHS = "",
                                      const QueryResults conditionRHS = QueryResults(),
                                      // must have only one row
                                      const std::string& conditionRHSName = ""
                                      // if empty, conditionRHS must have only one column
    );

    // Assume data type of condition RHS is std::string
    const QueryResults basicQueryView(const std::string& columnName,
                                      const std::string& schemaName,  // for nominal schema, use ""
                                      const std::string& viewName,
                                      const std::string& conditionLHS = "",
                                      const QueryResults conditionRHS = QueryResults(),
                                      // must have only one row
                                      const std::string& conditionRHSName = ""
                                      // if empty, conditionRHS must have only one column
    );

    // For any data type of condition RHS.
    // Example usage, for an int key:
    //   results = omdsReader.basicQueryGenericKey<int>(...) ;
    template <class T>
    const QueryResults basicQueryGenericKey(const std::vector<std::string>& columnNames,
                                            const std::string& schemaName,  // for nominal schema, use ""
                                            const std::string& tableName,
                                            const std::string& conditionLHS = "",
                                            const QueryResults conditionRHS = QueryResults(),
                                            // must have only one row
                                            const std::string& conditionRHSName = ""
                                            // if empty, conditionRHS must have only one column
    );

    // For any data type of condition RHS.
    // Example usage, for an int key:
    //   results = omdsReader.basicQueryGenericKey<int>(...) ;
    template <class T>
    const QueryResults basicQueryGenericKey(const std::string& columnName,
                                            const std::string& schemaName,  // for nominal schema, use ""
                                            const std::string& tableName,
                                            const std::string& conditionLHS = "",
                                            const QueryResults conditionRHS = QueryResults(),
                                            // must have only one row
                                            const std::string& conditionRHSName = ""
                                            // if empty, conditionRHS must have only one column
    );

    template <class T>
    const QueryResults singleAttribute(const T& data) const;

    std::vector<std::string> columnNames(const std::string& schemaName,  // for nominal schema, use ""
                                         const std::string& tableName);

    std::vector<std::string> columnNamesView(const std::string& schemaName,  // for nominal schema, use ""
                                             const std::string& viewName);

    // ---------- static member functions --------------------

    // ---------- member functions ---------------------------

    void connect(const std::string& connectString, const std::string& authenticationPath);

  private:
    // ---------- member data --------------------------------
  };

  template <class T>
  const OMDSReader::QueryResults OMDSReader::basicQueryGenericKey(const std::vector<std::string>& columnNames,
                                                                  const std::string& schemaName,
                                                                  const std::string& tableName,
                                                                  const std::string& conditionLHS,
                                                                  const QueryResults conditionRHS,
                                                                  const std::string& conditionRHSName) {
    coral::ISessionProxy& coralSession = session.coralSession();
    coral::ISchema& schema = schemaName.empty() ? coralSession.nominalSchema() : coralSession.schema(schemaName);

    coral::ITable& table = schema.tableHandle(tableName);

    // Pointer is deleted automatically at end of function.
    std::shared_ptr<coral::IQuery> query(table.newQuery());

    // Construct query
    std::vector<std::string>::const_iterator it = columnNames.begin();
    std::vector<std::string>::const_iterator end = columnNames.end();
    for (; it != end; ++it) {
      query->addToOutputList(*it);
    }

    // Only apply condition if RHS has one row.
    if (!conditionLHS.empty() && conditionRHS.numberRows() == 1) {
      if (!conditionRHSName.empty()) {
        // Use type of dummyVariable to determine type of condition RHS
        coral::AttributeList attList;
        attList.extend(conditionRHSName, typeid(T));
        T tmp = T();
        conditionRHS.fillVariable(conditionRHSName, tmp);
        attList[conditionRHSName].data<T>() = tmp;

        query->setCondition(conditionLHS + " = :" + conditionRHSName, attList);
      } else if (conditionRHS.columnNames().size() == 1)
      // check for only one column
      {
        query->setCondition(conditionLHS + " = :" + conditionRHS.columnNames().front(),
                            conditionRHS.attributeLists().front());
      }
    }

    coral::ICursor& cursor = query->execute();

    // Copy AttributeLists for external use because the cursor is deleted
    // when the query goes out of scope.
    std::vector<coral::AttributeList> atts;
    while (cursor.next()) {
      atts.push_back(cursor.currentRow());
    };

    return QueryResults(columnNames, atts);
  }

  template <class T>
  const OMDSReader::QueryResults OMDSReader::basicQueryGenericKey(const std::string& columnName,
                                                                  const std::string& schemaName,
                                                                  const std::string& tableName,
                                                                  const std::string& conditionLHS,
                                                                  const QueryResults conditionRHS,
                                                                  const std::string& conditionRHSName) {
    std::vector<std::string> columnNames;
    columnNames.push_back(columnName);
    return basicQueryGenericKey<T>(columnNames, schemaName, tableName, conditionLHS, conditionRHS, conditionRHSName);
  }

  template <class T>
  const OMDSReader::QueryResults OMDSReader::singleAttribute(const T& data) const {
    std::vector<std::string> names;
    names.push_back("dummy");

    coral::AttributeList attList;
    attList.extend("dummy", typeid(T));
    attList["dummy"].data<T>() = data;

    std::vector<coral::AttributeList> atts;
    atts.push_back(attList);

    return QueryResults(names, atts);
  }

  template <class T>
  bool OMDSReader::QueryResults::fillVariable(const std::string& columnName, T& outputVariable) const {
    return fillVariableFromRow(columnName, 0, outputVariable);
  }

  template <class T>
  bool OMDSReader::QueryResults::fillVariableFromRow(const std::string& columnName,
                                                     int rowNumber,
                                                     T& outputVariable) const {
    // Check index in bounds
    if (rowNumber < 0 || rowNumber >= numberRows())
      return false;
    const coral::AttributeList& row = m_attributeLists[rowNumber];
    if (row[columnName].isNull())
      return false;
    outputVariable = row[columnName].template data<T>();
    return true;
  }

  template <class T>
  bool OMDSReader::QueryResults::fillVariable(T& outputVariable) const {
    return fillVariableFromRow(0, outputVariable);
  }

  template <class T>
  bool OMDSReader::QueryResults::fillVariableFromRow(int rowNumber, T& outputVariable) const {
    // Check index in bounds and only one column
    if (rowNumber < 0 || rowNumber >= numberRows() || m_columnNames.size() != 1)
      return false;
    const coral::AttributeList& row = m_attributeLists[rowNumber];
    if (row[m_columnNames.front()].isNull())
      return false;
    outputVariable = row[m_columnNames.front()].template data<T>();
    return true;
  }

}  // namespace l1t
#endif