CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/CondTools/L1Trigger/src/OMDSReader.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     L1Trigger
00004 // Class  :     OMDSReader
00005 //
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:
00010 //         Created:  Sun Mar  2 01:46:46 CET 2008
00011 // $Id: OMDSReader.cc,v 1.13 2010/04/12 20:29:26 wsun Exp $
00012 //
00013 
00014 // system include files
00015 #include <set>
00016 #include <iostream>
00017 
00018 // user include files
00019 #include "CondTools/L1Trigger/interface/OMDSReader.h"
00020 #include "RelationalAccess/ITableDescription.h"
00021 #include "RelationalAccess/IColumn.h"
00022 #include "CondCore/DBCommon/interface/DbTransaction.h"
00023 
00024 //
00025 // constants, enums and typedefs
00026 //
00027 
00028 //
00029 // static data member definitions
00030 //
00031 
00032 namespace l1t
00033 {
00034 
00035 //
00036 // constructors and destructor
00037 //
00038   OMDSReader::OMDSReader()
00039     : DataManager()
00040   {}
00041 
00042   OMDSReader::OMDSReader( const std::string& connectString,
00043                           const std::string& authenticationPath )
00044     : DataManager( connectString, authenticationPath, true )
00045   {
00046     session->transaction().start( true ) ;
00047   }
00048 
00049   void
00050   OMDSReader::connect( const std::string& connectString,
00051                        const std::string& authenticationPath )
00052   {
00053     DataManager::connect( connectString, authenticationPath, true ) ;
00054     session->transaction().start( true ) ;
00055   }
00056 
00057 // OMDSReader::OMDSReader(const OMDSReader& rhs)
00058 // {
00059 //    // do actual copying here;
00060 // }
00061 
00062 OMDSReader::~OMDSReader()
00063 {
00064 }
00065 
00066 //
00067 // assignment operators
00068 //
00069 // const OMDSReader& OMDSReader::operator=(const OMDSReader& rhs)
00070 // {
00071 //   //An exception safe implementation is
00072 //   OMDSReader temp(rhs);
00073 //   swap(rhs);
00074 //
00075 //   return *this;
00076 // }
00077 
00078 //
00079 // member functions
00080 //
00081 
00082 //
00083 // const member functions
00084 //
00085 
00086   const OMDSReader::QueryResults
00087   OMDSReader::basicQuery(
00088     const std::vector< std::string >& columnNames,
00089     const std::string& schemaName,
00090     const std::string& tableName,
00091     const std::string& conditionLHS,
00092     const QueryResults conditionRHS,
00093     const std::string& conditionRHSName ) const
00094   {
00095     coral::ISchema& schema = schemaName.empty() ?
00096       session->nominalSchema() :
00097       session->schema( schemaName ) ;
00098 
00099     coral::ITable& table = schema.tableHandle( tableName ) ;
00100 
00101     // Pointer is deleted automatically at end of function.
00102     boost::shared_ptr< coral::IQuery > query( table.newQuery() ) ;
00103 
00104     // Construct query
00105     std::vector< std::string >::const_iterator it = columnNames.begin() ;
00106     std::vector< std::string >::const_iterator end = columnNames.end() ;
00107     for( ; it != end ; ++it )
00108       {
00109         query->addToOutputList( *it ) ;
00110       }
00111 
00112     // Only apply condition if RHS has one row.
00113     if( !conditionLHS.empty() && conditionRHS.numberRows() == 1 )
00114       {
00115         if( !conditionRHSName.empty() )
00116           {
00117             // Assume all RHS types are strings.
00118             coral::AttributeList attList ;
00119             attList.extend( conditionRHSName, typeid( std::string ) ) ;
00120             std::string tmp ;
00121             conditionRHS.fillVariable( conditionRHSName, tmp ) ;
00122             attList[ conditionRHSName ].data< std::string >() = tmp ;
00123 
00124             query->setCondition( conditionLHS + " = :" + conditionRHSName,
00125                                  attList ) ;
00126           }
00127         else if( conditionRHS.columnNames().size() == 1 )
00128           // check for only one column
00129           {
00130             query->setCondition( conditionLHS + " = :" +
00131                                    conditionRHS.columnNames().front(),
00132                                  conditionRHS.attributeLists().front() ) ;
00133           }
00134       }
00135 
00136     coral::ICursor& cursor = query->execute() ;
00137 
00138     // Copy AttributeLists for external use because the cursor is deleted
00139     // when the query goes out of scope.
00140     std::vector< coral::AttributeList > atts ;
00141     while( cursor.next() )
00142       {
00143         atts.push_back( cursor.currentRow() ) ;
00144       } ;
00145 
00146     return QueryResults( columnNames, atts ) ;
00147   }
00148 
00149   const OMDSReader::QueryResults
00150   OMDSReader::basicQuery(
00151     const std::string& columnName,
00152     const std::string& schemaName,
00153     const std::string& tableName,
00154     const std::string& conditionLHS,
00155     const QueryResults conditionRHS,
00156     const std::string& conditionRHSName ) const
00157   {
00158     std::vector< std::string > columnNames ;
00159     columnNames.push_back( columnName ) ;
00160     return basicQuery( columnNames, schemaName, tableName,
00161                         conditionLHS, conditionRHS, conditionRHSName ) ;
00162   }
00163 
00164   std::vector< std::string >
00165   OMDSReader::columnNames(
00166     const std::string& schemaName,
00167     const std::string& tableName ) const
00168   {
00169     coral::ISchema& schema = schemaName.empty() ?
00170       session->nominalSchema() :
00171       session->schema( schemaName ) ;
00172 
00173     coral::ITable& table = schema.tableHandle( tableName ) ;
00174     const coral::ITableDescription& tableDesc = table.description() ;
00175 
00176     std::vector< std::string > names ;
00177     int nCols = tableDesc.numberOfColumns() ;
00178 
00179     for( int i = 0 ; i < nCols ; ++i )
00180       {
00181         const coral::IColumn& column = tableDesc.columnDescription( i ) ;
00182         names.push_back( column.name() ) ;
00183       }
00184 
00185     return names ;
00186   }
00187 
00188   // VIEW
00189 
00190   const OMDSReader::QueryResults
00191   OMDSReader::basicQueryView(
00192     const std::vector< std::string >& columnNames,
00193     const std::string& schemaName,
00194     const std::string& viewName,
00195     const std::string& conditionLHS,
00196     const QueryResults conditionRHS,
00197     const std::string& conditionRHSName ) const
00198   {
00199     coral::ISchema& schema = schemaName.empty() ?
00200       session->nominalSchema() :
00201       session->schema( schemaName ) ;
00202 
00203     //    coral::IView& view = schema.viewHandle( viewName ) ;
00204 
00205     // Pointer is deleted automatically at end of function.
00206     coral::IQuery* query = schema.newQuery(); ;
00207 
00208     // Construct query
00209     for (std::vector<std::string>::const_iterator constIt = columnNames.begin(); constIt
00210             != columnNames.end(); ++constIt) {
00211         query->addToOutputList(*constIt);
00212     }
00213 
00214     query->addToTableList(viewName);
00215 
00216     // Only apply condition if RHS has one row.
00217     if (!conditionLHS.empty() && conditionRHS.numberRows() == 1) {
00218 
00219         if (!conditionRHSName.empty()) {
00220             // Assume all RHS types are strings.
00221             coral::AttributeList attList;
00222             attList.extend(conditionRHSName, typeid(std::string));
00223             std::string tmp;
00224             conditionRHS.fillVariable(conditionRHSName, tmp);
00225             attList[conditionRHSName].data<std::string> () = tmp;
00226 
00227             query->setCondition(conditionLHS + " = :" + conditionRHSName, attList);
00228         } else if (conditionRHS.columnNames().size() == 1)
00229         // check for only one column
00230         {
00231             query->setCondition(
00232                     conditionLHS + " = :" + conditionRHS.columnNames().front(),
00233                     conditionRHS.attributeLists().front());
00234         }
00235     }
00236 
00237     coral::ICursor& cursor = query->execute();
00238 
00239     // Copy AttributeLists for external use because the cursor is deleted
00240     // when the query goes out of scope.
00241     std::vector<coral::AttributeList> atts;
00242     while (cursor.next()) {
00243         atts.push_back(cursor.currentRow());
00244     };
00245 
00246     delete query;
00247 
00248 //    // Run a wildcard query on the view
00249 //    coral::IQuery* query2 = workingSchema.newQuery();
00250 //    query2->addToTableList(V0);
00251 //    coral::ICursor& cursor2 = query2->execute();
00252 //    while ( cursor2.next() ) {
00253 //      cursor2.currentRow().toOutputStream( std::cout ) << std::endl;
00254 //    }
00255 //    delete query2;
00256 
00257 
00258 
00259     return QueryResults(columnNames, atts);
00260   }
00261 
00262   const OMDSReader::QueryResults
00263   OMDSReader::basicQueryView(
00264     const std::string& columnName,
00265     const std::string& schemaName,
00266     const std::string& viewName,
00267     const std::string& conditionLHS,
00268     const QueryResults conditionRHS,
00269     const std::string& conditionRHSName ) const
00270   {
00271     std::vector< std::string > columnNames ;
00272     columnNames.push_back( columnName ) ;
00273     return basicQuery( columnNames, schemaName, viewName,
00274             conditionLHS, conditionRHS, conditionRHSName ) ;
00275   }
00276 
00277   std::vector< std::string >
00278   OMDSReader::columnNamesView(
00279     const std::string& schemaName,
00280     const std::string& viewName ) const
00281   {
00282     coral::ISchema& schema = schemaName.empty() ?
00283       session->nominalSchema() :
00284       session->schema( schemaName ) ;
00285 
00286     std::set< std::string > views = schema.listViews ();
00287     std::vector< std::string > names ;
00288 
00289     if (schema.existsView (viewName)) {
00290 
00291         coral::IView& view = schema.viewHandle( viewName ) ;
00292 
00293         int nCols = view.numberOfColumns() ;
00294 
00295         for (int i = 0; i < nCols; ++i) {
00296             const coral::IColumn& column = view.column(i);
00297             names.push_back(column.name());
00298         }
00299 
00300         return names ;
00301 
00302     }
00303 
00304     return names ;
00305 
00306   }
00307 
00308   //
00309 // static member functions
00310 //
00311 }