CMS 3D CMS Logo

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