00001 #include <stdexcept>
00002 #include <string>
00003 #include <string.h>
00004 #include <cstdlib>
00005
00006 #include "OnlineDB/Oracle/interface/Oracle.h"
00007
00008 #include "OnlineDB/EcalCondDB/interface/ODCCSConfig.h"
00009
00010 #include <limits>
00011
00012 #define MY_NULL 16777215 // numeric_limits<int>::quiet_NaN()
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 using namespace std;
00017 using namespace oracle::occi;
00018
00019 ODCCSConfig::ODCCSConfig()
00020 {
00021 m_env = NULL;
00022 m_conn = NULL;
00023 m_writeStmt = NULL;
00024 m_readStmt = NULL;
00025 m_config_tag="";
00026 m_ID=0;
00027 clear();
00028 }
00029
00030
00031 void ODCCSConfig::clear(){
00032 m_daccal=MY_NULL;
00033 m_delay=MY_NULL;
00034 m_gain="";
00035 m_memgain="";
00036 m_offset_high=MY_NULL;
00037 m_offset_low=MY_NULL;
00038 m_offset_mid=MY_NULL;
00039 m_trg_mode="";
00040 m_trg_filter="";
00041 m_bgo="";
00042 m_tts_mask=MY_NULL;
00043 m_daq=MY_NULL;
00044 m_trg=MY_NULL;
00045 m_bc0=MY_NULL;
00046 m_bc0_delay=MY_NULL;
00047 m_te_delay=MY_NULL;
00048 }
00049
00050
00051
00052 ODCCSConfig::~ODCCSConfig()
00053 {
00054 }
00055
00056
00057
00058 int ODCCSConfig::fetchNextId() throw(std::runtime_error) {
00059
00060 int result=0;
00061 try {
00062 this->checkConnection();
00063
00064 m_readStmt = m_conn->createStatement();
00065 m_readStmt->setSQL("select ecal_CCS_config_sq.NextVal from dual");
00066 ResultSet* rset = m_readStmt->executeQuery();
00067 while (rset->next ()){
00068 result= rset->getInt(1);
00069 }
00070 m_conn->terminateStatement(m_readStmt);
00071 return result;
00072
00073 } catch (SQLException &e) {
00074 throw(std::runtime_error("ODCCSConfig::fetchNextId(): "+e.getMessage()));
00075 }
00076
00077 }
00078
00079 void ODCCSConfig::prepareWrite()
00080 throw(std::runtime_error)
00081 {
00082 this->checkConnection();
00083 int next_id=fetchNextId();
00084
00085 try {
00086 m_writeStmt = m_conn->createStatement();
00087 m_writeStmt->setSQL("INSERT INTO ECAL_CCS_CONFIGURATION ( ccs_configuration_id, ccs_tag ,"
00088 " daccal, delay, gain, memgain, offset_high,offset_low,offset_mid, trg_mode, trg_filter, "
00089 " clock, BGO_SOURCE, TTS_MASK, DAQ_BCID_PRESET , TRIG_BCID_PRESET, BC0_COUNTER, BC0_DELAY, TE_DELAY ) "
00090 "VALUES ( "
00091 " :ccs_configuration_id, :ccs_tag, :daccal, :delay, :gain, :memgain, :offset_high,:offset_low,"
00092 " :offset_mid, :trg_mode, :trg_filter, :clock, :BGO_SOURCE, :TTS_MASK, "
00093 " :DAQ_BCID_PRESET , :TRIG_BCID_PRESET, :BC0_COUNTER, :BC0_DELAY, :TE_DELAY) ");
00094
00095 m_writeStmt->setInt(1, next_id);
00096 m_ID=next_id;
00097
00098 } catch (SQLException &e) {
00099 throw(std::runtime_error("ODCCSConfig::prepareWrite(): "+e.getMessage()));
00100 }
00101
00102 }
00103
00104 void ODCCSConfig::setParameters(std::map<string,string> my_keys_map){
00105
00106
00107
00108
00109 for( std::map<std::string, std::string >::iterator ci=
00110 my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
00111
00112 if(ci->first== "CCS_CONFIGURATION_ID") setConfigTag(ci->second);
00113 else if(ci->first== "DACCAL") setDaccal(atoi(ci->second.c_str()) );
00114 else if(ci->first== "GAIN") setGain(ci->second);
00115 else if(ci->first== "MEMGAIN") setMemGain(ci->second);
00116 else if(ci->first== "OFFSET_HIGH") setOffsetHigh(atoi(ci->second.c_str() ));
00117 else if(ci->first== "OFFSET_LOW") setOffsetLow(atoi(ci->second.c_str() ));
00118 else if(ci->first== "OFFSET_MID") setOffsetMid(atoi(ci->second.c_str() ));
00119 else if(ci->first== "TRG_MODE") setTrgMode(ci->second );
00120 else if(ci->first== "TRG_FILTER") setTrgFilter(ci->second );
00121 else if(ci->first== "CLOCK") setClock(atoi(ci->second.c_str()) );
00122 else if(ci->first== "BGO_SOURCE") setBGOSource(ci->second) ;
00123 else if(ci->first== "TTS_MASK") setTTSMask(atoi(ci->second.c_str()) );
00124 else if(ci->first== "DAQ_BCID_PRESET") setDAQBCIDPreset(atoi(ci->second.c_str() ));
00125 else if(ci->first== "TRIG_BCID_PRESET") setTrgBCIDPreset(atoi(ci->second.c_str()) );
00126 else if(ci->first== "BC0_COUNTER") setBC0Counter(atoi(ci->second.c_str() ));
00127 else if(ci->first== "BC0_DELAY") setBC0Delay(atoi(ci->second.c_str() ));
00128 else if(ci->first== "TE_DELAY") setTEDelay(atoi(ci->second.c_str() ));
00129 else if(ci->first== "DELAY") setDelay(atoi(ci->second.c_str() ));
00130
00131 }
00132
00133 }
00134
00135 void ODCCSConfig::writeDB()
00136 throw(std::runtime_error)
00137 {
00138 this->checkConnection();
00139 this->checkPrepare();
00140
00141 try {
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161 SET_STRING(m_writeStmt, 2, this->getConfigTag());
00162 SET_INT (m_writeStmt, 3, this->getDaccal());
00163 SET_INT (m_writeStmt, 4, this->getDelay());
00164 SET_STRING(m_writeStmt, 5, this->getGain());
00165 SET_STRING(m_writeStmt, 6, this->getMemGain());
00166 SET_INT (m_writeStmt, 7, this->getOffsetHigh());
00167 SET_INT (m_writeStmt, 8, this->getOffsetLow());
00168 SET_INT (m_writeStmt, 9, this->getOffsetMid());
00169 SET_STRING(m_writeStmt, 10, this->getTrgMode());
00170 SET_STRING(m_writeStmt, 11, this->getTrgFilter());
00171 SET_INT (m_writeStmt, 12, this->getClock());
00172 SET_STRING(m_writeStmt, 13, this->getBGOSource());
00173 SET_INT (m_writeStmt, 14, this->getTTSMask());
00174 SET_INT (m_writeStmt, 15, this->getDAQBCIDPreset());
00175 SET_INT (m_writeStmt, 16, this->getTrgBCIDPreset());
00176 SET_INT (m_writeStmt, 17, this->getBC0Counter());
00177 SET_INT (m_writeStmt, 18, this->getBC0Delay());
00178 SET_INT (m_writeStmt, 19, this->getTEDelay());
00179
00180 m_writeStmt->executeUpdate();
00181
00182
00183 } catch (SQLException &e) {
00184 throw(std::runtime_error("ODCCSConfig::writeDB(): "+e.getMessage()));
00185 }
00186
00187 if (!this->fetchID()) {
00188 throw(std::runtime_error("ODCCSConfig::writeDB: Failed to write"));
00189 }
00190
00191
00192 }
00193
00194
00195 void ODCCSConfig::fetchData(ODCCSConfig * result)
00196 throw(std::runtime_error)
00197 {
00198 this->checkConnection();
00199 result->clear();
00200 if(result->getId()==0 && (result->getConfigTag()=="") ){
00201 throw(std::runtime_error("ODCCSConfig::fetchData(): no Id defined for this ODCCSConfig "));
00202 }
00203
00204 try {
00205
00206 m_readStmt->setSQL("SELECT * "
00207 "FROM ECAL_CCS_CONFIGURATION "
00208 " where ( CCS_configuration_id = :1 or CCS_tag=:2 )" );
00209 m_readStmt->setInt(1, result->getId());
00210 m_readStmt->setString(2, result->getConfigTag());
00211 ResultSet* rset = m_readStmt->executeQuery();
00212
00213 rset->next();
00214
00215
00216
00217 result->setId(rset->getInt(1));
00218 result->setConfigTag(rset->getString(2));
00219
00220 result->setDaccal( rset->getInt(3) );
00221 result->setDelay( rset->getInt(4) );
00222 result->setGain( rset->getString(5) );
00223 result->setMemGain( rset->getString(6) );
00224 result->setOffsetHigh( rset->getInt(7) );
00225 result->setOffsetLow( rset->getInt(8) );
00226 result->setOffsetMid( rset->getInt(9) );
00227 result->setTrgMode( rset->getString(10) );
00228 result->setTrgFilter( rset->getString(11) );
00229 result->setClock( rset->getInt(12) );
00230 result->setBGOSource( rset->getString(13) );
00231 result->setTTSMask( rset->getInt(14) );
00232 result->setDAQBCIDPreset( rset->getInt(15) );
00233 result->setTrgBCIDPreset( rset->getInt(16) );
00234 result->setBC0Counter( rset->getInt(17) );
00235
00236
00237 } catch (SQLException &e) {
00238 throw(std::runtime_error("ODCCSConfig::fetchData(): "+e.getMessage()));
00239 }
00240 }
00241
00242 int ODCCSConfig::fetchID() throw(std::runtime_error)
00243 {
00244
00245 if (m_ID!=0) {
00246 return m_ID;
00247 }
00248
00249 this->checkConnection();
00250
00251 try {
00252 Statement* stmt = m_conn->createStatement();
00253 stmt->setSQL("SELECT ccs_configuration_id FROM ecal_ccs_configuration "
00254 "WHERE ccs_tag=:ccs_tag "
00255 );
00256
00257 stmt->setString(1, getConfigTag() );
00258
00259 ResultSet* rset = stmt->executeQuery();
00260
00261 if (rset->next()) {
00262 m_ID = rset->getInt(1);
00263 } else {
00264 m_ID = 0;
00265 }
00266 m_conn->terminateStatement(stmt);
00267 } catch (SQLException &e) {
00268 throw(std::runtime_error("ODCCSConfig::fetchID: "+e.getMessage()));
00269 }
00270
00271 return m_ID;
00272 }