test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ODDCCConfig.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 <cstdlib>
7 
9 
11 
12 using namespace std;
13 using namespace oracle::occi;
14 
16 {
17  m_env = NULL;
18  m_conn = NULL;
19  m_writeStmt = NULL;
20  m_readStmt = NULL;
21  m_size=0;
22  m_config_tag="";
23  m_ID=0;
24  m_wei="";
25  clear();
26 
27 }
28 
29 
30 
32 {
33 }
34 
35 int ODDCCConfig::fetchNextId() throw(std::runtime_error) {
36 
37  int result=0;
38  try {
39  this->checkConnection();
40 
41  m_readStmt = m_conn->createStatement();
42  m_readStmt->setSQL("select ecal_dcc_config_sq.NextVal from dual");
43  ResultSet* rset = m_readStmt->executeQuery();
44  while (rset->next ()){
45  result= rset->getInt(1);
46  }
47  m_conn->terminateStatement(m_readStmt);
48  return result;
49 
50  } catch (SQLException &e) {
51  throw(std::runtime_error("ODDCCConfig::fetchNextId(): "+e.getMessage()));
52  }
53 
54 }
55 
56 
57 
58 
60  throw(std::runtime_error)
61 {
62  this->checkConnection();
63 
64  int next_id=fetchNextId();
65 
66  try {
67  m_writeStmt = m_conn->createStatement();
68  m_writeStmt->setSQL("INSERT INTO ECAL_DCC_CONFIGURATION (dcc_configuration_id, dcc_tag, "
69  " DCC_CONFIGURATION_URL, TESTPATTERN_FILE_URL, "
70  " N_TESTPATTERNS_TO_LOAD , SM_HALF, weightsmode, "
71  " dcc_configuration) "
72  "VALUES (:1, :2, :3, :4, :5, :6 , :7 ,:8 )");
73  m_writeStmt->setInt(1, next_id);
74  m_writeStmt->setString(2, getConfigTag());
75  m_writeStmt->setString(3, getDCCConfigurationUrl());
76  m_writeStmt->setString(4, getTestPatternFileUrl());
77  m_writeStmt->setInt(5, getNTestPatternsToLoad());
78  m_writeStmt->setInt(6, getSMHalf());
79  m_writeStmt->setString(7, getDCCWeightsMode());
80 
81  // and now the clob
82  oracle::occi::Clob clob(m_conn);
83  clob.setEmpty();
84  m_writeStmt->setClob(8,clob);
85  m_writeStmt->executeUpdate ();
86  m_ID=next_id;
87 
88  m_conn->terminateStatement(m_writeStmt);
89  std::cout<<"DCC Clob inserted into CONFIGURATION with id="<<next_id<<std::endl;
90 
91  // now we read and update it
92  m_writeStmt = m_conn->createStatement();
93  m_writeStmt->setSQL ("SELECT dcc_configuration FROM ECAL_DCC_CONFIGURATION WHERE"
94  " dcc_configuration_id=:1 FOR UPDATE");
95 
96  std::cout<<"updating the clob 0"<<std::endl;
97 
98 
99 
100  } catch (SQLException &e) {
101  throw(std::runtime_error("ODDCCConfig::prepareWrite(): "+e.getMessage()));
102  }
103 
104  std::cout<<"updating the clob 1 "<<std::endl;
105 
106 
107 }
108 
109 
110 void ODDCCConfig::setParameters(const std::map<string,string>& my_keys_map){
111 
112  // parses the result of the XML parser that is a map of
113  // string string with variable name variable value
114 
115  for( std::map<std::string, std::string >::const_iterator ci=
116  my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
117 
118  if(ci->first== "DCC_CONFIGURATION_ID") setConfigTag(ci->second);
119  if(ci->first== "TESTPATTERN_FILE_URL") setTestPatternFileUrl(ci->second );
120  if(ci->first== "N_TESTPATTERNS_TO_LOAD") setNTestPatternsToLoad(atoi(ci->second.c_str() ));
121  if(ci->first== "SM_HALF") setSMHalf(atoi(ci->second.c_str() ));
122  if(ci->first== "WEIGHTSMODE") setDCCWeightsMode(ci->second.c_str() );
123  if(ci->first== "DCC_CONFIGURATION_URL") {
124  std::string fname=ci->second ;
125  setDCCConfigurationUrl(fname );
126 
127  // here we must open the file and read the DCC Clob
128  std::cout << "Going to read DCC file: " << fname << endl;
129 
130  ifstream inpFile;
131  inpFile.open(fname.c_str());
132 
133  // tell me size of file
134  int bufsize = 0;
135  inpFile.seekg( 0,ios::end );
136  bufsize = inpFile.tellg();
137  std::cout <<" bufsize ="<<bufsize<< std::endl;
138  // set file pointer to start again
139  inpFile.seekg( 0,ios::beg );
140 
141  m_size=bufsize;
142 
143  inpFile.close();
144 
145  }
146  }
147 
148 }
149 
150 
151 
152 
154  throw(std::runtime_error)
155 {
156 
157 
158  std::cout<<"updating the clob "<<std::endl;
159 
160 
161  try {
162 
163 
164  m_writeStmt->setInt(1, m_ID);
165  ResultSet* rset = m_writeStmt->executeQuery();
166 
167  rset->next ();
168  oracle::occi::Clob clob = rset->getClob (1);
169 
170  cout << "Opening the clob in read write mode" << endl;
171 
172  std::cout << "Populating the clob" << endl;
173 
174  populateClob (clob, getDCCConfigurationUrl(), m_size);
175  int clobLength=clob.length ();
176  cout << "Length of the clob is: " << clobLength << endl;
177 
178  m_writeStmt->executeUpdate();
179 
180  m_writeStmt->closeResultSet (rset);
181 
182  } catch (SQLException &e) {
183  throw(std::runtime_error("ODDCCConfig::writeDB(): "+e.getMessage()));
184  }
185  // Now get the ID
186  if (!this->fetchID()) {
187  throw(std::runtime_error("ODDCCConfig::writeDB: Failed to write"));
188  }
189 
190 
191 }
192 
193 
195 
196  m_dcc_url="";
197  m_test_url="";
198  m_ntest=0;
199  m_sm_half=0;
200  m_wei="";
201 
202 }
203 
204 
205 
206 
208  throw(std::runtime_error)
209 {
210  this->checkConnection();
211  // result->clear();
212  if(result->getId()==0 && (result->getConfigTag()=="") ){
213  // throw(std::runtime_error("ODDCCConfig::fetchData(): no Id defined for this ODDCCConfig "));
214  result->fetchID();
215  }
216 
217  try {
218 
219  m_readStmt->setSQL("SELECT * "
220  "FROM ECAL_DCC_CONFIGURATION "
221  " where dcc_configuration_id = :1 or dcc_tag=:2 " );
222  m_readStmt->setInt(1, result->getId());
223  m_readStmt->setString(2, result->getConfigTag());
224  ResultSet* rset = m_readStmt->executeQuery();
225 
226  rset->next();
227 
228  // 1 is the id and 2 is the config tag
229 
230  result->setId(rset->getInt(1));
231  result->setConfigTag(rset->getString(2));
232  result->setDCCConfigurationUrl(rset->getString(3));
233  result->setTestPatternFileUrl(rset->getString(4));
234  result->setNTestPatternsToLoad(rset->getInt(5));
235  result->setSMHalf(rset->getInt(6));
236 
237  Clob clob = rset->getClob (7);
238  m_size = clob.length();
239  Stream *instream = clob.getStream (1,0);
240  unsigned char *buffer = new unsigned char[m_size];
241  memset (buffer, 0, m_size);
242  instream->readBuffer ((char*)buffer, m_size);
243  /*
244  cout << "Opening the clob in Read only mode" << endl;
245  clob.open (OCCI_LOB_READONLY);
246  int clobLength=clob.length ();
247  cout << "Length of the clob is: " << clobLength << endl;
248  m_size=clobLength;
249  unsigned char* buffer = readClob (clob, m_size);
250  clob.close ();
251  cout<< "the clob buffer is:"<<endl;
252  for (int i = 0; i < clobLength; ++i)
253  cout << (char) buffer[i];
254  cout << endl;
255 
256 
257  */
258  result->setDCCClob(buffer );
259  result->setDCCWeightsMode(rset->getString(8));
260 
261 
262  } catch (SQLException &e) {
263  throw(std::runtime_error("ODDCCConfig::fetchData(): "+e.getMessage()));
264  }
265 }
266 
267 
268 
269 int ODDCCConfig::fetchID() throw(std::runtime_error)
270 {
271  if (m_ID!=0) {
272  return m_ID;
273  }
274 
275  this->checkConnection();
276 
277  try {
278  Statement* stmt = m_conn->createStatement();
279  stmt->setSQL("SELECT DCC_configuration_id FROM ecal_dcc_configuration "
280  "WHERE dcc_tag=:dcc_tag "
281  );
282 
283  stmt->setString(1, getConfigTag() );
284 
285 
286  ResultSet* rset = stmt->executeQuery();
287 
288  if (rset->next()) {
289  m_ID = rset->getInt(1);
290  } else {
291  m_ID = 0;
292  }
293  m_conn->terminateStatement(stmt);
294  } catch (SQLException &e) {
295  throw(std::runtime_error("ODDCCConfig::fetchID: "+e.getMessage()));
296  }
297 
298 
299  return m_ID;
300 }
int fetchNextId()
Definition: ODDCCConfig.cc:35
oracle::occi::Stream Stream
Definition: IODConfig.h:24
#define NULL
Definition: scimark2.h:8
void clear(CLHEP::HepGenMatrix &m)
Helper function: Reset all elements of a matrix to 0.
Definition: matutil.cc:167
tuple result
Definition: query.py:137
#define end
Definition: vmac.h:37
oracle::occi::Statement Statement
Definition: IODConfig.h:23
oracle::occi::Clob Clob
Definition: IODConfig.h:25
oracle::occi::ResultSet ResultSet
Definition: HcalDbOmds.cc:26
string fname
main script
oracle::occi::SQLException SQLException
Definition: IODConfig.h:22
void writeDB()
Definition: ODDCCConfig.cc:153
tuple cout
Definition: gather_cfg.py:121
void setParameters(const std::map< std::string, std::string > &my_keys_map)
Definition: ODDCCConfig.cc:110
void fetchData(ODDCCConfig *result)
Definition: ODDCCConfig.cc:207
void clear()
Definition: ODDCCConfig.cc:194
void prepareWrite()
Definition: ODDCCConfig.cc:59