CMS 3D CMS Logo

ODLTCConfig.cc
Go to the documentation of this file.
1 #include <fstream>
2 #include <iostream>
3 #include <cstdio>
4 #include <stdexcept>
5 #include <string>
7 
9 
10 using namespace std;
11 using namespace oracle::occi;
12 
14 {
15  m_env = nullptr;
16  m_conn = nullptr;
17  m_writeStmt = nullptr;
18  m_readStmt = nullptr;
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 
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(std::string("ODLTCConfig::fetchNextId(): ")+getOraMessage(&e)));
51  }
52 
53 }
54 
55 
56 
57 
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(std::string("ODLTCConfig::prepareWrite(): ")+getOraMessage(&e)));
96  }
97 
98  std::cout<<"updating the clob 1 "<<std::endl;
99 
100 }
101 
102 void ODLTCConfig::setParameters(const 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 >::const_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  noexcept(false)
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(std::string("ODLTCConfig::writeDB(): ")+getOraMessage(&e)));
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  noexcept(false)
201 {
202  this->checkConnection();
203  result->clear();
204  if(result->getId()==0 && result->getConfigTag().empty()){
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(getOraString(rset,2));
222  result->setLTCConfigurationFile(getOraString(rset,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(std::string("ODLTCConfig::fetchData(): ")+getOraMessage(&e)));
243  }
244 }
245 
246 
247 
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(std::string("ODLTCConfig::fetchID: ")+getOraMessage(&e)));
274  }
275 
276  return m_ID;
277 }
278 
void prepareWrite() noexcept(false) override
Definition: ODLTCConfig.cc:58
void writeDB() noexcept(false)
Definition: ODLTCConfig.cc:148
#define noexcept
void fetchData(ODLTCConfig *result) noexcept(false)
Definition: ODLTCConfig.cc:199
void clear(CLHEP::HepGenMatrix &m)
Helper function: Reset all elements of a matrix to 0.
Definition: matutil.cc:167
~ODLTCConfig() override
Definition: ODLTCConfig.cc:29
int fetchID() noexcept(false)
Definition: ODLTCConfig.cc:248
#define end
Definition: vmac.h:39
oracle::occi::Statement Statement
Definition: IODConfig.h:23
oracle::occi::Clob Clob
Definition: IODConfig.h:25
void clear()
Definition: ODLTCConfig.cc:190
string fname
main script
oracle::occi::SQLException SQLException
Definition: IODConfig.h:22
int fetchNextId() noexcept(false)
Definition: ODLTCConfig.cc:34
void setParameters(const std::map< std::string, std::string > &my_keys_map)
Definition: ODLTCConfig.cc:102