00001 #include <stdexcept>
00002 #include <string>
00003 #include "OnlineDB/Oracle/interface/Oracle.h"
00004
00005 #include "OnlineDB/EcalCondDB/interface/ODWeightsDat.h"
00006
00007 using namespace std;
00008 using namespace oracle::occi;
00009
00010 ODWeightsDat::ODWeightsDat()
00011 {
00012 m_env = NULL;
00013 m_conn = NULL;
00014 m_writeStmt = NULL;
00015 m_readStmt = NULL;
00016
00017 m_sm = 0;
00018 m_fed = 0;
00019 m_tt = 0;
00020 m_xt = 0;
00021
00022 }
00023
00024
00025
00026 ODWeightsDat::~ODWeightsDat()
00027 {
00028 }
00029
00030
00031
00032 void ODWeightsDat::prepareWrite()
00033 throw(std::runtime_error)
00034 {
00035 this->checkConnection();
00036
00037 try {
00038 m_writeStmt = m_conn->createStatement();
00039 m_writeStmt->setSQL("INSERT INTO "+getTable()+" (rec_id, sm_id, fed_id, tt_id, cry_id, wei0, wei1, wei2, wei3, wei4, wei5 ) "
00040 "VALUES (:1, :2, :3, :4, :5, :6, :7, :8 , :9, :10, :11 )");
00041 } catch (SQLException &e) {
00042 throw(std::runtime_error("ODWeightsDat::prepareWrite(): "+e.getMessage()));
00043 }
00044 }
00045
00046
00047
00048 void ODWeightsDat::writeDB(const ODWeightsDat* item, ODFEWeightsInfo* iov )
00049 throw(std::runtime_error)
00050 {
00051 this->checkConnection();
00052
00053 try {
00054 m_writeStmt->setInt(1, item->getId());
00055 m_writeStmt->setInt(2, item->getSMId());
00056 m_writeStmt->setInt(3, item->getFedId() );
00057 m_writeStmt->setInt(4, item->getTTId() );
00058 m_writeStmt->setInt(5, item->getCrystalId() );
00059
00060 m_writeStmt->setFloat(6, item->getWeight0() );
00061 m_writeStmt->setFloat(7, item->getWeight1() );
00062 m_writeStmt->setFloat(8, item->getWeight2() );
00063 m_writeStmt->setFloat(9, item->getWeight3() );
00064 m_writeStmt->setFloat(10, item->getWeight4() );
00065 m_writeStmt->setFloat(11, item->getWeight5() );
00066
00067 m_writeStmt->executeUpdate();
00068 } catch (SQLException &e) {
00069 throw(std::runtime_error("ODWeightsDat::writeDB(): "+e.getMessage()));
00070 }
00071 }
00072
00073
00074
00075 void ODWeightsDat::fetchData(std::vector< ODWeightsDat >* p, ODFEWeightsInfo* iov)
00076 throw(std::runtime_error)
00077 {
00078 this->checkConnection();
00079
00080 iov->setConnection(m_env, m_conn);
00081 int iovID = iov->fetchID();
00082 if (!iovID) {
00083 std::cout <<"ID not in the DB"<< endl;
00084 return;
00085 }
00086
00087 try {
00088 m_readStmt->setSQL("SELECT * FROM " + getTable() + " WHERE rec_id = :rec_id order by sm_id, fed_id, tt_id, cry_id");
00089 m_readStmt->setInt(1, iovID);
00090 ResultSet* rset = m_readStmt->executeQuery();
00091
00092
00093 ODWeightsDat dat;
00094 while(rset->next()) {
00095
00096 dat.setSMId( rset->getInt(2) );
00097 dat.setFedId( rset->getInt(3) );
00098 dat.setTTId( rset->getInt(4) );
00099 dat.setCrystalId( rset->getInt(5) );
00100 dat.setWeight0( rset->getFloat(6) );
00101 dat.setWeight1( rset->getFloat(7) );
00102 dat.setWeight2( rset->getFloat(8) );
00103 dat.setWeight3( rset->getFloat(9) );
00104 dat.setWeight4( rset->getFloat(10) );
00105 dat.setWeight5( rset->getFloat(11) );
00106
00107 p->push_back( dat);
00108
00109 }
00110
00111
00112 } catch (SQLException &e) {
00113 throw(std::runtime_error("ODWeightsDat::fetchData(): "+e.getMessage()));
00114 }
00115 }
00116
00117
00118
00119 void ODWeightsDat::writeArrayDB(const std::vector< ODWeightsDat > data, ODFEWeightsInfo* iov)
00120 throw(std::runtime_error)
00121 {
00122 this->checkConnection();
00123
00124 int iovID = iov->fetchID();
00125 if (!iovID) { throw(std::runtime_error("ODDelays::writeArrayDB: ODFEDelaysInfo not in DB")); }
00126
00127
00128 int nrows=data.size();
00129 int* ids= new int[nrows];
00130 int* xx= new int[nrows];
00131 int* yy= new int[nrows];
00132 int* zz= new int[nrows];
00133 int* st= new int[nrows];
00134 float* xx1= new float[nrows];
00135 float* yy1= new float[nrows];
00136 float* zz1= new float[nrows];
00137 float* xx2= new float[nrows];
00138 float* yy2= new float[nrows];
00139 float* zz2= new float[nrows];
00140
00141
00142 ub2* ids_len= new ub2[nrows];
00143 ub2* x_len= new ub2[nrows];
00144 ub2* y_len= new ub2[nrows];
00145 ub2* z_len= new ub2[nrows];
00146 ub2* st_len= new ub2[nrows];
00147 ub2* x1_len= new ub2[nrows];
00148 ub2* y1_len= new ub2[nrows];
00149 ub2* z1_len= new ub2[nrows];
00150 ub2* x2_len= new ub2[nrows];
00151 ub2* y2_len= new ub2[nrows];
00152 ub2* z2_len= new ub2[nrows];
00153
00154 ODWeightsDat dataitem;
00155
00156 int n_data= (int) data.size();
00157 for (int count = 0; count <n_data ; count++) {
00158 dataitem=data[count];
00159 ids[count]=iovID;
00160 xx[count]=dataitem.getSMId();
00161 yy[count]=dataitem.getFedId();
00162 zz[count]=dataitem.getTTId();
00163 st[count]=dataitem.getCrystalId();
00164 xx1[count]=dataitem.getWeight0();
00165 yy1[count]=dataitem.getWeight1();
00166 zz1[count]=dataitem.getWeight2();
00167 xx2[count]=dataitem.getWeight3();
00168 yy2[count]=dataitem.getWeight4();
00169 zz2[count]=dataitem.getWeight5();
00170
00171
00172 ids_len[count]=sizeof(ids[count]);
00173 x_len[count]=sizeof(xx[count]);
00174 y_len[count]=sizeof(yy[count]);
00175 z_len[count]=sizeof(zz[count]);
00176 st_len[count]=sizeof(st[count]);
00177 x1_len[count]=sizeof(xx1[count]);
00178 y1_len[count]=sizeof(yy1[count]);
00179 z1_len[count]=sizeof(zz1[count]);
00180 x2_len[count]=sizeof(xx2[count]);
00181 y2_len[count]=sizeof(yy2[count]);
00182 z2_len[count]=sizeof(zz2[count]);
00183
00184 }
00185
00186
00187 try {
00188 m_writeStmt->setDataBuffer(1, (dvoid*)ids, OCCIINT, sizeof(ids[0]),ids_len);
00189 m_writeStmt->setDataBuffer(2, (dvoid*)xx, OCCIINT , sizeof(xx[0]), x_len );
00190 m_writeStmt->setDataBuffer(3, (dvoid*)yy, OCCIINT , sizeof(yy[0]), y_len );
00191 m_writeStmt->setDataBuffer(4, (dvoid*)zz, OCCIINT , sizeof(zz[0]), z_len );
00192 m_writeStmt->setDataBuffer(5, (dvoid*)st, OCCIINT , sizeof(st[0]), st_len );
00193 m_writeStmt->setDataBuffer(6, (dvoid*)xx1, OCCIFLOAT , sizeof(xx1[0]), x1_len );
00194 m_writeStmt->setDataBuffer(7, (dvoid*)yy1, OCCIFLOAT , sizeof(yy1[0]), y1_len );
00195 m_writeStmt->setDataBuffer(8, (dvoid*)zz1, OCCIFLOAT , sizeof(zz1[0]), z1_len );
00196 m_writeStmt->setDataBuffer(9, (dvoid*)xx2, OCCIFLOAT , sizeof(xx2[0]), x2_len );
00197 m_writeStmt->setDataBuffer(10, (dvoid*)yy2, OCCIFLOAT , sizeof(yy2[0]), y2_len );
00198 m_writeStmt->setDataBuffer(11, (dvoid*)zz2, OCCIFLOAT , sizeof(zz2[0]), z2_len );
00199
00200
00201 m_writeStmt->executeArrayUpdate(nrows);
00202
00203 delete [] ids;
00204 delete [] xx;
00205 delete [] yy;
00206 delete [] zz;
00207 delete [] st;
00208 delete [] xx1;
00209 delete [] yy1;
00210 delete [] zz1;
00211 delete [] xx2;
00212 delete [] yy2;
00213 delete [] zz2;
00214
00215 delete [] ids_len;
00216 delete [] x_len;
00217 delete [] y_len;
00218 delete [] z_len;
00219 delete [] st_len;
00220 delete [] x1_len;
00221 delete [] y1_len;
00222 delete [] z1_len;
00223 delete [] x2_len;
00224 delete [] y2_len;
00225 delete [] z2_len;
00226
00227 } catch (SQLException &e) {
00228 throw(std::runtime_error("ODWeightsDat::writeArrayDB(): "+e.getMessage()));
00229 }
00230 }