CMS 3D CMS Logo

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