CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CSCOnlineDB.cc
Go to the documentation of this file.
2 
6  condbon::condbon () throw (oracle::occi::SQLException)
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 
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  std::cout<<ex.getMessage() << std::endl;
114  }
115  }
116 
117  sqlStmt = "SELECT max(map_id) from "+tab_map;
118  stmt->setSQL(sqlStmt);
119  rset = stmt->executeQuery ();
120  // try{
121  while (rset->next ())
122  {map_id= rset->getInt (1);}
123  // }catch(oracle::occi::SQLException ex)
124  //{
125  // std::cout<<"Exception thrown: "<<std::endl;
126  // std::cout<<"Error number: "<< ex.getErrorCode() << std::endl;
127  // std::cout<<ex.getMessage() << std::endl;
128  //}
129  stmt->closeResultSet (rset);
130 
131  std::ostringstream ss;
132  ss<<record;
133 
134  sqlStmt = "SELECT max(map_index) from "+tab_map+" where record_id="+ss.str();
135  ss.str(""); // clear
136  stmt->setSQL(sqlStmt);
137  rset = stmt->executeQuery ();
138  // try{
139  while (rset->next ())
140  {map_index= rset->getInt (1);}
141  // }catch(oracle::occi::SQLException ex)
142  //{
143  // std::cout<<"Exception thrown: "<<std::endl;
144  // std::cout<<"Error number: "<< ex.getErrorCode() << std::endl;
145  // std::cout<<ex.getMessage() << std::endl;
146  //}
147  stmt->closeResultSet (rset);
148 
149  sqlStmt = "INSERT INTO "+tab_map+" VALUES (:1, :2, :3, :4)";
150  stmt->setSQL(sqlStmt);
151 
152  std::map<int,std::vector<std::vector<float> > >::const_iterator itm;
153  itm=obj->obj.begin();
154  int sizeint=itm->second[0].size();
155  sqlStmt1 = "INSERT INTO "+tab_data+" VALUES (:1, :2";
156  for(i=1;i<sizeint+1;++i){
157  ss<<i+2;
158  sqlStmt1=sqlStmt1+", :"+ss.str();
159  ss.str(""); // clear
160  }
161  sqlStmt1=sqlStmt1+")";
162 
163  sb4 si=sizeof(int);
164  sb4 sf=sizeof(float);
165  ub2 elensi[200];
166  ub2 elensf[200];
167  int c1[200],c2[200];
168  float c[100][200];
169  for(i=0;i<200;++i){
170  elensi[i]=si;
171  elensf[i]=sf;
172  }
173 
174  stmt1 = con->createStatement ();
175  stmt1->setSQL(sqlStmt1);
176 
177  for(itm=obj->obj.begin();itm!=obj->obj.end(); ++itm){
178  int id_det=itm->first;
179  int sizev=obj->obj[id_det].size();
180 
181  map_id=map_id+1;
182  map_index=map_index+1;
183  // try{
184  stmt->setInt (1, map_id);
185  stmt->setInt (2, record);
186  stmt->setInt (3, map_index);
187  stmt->setInt (4, id_det);
188  if(obj_name!="test") stmt->executeUpdate ();
189  //}catch(oracle::occi::SQLException ex)
190  //{
191  //std::cout<<"Exception thrown for insertBind"<<std::endl;
192  //std::cout<<"Error number: "<< ex.getErrorCode() << std::endl;
193  //std::cout<<ex.getMessage() << std::endl;
194  //}
195 
196  k=0;
197  for(i=0;i<sizev;++i){
198  int sizei=obj->obj[id_det][i].size();
199  if(sizei!=sizeint){
200  std::cout<<"Inconsistent object - dimention of internal vector is not "
201  <<sizeint<<std::endl;
202  exit(1);
203  }
204  c1[i]=map_id;
205  k=k+1;
206  c2[i]=k;
207  for(j=0;j<sizei;++j){
208  c[j][i]=obj->obj[id_det][i][j];
209  }
210  }
211  // try{
212  stmt1->setDataBuffer(1, c1, oracle::occi::OCCIINT, si , &elensi[0]);
213  stmt1->setDataBuffer(2, c2, oracle::occi::OCCIINT, si , &elensi[0]);
214  for(j=0;j<sizeint;++j){
215  stmt1->setDataBuffer(j+3, c[j], oracle::occi::OCCIFLOAT, sf, &elensf[0]);
216  }
217  if(obj_name!="test") stmt1->executeArrayUpdate(sizev);
218  // }catch(oracle::occi::SQLException ex)
219  // {
220  // std::cout<<"Exception thrown: "<<std::endl;
221  // std::cout<<"Error number: "<< ex.getErrorCode() << std::endl;
222  // std::cout<<ex.getMessage() << std::endl;
223  // }
224  }
225  con->commit();
226  con->terminateStatement (stmt);
227  con->terminateStatement (stmt1);
228 } //end of cdbon_write
229 
231 {
232  std::string sqlStmt;
233  sqlStmt = "SELECT max(record_id) from "+obj_name;
234  stmt=con->createStatement ();
235  stmt->setSQL(sqlStmt);
237  rset = stmt->executeQuery ();
238  //try{
239  while (rset->next ())
240  {*record = rset->getInt (1);}
241  // }catch(oracle::occi::SQLException ex)
242  //{
243  // std::cout<<"Exception thrown: "<<std::endl;
244  // std::cout<<"Error number: "<< ex.getErrorCode() << std::endl;
245  // std::cout<<ex.getMessage() << std::endl;
246  //}
247  con->terminateStatement (stmt);
248 } // end of cdbon_last_record
249 
251  CSCobject *obj)
252 {
253  int i,len;
254  int map_id=0, layer_id=0;
255  std::string tab, tab_map, tab_data;
256  tab=obj_name;
257  tab_map=obj_name+"_map";
258  tab_data=obj_name+"_data";
259  int num_var =0;
260  int vec_index;
261 
262  std::string sqlStmt,sqlStmt1;
263  stmt=con->createStatement ();
264  stmt1=con->createStatement ();
265  oracle::occi::ResultSet *rset, *rset1;
266  std::ostringstream ss;
267 
268  char d;
269  const char* p=tab_data.c_str();
270  len=tab_data.length();
271  for(i=0;i<len;++i){
272  d=toupper(*(p+i));
273  ss<<d;
274  }
275  sqlStmt = "SELECT count(column_name) from user_tab_columns where table_name='"+ss.str()+"'";
276  ss.str(""); // clear
277  stmt->setSQL(sqlStmt);
278  rset = stmt->executeQuery ();
279  // try{
280  while (rset->next ())
281  {num_var= rset->getInt(1) - 2;}
282  // }catch(oracle::occi::SQLException ex)
283  //{
284  // std::cout<<"Exception thrown: "<<std::endl;
285  // std::cout<<"Error number: "<< ex.getErrorCode() << std::endl;
286  // std::cout<<ex.getMessage() << std::endl;
287  //}
288  stmt->closeResultSet (rset);
289 
290  ss<<record;
291 
292 sqlStmt = "SELECT map_id,map_index,layer_id from "+tab_map+" where record_id="+ss.str()+" order by map_index";
293  ss.str(""); // clear
294  stmt->setSQL(sqlStmt);
295  rset = stmt->executeQuery ();
296  // try{
297  while (rset->next ())
298  {map_id = rset->getInt (1);
299  rset->getInt (2);
300  layer_id = rset->getInt (3);
301  ss<<map_id;
302  sqlStmt1 = "SELECT * from "+tab_data+" where map_id="+ss.str()+" order by vec_index";
303  ss.str(""); // clear
304  stmt1->setSQL(sqlStmt1);
305  rset1 = stmt1->executeQuery ();
306  // try{
307  while (rset1->next ())
308  {vec_index = rset1->getInt (2);
309  obj->obj[layer_id].resize(vec_index);
310  for(i=0;i<num_var;++i){
311  obj->obj[layer_id][vec_index-1].push_back(rset1->getFloat(3+i));
312  }
313  }
314  // }catch(oracle::occi::SQLException ex)
315  //{
316  // std::cout<<"Exception thrown: "<<std::endl;
317  // std::cout<<"Error number: "<< ex.getErrorCode() << std::endl;
318  // std::cout<<ex.getMessage() << std::endl;
319  //}
320  }
321  // }catch(oracle::occi::SQLException ex)
322  //{
323  // std::cout<<"Exception thrown: "<<std::endl;
324  // std::cout<<"Error number: "<< ex.getErrorCode() << std::endl;
325  // std::cout<<ex.getMessage() << std::endl;
326  //}
327 } // end of cdbon_read_rec
int i
Definition: DBlmapReader.cc:9
JetCorrectorParameters::Record record
Definition: classes.h:7
oracle::occi::Statement * stmt
Definition: CSCOnlineDB.h:23
oracle::occi::SQLException SQLException
Definition: HcalDbOmds.cc:27
tuple c2
Definition: counter.py:145
void cdbon_last_record(std::string obj_name, int *record)
Definition: CSCOnlineDB.cc:230
int j
Definition: DBlmapReader.cc:9
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
oracle::occi::Connection * con
Definition: CSCOnlineDB.h:22
oracle::occi::ResultSet ResultSet
Definition: HcalDbOmds.cc:26
oracle::occi::Environment * env
Definition: CSCOnlineDB.h:21
tuple cout
Definition: gather_cfg.py:121
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:250