CMS 3D CMS Logo

CSCOnlineDB.cc
Go to the documentation of this file.
2 
7  {
8  std::string db_user;
9  std::string db_pass;
10  env = oracle::occi::Environment::createEnvironment (oracle::occi::Environment::OBJECT);
11  char* c_user = ::getenv("CONDBON_AUTH_USER");
12  char* c_pass = ::getenv("CONDBON_AUTH_PASSWORD");
13  db_user=std::string(c_user);
14  db_pass=std::string(c_pass);
15  con = env->createConnection (db_user,db_pass,"omds");
16  std::cout << "Connection to Online DB is done." << std::endl;
17  }// end of constructor condbon ()
22  {
23  env->terminateConnection (con);
24  oracle::occi::Environment::terminateEnvironment (env);
25  } // end of ~condbon ()
26 
28  int global_run, std::string data_time)
29 {
30  int i,j,k;
31  std::string tab, tab_map, tab_data;
32  std::string sqlStmt, sqlStmt1;
33  int rec_id =0, map_id =0, map_index =0;
34  tm curtime;
35  time_t now;
36 
37  tab=obj_name;
38  tab_map=obj_name+"_map";
39  tab_data=obj_name+"_data";
40  if(obj_name=="test"){
41  tab="gains";
42  tab_map="gains_map";
43  tab_data="gains_data";
44  }
45  stmt = con->createStatement ();
46 
47  oracle::occi::ResultSet *rset;
48 
49  sqlStmt = "SELECT max(record_id) from "+tab;
50  stmt->setSQL(sqlStmt);
51  rset = stmt->executeQuery ();
52  // try{
53  while (rset->next ())
54  {rec_id= rset->getInt (1);}
55  // }catch(oracle::occi::SQLException ex)
56  // {
57  // std::cout<<"Exception thrown: "<<std::endl;
58  // std::cout<<"Error number: "<< ex.getErrorCode() << std::endl;
59  // std::cout<<ex.getMessage() << std::endl;
60  //}
61  stmt->closeResultSet (rset);
62 
63  if(record>rec_id){
64  sqlStmt = "INSERT INTO "+tab+" VALUES (:1, :2, :3, :4, :5, null)";
65  stmt->setSQL(sqlStmt);
66  time(&now);
67  curtime=*localtime(&now);
68  try{
69  stmt->setInt (1, record);
70  stmt->setInt (2, global_run);
71  stmt->setInt (5, 0);
72 /* For time as "05/17/2006 16:30:07"
73  std::string st=data_time.substr(0,2);
74  int mon=atoi(st.c_str());
75  st=data_time.substr(3,2);
76  int mday=atoi(st.c_str());
77  st=data_time.substr(6,4);
78  int year=atoi(st.c_str());
79  st=data_time.substr(11,2);
80  int hour=atoi(st.c_str());
81  st=data_time.substr(14,2);
82  int min=atoi(st.c_str());
83  st=data_time.substr(17,2);
84  int sec=atoi(st.c_str());
85 */
86 /* For time of format "Mon May 29 10:28:58 2006" */
87  std::map <std::string, int> month;
88  month["Jan"]=1; month["Feb"]=2; month["Mar"]=3; month["Apr"]=4;
89  month["May"]=5; month["Jun"]=6; month["Jul"]=7; month["Aug"]=8;
90  month["Sep"]=9; month["Oct"]=10; month["Nov"]=11; month["Dec"]=12;
91  std::string st=data_time.substr(4,3);
92  int mon=month[st];
93  st=data_time.substr(8,2);
94  int mday=atoi(st.c_str());
95  st=data_time.substr(20,4);
96  int year=atoi(st.c_str());
97  st=data_time.substr(11,2);
98  int hour=atoi(st.c_str());
99  st=data_time.substr(14,2);
100  int min=atoi(st.c_str());
101  st=data_time.substr(17,2);
102  int sec=atoi(st.c_str());
103  oracle::occi::Date edate(env, year, mon, mday, hour, min, sec);
104  stmt->setDate(3, edate);
105  oracle::occi::Date edate_c(env, curtime.tm_year+1900, curtime.tm_mon+1, curtime.tm_mday,
106  curtime.tm_hour, curtime.tm_min, curtime.tm_sec);
107  stmt->setDate(4, edate_c);
108  if(obj_name!="test") stmt->executeUpdate ();
109  }catch(oracle::occi::SQLException &ex)
110  {
111  std::cout<<"Exception thrown for insertBind"<<std::endl;
112  std::cout<<"Error number: "<< ex.getErrorCode() << std::endl;
113 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
114  std::cout << getOraMessage(&ex) << std::endl;
115 #endif
116  }
117  }
118 
119  sqlStmt = "SELECT max(map_id) from "+tab_map;
120  stmt->setSQL(sqlStmt);
121  rset = stmt->executeQuery ();
122  // try{
123  while (rset->next ())
124  {map_id= rset->getInt (1);}
125  // }catch(oracle::occi::SQLException ex)
126  //{
127  // std::cout<<"Exception thrown: "<<std::endl;
128  // std::cout<<"Error number: "<< ex.getErrorCode() << std::endl;
129  // std::cout<<ex.getMessage() << std::endl;
130  //}
131  stmt->closeResultSet (rset);
132 
133  std::ostringstream ss;
134  ss<<record;
135 
136  sqlStmt = "SELECT max(map_index) from "+tab_map+" where record_id="+ss.str();
137  ss.str(""); // clear
138  stmt->setSQL(sqlStmt);
139  rset = stmt->executeQuery ();
140  // try{
141  while (rset->next ())
142  {map_index= rset->getInt (1);}
143  // }catch(oracle::occi::SQLException ex)
144  //{
145  // std::cout<<"Exception thrown: "<<std::endl;
146  // std::cout<<"Error number: "<< ex.getErrorCode() << std::endl;
147  // std::cout<<ex.getMessage() << std::endl;
148  //}
149  stmt->closeResultSet (rset);
150 
151  sqlStmt = "INSERT INTO "+tab_map+" VALUES (:1, :2, :3, :4)";
152  stmt->setSQL(sqlStmt);
153 
154  std::map<int,std::vector<std::vector<float> > >::const_iterator itm;
155  itm=obj->obj.begin();
156  int sizeint=itm->second[0].size();
157  sqlStmt1 = "INSERT INTO "+tab_data+" VALUES (:1, :2";
158  for(i=1;i<sizeint+1;++i){
159  ss<<i+2;
160  sqlStmt1=sqlStmt1+", :"+ss.str();
161  ss.str(""); // clear
162  }
163  sqlStmt1=sqlStmt1+")";
164 
165  sb4 si=sizeof(int);
166  sb4 sf=sizeof(float);
167  ub2 elensi[200];
168  ub2 elensf[200];
169  int c1[200],c2[200];
170  float c[100][200];
171  for(i=0;i<200;++i){
172  elensi[i]=si;
173  elensf[i]=sf;
174  }
175 
176  stmt1 = con->createStatement ();
177  stmt1->setSQL(sqlStmt1);
178 
179  for(itm=obj->obj.begin();itm!=obj->obj.end(); ++itm){
180  int id_det=itm->first;
181  int sizev=obj->obj[id_det].size();
182 
183  map_id=map_id+1;
184  map_index=map_index+1;
185  // try{
186  stmt->setInt (1, map_id);
187  stmt->setInt (2, record);
188  stmt->setInt (3, map_index);
189  stmt->setInt (4, id_det);
190  if(obj_name!="test") stmt->executeUpdate ();
191  //}catch(oracle::occi::SQLException ex)
192  //{
193  //std::cout<<"Exception thrown for insertBind"<<std::endl;
194  //std::cout<<"Error number: "<< ex.getErrorCode() << std::endl;
195  //std::cout<<ex.getMessage() << std::endl;
196  //}
197 
198  k=0;
199  for(i=0;i<sizev;++i){
200  int sizei=obj->obj[id_det][i].size();
201  if(sizei!=sizeint){
202  std::cout<<"Inconsistent object - dimention of internal vector is not "
203  <<sizeint<<std::endl;
204  exit(1);
205  }
206  c1[i]=map_id;
207  k=k+1;
208  c2[i]=k;
209  for(j=0;j<sizei;++j){
210  c[j][i]=obj->obj[id_det][i][j];
211  }
212  }
213  // try{
214  stmt1->setDataBuffer(1, c1, oracle::occi::OCCIINT, si , &elensi[0]);
215  stmt1->setDataBuffer(2, c2, oracle::occi::OCCIINT, si , &elensi[0]);
216  for(j=0;j<sizeint;++j){
217  stmt1->setDataBuffer(j+3, c[j], oracle::occi::OCCIFLOAT, sf, &elensf[0]);
218  }
219  if(obj_name!="test") stmt1->executeArrayUpdate(sizev);
220  // }catch(oracle::occi::SQLException ex)
221  // {
222  // std::cout<<"Exception thrown: "<<std::endl;
223  // std::cout<<"Error number: "<< ex.getErrorCode() << std::endl;
224  // std::cout<<ex.getMessage() << std::endl;
225  // }
226  }
227  con->commit();
228  con->terminateStatement (stmt);
229  con->terminateStatement (stmt1);
230 } //end of cdbon_write
231 
233 {
234  std::string sqlStmt;
235  sqlStmt = "SELECT max(record_id) from "+obj_name;
236  stmt=con->createStatement ();
237  stmt->setSQL(sqlStmt);
238  oracle::occi::ResultSet *rset;
239  rset = stmt->executeQuery ();
240  //try{
241  while (rset->next ())
242  {*record = rset->getInt (1);}
243  // }catch(oracle::occi::SQLException ex)
244  //{
245  // std::cout<<"Exception thrown: "<<std::endl;
246  // std::cout<<"Error number: "<< ex.getErrorCode() << std::endl;
247  // std::cout<<ex.getMessage() << std::endl;
248  //}
249  con->terminateStatement (stmt);
250 } // end of cdbon_last_record
251 
253  CSCobject *obj)
254 {
255  int i,len;
256  int map_id=0, layer_id=0;
257  std::string tab, tab_map, tab_data;
258  tab=obj_name;
259  tab_map=obj_name+"_map";
260  tab_data=obj_name+"_data";
261  int num_var =0;
262  int vec_index;
263 
264  std::string sqlStmt,sqlStmt1;
265  stmt=con->createStatement ();
266  stmt1=con->createStatement ();
267  oracle::occi::ResultSet *rset, *rset1;
268  std::ostringstream ss;
269 
270  char d;
271  const char* p=tab_data.c_str();
272  len=tab_data.length();
273  for(i=0;i<len;++i){
274  d=toupper(*(p+i));
275  ss<<d;
276  }
277  sqlStmt = "SELECT count(column_name) from user_tab_columns where table_name='"+ss.str()+"'";
278  ss.str(""); // clear
279  stmt->setSQL(sqlStmt);
280  rset = stmt->executeQuery ();
281  // try{
282  while (rset->next ())
283  {num_var= rset->getInt(1) - 2;}
284  // }catch(oracle::occi::SQLException ex)
285  //{
286  // std::cout<<"Exception thrown: "<<std::endl;
287  // std::cout<<"Error number: "<< ex.getErrorCode() << std::endl;
288  // std::cout<<ex.getMessage() << std::endl;
289  //}
290  stmt->closeResultSet (rset);
291 
292  ss<<record;
293 
294 sqlStmt = "SELECT map_id,map_index,layer_id from "+tab_map+" where record_id="+ss.str()+" order by map_index";
295  ss.str(""); // clear
296  stmt->setSQL(sqlStmt);
297  rset = stmt->executeQuery ();
298  // try{
299  while (rset->next ())
300  {map_id = rset->getInt (1);
301  rset->getInt (2);
302  layer_id = rset->getInt (3);
303  ss<<map_id;
304  sqlStmt1 = "SELECT * from "+tab_data+" where map_id="+ss.str()+" order by vec_index";
305  ss.str(""); // clear
306  stmt1->setSQL(sqlStmt1);
307  rset1 = stmt1->executeQuery ();
308  // try{
309  while (rset1->next ())
310  {vec_index = rset1->getInt (2);
311  obj->obj[layer_id].resize(vec_index);
312  for(i=0;i<num_var;++i){
313  obj->obj[layer_id][vec_index-1].push_back(rset1->getFloat(3+i));
314  }
315  }
316  // }catch(oracle::occi::SQLException ex)
317  //{
318  // std::cout<<"Exception thrown: "<<std::endl;
319  // std::cout<<"Error number: "<< ex.getErrorCode() << std::endl;
320  // std::cout<<ex.getMessage() << std::endl;
321  //}
322  }
323  // }catch(oracle::occi::SQLException ex)
324  //{
325  // std::cout<<"Exception thrown: "<<std::endl;
326  // std::cout<<"Error number: "<< ex.getErrorCode() << std::endl;
327  // std::cout<<ex.getMessage() << std::endl;
328  //}
329 } // end of cdbon_read_rec
condbon() noexcept(false)
Definition: CSCOnlineDB.cc:6
JetCorrectorParameters::Record record
Definition: classes.h:7
#define noexcept
oracle::occi::Statement * stmt
Definition: CSCOnlineDB.h:23
void cdbon_last_record(std::string obj_name, int *record)
Definition: CSCOnlineDB.cc:232
T min(T a, T b)
Definition: MathUtil.h:58
void cdbon_write(CSCobject *obj, std::string obj_name, int record, int global_run, std::string time)
Definition: CSCOnlineDB.cc:27
int k[5][pyjets_maxn]
oracle::occi::Connection * con
Definition: CSCOnlineDB.h:22
oracle::occi::Environment * env
Definition: CSCOnlineDB.h:21
~condbon() noexcept(false)
Definition: CSCOnlineDB.cc:21
std::map< int, std::vector< std::vector< float > > > obj
Definition: CSCobject.h:12
oracle::occi::Statement * stmt1
Definition: CSCOnlineDB.h:23
void cdbon_read_rec(std::string obj_name, int record, CSCobject *obj)
Definition: CSCOnlineDB.cc:252