00001 #include <stdexcept>
00002 #include <string>
00003 #include "OnlineDB/Oracle/interface/Oracle.h"
00004
00005 #include "OnlineDB/EcalCondDB/interface/FEConfigTimingDat.h"
00006 #include "OnlineDB/EcalCondDB/interface/FEConfigTimingInfo.h"
00007
00008 using namespace std;
00009 using namespace oracle::occi;
00010
00011 FEConfigTimingDat::FEConfigTimingDat()
00012 {
00013 m_env = NULL;
00014 m_conn = NULL;
00015 m_writeStmt = NULL;
00016 m_readStmt = NULL;
00017
00018 m_par1 = 0;
00019 m_par2 = 0;
00020
00021 }
00022
00023
00024
00025 FEConfigTimingDat::~FEConfigTimingDat()
00026 {
00027 }
00028
00029
00030
00031 void FEConfigTimingDat::prepareWrite()
00032 throw(std::runtime_error)
00033 {
00034 this->checkConnection();
00035
00036 try {
00037 m_writeStmt = m_conn->createStatement();
00038 m_writeStmt->setSQL("INSERT INTO "+getTable()+" (tim_conf_id, logic_id, "
00039 "time_par1, time_par2 ) VALUES (:tim_conf_id, :logic_id, :time_par1, :time_par2 )" );
00040 } catch (SQLException &e) {
00041 throw(std::runtime_error("FEConfigTimingDat::prepareWrite(): "+e.getMessage()));
00042 }
00043 }
00044
00045
00046 void FEConfigTimingDat::writeDB(const EcalLogicID* ecid, const FEConfigTimingDat* item, FEConfigTimingInfo* iconf)
00047 throw(std::runtime_error)
00048 {
00049 this->checkConnection();
00050 this->checkPrepare();
00051
00052 int iconfID = iconf->fetchID();
00053 if (!iconfID) { throw(std::runtime_error("FEConfigTimingDat::writeDB: ICONF not in DB")); }
00054
00055 int logicID = ecid->getLogicID();
00056 if (!logicID) { throw(std::runtime_error("FEConfigTimingDat::writeDB: Bad EcalLogicID")); }
00057
00058 try {
00059 m_writeStmt->setInt(1, iconfID);
00060 m_writeStmt->setInt(2, logicID);
00061 m_writeStmt->setInt(3, item->getTimingPar1());
00062 m_writeStmt->setInt(4, item->getTimingPar2());
00063
00064 m_writeStmt->executeUpdate();
00065 } catch (SQLException &e) {
00066 throw(std::runtime_error("FEConfigTimingDat::writeDB(): "+e.getMessage()));
00067 }
00068 }
00069
00070
00071
00072 void FEConfigTimingDat::fetchData(map< EcalLogicID, FEConfigTimingDat >* fillMap, FEConfigTimingInfo* iconf)
00073 throw(std::runtime_error)
00074 {
00075 this->checkConnection();
00076 fillMap->clear();
00077
00078 iconf->setConnection(m_env, m_conn);
00079 int iconfID = iconf->fetchID();
00080 if (!iconfID) {
00081 return;
00082 }
00083
00084 try {
00085
00086 m_readStmt->setSQL("SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
00087 "d.time_par1, d.time_par2 "
00088 "FROM channelview cv JOIN " +getTable() + " d ON cv.logic_id = d.logic_id AND cv.name = cv.maps_to "
00089 "WHERE tim_conf_id = :tim_conf_id");
00090 m_readStmt->setInt(1, iconfID);
00091 ResultSet* rset = m_readStmt->executeQuery();
00092
00093 std::pair< EcalLogicID, FEConfigTimingDat > p;
00094 FEConfigTimingDat dat;
00095 while(rset->next()) {
00096 p.first = EcalLogicID( rset->getString(1),
00097 rset->getInt(2),
00098 rset->getInt(3),
00099 rset->getInt(4),
00100 rset->getInt(5),
00101 rset->getString(6));
00102
00103 dat.setTimingPar1( rset->getInt(7) );
00104 dat.setTimingPar2( rset->getInt(8) );
00105
00106 p.second = dat;
00107 fillMap->insert(p);
00108 }
00109 } catch (SQLException &e) {
00110 throw(std::runtime_error("FEConfigTimingDat::fetchData: "+e.getMessage()));
00111 }
00112 }
00113
00114 void FEConfigTimingDat::writeArrayDB(const std::map< EcalLogicID, FEConfigTimingDat >* data, FEConfigTimingInfo* iconf)
00115 throw(std::runtime_error)
00116 {
00117 this->checkConnection();
00118 this->checkPrepare();
00119
00120 int iconfID = iconf->fetchID();
00121 if (!iconfID) { throw(std::runtime_error("FEConfigTimingDat::writeArrayDB: tim_conf_id not in DB")); }
00122
00123
00124 int nrows=data->size();
00125 int* ids= new int[nrows];
00126 int* iconfid_vec= new int[nrows];
00127 int* xx= new int[nrows];
00128 int* yy= new int[nrows];
00129
00130 ub2* ids_len= new ub2[nrows];
00131 ub2* iconf_len= new ub2[nrows];
00132 ub2* x_len= new ub2[nrows];
00133 ub2* y_len= new ub2[nrows];
00134
00135 const EcalLogicID* channel;
00136 const FEConfigTimingDat* dataitem;
00137 int count=0;
00138 typedef map< EcalLogicID, FEConfigTimingDat >::const_iterator CI;
00139 for (CI p = data->begin(); p != data->end(); ++p) {
00140 channel = &(p->first);
00141 int logicID = channel->getLogicID();
00142 if (!logicID) { throw(std::runtime_error("FEConfigTimingDat::writeArrayDB: Bad EcalLogicID")); }
00143 ids[count]=logicID;
00144 iconfid_vec[count]=iconfID;
00145
00146 dataitem = &(p->second);
00147
00148 int x=dataitem->getTimingPar1();
00149 int y=dataitem->getTimingPar2();
00150
00151 xx[count]=x;
00152 yy[count]=y;
00153
00154 ids_len[count]=sizeof(ids[count]);
00155 iconf_len[count]=sizeof(iconfid_vec[count]);
00156
00157 x_len[count]=sizeof(xx[count]);
00158 y_len[count]=sizeof(yy[count]);
00159
00160 count++;
00161 }
00162
00163
00164 try {
00165 m_writeStmt->setDataBuffer(1, (dvoid*)iconfid_vec, OCCIINT, sizeof(iconfid_vec[0]),iconf_len);
00166 m_writeStmt->setDataBuffer(2, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_len );
00167 m_writeStmt->setDataBuffer(3, (dvoid*)xx, OCCIINT , sizeof(xx[0]), x_len );
00168 m_writeStmt->setDataBuffer(4, (dvoid*)yy, OCCIINT , sizeof(yy[0]), y_len );
00169
00170 m_writeStmt->executeArrayUpdate(nrows);
00171
00172 delete [] ids;
00173 delete [] iconfid_vec;
00174 delete [] xx;
00175 delete [] yy;
00176
00177 delete [] ids_len;
00178 delete [] iconf_len;
00179 delete [] x_len;
00180 delete [] y_len;
00181
00182 } catch (SQLException &e) {
00183 throw(std::runtime_error("FEConfigTimingDat::writeArrayDB(): "+e.getMessage()));
00184 }
00185 }