CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MODCCSHFDat.cc
Go to the documentation of this file.
1 #include <stdexcept>
2 #include <string>
3 #include <fstream>
4 #include <iostream>
5 #include <stdio.h>
6 #include <cstring>
7 
9 
12 
13 
14 using namespace std;
15 using namespace oracle::occi;
16 
18 {
19  m_env = NULL;
20  m_conn = NULL;
21  m_writeStmt = NULL;
22  m_readStmt = NULL;
23 
24  // m_clob = 0;
25  m_size=0;
26  m_file="";
27 
28 }
29 
30 
31 
33 {
34 }
35 
36 
38  m_file=x;
39  //try {
40  std::cout<< "file is "<< m_file<<endl;
41  // }catch (Exception &e) {
42  //throw(std::runtime_error("MODCCSHFDat::setFile(): "+e.getMessage()));
43  //}
44  // here we must open the file and read the CCS Clob
45  std::cout << "Going to read CCS file: " << m_file << endl;
46  ifstream inpFile;
47  inpFile.open(m_file.c_str());
48  // tell me size of file
49  int bufsize = 0;
50  inpFile.seekg( 0,ios::end );
51  bufsize = inpFile.tellg();
52  std::cout <<" bufsize ="<<bufsize<< std::endl;
53  // set file pointer to start again
54  inpFile.seekg( 0,ios::beg );
55  m_size=bufsize;
56  inpFile.close();
57  std::cout << "Going to read CCS file: " << m_file << endl;
58 
59 }
60 
62  throw(std::runtime_error)
63 {
64  this->checkConnection();
65 
66  try {
67  m_writeStmt = m_conn->createStatement();
68  m_writeStmt->setSQL("INSERT INTO OD_CCS_HF_dat (iov_id, logic_id, "
69  "ccs_log) "
70  "VALUES (:iov_id, :logic_id, "
71  ":ccs_log )");
72 
73 
74  } catch (SQLException &e) {
75  throw(std::runtime_error("MODCCSHFDat::prepareWrite(): "+e.getMessage()));
76  }
77 }
78 
79 
80 
81 void MODCCSHFDat::writeDB(const EcalLogicID* ecid, const MODCCSHFDat* item, MODRunIOV* iov )
82  throw(std::runtime_error)
83 {
84 
85 
86 
87  this->checkConnection();
88  this->checkPrepare();
89 
90  int iovID = iov->fetchID();
91  if (!iovID) { throw(std::runtime_error("MODCCSHFDat::writeDB: IOV not in DB")); }
92 
93  int logicID = ecid->getLogicID();
94  if (!logicID) { throw(std::runtime_error("MODCCSHFDat::writeDB: Bad EcalLogicID")); }
95 
96  std::string fname=item->getFile();
97  std::cout << "Going to read CCS file: " << fname << endl;
98 
99 
100  try {
101 
102  m_writeStmt->setInt(1, iovID);
103  m_writeStmt->setInt(2, logicID);
104 
105  // and now the clob
106  oracle::occi::Clob clob(m_conn);
107  clob.setEmpty();
108  m_writeStmt->setClob(3,clob);
109  m_writeStmt->executeUpdate ();
110 
111  m_conn->terminateStatement(m_writeStmt);
112  std::cout<<"empty CCS Clob inserted into DB" <<std::endl;
113 
114  // now we read and update it
115  m_writeStmt = m_conn->createStatement();
116  m_writeStmt->setSQL ("SELECT CCS_LOG FROM OD_CCS_HF_DAT WHERE"
117  " iov_ID=:1 and logic_ID=:2 FOR UPDATE");
118  m_writeStmt->setInt(1, iovID);
119  m_writeStmt->setInt(2, logicID);
120  ResultSet* rset = m_writeStmt->executeQuery();
121  rset->next ();
122  oracle::occi::Clob clob2 = rset->getClob (1);
123  cout << "Opening the clob in read write mode" << endl;
124 
125  unsigned int clob_size=item->getSize();
126 
127  populateClob (clob2 , fname, clob_size);
128 
129  int clobLength=clob2.length ();
130  cout << "Length of the clob is: " << clobLength << endl;
131 
132  m_writeStmt->executeUpdate();
133  m_writeStmt->closeResultSet (rset);
134 
135  } catch (SQLException &e) {
136  throw(std::runtime_error("MODCCSHFDat::writeDB(): "+e.getMessage()));
137  }
138 }
139 
140 
141 
142 void MODCCSHFDat::fetchData(std::map< EcalLogicID, MODCCSHFDat >* fillMap, MODRunIOV* iov)
143  throw(std::runtime_error)
144 {
145  this->checkConnection();
146  fillMap->clear();
147 
148  iov->setConnection(m_env, m_conn);
149  int iovID = iov->fetchID();
150  if (!iovID) {
151  // throw(std::runtime_error("MODCCSHFDat::writeDB: IOV not in DB"));
152  return;
153  }
154 
155  try {
156 
157  m_readStmt->setSQL("SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
158  " d.ccs_log "
159  "FROM channelview cv JOIN OD_CCS_HF_dat d "
160  "ON cv.logic_id = d.logic_id AND cv.name = cv.maps_to "
161  "WHERE d.iov_id = :iov_id");
162  m_readStmt->setInt(1, iovID);
163  ResultSet* rset = m_readStmt->executeQuery();
164 
165  std::pair< EcalLogicID, MODCCSHFDat > p;
166  MODCCSHFDat dat;
167  while(rset->next()) {
168  p.first = EcalLogicID( rset->getString(1), // name
169  rset->getInt(2), // logic_id
170  rset->getInt(3), // id1
171  rset->getInt(4), // id2
172  rset->getInt(5), // id3
173  rset->getString(6)); // maps_to
174  // to be corrected
175  // dat.setClob( rset->getString(7) );
176 
177  p.second = dat;
178  fillMap->insert(p);
179  }
180  } catch (SQLException &e) {
181  throw(std::runtime_error("MODCCSHFDat::fetchData(): "+e.getMessage()));
182  }
183 }
184 
185 void MODCCSHFDat::writeArrayDB(const std::map< EcalLogicID, MODCCSHFDat >* data, MODRunIOV* iov)
186  throw(std::runtime_error)
187 {
188  this->checkConnection();
189  this->checkPrepare();
190 
191  int iovID = iov->fetchID();
192  if (!iovID) { throw(std::runtime_error("MODCCSHFDat::writeArrayDB: IOV not in DB")); }
193 
194 
195  int nrows=data->size();
196  int* ids= new int[nrows];
197  int* iovid_vec= new int[nrows];
198  int* xx= new int[nrows];
199 
200  ub2* ids_len= new ub2[nrows];
201  ub2* iov_len= new ub2[nrows];
202  ub2* x_len= new ub2[nrows];
203 
204  const EcalLogicID* channel;
205  //const MODCCSHFDat* dataitem;
206  int count=0;
207  typedef map< EcalLogicID, MODCCSHFDat >::const_iterator CI;
208  for (CI p = data->begin(); p != data->end(); ++p) {
209  channel = &(p->first);
210  int logicID = channel->getLogicID();
211  if (!logicID) { throw(std::runtime_error("MODCCSHFDat::writeArrayDB: Bad EcalLogicID")); }
212  ids[count]=logicID;
213  iovid_vec[count]=iovID;
214 
215  // dataitem = &(p->second);
216  // dataIface.writeDB( channel, dataitem, iov);
217  // to be corrected
218 
219  int x=0;
220  //=dataitem->getWord();
221 
222  xx[count]=x;
223 
224  ids_len[count]=sizeof(ids[count]);
225  iov_len[count]=sizeof(iovid_vec[count]);
226 
227  x_len[count]=sizeof(xx[count]);
228 
229  count++;
230  }
231 
232 
233  try {
234  m_writeStmt->setDataBuffer(1, (dvoid*)iovid_vec, OCCIINT, sizeof(iovid_vec[0]),iov_len);
235  m_writeStmt->setDataBuffer(2, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_len );
236  m_writeStmt->setDataBuffer(3, (dvoid*)xx, OCCIINT , sizeof(xx[0]), x_len );
237 
238  m_writeStmt->executeArrayUpdate(nrows);
239 
240  delete [] ids;
241  delete [] iovid_vec;
242  delete [] xx;
243 
244 
245  delete [] ids_len;
246  delete [] iov_len;
247  delete [] x_len;
248 
249 
250 
251  } catch (SQLException &e) {
252  throw(std::runtime_error("MonPedestalsDat::writeArrayDB(): "+e.getMessage()));
253  }
254 }
255 
256 
257 
258 void MODCCSHFDat::populateClob (Clob &clob, std::string fname, unsigned int clob_size )
259  throw (std::runtime_error)
260 {
261 
262  try{
263  // Uses stream here
264  cout << "Populating the Clob using writeBuffer(Stream) method" << endl;
265  std::cout<<"we are here0"<<std::endl;
266 
267  char *file = (char *)fname.c_str();
268  std::cout<<"we are here0.5 file is:"<<fname<<std::endl;
269 
270  ifstream inFile;
271  inFile.open(file,ios::in);
272  if (!inFile)
273  {
274  cout << fname<<" file not found\n";
275  inFile.close();
276 
277  std::string fname2="/u1/fra/null_file.txt";
278  inFile.open((char*)fname2.c_str(),ios::in);
279 
280 
281 
282  }
283  if(clob_size==0){
284 
285 
286  inFile.seekg( 0,ios::end );
287  clob_size = inFile.tellg();
288  std::cout <<" bufsize ="<<clob_size<< std::endl;
289  // set file pointer to start again
290  inFile.seekg( 0,ios::beg );
291 
292  }
293 
294  char *buffer = new char[clob_size + 1];
295 
296 
297  std::cout<<"we are here1"<<std::endl;
298  unsigned int size;
299  Stream *strm=clob.getStream();
300  std::cout<<"we are here2"<<std::endl;
301  // while(inFile)
302  // {
303  int buf=0;
304  memset (buffer, buf, clob_size + 1);
305  inFile.read(buffer,clob_size);
306  std::cout<<"we are here2.5"<<std::endl;
307 
308  strm->writeBuffer(buffer,strlen(buffer));
309  std::cout<<"we are here2.6"<<std::endl;
310 
311  //}
312  std::cout<<"we are here3"<<std::endl;
313  strcpy(buffer," ");
314  size=strlen(buffer);
315  strm->writeLastBuffer(buffer,size);
316  clob.closeStream(strm);
317  inFile.close();
318  std::cout<<"we are here4"<<std::endl;
319  delete[] buffer;
320 
321 
322  }catch (SQLException &e) {
323  throw(std::runtime_error("populateClob(): "+e.getMessage()));
324  }
325 
326  cout << "Populating the Clob - Success" << endl;
327 }
328 
329 
330 unsigned char* MODCCSHFDat::readClob (oracle::occi::Clob &clob, int size)
331  throw (std::runtime_error)
332 {
333 
334  try{
335  Stream *instream = clob.getStream (1,0);
336  unsigned char *buffer= new unsigned char[size];
337  int buf=0;
338  memset (buffer, buf, size);
339 
340  instream->readBuffer ((char*)buffer, size);
341  cout << "remember to delete the char* at the end of the program ";
342  for (int i = 0; i < size; ++i)
343  cout << (char) buffer[i];
344  cout << endl;
345 
346 
347  clob.closeStream (instream);
348 
349  return buffer;
350 
351  }catch (SQLException &e) {
352  throw(std::runtime_error("readClob(): "+e.getMessage()));
353  }
354 
355 }
int i
Definition: DBlmapReader.cc:9
oracle::occi::Clob Clob
Definition: MODCCSHFDat.h:21
#define NULL
Definition: scimark2.h:8
oracle::occi::SQLException SQLException
Definition: HcalDbOmds.cc:27
void writeDB(const EcalLogicID *ecid, const MODCCSHFDat *item, MODRunIOV *iov)
Definition: MODCCSHFDat.cc:81
tuple iov
Definition: o2o.py:307
void writeArrayDB(const std::map< EcalLogicID, MODCCSHFDat > *data, MODRunIOV *iov)
Definition: MODCCSHFDat.cc:185
void fetchData(std::map< EcalLogicID, MODCCSHFDat > *fillMap, MODRunIOV *iov)
Definition: MODCCSHFDat.cc:142
#define end
Definition: vmac.h:37
int getLogicID() const
Definition: EcalLogicID.cc:41
void setFile(std::string x)
Definition: MODCCSHFDat.cc:37
oracle::occi::ResultSet ResultSet
Definition: HcalDbOmds.cc:26
string fname
main script
unsigned char * readClob(Clob &clob, int size)
Definition: MODCCSHFDat.cc:330
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
tuple cout
Definition: gather_cfg.py:145
void prepareWrite()
Definition: MODCCSHFDat.cc:61
tuple size
Write out results.
void populateClob(Clob &clob, std::string fname, unsigned int clob_size)
Definition: MODCCSHFDat.cc:258