CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ODTTCciConfig.cc
Go to the documentation of this file.
1 #include <fstream>
2 #include <cstdlib>
3 #include <iostream>
4 #include <stdio.h>
5 #include <stdexcept>
6 #include <string>
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_config_tag="";
22  m_configuration_script="";
23  m_configuration_script_params="";
24  m_ID=0;
25  clear();
26  m_size=0;
27 }
28 
30  std::cout <<"entering clear" << std::endl;
31  m_ttcci_file="";
32  m_configuration_script="";
33  m_configuration_script_params="";
34  m_trg_mode="";
35  m_trg_sleep=0;
36 
37 }
38 
40 {
41 }
42 
43 int ODTTCciConfig::fetchNextId() throw(std::runtime_error) {
44 
45  int result=0;
46  try {
47  this->checkConnection();
48 
49  m_readStmt = m_conn->createStatement();
50  m_readStmt->setSQL("select ecal_ttcci_config_sq.NextVal from dual");
51  ResultSet* rset = m_readStmt->executeQuery();
52  while (rset->next ()){
53  result= rset->getInt(1);
54  }
55  m_conn->terminateStatement(m_readStmt);
56  return result;
57 
58  } catch (SQLException &e) {
59  throw(std::runtime_error("ODTTCciConfig::fetchNextId(): "+e.getMessage()));
60  }
61 
62 }
63 
64 
65 
66 
68  throw(std::runtime_error)
69 {
70  this->checkConnection();
71 
72  int next_id=fetchNextId();
73 
74  try {
75  m_writeStmt = m_conn->createStatement();
76  m_writeStmt->setSQL("INSERT INTO ECAL_TTCci_CONFIGURATION (ttcci_configuration_id, ttcci_tag, "
77  " TTCCI_configuration_file, TRG_MODE, TRG_SLEEP, Configuration, configuration_script, configuration_script_params ) "
78  "VALUES (:1, :2, :3, :4, :5, :6, :7, :8 )");
79  m_writeStmt->setInt(1, next_id);
80  m_writeStmt->setString(2, getConfigTag());
81  m_writeStmt->setString(3, getTTCciConfigurationFile());
82  m_writeStmt->setString(4, getTrgMode());
83  m_writeStmt->setInt(5, getTrgSleep());
84  m_writeStmt->setString(7, getConfigurationScript());
85  m_writeStmt->setString(8, getConfigurationScriptParams());
86 
87  // and now the clob
88  oracle::occi::Clob clob(m_conn);
89  clob.setEmpty();
90  m_writeStmt->setClob(6,clob);
91  m_writeStmt->executeUpdate ();
92  m_ID=next_id;
93 
94  m_conn->terminateStatement(m_writeStmt);
95  std::cout<<"TTCci Clob inserted into CONFIGURATION with id="<<next_id<<std::endl;
96 
97  // now we read and update it
98  m_writeStmt = m_conn->createStatement();
99  m_writeStmt->setSQL ("SELECT Configuration FROM ECAL_TTCci_CONFIGURATION WHERE"
100  " ttcci_configuration_id=:1 FOR UPDATE");
101 
102  std::cout<<"updating the clob 0"<<std::endl;
103 
104 
105  } catch (SQLException &e) {
106  throw(std::runtime_error("ODTTCciConfig::prepareWrite(): "+e.getMessage()));
107  }
108 
109  std::cout<<"updating the clob 1 "<<std::endl;
110 
111 }
112 
113 void ODTTCciConfig::setParameters(const std::map<string,string>& my_keys_map){
114 
115  // parses the result of the XML parser that is a map of
116  // string string with variable name variable value
117 
118  for( std::map<std::string, std::string >::const_iterator ci=
119  my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
120 
121  if(ci->first== "TRG_MODE") setTrgMode(ci->second);
122  if(ci->first== "TRG_SLEEP") setTrgSleep(atoi(ci->second.c_str()));
123  if(ci->first== "TTCci_CONFIGURATION_ID") setConfigTag(ci->second);
124  if(ci->first== "CONFIGURATION_SCRIPT" ) setConfigurationScript(ci->second);
125  if(ci->first== "CONFIGURATION_SCRIPT_PARAMS" ) setConfigurationScriptParams(ci->second);
126  if(ci->first== "CONFIGURATION_SCRIPT_PARAMETERS" ) setConfigurationScriptParams(ci->second);
127  if(ci->first== "Configuration") {
128  std::string fname=ci->second ;
129  string str3;
130  size_t pos, pose;
131 
132  pos = fname.find("="); // position of "live" in str
133  pose = fname.size(); // position of "]" in str
134  str3 = fname.substr (pos+1, pose-pos-2);
135 
136  cout << "fname="<<fname<< " and reduced is: "<<str3 << endl;
137  setTTCciConfigurationFile(str3 );
138 
139 
140  // here we must open the file and read the LTC Clob
141  std::cout << "Going to read file: " << str3 << endl;
142 
143  ifstream inpFile;
144  inpFile.open(str3.c_str());
145 
146  // tell me size of file
147  int bufsize = 0;
148  inpFile.seekg( 0,ios::end );
149  bufsize = inpFile.tellg();
150  std::cout <<" bufsize ="<<bufsize<< std::endl;
151  // set file pointer to start again
152  inpFile.seekg( 0,ios::beg );
153 
154  inpFile.close();
155  m_size=bufsize;
156 
157  }
158  }
159 
160 }
161 
162 
164  throw(std::runtime_error)
165 {
166 
167 
168  std::cout<<"updating the clob 2"<<std::endl;
169 
170 
171  try {
172 
173 
174  m_writeStmt->setInt(1, m_ID);
175  ResultSet* rset = m_writeStmt->executeQuery();
176 
177  while (rset->next ())
178  {
179  oracle::occi::Clob clob = rset->getClob (1);
180  cout << "Opening the clob in read write mode" << endl;
181  cout << "Populating the clob" << endl;
182  populateClob (clob, getTTCciConfigurationFile(), m_size);
183  int clobLength=clob.length ();
184  cout << "Length of the clob is: " << clobLength << endl;
185  // clob.close ();
186  }
187 
188  m_writeStmt->executeUpdate();
189 
190  m_writeStmt->closeResultSet (rset);
191 
192  } catch (SQLException &e) {
193  throw(std::runtime_error("ODTTCciConfig::writeDB(): "+e.getMessage()));
194  }
195  // Now get the ID
196  if (!this->fetchID()) {
197  throw(std::runtime_error("ODTTCciConfig::writeDB: Failed to write"));
198  }
199 
200 }
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("ODTTCciConfig::fetchData(): no Id defined for this ODTTCciConfig "));
214  }
215 
216  try {
217 
218  m_readStmt->setSQL("SELECT * "
219  "FROM ECAL_TTCci_CONFIGURATION "
220  " where ( ttcci_configuration_id = :1 or ttcci_tag=:2 )" );
221  m_readStmt->setInt(1, result->getId());
222  m_readStmt->setString(2, result->getConfigTag());
223  ResultSet* rset = m_readStmt->executeQuery();
224 
225  rset->next();
226  // 1 is the id and 2 is the config tag
227 
228  result->setId(rset->getInt(1));
229  result->setConfigTag(rset->getString(2));
230 
231 
232  result->setTTCciConfigurationFile(rset->getString(3));
233  result->setTrgMode(rset->getString(4));
234  result->setTrgSleep(rset->getInt(5));
235 
236  result->setConfigurationScript(rset->getString(7));
237  result->setConfigurationScriptParams(rset->getString(8));
238 
239  Clob clob = rset->getClob (6);
240  cout << "Opening the clob in Read only mode" << endl;
241  clob.open (OCCI_LOB_READONLY);
242  int clobLength=clob.length ();
243  cout << "Length of the clob is: " << clobLength << endl;
244  m_size=clobLength;
245  unsigned char* buffer = readClob (clob, m_size);
246  clob.close ();
247  cout<< "the clob buffer is:"<<endl;
248  for (int i = 0; i < clobLength; ++i)
249  cout << (char) buffer[i];
250  cout << endl;
251 
252 
253  result->setTTCciClob(buffer );
254 
255  } catch (SQLException &e) {
256  throw(std::runtime_error("ODTTCciConfig::fetchData(): "+e.getMessage()));
257  }
258 }
259 
260 
261 
262 int ODTTCciConfig::fetchID() throw(std::runtime_error)
263 {
264  if (m_ID!=0) {
265  return m_ID;
266  }
267 
268  this->checkConnection();
269 
270  try {
271  Statement* stmt = m_conn->createStatement();
272  stmt->setSQL("SELECT ttcci_configuration_id FROM ecal_ttcci_configuration "
273  "WHERE ttcci_tag=:ttcci_tag "
274  );
275 
276  stmt->setString(1, getConfigTag() );
277 
278  ResultSet* rset = stmt->executeQuery();
279 
280  if (rset->next()) {
281  m_ID = rset->getInt(1);
282  } else {
283  m_ID = 0;
284  }
285  m_conn->terminateStatement(stmt);
286  } catch (SQLException &e) {
287  throw(std::runtime_error("ODTTCciConfig::fetchID: "+e.getMessage()));
288  }
289  return m_ID;
290 }
int i
Definition: DBlmapReader.cc:9
#define NULL
Definition: scimark2.h:8
void setParameters(const std::map< std::string, std::string > &my_keys_map)
void prepareWrite()
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
tuple cout
Definition: gather_cfg.py:121
void fetchData(ODTTCciConfig *result)