Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-12 04:20:55

0001 #include "OnlineDB/CSCCondDB/interface/CSCOnlineDB.h"
0002 #include <cassert>
0003 
0004 /**
0005    * Constructor for condbon
0006    */
0007 condbon::condbon() noexcept(false) {
0008   std::string db_user;
0009   std::string db_pass;
0010   env = oracle::occi::Environment::createEnvironment(oracle::occi::Environment::OBJECT);
0011   char *c_user = std::getenv("CONDBON_AUTH_USER");
0012   char *c_pass = std::getenv("CONDBON_AUTH_PASSWORD");
0013   assert(c_user);
0014   assert(c_pass);
0015   db_user = std::string(c_user);
0016   db_pass = std::string(c_pass);
0017   con = env->createConnection(db_user, db_pass, "omds");
0018   std::cout << "Connection to Online DB is done." << std::endl;
0019 }  // end of constructor condbon ()
0020 /**
0021    * Destructor for condbon.
0022    */
0023 condbon::~condbon() noexcept(false) {
0024   env->terminateConnection(con);
0025   oracle::occi::Environment::terminateEnvironment(env);
0026 }  // end of ~condbon ()
0027 
0028 void condbon::cdbon_write(CSCobject *obj, std::string obj_name, int record, int global_run, std::string data_time) {
0029   int i, j, k;
0030   std::string tab, tab_map, tab_data;
0031   std::string sqlStmt, sqlStmt1;
0032   int rec_id = 0, map_id = 0, map_index = 0;
0033   tm curtime;
0034   time_t now;
0035 
0036   tab = obj_name;
0037   tab_map = obj_name + "_map";
0038   tab_data = obj_name + "_data";
0039   if (obj_name == "test") {
0040     tab = "gains";
0041     tab_map = "gains_map";
0042     tab_data = "gains_data";
0043   }
0044   stmt = con->createStatement();
0045 
0046   oracle::occi::ResultSet *rset;
0047 
0048   sqlStmt = "SELECT max(record_id) from " + tab;
0049   stmt->setSQL(sqlStmt);
0050   rset = stmt->executeQuery();
0051   //  try{
0052   while (rset->next()) {
0053     rec_id = rset->getInt(1);
0054   }
0055   //     }catch(oracle::occi::SQLException ex)
0056   //    {
0057   // std::cout<<"Exception thrown: "<<std::endl;
0058   // std::cout<<"Error number: "<<  ex.getErrorCode() << std::endl;
0059   // std::cout<<ex.getMessage() << std::endl;
0060   //}
0061   stmt->closeResultSet(rset);
0062 
0063   if (record > rec_id) {
0064     sqlStmt = "INSERT INTO " + tab + " VALUES (:1, :2, :3, :4, :5, null)";
0065     stmt->setSQL(sqlStmt);
0066     time(&now);
0067     curtime = *localtime(&now);
0068     try {
0069       stmt->setInt(1, record);
0070       stmt->setInt(2, global_run);
0071       stmt->setInt(5, 0);
0072       /* For time as "05/17/2006 16:30:07"
0073   std::string st=data_time.substr(0,2);
0074   int mon=atoi(st.c_str());
0075   st=data_time.substr(3,2);
0076   int mday=atoi(st.c_str());
0077   st=data_time.substr(6,4);
0078   int year=atoi(st.c_str());
0079   st=data_time.substr(11,2);
0080   int hour=atoi(st.c_str());
0081   st=data_time.substr(14,2);
0082   int min=atoi(st.c_str());
0083   st=data_time.substr(17,2);
0084   int sec=atoi(st.c_str());
0085 */
0086       /* For time of format "Mon May 29 10:28:58 2006" */
0087       std::map<std::string, int> month;
0088       month["Jan"] = 1;
0089       month["Feb"] = 2;
0090       month["Mar"] = 3;
0091       month["Apr"] = 4;
0092       month["May"] = 5;
0093       month["Jun"] = 6;
0094       month["Jul"] = 7;
0095       month["Aug"] = 8;
0096       month["Sep"] = 9;
0097       month["Oct"] = 10;
0098       month["Nov"] = 11;
0099       month["Dec"] = 12;
0100       std::string st = data_time.substr(4, 3);
0101       int mon = month[st];
0102       st = data_time.substr(8, 2);
0103       int mday = atoi(st.c_str());
0104       st = data_time.substr(20, 4);
0105       int year = atoi(st.c_str());
0106       st = data_time.substr(11, 2);
0107       int hour = atoi(st.c_str());
0108       st = data_time.substr(14, 2);
0109       int min = atoi(st.c_str());
0110       st = data_time.substr(17, 2);
0111       int sec = atoi(st.c_str());
0112       oracle::occi::Date edate(env, year, mon, mday, hour, min, sec);
0113       stmt->setDate(3, edate);
0114       oracle::occi::Date edate_c(env,
0115                                  curtime.tm_year + 1900,
0116                                  curtime.tm_mon + 1,
0117                                  curtime.tm_mday,
0118                                  curtime.tm_hour,
0119                                  curtime.tm_min,
0120                                  curtime.tm_sec);
0121       stmt->setDate(4, edate_c);
0122       if (obj_name != "test")
0123         stmt->executeUpdate();
0124     } catch (oracle::occi::SQLException &ex) {
0125       std::cout << "Exception thrown for insertBind" << std::endl;
0126       std::cout << "Error number: " << ex.getErrorCode() << std::endl;
0127 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
0128       std::cout << ex.getMessage() << std::endl;
0129 #endif
0130     }
0131   }
0132 
0133   sqlStmt = "SELECT max(map_id) from " + tab_map;
0134   stmt->setSQL(sqlStmt);
0135   rset = stmt->executeQuery();
0136   //  try{
0137   while (rset->next()) {
0138     map_id = rset->getInt(1);
0139   }
0140   //     }catch(oracle::occi::SQLException ex)
0141   //{
0142   // std::cout<<"Exception thrown: "<<std::endl;
0143   // std::cout<<"Error number: "<<  ex.getErrorCode() << std::endl;
0144   // std::cout<<ex.getMessage() << std::endl;
0145   //}
0146   stmt->closeResultSet(rset);
0147 
0148   std::ostringstream ss;
0149   ss << record;
0150 
0151   sqlStmt = "SELECT max(map_index) from " + tab_map + " where record_id=" + ss.str();
0152   ss.str("");  // clear
0153   stmt->setSQL(sqlStmt);
0154   rset = stmt->executeQuery();
0155   //  try{
0156   while (rset->next()) {
0157     map_index = rset->getInt(1);
0158   }
0159   //     }catch(oracle::occi::SQLException ex)
0160   //{
0161   // std::cout<<"Exception thrown: "<<std::endl;
0162   // std::cout<<"Error number: "<<  ex.getErrorCode() << std::endl;
0163   // std::cout<<ex.getMessage() << std::endl;
0164   //}
0165   stmt->closeResultSet(rset);
0166 
0167   sqlStmt = "INSERT INTO " + tab_map + " VALUES (:1, :2, :3, :4)";
0168   stmt->setSQL(sqlStmt);
0169 
0170   std::map<int, std::vector<std::vector<float> > >::const_iterator itm;
0171   itm = obj->obj.begin();
0172   int sizeint = itm->second[0].size();
0173   sqlStmt1 = "INSERT INTO " + tab_data + " VALUES (:1, :2";
0174   for (i = 1; i < sizeint + 1; ++i) {
0175     ss << i + 2;
0176     sqlStmt1 = sqlStmt1 + ", :" + ss.str();
0177     ss.str("");  // clear
0178   }
0179   sqlStmt1 = sqlStmt1 + ")";
0180 
0181   sb4 si = sizeof(int);
0182   sb4 sf = sizeof(float);
0183   ub2 elensi[200];
0184   ub2 elensf[200];
0185   int c1[200], c2[200];
0186   float c[100][200];
0187   for (i = 0; i < 200; ++i) {
0188     elensi[i] = si;
0189     elensf[i] = sf;
0190   }
0191 
0192   stmt1 = con->createStatement();
0193   stmt1->setSQL(sqlStmt1);
0194 
0195   for (itm = obj->obj.begin(); itm != obj->obj.end(); ++itm) {
0196     int id_det = itm->first;
0197     int sizev = obj->obj[id_det].size();
0198 
0199     map_id = map_id + 1;
0200     map_index = map_index + 1;
0201     //  try{
0202     stmt->setInt(1, map_id);
0203     stmt->setInt(2, record);
0204     stmt->setInt(3, map_index);
0205     stmt->setInt(4, id_det);
0206     if (obj_name != "test")
0207       stmt->executeUpdate();
0208     //}catch(oracle::occi::SQLException ex)
0209     //{
0210     //std::cout<<"Exception thrown for insertBind"<<std::endl;
0211     //std::cout<<"Error number: "<<  ex.getErrorCode() << std::endl;
0212     //std::cout<<ex.getMessage() << std::endl;
0213     //}
0214 
0215     k = 0;
0216     for (i = 0; i < sizev; ++i) {
0217       int sizei = obj->obj[id_det][i].size();
0218       if (sizei != sizeint) {
0219         std::cout << "Inconsistent object - dimention of internal vector is not " << sizeint << std::endl;
0220         exit(1);
0221       }
0222       c1[i] = map_id;
0223       k = k + 1;
0224       c2[i] = k;
0225       for (j = 0; j < sizei; ++j) {
0226         c[j][i] = obj->obj[id_det][i][j];
0227       }
0228     }
0229     //  try{
0230     stmt1->setDataBuffer(1, c1, oracle::occi::OCCIINT, si, &elensi[0]);
0231     stmt1->setDataBuffer(2, c2, oracle::occi::OCCIINT, si, &elensi[0]);
0232     for (j = 0; j < sizeint; ++j) {
0233       stmt1->setDataBuffer(j + 3, c[j], oracle::occi::OCCIFLOAT, sf, &elensf[0]);
0234     }
0235     if (obj_name != "test")
0236       stmt1->executeArrayUpdate(sizev);
0237     //     }catch(oracle::occi::SQLException ex)
0238     // {
0239     //  std::cout<<"Exception thrown: "<<std::endl;
0240     //  std::cout<<"Error number: "<<  ex.getErrorCode() << std::endl;
0241     //  std::cout<<ex.getMessage() << std::endl;
0242     // }
0243   }
0244   con->commit();
0245   con->terminateStatement(stmt);
0246   con->terminateStatement(stmt1);
0247 }  //end of cdbon_write
0248 
0249 void condbon::cdbon_last_record(std::string obj_name, int *record) {
0250   std::string sqlStmt;
0251   sqlStmt = "SELECT max(record_id) from " + obj_name;
0252   stmt = con->createStatement();
0253   stmt->setSQL(sqlStmt);
0254   oracle::occi::ResultSet *rset;
0255   rset = stmt->executeQuery();
0256   //try{
0257   while (rset->next()) {
0258     *record = rset->getInt(1);
0259   }
0260   //     }catch(oracle::occi::SQLException ex)
0261   //{
0262   // std::cout<<"Exception thrown: "<<std::endl;
0263   // std::cout<<"Error number: "<<  ex.getErrorCode() << std::endl;
0264   // std::cout<<ex.getMessage() << std::endl;
0265   //}
0266   con->terminateStatement(stmt);
0267 }  // end of cdbon_last_record
0268 
0269 void condbon::cdbon_read_rec(std::string obj_name, int record, CSCobject *obj) {
0270   int i, len;
0271   int map_id = 0, layer_id = 0;
0272   std::string tab, tab_map, tab_data;
0273   tab = obj_name;
0274   tab_map = obj_name + "_map";
0275   tab_data = obj_name + "_data";
0276   int num_var = 0;
0277   int vec_index;
0278 
0279   std::string sqlStmt, sqlStmt1;
0280   stmt = con->createStatement();
0281   stmt1 = con->createStatement();
0282   oracle::occi::ResultSet *rset, *rset1;
0283   std::ostringstream ss;
0284 
0285   char d;
0286   const char *p = tab_data.c_str();
0287   len = tab_data.length();
0288   for (i = 0; i < len; ++i) {
0289     d = toupper(*(p + i));
0290     ss << d;
0291   }
0292   sqlStmt = "SELECT count(column_name) from user_tab_columns where table_name='" + ss.str() + "'";
0293   ss.str("");  // clear
0294   stmt->setSQL(sqlStmt);
0295   rset = stmt->executeQuery();
0296   //  try{
0297   while (rset->next()) {
0298     num_var = rset->getInt(1) - 2;
0299   }
0300   //     }catch(oracle::occi::SQLException ex)
0301   //{
0302   // std::cout<<"Exception thrown: "<<std::endl;
0303   // std::cout<<"Error number: "<<  ex.getErrorCode() << std::endl;
0304   // std::cout<<ex.getMessage() << std::endl;
0305   //}
0306   stmt->closeResultSet(rset);
0307 
0308   ss << record;
0309 
0310   sqlStmt = "SELECT map_id,map_index,layer_id from " + tab_map + " where record_id=" + ss.str() + " order by map_index";
0311   ss.str("");  // clear
0312   stmt->setSQL(sqlStmt);
0313   rset = stmt->executeQuery();
0314   //  try{
0315   while (rset->next()) {
0316     map_id = rset->getInt(1);
0317     rset->getInt(2);
0318     layer_id = rset->getInt(3);
0319     ss << map_id;
0320     sqlStmt1 = "SELECT * from " + tab_data + " where map_id=" + ss.str() + " order by vec_index";
0321     ss.str("");  // clear
0322     stmt1->setSQL(sqlStmt1);
0323     rset1 = stmt1->executeQuery();
0324     //     try{
0325     while (rset1->next()) {
0326       vec_index = rset1->getInt(2);
0327       obj->obj[layer_id].resize(vec_index);
0328       for (i = 0; i < num_var; ++i) {
0329         obj->obj[layer_id][vec_index - 1].push_back(rset1->getFloat(3 + i));
0330       }
0331     }
0332     //   }catch(oracle::occi::SQLException ex)
0333     //{
0334     // std::cout<<"Exception thrown: "<<std::endl;
0335     // std::cout<<"Error number: "<<  ex.getErrorCode() << std::endl;
0336     // std::cout<<ex.getMessage() << std::endl;
0337     //}
0338   }
0339   // }catch(oracle::occi::SQLException ex)
0340   //{
0341   // std::cout<<"Exception thrown: "<<std::endl;
0342   // std::cout<<"Error number: "<<  ex.getErrorCode() << std::endl;
0343   // std::cout<<ex.getMessage() << std::endl;
0344   //}
0345 }  // end of cdbon_read_rec