00001 #include "OnlineDB/CSCCondDB/interface/CSCOnlineDB.h"
00002
00006 condbon::condbon () throw (oracle::occi::SQLException)
00007 {
00008 std::string db_user;
00009 std::string db_pass;
00010 env = oracle::occi::Environment::createEnvironment (oracle::occi::Environment::OBJECT);
00011 char* c_user = ::getenv("CONDBON_AUTH_USER");
00012 char* c_pass = ::getenv("CONDBON_AUTH_PASSWORD");
00013 db_user=std::string(c_user);
00014 db_pass=std::string(c_pass);
00015 con = env->createConnection (db_user,db_pass,"omds");
00016 std::cout << "Connection to Online DB is done." << std::endl;
00017 }
00021 condbon::~condbon () throw (oracle::occi::SQLException)
00022 {
00023 env->terminateConnection (con);
00024 oracle::occi::Environment::terminateEnvironment (env);
00025 }
00026
00027 void condbon::cdbon_write (CSCobject *obj, std::string obj_name, int record,
00028 int global_run, std::string data_time)
00029 {
00030 int i,j,k;
00031 std::string tab, tab_map, tab_data;
00032 std::string sqlStmt, sqlStmt1;
00033 int rec_id =0, map_id =0, map_index =0;
00034 tm curtime;
00035 time_t now;
00036
00037 tab=obj_name;
00038 tab_map=obj_name+"_map";
00039 tab_data=obj_name+"_data";
00040 if(obj_name=="test"){
00041 tab="gains";
00042 tab_map="gains_map";
00043 tab_data="gains_data";
00044 }
00045 stmt = con->createStatement ();
00046
00047 oracle::occi::ResultSet *rset;
00048
00049 sqlStmt = "SELECT max(record_id) from "+tab;
00050 stmt->setSQL(sqlStmt);
00051 rset = stmt->executeQuery ();
00052
00053 while (rset->next ())
00054 {rec_id= rset->getInt (1);}
00055
00056
00057
00058
00059
00060
00061 stmt->closeResultSet (rset);
00062
00063 if(record>rec_id){
00064 sqlStmt = "INSERT INTO "+tab+" VALUES (:1, :2, :3, :4, :5, null)";
00065 stmt->setSQL(sqlStmt);
00066 time(&now);
00067 curtime=*localtime(&now);
00068 try{
00069 stmt->setInt (1, record);
00070 stmt->setInt (2, global_run);
00071 stmt->setInt (5, 0);
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 std::map <std::string, int> month;
00088 month["Jan"]=1; month["Feb"]=2; month["Mar"]=3; month["Apr"]=4;
00089 month["May"]=5; month["Jun"]=6; month["Jul"]=7; month["Aug"]=8;
00090 month["Sep"]=9; month["Oct"]=10; month["Nov"]=11; month["Dec"]=12;
00091 std::string st=data_time.substr(4,3);
00092 int mon=month[st];
00093 st=data_time.substr(8,2);
00094 int mday=atoi(st.c_str());
00095 st=data_time.substr(20,4);
00096 int year=atoi(st.c_str());
00097 st=data_time.substr(11,2);
00098 int hour=atoi(st.c_str());
00099 st=data_time.substr(14,2);
00100 int min=atoi(st.c_str());
00101 st=data_time.substr(17,2);
00102 int sec=atoi(st.c_str());
00103 oracle::occi::Date edate(env, year, mon, mday, hour, min, sec);
00104 stmt->setDate(3, edate);
00105 oracle::occi::Date edate_c(env, curtime.tm_year+1900, curtime.tm_mon+1, curtime.tm_mday,
00106 curtime.tm_hour, curtime.tm_min, curtime.tm_sec);
00107 stmt->setDate(4, edate_c);
00108 if(obj_name!="test") stmt->executeUpdate ();
00109 }catch(oracle::occi::SQLException ex)
00110 {
00111 std::cout<<"Exception thrown for insertBind"<<std::endl;
00112 std::cout<<"Error number: "<< ex.getErrorCode() << std::endl;
00113 std::cout<<ex.getMessage() << std::endl;
00114 }
00115 }
00116
00117 sqlStmt = "SELECT max(map_id) from "+tab_map;
00118 stmt->setSQL(sqlStmt);
00119 rset = stmt->executeQuery ();
00120
00121 while (rset->next ())
00122 {map_id= rset->getInt (1);}
00123
00124
00125
00126
00127
00128
00129 stmt->closeResultSet (rset);
00130
00131 std::ostringstream ss;
00132 ss<<record;
00133
00134 sqlStmt = "SELECT max(map_index) from "+tab_map+" where record_id="+ss.str();
00135 ss.str("");
00136 stmt->setSQL(sqlStmt);
00137 rset = stmt->executeQuery ();
00138
00139 while (rset->next ())
00140 {map_index= rset->getInt (1);}
00141
00142
00143
00144
00145
00146
00147 stmt->closeResultSet (rset);
00148
00149 sqlStmt = "INSERT INTO "+tab_map+" VALUES (:1, :2, :3, :4)";
00150 stmt->setSQL(sqlStmt);
00151
00152 std::map<int,std::vector<std::vector<float> > >::const_iterator itm;
00153 itm=obj->obj.begin();
00154 int sizeint=itm->second[0].size();
00155 sqlStmt1 = "INSERT INTO "+tab_data+" VALUES (:1, :2";
00156 for(i=1;i<sizeint+1;++i){
00157 ss<<i+2;
00158 sqlStmt1=sqlStmt1+", :"+ss.str();
00159 ss.str("");
00160 }
00161 sqlStmt1=sqlStmt1+")";
00162
00163 sb4 si=sizeof(int);
00164 sb4 sf=sizeof(float);
00165 ub2 elensi[200];
00166 ub2 elensf[200];
00167 int c1[200],c2[200];
00168 float c[100][200];
00169 for(i=0;i<200;++i){
00170 elensi[i]=si;
00171 elensf[i]=sf;
00172 }
00173
00174 stmt1 = con->createStatement ();
00175 stmt1->setSQL(sqlStmt1);
00176
00177 for(itm=obj->obj.begin();itm!=obj->obj.end(); ++itm){
00178 int id_det=itm->first;
00179 int sizev=obj->obj[id_det].size();
00180
00181 map_id=map_id+1;
00182 map_index=map_index+1;
00183
00184 stmt->setInt (1, map_id);
00185 stmt->setInt (2, record);
00186 stmt->setInt (3, map_index);
00187 stmt->setInt (4, id_det);
00188 if(obj_name!="test") stmt->executeUpdate ();
00189
00190
00191
00192
00193
00194
00195
00196 k=0;
00197 for(i=0;i<sizev;++i){
00198 int sizei=obj->obj[id_det][i].size();
00199 if(sizei!=sizeint){
00200 std::cout<<"Inconsistent object - dimention of internal vector is not "
00201 <<sizeint<<std::endl;
00202 exit(1);
00203 }
00204 c1[i]=map_id;
00205 k=k+1;
00206 c2[i]=k;
00207 for(j=0;j<sizei;++j){
00208 c[j][i]=obj->obj[id_det][i][j];
00209 }
00210 }
00211
00212 stmt1->setDataBuffer(1, c1, oracle::occi::OCCIINT, si , &elensi[0]);
00213 stmt1->setDataBuffer(2, c2, oracle::occi::OCCIINT, si , &elensi[0]);
00214 for(j=0;j<sizeint;++j){
00215 stmt1->setDataBuffer(j+3, c[j], oracle::occi::OCCIFLOAT, sf, &elensf[0]);
00216 }
00217 if(obj_name!="test") stmt1->executeArrayUpdate(sizev);
00218
00219
00220
00221
00222
00223
00224 }
00225 con->commit();
00226 con->terminateStatement (stmt);
00227 con->terminateStatement (stmt1);
00228 }
00229
00230 void condbon::cdbon_last_record (std::string obj_name, int *record)
00231 {
00232 std::string sqlStmt;
00233 sqlStmt = "SELECT max(record_id) from "+obj_name;
00234 stmt=con->createStatement ();
00235 stmt->setSQL(sqlStmt);
00236 oracle::occi::ResultSet *rset;
00237 rset = stmt->executeQuery ();
00238
00239 while (rset->next ())
00240 {*record = rset->getInt (1);}
00241
00242
00243
00244
00245
00246
00247 con->terminateStatement (stmt);
00248 }
00249
00250 void condbon::cdbon_read_rec (std::string obj_name, int record,
00251 CSCobject *obj)
00252 {
00253 int i,len;
00254 int map_id=0, layer_id=0;
00255 std::string tab, tab_map, tab_data;
00256 tab=obj_name;
00257 tab_map=obj_name+"_map";
00258 tab_data=obj_name+"_data";
00259 int num_var =0;
00260 int vec_index;
00261
00262 std::string sqlStmt,sqlStmt1;
00263 stmt=con->createStatement ();
00264 stmt1=con->createStatement ();
00265 oracle::occi::ResultSet *rset, *rset1;
00266 std::ostringstream ss;
00267
00268 char d;
00269 const char* p=tab_data.c_str();
00270 len=tab_data.length();
00271 for(i=0;i<len;++i){
00272 d=toupper(*(p+i));
00273 ss<<d;
00274 }
00275 sqlStmt = "SELECT count(column_name) from user_tab_columns where table_name='"+ss.str()+"'";
00276 ss.str("");
00277 stmt->setSQL(sqlStmt);
00278 rset = stmt->executeQuery ();
00279
00280 while (rset->next ())
00281 {num_var= rset->getInt(1) - 2;}
00282
00283
00284
00285
00286
00287
00288 stmt->closeResultSet (rset);
00289
00290 ss<<record;
00291
00292 sqlStmt = "SELECT map_id,map_index,layer_id from "+tab_map+" where record_id="+ss.str()+" order by map_index";
00293 ss.str("");
00294 stmt->setSQL(sqlStmt);
00295 rset = stmt->executeQuery ();
00296
00297 while (rset->next ())
00298 {map_id = rset->getInt (1);
00299 rset->getInt (2);
00300 layer_id = rset->getInt (3);
00301 ss<<map_id;
00302 sqlStmt1 = "SELECT * from "+tab_data+" where map_id="+ss.str()+" order by vec_index";
00303 ss.str("");
00304 stmt1->setSQL(sqlStmt1);
00305 rset1 = stmt1->executeQuery ();
00306
00307 while (rset1->next ())
00308 {vec_index = rset1->getInt (2);
00309 obj->obj[layer_id].resize(vec_index);
00310 for(i=0;i<num_var;++i){
00311 obj->obj[layer_id][vec_index-1].push_back(rset1->getFloat(3+i));
00312 }
00313 }
00314
00315
00316
00317
00318
00319
00320 }
00321
00322
00323
00324
00325
00326
00327 }