CMS 3D CMS Logo

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