00001 #include <stdexcept>
00002 #include <string>
00003 #include <string.h>
00004 #include "OnlineDB/Oracle/interface/Oracle.h"
00005
00006 #include "OnlineDB/EcalCondDB/interface/ODFEDAQConfig.h"
00007
00008 using namespace std;
00009 using namespace oracle::occi;
00010
00011 ODFEDAQConfig::ODFEDAQConfig()
00012 {
00013 m_env = NULL;
00014 m_conn = NULL;
00015 m_writeStmt = NULL;
00016 m_readStmt = NULL;
00017 m_config_tag="";
00018 m_ID=0;
00019 clear();
00020 }
00021
00022
00023 void ODFEDAQConfig::clear(){
00024 m_del=0;
00025 m_wei=0;
00026 m_ped=0;
00027 m_version=0;
00028 m_com="";
00029 }
00030
00031
00032
00033 ODFEDAQConfig::~ODFEDAQConfig()
00034 {
00035 }
00036
00037
00038
00039 int ODFEDAQConfig::fetchNextId() throw(std::runtime_error) {
00040
00041 int result=0;
00042 try {
00043 this->checkConnection();
00044
00045 m_readStmt = m_conn->createStatement();
00046 m_readStmt->setSQL("select fe_daq_conDfig_sq.nextVal from dual");
00047 ResultSet* rset = m_readStmt->executeQuery();
00048 while (rset->next ()){
00049 result= rset->getInt(1);
00050 }
00051 m_conn->terminateStatement(m_readStmt);
00052 return result;
00053
00054 } catch (SQLException &e) {
00055 throw(runtime_error("ODFEDAQConfig::fetchNextId(): "+e.getMessage()));
00056 }
00057
00058 }
00059
00060 void ODFEDAQConfig::prepareWrite()
00061 throw(runtime_error)
00062 {
00063 this->checkConnection();
00064 int next_id=fetchNextId();
00065
00066 try {
00067 m_writeStmt = m_conn->createStatement();
00068 m_writeStmt->setSQL("INSERT INTO FE_DAQ_CONFIG ( config_id, tag, version, ped_id, "
00069 " del_id, wei_id, user_comment ) "
00070 "VALUES ( "
00071 " :1, :2, :3, :4, :5, :6, :7 )" );
00072
00073 m_writeStmt->setInt(1, next_id);
00074 m_ID=next_id;
00075
00076 } catch (SQLException &e) {
00077 throw(runtime_error("ODFEDAQConfig::prepareWrite(): "+e.getMessage()));
00078 }
00079
00080 }
00081
00082 void ODFEDAQConfig::setParameters(std::map<string,string> my_keys_map){
00083
00084
00085
00086
00087 for( std::map<std::string, std::string >::iterator ci=
00088 my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
00089
00090 if(ci->first== "VERSION") setVersion(atoi(ci->second.c_str()) );
00091 if(ci->first== "PED_ID") setPedestalId(atoi(ci->second.c_str()) );
00092 if(ci->first== "DEL_ID") setDelayId(atoi(ci->second.c_str()));
00093 if(ci->first== "WEI_ID") setWeightId(atoi(ci->second.c_str()));
00094 if(ci->first== "COMMENT" || ci->first== "USER_COMMENT") setComment(ci->second);
00095
00096 }
00097
00098 }
00099
00100 void ODFEDAQConfig::writeDB()
00101 throw(runtime_error)
00102 {
00103 this->checkConnection();
00104 this->checkPrepare();
00105
00106 try {
00107
00108
00109 m_writeStmt->setString(2, this->getConfigTag());
00110 m_writeStmt->setInt(3, this->getVersion());
00111 m_writeStmt->setInt(4, this->getPedestalId());
00112 m_writeStmt->setInt(5, this->getDelayId());
00113 m_writeStmt->setInt(6, this->getWeightId());
00114 m_writeStmt->setString(7, this->getComment());
00115
00116 m_writeStmt->executeUpdate();
00117
00118
00119 } catch (SQLException &e) {
00120 throw(runtime_error("ODFEDAQConfig::writeDB(): "+e.getMessage()));
00121 }
00122
00123 if (!this->fetchID()) {
00124 throw(runtime_error("ODFEDAQConfig::writeDB: Failed to write"));
00125 }
00126
00127
00128 }
00129
00130
00131 void ODFEDAQConfig::fetchData(ODFEDAQConfig * result)
00132 throw(runtime_error)
00133 {
00134 this->checkConnection();
00135 result->clear();
00136 if(result->getId()==0 && (result->getConfigTag()=="") ){
00137 throw(runtime_error("ODFEDAQConfig::fetchData(): no Id defined for this ODFEDAQConfig "));
00138 }
00139
00140 try {
00141
00142 m_readStmt->setSQL("SELECT * FROM " + getTable() +
00143 " where ( config_id = :1 or (tag=:2 AND version=:3 ) )" );
00144 m_readStmt->setInt(1, result->getId());
00145 m_readStmt->setString(2, result->getConfigTag());
00146 m_readStmt->setInt(3, result->getVersion());
00147 ResultSet* rset = m_readStmt->executeQuery();
00148
00149 rset->next();
00150
00151
00152
00153 result->setId(rset->getInt(1));
00154 result->setConfigTag(rset->getString(2));
00155 result->setVersion(rset->getInt(3));
00156
00157 result->setPedestalId( rset->getInt(4) );
00158 result->setDelayId( rset->getInt(5) );
00159 result->setWeightId( rset->getInt(6) );
00160 result->setComment( rset->getString(7) );
00161
00162 } catch (SQLException &e) {
00163 throw(runtime_error("ODFEDAQConfig::fetchData(): "+e.getMessage()));
00164 }
00165 }
00166
00167 int ODFEDAQConfig::fetchID() throw(std::runtime_error)
00168 {
00169
00170 if (m_ID!=0) {
00171 return m_ID;
00172 }
00173
00174 this->checkConnection();
00175
00176 try {
00177 Statement* stmt = m_conn->createStatement();
00178 stmt->setSQL("SELECT config_id FROM "+ getTable()+
00179 "WHERE tag=:1 and version=:2 " );
00180
00181 stmt->setString(1, getConfigTag() );
00182 stmt->setInt(2, getVersion() );
00183
00184 ResultSet* rset = stmt->executeQuery();
00185
00186 if (rset->next()) {
00187 m_ID = rset->getInt(1);
00188 } else {
00189 m_ID = 0;
00190 }
00191 m_conn->terminateStatement(stmt);
00192 } catch (SQLException &e) {
00193 throw(runtime_error("ODFEDAQConfig::fetchID: "+e.getMessage()));
00194 }
00195
00196 return m_ID;
00197 }