00001 #include "CondCore/MetaDataService/interface/MetaData.h"
00002 #include "CondCore/MetaDataService/interface/MetaDataSchemaUtility.h"
00003 #include "CondCore/MetaDataService/interface/MetaDataNames.h"
00004 #include "CondCore/MetaDataService/interface/MetaDataExceptions.h"
00005 #include "CondCore/DBCommon/interface/Exception.h"
00006 #include "CondCore/DBCommon/interface/CoralTransaction.h"
00007 #include "RelationalAccess/SchemaException.h"
00008 #include "RelationalAccess/ISchema.h"
00009 #include "RelationalAccess/ITable.h"
00010 #include "RelationalAccess/TableDescription.h"
00011 #include "RelationalAccess/ITablePrivilegeManager.h"
00012 #include "RelationalAccess/ICursor.h"
00013 #include "RelationalAccess/IQuery.h"
00014 #include "RelationalAccess/ITableDataEditor.h"
00015 #include "CoralBase/AttributeList.h"
00016 #include "CoralBase/AttributeSpecification.h"
00017 #include "CoralBase/Attribute.h"
00018
00019 cond::MetaData::MetaData(cond::CoralTransaction& coraldb):m_coraldb(coraldb){
00020 }
00021 cond::MetaData::~MetaData(){
00022 }
00023 bool
00024 cond::MetaData::addMapping(const std::string& name, const std::string& iovtoken, cond::TimeType timetype ){
00025 cond::MetaDataSchemaUtility ut(m_coraldb);
00026 try{
00027 ut.create();
00028 coral::ITable& mytable=m_coraldb.nominalSchema().tableHandle(cond::MetaDataNames::metadataTable());
00029 coral::AttributeList rowBuffer;
00030 coral::ITableDataEditor& dataEditor = mytable.dataEditor();
00031 dataEditor.rowBuffer( rowBuffer );
00032 rowBuffer[cond::MetaDataNames::tagColumn()].data<std::string>()=name;
00033 rowBuffer[cond::MetaDataNames::tokenColumn()].data<std::string>()=iovtoken;
00034 rowBuffer[cond::MetaDataNames::timetypeColumn()].data<int>()=timetype;
00035 dataEditor.insertRow( rowBuffer );
00036 }catch( const coral::DuplicateEntryInUniqueKeyException& er ){
00037 throw cond::MetaDataDuplicateEntryException("addMapping",name);
00038 }catch(std::exception& er){
00039 throw cond::Exception(std::string("MetaData::addMapping error: ")+er.what());
00040 }
00041 return true;
00042 }
00043 bool
00044 cond::MetaData::replaceToken(const std::string& name, const std::string& newtoken){
00045 try{
00046 if(!m_coraldb.nominalSchema().existsTable(cond::MetaDataNames::metadataTable())){
00047 throw cond::Exception( "MetaData::replaceToken MetaData table doesnot exist" );
00048 }
00049 coral::ITable& mytable=m_coraldb.nominalSchema().tableHandle(cond::MetaDataNames::metadataTable());
00050 coral::AttributeList inputData;
00051 coral::ITableDataEditor& dataEditor = mytable.dataEditor();
00052 inputData.extend<std::string>("newToken");
00053 inputData.extend<std::string>("oldTag");
00054 inputData[0].data<std::string>() = newtoken;
00055 inputData[1].data<std::string>() = name;
00056 std::string setClause(cond::MetaDataNames::tokenColumn());
00057 setClause+="= :newToken";
00058 std::string condition( cond::MetaDataNames::tagColumn() );
00059 condition+="= :oldTag";
00060 dataEditor.updateRows( setClause, condition, inputData );
00061 }catch( coral::DuplicateEntryInUniqueKeyException& er ){
00063 throw cond::MetaDataDuplicateEntryException("MetaData::replaceToken",name);
00064 }catch(std::exception& er){
00065 throw cond::Exception(std::string("MetaData::replaceToken error: ")+er.what());
00066 }
00067 return true;
00068 }
00069 const std::string
00070 cond::MetaData::getToken( const std::string& name ) const{
00071 std::string iovtoken;
00072 try{
00073 coral::ITable& mytable=m_coraldb.nominalSchema().tableHandle( cond::MetaDataNames::metadataTable() );
00074 std::auto_ptr< coral::IQuery > query(mytable.newQuery());
00075 query->setRowCacheSize( 100 );
00076 coral::AttributeList emptyBindVariableList;
00077 std::string condition=cond::MetaDataNames::tagColumn()+" = '"+name+"'";
00078 query->setCondition( condition, emptyBindVariableList );
00079 query->addToOutputList( cond::MetaDataNames::tokenColumn() );
00080 coral::ICursor& cursor = query->execute();
00081 while( cursor.next() ) {
00082 const coral::AttributeList& row = cursor.currentRow();
00083 iovtoken=row[ cond::MetaDataNames::tokenColumn() ].data<std::string>();
00084 }
00085 }catch(const coral::TableNotExistingException& er){
00087
00088 return "";
00089 }catch(const std::exception& er){
00090 throw cond::Exception( std::string("MetaData::getToken error: ")+er.what() );
00091 }
00092 return iovtoken;
00093 }
00094 void
00095 cond::MetaData::getEntryByTag( const std::string& tagname, cond::MetaDataEntry& result )const{
00096 result.tagname=tagname;
00097 try{
00098 coral::ITable& mytable=m_coraldb.nominalSchema().tableHandle( cond::MetaDataNames::metadataTable() );
00099 std::auto_ptr< coral::IQuery > query(mytable.newQuery());
00100 query->setRowCacheSize( 100 );
00101 coral::AttributeList emptyBindVariableList;
00102 std::string condition=cond::MetaDataNames::tagColumn()+" = '"+tagname+"'";
00103 query->setCondition( condition, emptyBindVariableList );
00104 query->addToOutputList( cond::MetaDataNames::tokenColumn() );
00105 query->addToOutputList( cond::MetaDataNames::timetypeColumn() );
00106 coral::ICursor& cursor = query->execute();
00107 while( cursor.next() ) {
00108 const coral::AttributeList& row = cursor.currentRow();
00109 result.iovtoken=row[ cond::MetaDataNames::tokenColumn() ].data<std::string>();
00110 int tp=row[ cond::MetaDataNames::timetypeColumn() ].data<int>();
00111 result.timetype=(cond::TimeType)tp;
00112
00113 }
00114 }catch(const coral::TableNotExistingException& er){
00116
00117 return;
00118 }catch(const std::exception& er){
00119 throw cond::Exception( std::string("MetaData::getEntryByTag error: ")+er.what() );
00120 }
00121 return;
00122 }
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 bool cond::MetaData::hasTag( const std::string& name ) const{
00141 bool result=false;
00142 try{
00143 coral::ITable& mytable=m_coraldb.nominalSchema().tableHandle( cond::MetaDataNames::metadataTable() );
00144 std::auto_ptr< coral::IQuery > query(mytable.newQuery());
00145 coral::AttributeList emptyBindVariableList;
00146 std::string condition=cond::MetaDataNames::tagColumn()+" = '"+name+"'";
00147 query->setCondition( condition, emptyBindVariableList );
00148 coral::ICursor& cursor = query->execute();
00149 if( cursor.next() ) result=true;
00150 cursor.close();
00151 }catch(const coral::TableNotExistingException& er){
00153 return false;
00154 }catch(const std::exception& er){
00155 throw cond::Exception( std::string("MetaData::hasTag: " )+er.what() );
00156 }
00157 return result;
00158 }
00159 void
00160 cond::MetaData::listAllTags( std::vector<std::string>& result ) const{
00161 try{
00162 coral::ITable& mytable=m_coraldb.nominalSchema().tableHandle( cond::MetaDataNames::metadataTable() );
00163 std::auto_ptr< coral::IQuery > query(mytable.newQuery());
00164 query->addToOutputList( cond::MetaDataNames::tagColumn() );
00165 query->setMemoryCacheSize( 100 );
00166 coral::ICursor& cursor = query->execute();
00167 while( cursor.next() ){
00168 const coral::AttributeList& row = cursor.currentRow();
00169 result.push_back(row[cond::MetaDataNames::tagColumn()].data<std::string>());
00170 }
00171 cursor.close();
00172 }catch(const coral::TableNotExistingException& er){
00174 return;
00175 }catch(const std::exception& er){
00176 throw cond::Exception( std::string("MetaData::listAllTag: " )+er.what() );
00177 }
00178 }
00179 void
00180 cond::MetaData::listAllEntries( std::vector<cond::MetaDataEntry>& result ) const{
00181 try{
00182 coral::ITable& mytable=m_coraldb.nominalSchema().tableHandle( cond::MetaDataNames::metadataTable() );
00183 std::auto_ptr< coral::IQuery > query(mytable.newQuery());
00184 query->addToOutputList( cond::MetaDataNames::tagColumn() );
00185 query->setMemoryCacheSize( 100 );
00186 coral::ICursor& cursor = query->execute();
00187 while( cursor.next() ){
00188 cond::MetaDataEntry r;
00189 const coral::AttributeList& row = cursor.currentRow();
00190 r.tagname=row[cond::MetaDataNames::tagColumn()].data<std::string>();
00191 r.iovtoken=row[cond::MetaDataNames::tokenColumn()].data<std::string>();
00192 int tp=row[cond::MetaDataNames::timetypeColumn()].data<int>();
00193 r.timetype=(cond::TimeType)tp;
00194 result.push_back(r);
00195 }
00196 cursor.close();
00197 }catch(const coral::TableNotExistingException& er){
00199 return;
00200 }catch(const std::exception& er){
00201 throw cond::Exception( std::string("MetaData::listAllEntries: " )+er.what() );
00202 }
00203 }
00204 void
00205 cond::MetaData::deleteAllEntries(){
00206 coral::AttributeList emptybinddata;
00207 try{
00208 coral::ITable& table=m_coraldb.nominalSchema().tableHandle(cond::MetaDataNames::metadataTable());
00209 coral::ITableDataEditor& dataEditor = table.dataEditor();
00210 dataEditor.deleteRows( "",emptybinddata );
00211 }catch(const coral::TableNotExistingException& er){
00213 return;
00214 }catch(const std::exception& er){
00215 throw cond::Exception( std::string("MetaData::deleteAllEntries: " )+er.what() );
00216 }
00217 }
00218 void cond::MetaData::deleteEntryByToken( const std::string& token ){
00219 coral::AttributeList deletecondition;
00220 deletecondition.extend( cond::MetaDataNames::tokenColumn(), typeid(std::string) );
00221 deletecondition[cond::MetaDataNames::tokenColumn()].data<std::string>()=token;
00222 std::string whereClause=cond::MetaDataNames::tokenColumn()+"=:"+cond::MetaDataNames::tokenColumn();
00223 try{
00224 coral::ITable& table=m_coraldb.nominalSchema().tableHandle(cond::MetaDataNames::metadataTable());
00225 coral::ITableDataEditor& dataEditor = table.dataEditor();
00226 dataEditor.deleteRows(whereClause,deletecondition );
00227 return;
00228 }catch(const coral::TableNotExistingException& er){
00230 return;
00231 }catch(const std::exception& er){
00232 throw cond::Exception( std::string("MetaData::deleteEntryByToken: " )+er.what() );
00233 }
00234 }
00235 void cond::MetaData::deleteEntryByTag( const std::string& tag ){
00236 coral::AttributeList deletecondition;
00237 deletecondition.extend( cond::MetaDataNames::tagColumn(), typeid(std::string) );
00238 deletecondition[cond::MetaDataNames::tagColumn()].data<std::string>()=tag;
00239 std::string whereClause=cond::MetaDataNames::tagColumn()+"=:"+cond::MetaDataNames::tagColumn();
00240 try{
00241 coral::ITable& table=m_coraldb.nominalSchema().tableHandle(cond::MetaDataNames::metadataTable());
00242 coral::ITableDataEditor& dataEditor = table.dataEditor();
00243 dataEditor.deleteRows(whereClause,deletecondition );
00244 }catch(const coral::TableNotExistingException& er){
00246 return;
00247 }catch(const std::exception& er){
00248 throw cond::Exception( std::string("MetaData::deleteEntryByTag: " )+er.what() );
00249 }
00250 }