00001 #include <stdexcept>
00002 #include <cstdlib>
00003 #include <string>
00004 #include <string.h>
00005 #include "OnlineDB/Oracle/interface/Oracle.h"
00006
00007 #include "OnlineDB/EcalCondDB/interface/ODFEDAQConfig.h"
00008
00009 using namespace std;
00010 using namespace oracle::occi;
00011
00012 #define MY_NULL -1
00013 #define SET_INT( statement, paramNum, paramVal ) if( paramVal != MY_NULL ) { statement->setInt(paramNum, paramVal); } else { statement->setNull(paramNum,OCCINUMBER); }
00014 #define SET_STRING( statement, paramNum, paramVal ) if( ! paramVal.empty() ) { statement->setString(paramNum, paramVal); } else { statement->setNull(paramNum,OCCICHAR); }
00015
00016 int getInt(ResultSet * rset, int ipar )
00017 {
00018 return rset->isNull(ipar) ? MY_NULL : rset->getInt(ipar) ;
00019 }
00020
00021 ODFEDAQConfig::ODFEDAQConfig()
00022 {
00023 m_env = NULL;
00024 m_conn = NULL;
00025 m_writeStmt = NULL;
00026 m_readStmt = NULL;
00027 m_config_tag="";
00028 m_ID=0;
00029 clear();
00030 }
00031
00032
00033 void ODFEDAQConfig::clear(){
00034 m_del=MY_NULL;
00035 m_wei=MY_NULL;
00036 m_ped=MY_NULL;
00037
00038 m_bxt =MY_NULL;
00039 m_btt =MY_NULL;
00040 m_tbtt=MY_NULL;
00041 m_tbxt=MY_NULL;
00042
00043 m_version=0;
00044 m_com="";
00045 }
00046
00047
00048
00049 ODFEDAQConfig::~ODFEDAQConfig()
00050 {
00051 }
00052
00053
00054
00055 int ODFEDAQConfig::fetchNextId() throw(std::runtime_error) {
00056
00057 int result=0;
00058 try {
00059 this->checkConnection();
00060
00061 m_readStmt = m_conn->createStatement();
00062 m_readStmt->setSQL("select fe_daq_conDfig_sq.nextVal from dual");
00063 ResultSet* rset = m_readStmt->executeQuery();
00064 while (rset->next ()){
00065 result= rset->getInt(1);
00066 }
00067 m_conn->terminateStatement(m_readStmt);
00068 return result;
00069
00070 } catch (SQLException &e) {
00071 throw(std::runtime_error("ODFEDAQConfig::fetchNextId(): "+e.getMessage()));
00072 }
00073
00074 }
00075
00076 void ODFEDAQConfig::prepareWrite()
00077 throw(std::runtime_error)
00078 {
00079 this->checkConnection();
00080 int next_id=fetchNextId();
00081
00082 try {
00083 m_writeStmt = m_conn->createStatement();
00084 m_writeStmt->setSQL("INSERT INTO FE_DAQ_CONFIG ( config_id, tag, version, ped_id, "
00085 " del_id, wei_id,bxt_id, btt_id, tr_bxt_id, tr_btt_id, user_comment ) "
00086 "VALUES ( :1, :2, :3, :4, :5, :6, :7 ,:8, :9, :10, :11 )" );
00087
00088 m_writeStmt->setInt(1, next_id);
00089 m_ID=next_id;
00090
00091 } catch (SQLException &e) {
00092 throw(std::runtime_error("ODFEDAQConfig::prepareWrite(): "+e.getMessage()));
00093 }
00094
00095 }
00096
00097 void ODFEDAQConfig::setParameters(std::map<string,string> my_keys_map){
00098
00099
00100
00101
00102 for( std::map<std::string, std::string >::iterator ci=
00103 my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
00104
00105 if(ci->first== "VERSION") setVersion(atoi(ci->second.c_str()) );
00106 if(ci->first== "PED_ID") setPedestalId(atoi(ci->second.c_str()) );
00107 if(ci->first== "DEL_ID") setDelayId(atoi(ci->second.c_str()));
00108 if(ci->first== "WEI_ID") setWeightId(atoi(ci->second.c_str()));
00109
00110 if(ci->first== "BXT_ID") setBadXtId(atoi(ci->second.c_str()));
00111 if(ci->first== "BTT_ID") setBadTTId(atoi(ci->second.c_str()));
00112 if(ci->first== "TRIG_BXT_ID") setTriggerBadXtId(atoi(ci->second.c_str()));
00113 if(ci->first== "TRIG_BTT_ID") setTriggerBadTTId(atoi(ci->second.c_str()));
00114
00115 if(ci->first== "COMMENT" || ci->first== "USER_COMMENT") setComment(ci->second);
00116
00117 }
00118
00119 }
00120
00121 void ODFEDAQConfig::writeDB()
00122 throw(std::runtime_error)
00123 {
00124 this->checkConnection();
00125 this->checkPrepare();
00126
00127 try {
00128
00129
00130 m_writeStmt->setString(2, this->getConfigTag());
00131 m_writeStmt->setInt(3, this->getVersion());
00132 SET_INT(m_writeStmt,4, this->getPedestalId());
00133 SET_INT(m_writeStmt,5, this->getDelayId());
00134 SET_INT(m_writeStmt,6, this->getWeightId());
00135 SET_INT(m_writeStmt,7, this->getBadXtId());
00136 SET_INT(m_writeStmt,8, this->getBadTTId());
00137 SET_INT(m_writeStmt,9, this->getTriggerBadXtId());
00138 SET_INT(m_writeStmt,10,this->getTriggerBadTTId());
00139
00140 m_writeStmt->setString(11, this->getComment());
00141
00142 m_writeStmt->executeUpdate();
00143
00144
00145 } catch (SQLException &e) {
00146 throw(std::runtime_error("ODFEDAQConfig::writeDB(): "+e.getMessage()));
00147 }
00148
00149 if (!this->fetchID()) {
00150 throw(std::runtime_error("ODFEDAQConfig::writeDB: Failed to write"));
00151 }
00152
00153
00154 }
00155
00156
00157 void ODFEDAQConfig::fetchData(ODFEDAQConfig * result)
00158 throw(std::runtime_error)
00159 {
00160 this->checkConnection();
00161 result->clear();
00162 if(result->getId()==0 && (result->getConfigTag()=="") ){
00163 throw(std::runtime_error("ODFEDAQConfig::fetchData(): no Id defined for this ODFEDAQConfig "));
00164 }
00165
00166 if(result->getConfigTag()!="" && result->getVersion() ==0 ){
00167 int new_version=0;
00168 std::cout<< "using new method : retrieving last version for this tag "<<endl;
00169 try {
00170 this->checkConnection();
00171
00172 m_readStmt->setSQL("select max(version) from "+getTable()+" where tag=:tag " );
00173 m_readStmt->setString(1, result->getConfigTag());
00174 ResultSet* rset = m_readStmt->executeQuery();
00175 while (rset->next ()){
00176 new_version= rset->getInt(1);
00177 }
00178 m_conn->terminateStatement(m_readStmt);
00179
00180 m_readStmt = m_conn->createStatement();
00181
00182 result->setVersion(new_version);
00183
00184 } catch (SQLException &e) {
00185 throw(std::runtime_error("ODFEDAQConfig::fetchData(): "+e.getMessage()));
00186 }
00187
00188
00189
00190 }
00191
00192 try {
00193
00194 m_readStmt->setSQL("SELECT * FROM " + getTable() +
00195 " where ( config_id = :1 or (tag=:2 AND version=:3 ) )" );
00196 m_readStmt->setInt(1, result->getId());
00197 m_readStmt->setString(2, result->getConfigTag());
00198 m_readStmt->setInt(3, result->getVersion());
00199 ResultSet* rset = m_readStmt->executeQuery();
00200
00201 rset->next();
00202
00203
00204
00205 result->setId(rset->getInt(1));
00206 result->setConfigTag(rset->getString(2));
00207 result->setVersion(rset->getInt(3));
00208
00209 result->setPedestalId( getInt(rset,4) );
00210 result->setDelayId( getInt(rset,5) );
00211 result->setWeightId( getInt(rset,6) );
00212 result->setBadXtId( getInt(rset,7) );
00213 result->setBadTTId( getInt(rset,8) );
00214 result->setTriggerBadXtId( getInt(rset,9) );
00215 result->setTriggerBadTTId( getInt(rset,10) );
00216 result->setComment( rset->getString(11) );
00217
00218 } catch (SQLException &e) {
00219 throw(std::runtime_error("ODFEDAQConfig::fetchData(): "+e.getMessage()));
00220 }
00221 }
00222
00223 int ODFEDAQConfig::fetchID() throw(std::runtime_error)
00224 {
00225
00226 if (m_ID!=0) {
00227 return m_ID;
00228 }
00229
00230 this->checkConnection();
00231
00232 try {
00233 Statement* stmt = m_conn->createStatement();
00234 stmt->setSQL("SELECT config_id FROM "+ getTable()+
00235 "WHERE tag=:1 and version=:2 " );
00236
00237 stmt->setString(1, getConfigTag() );
00238 stmt->setInt(2, getVersion() );
00239
00240 ResultSet* rset = stmt->executeQuery();
00241
00242 if (rset->next()) {
00243 m_ID = rset->getInt(1);
00244 } else {
00245 m_ID = 0;
00246 }
00247 m_conn->terminateStatement(stmt);
00248 } catch (SQLException &e) {
00249 throw(std::runtime_error("ODFEDAQConfig::fetchID: "+e.getMessage()));
00250 }
00251
00252 return m_ID;
00253 }