00001 #include <stdexcept>
00002 #include <string>
00003 #include "OnlineDB/Oracle/interface/Oracle.h"
00004
00005 #include "OnlineDB/EcalCondDB/interface/FEConfigFgrParamDat.h"
00006 #include "OnlineDB/EcalCondDB/interface/FEConfigFgrInfo.h"
00007
00008 using namespace std;
00009 using namespace oracle::occi;
00010
00011 FEConfigFgrParamDat::FEConfigFgrParamDat()
00012 {
00013 m_env = NULL;
00014 m_conn = NULL;
00015 m_writeStmt = NULL;
00016 m_readStmt = NULL;
00017
00018 m_fglowthresh = 0;
00019 m_fghighthresh = 0;
00020 m_lowratio = 0;
00021 m_highratio = 0;
00022
00023 }
00024
00025
00026
00027 FEConfigFgrParamDat::~FEConfigFgrParamDat()
00028 {
00029 }
00030
00031
00032
00033 void FEConfigFgrParamDat::prepareWrite()
00034 throw(std::runtime_error)
00035 {
00036 this->checkConnection();
00037
00038 try {
00039 m_writeStmt = m_conn->createStatement();
00040 m_writeStmt->setSQL("INSERT INTO "+ getTable()+" (fgr_conf_id, logic_id, "
00041 " fg_lowthresh, fg_highthresh, fg_lowratio, fg_highratio ) "
00042 "VALUES (:fgr_conf_id, :logic_id, "
00043 " :fg_lowthresh, :fg_highthresh, :fg_lowratio, :fg_highratio )" );
00044 } catch (SQLException &e) {
00045 throw(std::runtime_error("FEConfigFgrParamDat::prepareWrite(): "+e.getMessage()));
00046 }
00047 }
00048
00049
00050
00051 void FEConfigFgrParamDat::writeDB(const EcalLogicID* ecid, const FEConfigFgrParamDat* item, FEConfigFgrInfo* iconf)
00052 throw(std::runtime_error)
00053 {
00054 this->checkConnection();
00055 this->checkPrepare();
00056
00057 int iconfID = iconf->fetchID();
00058 if (!iconfID) { throw(std::runtime_error("FEConfigFgrParamDat::writeDB: ICONF not in DB")); }
00059
00060 int logicID = ecid->getLogicID();
00061 if (!logicID) { throw(std::runtime_error("FEConfigFgrParamDat::writeDB: Bad EcalLogicID")); }
00062
00063 try {
00064 m_writeStmt->setInt(1, iconfID);
00065 m_writeStmt->setInt(2, logicID);
00066 m_writeStmt->setFloat(3, item->getFGlowthresh());
00067 m_writeStmt->setFloat(4, item->getFGhighthresh());
00068 m_writeStmt->setFloat(5, item->getFGlowratio());
00069 m_writeStmt->setFloat(6, item->getFGhighratio());
00070
00071 m_writeStmt->executeUpdate();
00072 } catch (SQLException &e) {
00073 throw(std::runtime_error("FEConfigFgrParamDat::writeDB(): "+e.getMessage()));
00074 }
00075 }
00076
00077
00078
00079 void FEConfigFgrParamDat::fetchData(map< EcalLogicID, FEConfigFgrParamDat >* fillMap, FEConfigFgrInfo* iconf)
00080 throw(std::runtime_error)
00081 {
00082 this->checkConnection();
00083 fillMap->clear();
00084
00085 iconf->setConnection(m_env, m_conn);
00086 int iconfID = iconf->fetchID();
00087 if (!iconfID) {
00088
00089 return;
00090 }
00091
00092 try {
00093
00094 m_readStmt->setSQL("SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
00095 " d.fg_lowthresh, d.fg_highthresh, d.fg_lowratio, d.fg_highratio "
00096 "FROM channelview cv JOIN "+ getTable() +" d "
00097 "ON cv.logic_id = d.logic_id AND cv.name = cv.maps_to "
00098 "WHERE fgr_conf_id = :fgr_conf_id");
00099 m_readStmt->setInt(1, iconfID);
00100 ResultSet* rset = m_readStmt->executeQuery();
00101
00102 std::pair< EcalLogicID, FEConfigFgrParamDat > p;
00103 FEConfigFgrParamDat dat;
00104 while(rset->next()) {
00105 p.first = EcalLogicID( rset->getString(1),
00106 rset->getInt(2),
00107 rset->getInt(3),
00108 rset->getInt(4),
00109 rset->getInt(5),
00110 rset->getString(6));
00111
00112 dat.setFGlowthresh( rset->getFloat(7) );
00113 dat.setFGhighthresh( rset->getFloat(8) );
00114 dat.setFGlowratio( rset->getFloat(9) );
00115 dat.setFGhighratio( rset->getFloat(10) );
00116
00117 p.second = dat;
00118 fillMap->insert(p);
00119 }
00120 } catch (SQLException &e) {
00121 throw(std::runtime_error("FEConfigFgrParamDat::fetchData: "+e.getMessage()));
00122 }
00123 }
00124
00125 void FEConfigFgrParamDat::writeArrayDB(const std::map< EcalLogicID, FEConfigFgrParamDat >* data, FEConfigFgrInfo* iconf)
00126 throw(std::runtime_error)
00127 {
00128 this->checkConnection();
00129 this->checkPrepare();
00130
00131 int iconfID = iconf->fetchID();
00132 if (!iconfID) { throw(std::runtime_error("FEConfigFgrParamDat::writeArrayDB: ICONF not in DB")); }
00133
00134
00135 int nrows=data->size();
00136 int* ids= new int[nrows];
00137 int* iov_vec= new int[nrows];
00138 float* ww= new float[nrows];
00139 float* uu= new float[nrows];
00140 float* tt= new float[nrows];
00141 float* st= new float[nrows];
00142
00143 ub2* ids_len= new ub2[nrows];
00144 ub2* iov_len= new ub2[nrows];
00145 ub2* w_len= new ub2[nrows];
00146 ub2* u_len= new ub2[nrows];
00147 ub2* t_len= new ub2[nrows];
00148 ub2* st_len= new ub2[nrows];
00149
00150 const EcalLogicID* channel;
00151 const FEConfigFgrParamDat* dataitem;
00152 int count=0;
00153 typedef map< EcalLogicID, FEConfigFgrParamDat >::const_iterator CI;
00154 for (CI p = data->begin(); p != data->end(); ++p) {
00155 channel = &(p->first);
00156 int logicID = channel->getLogicID();
00157 if (!logicID) { throw(std::runtime_error("FEConfigFgrParamDat::writeArrayDB: Bad EcalLogicID")); }
00158 ids[count]=logicID;
00159 iov_vec[count]=iconfID;
00160
00161 dataitem = &(p->second);
00162
00163 float w=dataitem->getFGlowthresh();
00164 float u=dataitem->getFGhighthresh();
00165 float t=dataitem->getFGlowratio();
00166 float r=dataitem->getFGhighratio();
00167
00168 ww[count]=w;
00169 uu[count]=u;
00170 tt[count]=t;
00171 st[count]=r;
00172
00173
00174 ids_len[count]=sizeof(ids[count]);
00175 iov_len[count]=sizeof(iov_vec[count]);
00176
00177 w_len[count]=sizeof(ww[count]);
00178 u_len[count]=sizeof(uu[count]);
00179 t_len[count]=sizeof(tt[count]);
00180 st_len[count]=sizeof(st[count]);
00181
00182 count++;
00183 }
00184
00185
00186 try {
00187 m_writeStmt->setDataBuffer(1, (dvoid*)iov_vec, OCCIINT, sizeof(iov_vec[0]),iov_len);
00188 m_writeStmt->setDataBuffer(2, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_len );
00189 m_writeStmt->setDataBuffer(3, (dvoid*)ww, OCCIFLOAT , sizeof(ww[0]), w_len );
00190 m_writeStmt->setDataBuffer(4, (dvoid*)uu, OCCIFLOAT , sizeof(uu[0]), u_len );
00191 m_writeStmt->setDataBuffer(5, (dvoid*)tt, OCCIFLOAT , sizeof(tt[0]), t_len );
00192 m_writeStmt->setDataBuffer(6, (dvoid*)st, OCCIFLOAT , sizeof(st[0]), st_len );
00193
00194
00195 m_writeStmt->executeArrayUpdate(nrows);
00196
00197 delete [] ids;
00198 delete [] iov_vec;
00199 delete [] ww;
00200 delete [] uu;
00201 delete [] tt;
00202 delete [] st;
00203
00204 delete [] ids_len;
00205 delete [] iov_len;
00206 delete [] w_len;
00207 delete [] u_len;
00208 delete [] t_len;
00209 delete [] st_len;
00210
00211 } catch (SQLException &e) {
00212 throw(std::runtime_error("FEConfigFgrParamDat::writeArrayDB(): "+e.getMessage()));
00213 }
00214 }