00001 #ifndef CondTools_L1Trigger_OMDSReader_h
00002 #define CondTools_L1Trigger_OMDSReader_h
00003
00004
00005
00006
00007
00016
00017
00018
00019
00020
00021
00022
00023 #include <memory>
00024 #include "boost/shared_ptr.hpp"
00025
00026
00027 #include "CondTools/L1Trigger/interface/DataManager.h"
00028 #include "RelationalAccess/IQuery.h"
00029 #include "CoralBase/AttributeList.h"
00030 #include "CoralBase/AttributeSpecification.h"
00031 #include "CoralBase/Attribute.h"
00032
00033 #include "RelationalAccess/ITable.h"
00034 #include "RelationalAccess/IView.h"
00035 #include "RelationalAccess/ISchema.h"
00036 #include "RelationalAccess/ISessionProxy.h"
00037 #include "RelationalAccess/ICursor.h"
00038
00039
00040
00041 namespace l1t
00042 {
00043
00044 class OMDSReader : public DataManager
00045 {
00046
00047 public:
00048
00049
00050
00051
00052
00053 class QueryResults
00054 {
00055 public:
00056 QueryResults() {}
00057 QueryResults( const std::vector< std::string >& columnNames,
00058 const std::vector< coral::AttributeList >& attLists )
00059 : m_columnNames( columnNames), m_attributeLists( attLists ) {}
00060
00061 virtual ~QueryResults() {}
00062
00063 const std::vector< std::string >& columnNames() const
00064 { return m_columnNames ; }
00065 const std::vector< coral::AttributeList >& attributeLists() const
00066 { return m_attributeLists ; }
00067 bool queryFailed() const { return m_attributeLists.size() == 0 ; }
00068 int numberRows() const { return m_attributeLists.size() ; }
00069
00070
00071 template< class T >
00072 bool fillVariable( const std::string& columnName,
00073 T& outputVariable ) const ;
00074
00075 template< class T >
00076 bool fillVariableFromRow( const std::string& columnName,
00077 int rowNumber,
00078 T& outputVariable ) const ;
00079
00080
00081 template< class T >
00082 bool fillVariable( T& outputVariable ) const ;
00083
00084 template< class T >
00085 bool fillVariableFromRow( int rowNumber,
00086 T& outputVariable ) const ;
00087
00088 private:
00089 std::vector< std::string > m_columnNames ;
00090 std::vector< coral::AttributeList > m_attributeLists ;
00091 } ;
00092
00093 OMDSReader() ;
00094
00095 OMDSReader( const std::string& connectString,
00096 const std::string& authenticationPath ) ;
00097
00098 virtual ~OMDSReader();
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 const QueryResults basicQuery(
00113 const std::vector< std::string >& columnNames,
00114 const std::string& schemaName,
00115 const std::string& tableName,
00116 const std::string& conditionLHS = "",
00117 const QueryResults conditionRHS = QueryResults(),
00118
00119 const std::string& conditionRHSName = ""
00120
00121 ) const ;
00122
00123
00124 const QueryResults basicQuery(
00125 const std::string& columnName,
00126 const std::string& schemaName,
00127 const std::string& tableName,
00128 const std::string& conditionLHS = "",
00129 const QueryResults conditionRHS = QueryResults(),
00130
00131 const std::string& conditionRHSName = ""
00132
00133 ) const ;
00134
00135
00136
00137 const QueryResults basicQueryView(
00138 const std::vector< std::string >& columnNames,
00139 const std::string& schemaName,
00140 const std::string& viewName,
00141 const std::string& conditionLHS = "",
00142 const QueryResults conditionRHS = QueryResults(),
00143
00144 const std::string& conditionRHSName = ""
00145
00146 ) const ;
00147
00148
00149 const QueryResults basicQueryView(
00150 const std::string& columnName,
00151 const std::string& schemaName,
00152 const std::string& viewName,
00153 const std::string& conditionLHS = "",
00154 const QueryResults conditionRHS = QueryResults(),
00155
00156 const std::string& conditionRHSName = ""
00157
00158 ) const ;
00159
00160
00161
00162
00163 template< class T >
00164 const QueryResults basicQueryGenericKey(
00165 const std::vector< std::string >& columnNames,
00166 const std::string& schemaName,
00167 const std::string& tableName,
00168 const std::string& conditionLHS = "",
00169 const QueryResults conditionRHS = QueryResults(),
00170
00171 const std::string& conditionRHSName = ""
00172
00173 ) const ;
00174
00175
00176
00177
00178 template< class T >
00179 const QueryResults basicQueryGenericKey(
00180 const std::string& columnName,
00181 const std::string& schemaName,
00182 const std::string& tableName,
00183 const std::string& conditionLHS = "",
00184 const QueryResults conditionRHS = QueryResults(),
00185
00186 const std::string& conditionRHSName = ""
00187
00188 ) const ;
00189
00190 template< class T >
00191 const QueryResults singleAttribute( const T& data ) const ;
00192
00193 std::vector< std::string > columnNames(
00194 const std::string& schemaName,
00195 const std::string& tableName ) const ;
00196
00197 std::vector< std::string > columnNamesView(
00198 const std::string& schemaName,
00199 const std::string& viewName ) const ;
00200
00201
00202
00203
00204
00205 void connect( const std::string& connectString,
00206 const std::string& authenticationPath ) ;
00207
00208 private:
00209 OMDSReader(const OMDSReader&);
00210
00211 const OMDSReader& operator=(const OMDSReader&);
00212
00213
00214 };
00215
00216 template< class T > const OMDSReader::QueryResults
00217 OMDSReader::basicQueryGenericKey(
00218 const std::vector< std::string >& columnNames,
00219 const std::string& schemaName,
00220 const std::string& tableName,
00221 const std::string& conditionLHS,
00222 const QueryResults conditionRHS,
00223 const std::string& conditionRHSName ) const
00224 {
00225 coral::ISchema& schema = schemaName.empty() ?
00226 session->nominalSchema() :
00227 session->schema( schemaName ) ;
00228
00229 coral::ITable& table = schema.tableHandle( tableName ) ;
00230
00231
00232 boost::shared_ptr< coral::IQuery > query( table.newQuery() ) ;
00233
00234
00235 std::vector< std::string >::const_iterator it = columnNames.begin() ;
00236 std::vector< std::string >::const_iterator end = columnNames.end() ;
00237 for( ; it != end ; ++it )
00238 {
00239 query->addToOutputList( *it ) ;
00240 }
00241
00242
00243 if( !conditionLHS.empty() && conditionRHS.numberRows() == 1 )
00244 {
00245 if( !conditionRHSName.empty() )
00246 {
00247
00248 coral::AttributeList attList ;
00249 attList.extend( conditionRHSName, typeid( T ) ) ;
00250 T tmp = T();
00251 conditionRHS.fillVariable( conditionRHSName, tmp ) ;
00252 attList[ conditionRHSName ].data< T >() = tmp ;
00253
00254 query->setCondition( conditionLHS + " = :" + conditionRHSName,
00255 attList ) ;
00256 }
00257 else if( conditionRHS.columnNames().size() == 1 )
00258
00259 {
00260 query->setCondition( conditionLHS + " = :" +
00261 conditionRHS.columnNames().front(),
00262 conditionRHS.attributeLists().front() ) ;
00263 }
00264 }
00265
00266 coral::ICursor& cursor = query->execute() ;
00267
00268
00269
00270 std::vector< coral::AttributeList > atts ;
00271 while( cursor.next() )
00272 {
00273 atts.push_back( cursor.currentRow() ) ;
00274 } ;
00275
00276 return QueryResults( columnNames, atts ) ;
00277 }
00278
00279 template< class T > const OMDSReader::QueryResults
00280 OMDSReader::basicQueryGenericKey(
00281 const std::string& columnName,
00282 const std::string& schemaName,
00283 const std::string& tableName,
00284 const std::string& conditionLHS,
00285 const QueryResults conditionRHS,
00286 const std::string& conditionRHSName ) const
00287 {
00288 std::vector< std::string > columnNames ;
00289 columnNames.push_back( columnName ) ;
00290 return basicQueryGenericKey< T >( columnNames, schemaName, tableName,
00291 conditionLHS, conditionRHS, conditionRHSName);
00292 }
00293
00294 template< class T > const OMDSReader::QueryResults
00295 OMDSReader::singleAttribute( const T& data ) const
00296 {
00297 std::vector< std::string > names ;
00298 names.push_back( "dummy" ) ;
00299
00300 coral::AttributeList attList ;
00301 attList.extend( "dummy", typeid( T ) ) ;
00302 attList[ "dummy" ].data< T >() = data ;
00303
00304 std::vector< coral::AttributeList > atts ;
00305 atts.push_back( attList ) ;
00306
00307 return QueryResults( names, atts ) ;
00308 }
00309
00310 template< class T > bool
00311 OMDSReader::QueryResults::fillVariable(
00312 const std::string& columnName,
00313 T& outputVariable ) const
00314 {
00315 return fillVariableFromRow( columnName, 0, outputVariable ) ;
00316 }
00317
00318 template< class T > bool
00319 OMDSReader::QueryResults::fillVariableFromRow(
00320 const std::string& columnName,
00321 int rowNumber,
00322 T& outputVariable ) const
00323 {
00324
00325 if( rowNumber < 0 || rowNumber >= numberRows() ) return false ;
00326 const coral::AttributeList& row = m_attributeLists[ rowNumber ] ;
00327 if( row[ columnName ].isNull() ) return false ;
00328 outputVariable = row[ columnName ].template data< T >() ;
00329 return true ;
00330 }
00331
00332 template< class T > bool
00333 OMDSReader::QueryResults::fillVariable( T& outputVariable ) const
00334 {
00335 return fillVariableFromRow( 0, outputVariable ) ;
00336 }
00337
00338 template< class T > bool
00339 OMDSReader::QueryResults::fillVariableFromRow( int rowNumber,
00340 T& outputVariable ) const
00341 {
00342
00343 if( rowNumber < 0 || rowNumber >= numberRows() ||
00344 m_columnNames.size() != 1 ) return false ;
00345 const coral::AttributeList& row = m_attributeLists[ rowNumber ] ;
00346 if( row[ m_columnNames.front() ].isNull() ) return false ;
00347 outputVariable = row[ m_columnNames.front() ].template data< T >() ;
00348 return true ;
00349 }
00350
00351 }
00352 #endif