CMS 3D CMS Logo

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