00001
00002
00003 #include <iostream>
00004 #include <string>
00005 #include <vector>
00006 #include <sstream>
00007 #include <stdlib.h>
00008 #include <cstdlib>
00009 #include <time.h>
00010 #include <stdexcept>
00011 #include "OnlineDB/Oracle/interface/Oracle.h"
00012
00013 #include "OnlineDB/EcalCondDB/interface/IDBObject.h"
00014 #include "OnlineDB/EcalCondDB/interface/EcalCondDBInterface.h"
00015 #include "OnlineDB/EcalCondDB/interface/EcalDBConnection.h"
00016 #include "OnlineDB/EcalCondDB/interface/EcalLogicID.h"
00017 #include "OnlineDB/EcalCondDB/interface/DCSPTMTempList.h"
00018
00019 #include "OnlineDB/EcalCondDB/interface/RunTag.h"
00020 #include "OnlineDB/EcalCondDB/interface/RunIOV.h"
00021 #include "OnlineDB/EcalCondDB/interface/RunList.h"
00022 #include "OnlineDB/EcalCondDB/interface/LMFRunList.h"
00023 #include "OnlineDB/EcalCondDB/interface/MonRunList.h"
00024 #include "OnlineDB/EcalCondDB/interface/MonRunTag.h"
00025 #include "OnlineDB/EcalCondDB/interface/LMFRunTag.h"
00026 #include "OnlineDB/EcalCondDB/interface/LMFMatacqBlueDat.h"
00027
00028 using namespace std;
00029 using namespace oracle::occi;
00030
00031
00032 EcalLogicID EcalCondDBInterface::getEcalLogicID( int logicID )
00033 throw(runtime_error)
00034 {
00035
00036 string sql = "SELECT name, logic_id, id1, id2, id3, maps_to FROM channelView WHERE logic_id = :logicID AND name=maps_to";
00037
00038 int id1, id2, id3;
00039 string name, mapsTo;
00040
00041 try {
00042 stmt->setSQL(sql);
00043 stmt->setInt(1, logicID);
00044 ResultSet* rset = stmt->executeQuery();
00045
00046 if (rset->next()) {
00047 name = rset->getString(1);
00048 logicID = rset->getInt(2);
00049 id1 = rset->getInt(3);
00050 if (rset->isNull(3)) { id1 = EcalLogicID::NULLID; }
00051 id2 = rset->getInt(4);
00052 if (rset->isNull(4)) { id2 = EcalLogicID::NULLID; }
00053 id3 = rset->getInt(5);
00054 if (rset->isNull(5)) { id3 = EcalLogicID::NULLID; }
00055 mapsTo = rset->getString(6);
00056 } else {
00057 stringstream msg;
00058 msg << "ERROR: Cannot build EcalLogicID for logic_id " << logicID;
00059 throw(runtime_error(msg.str()));
00060 }
00061
00062 } catch (SQLException &e) {
00063 throw(runtime_error("ERROR: Failed to retrive ids: " + e.getMessage() ));
00064 }
00065
00066 return EcalLogicID( name, logicID, id1, id2, id3, mapsTo );
00067 }
00068
00069
00070
00071 EcalLogicID EcalCondDBInterface::getEcalLogicID( string name,
00072 int id1,
00073 int id2,
00074 int id3,
00075 string mapsTo )
00076 throw(runtime_error)
00077 {
00078
00079 if (mapsTo == "") {
00080 mapsTo = name;
00081 }
00082
00083
00084 stringstream ss;
00085 ss << "SELECT logic_id FROM channelView WHERE name = :n AND";
00086 int idarray[] = {id1, id2, id3};
00087 for (int i=1; i<=3; i++) {
00088 if (idarray[i-1] == EcalLogicID::NULLID) {
00089 ss << " id"<<i<<" IS NULL AND";
00090 } else {
00091 ss << " id"<<i<<" = :id"<<i<<" AND";
00092 }
00093 }
00094 ss <<" maps_to = :m";
00095
00096
00097
00098 int logic_id;
00099 try {
00100 stmt->setSQL(ss.str());
00101
00102
00103 int j = 1;
00104 stmt->setString(j, name);
00105 j++;
00106 for (int i=0; i<3; i++) {
00107 if (idarray[i] != EcalLogicID::NULLID) {
00108 stmt->setInt(j, idarray[i]);
00109 j++;
00110 }
00111 }
00112 stmt->setString(j, mapsTo);
00113
00114
00115 ResultSet* rset = stmt->executeQuery();
00116 if ( rset->next() ) {
00117 logic_id = rset->getInt(1);
00118 } else {
00119 stringstream msg;
00120 msg << "ERROR: Query for EcalLogicID failed for parameters [" <<
00121 "name=" << name << ",maps_to=" << mapsTo <<
00122 ",id1=" << id1 << ",id2=" << id2 << ",id3=" << id3 << "]";
00123 throw(runtime_error(msg.str()));
00124 }
00125 } catch (SQLException &e) {
00126 throw(runtime_error("ERROR: Failed to retrive logic_id: " + e.getMessage() ));
00127 }
00128
00129
00130 return EcalLogicID(name, logic_id, id1, id2, id3, mapsTo);
00131 }
00132
00133
00134
00135 vector<EcalLogicID> EcalCondDBInterface::getEcalLogicIDSet( string name,
00136 int fromId1, int toId1,
00137 int fromId2, int toId2,
00138 int fromId3, int toId3,
00139 string mapsTo )
00140 throw(runtime_error)
00141 {
00142 if (mapsTo == "") {
00143 mapsTo = name;
00144 }
00145
00146 int idArray[] = { fromId1, toId1, fromId2, toId2, fromId3, toId3 };
00147 int from, to;
00148
00149 stringstream ss;
00150 ss << "SELECT name, logic_id, id1, id2, id3, maps_to FROM channelView WHERE name = :name AND ";
00151
00152
00153 for (int i=1; i<=3; i++) {
00154 from = idArray[2*(i-1)];
00155 to = idArray[2*(i-1) + 1];
00156
00157
00158 if ((from == EcalLogicID::NULLID && to != EcalLogicID::NULLID) ||
00159 (from != EcalLogicID::NULLID && to == EcalLogicID::NULLID) ||
00160 (from > to)) {
00161 throw(runtime_error("ERROR: Bad arguments for getEcalLogicIDSet"));
00162 }
00163
00164
00165 if (from == EcalLogicID::NULLID && to == EcalLogicID::NULLID) {
00166 ss << "id" << i << " IS NULL AND ";
00167 } else {
00168 ss << "id" << i << " >= :id" << i << "from AND " <<
00169 "id" << i << " <= :id" << i << "to AND ";
00170 }
00171 }
00172 ss << "maps_to = :maps_to ORDER BY id1, id2, id3";
00173
00174 vector<EcalLogicID> result;
00175
00176 try {
00177 stmt->setSQL(ss.str());
00178
00179
00180 int j = 1;
00181 stmt->setString(j, name);
00182 j++;
00183
00184 for (int i=0; i<3; i++) {
00185 from = idArray[2*i];
00186 to = idArray[2*i + 1];
00187 if (from != EcalLogicID::NULLID) {
00188 stmt->setInt(j, from);
00189 j++;
00190 stmt->setInt(j, to);
00191 j++;
00192 }
00193 }
00194
00195 stmt->setString(j, mapsTo);
00196
00197
00198 stmt->setPrefetchRowCount(IDBObject::ECALDB_NROWS);
00199
00200 ResultSet* rset = stmt->executeQuery();
00201
00202 int id1, id2, id3, logicId;
00203
00204 while (rset->next()) {
00205 name = rset->getString(1);
00206 logicId = rset->getInt(2);
00207 id1 = rset->getInt(3);
00208 if (rset->isNull(3)) { id1 = EcalLogicID::NULLID; }
00209 id2 = rset->getInt(4);
00210 if (rset->isNull(4)) { id2 = EcalLogicID::NULLID; }
00211 id3 = rset->getInt(5);
00212 if (rset->isNull(5)) { id3 = EcalLogicID::NULLID; }
00213 mapsTo = rset->getString(6);
00214
00215 EcalLogicID ecid = EcalLogicID( name, logicId, id1, id2, id3, mapsTo );
00216 result.push_back(ecid);
00217 }
00218 stmt->setPrefetchRowCount(0);
00219
00220 } catch (SQLException &e) {
00221 throw(runtime_error("ERROR: Failure while getting EcalLogicID set: " + e.getMessage() ));
00222 }
00223
00224 return result;
00225 }
00226
00227 vector<EcalLogicID> EcalCondDBInterface::getEcalLogicIDSetOrdered( string name,
00228 int fromId1, int toId1,
00229 int fromId2, int toId2,
00230 int fromId3, int toId3,
00231 string mapsTo, int orderedBy )
00232
00233
00234 throw(runtime_error)
00235 {
00236 if (mapsTo == "") {
00237 mapsTo = name;
00238 }
00239
00240 int idArray[] = { fromId1, toId1, fromId2, toId2, fromId3, toId3 };
00241 int from, to;
00242
00243 stringstream ss;
00244 ss << "SELECT name, logic_id, id1, id2, id3, maps_to FROM channelView WHERE name = :name AND ";
00245
00246
00247 for (int i=1; i<=3; i++) {
00248 from = idArray[2*(i-1)];
00249 to = idArray[2*(i-1) + 1];
00250
00251
00252 if ((from == EcalLogicID::NULLID && to != EcalLogicID::NULLID) ||
00253 (from != EcalLogicID::NULLID && to == EcalLogicID::NULLID) ||
00254 (from > to)) {
00255 throw(runtime_error("ERROR: Bad arguments for getEcalLogicIDSet"));
00256 }
00257
00258
00259 if (from == EcalLogicID::NULLID && to == EcalLogicID::NULLID) {
00260 ss << "id" << i << " IS NULL AND ";
00261 } else {
00262 ss << "id" << i << " >= :id" << i << "from AND " <<
00263 "id" << i << " <= :id" << i << "to AND ";
00264 }
00265 }
00266 ss << "maps_to = :maps_to ";
00267
00268 if(orderedBy==EcalLogicID::NULLID){
00269 ss<<" ORDER BY id1, id2, id3";
00270 } else if(orderedBy==1 || orderedBy==12 || orderedBy==123){
00271 ss<<" ORDER BY id1, id2, id3 ";
00272 } else if (orderedBy==213 || orderedBy==21 ){
00273 ss<<" ORDER BY id2, id1, id3 ";
00274 } else if (orderedBy==231|| orderedBy==23){
00275 ss<<" ORDER BY id2, id3, id1 ";
00276 } else if (orderedBy==321|| orderedBy==32){
00277 ss<<" ORDER BY id3, id2, id1 ";
00278 } else if (orderedBy==312|| orderedBy==31){
00279 ss<<" ORDER BY id3, id1, id2 ";
00280 } else if (orderedBy==132|| orderedBy==13){
00281 ss<<" ORDER BY id1, id3, id2 ";
00282 } else if (orderedBy==4) {
00283 ss<<" ORDER BY logic_id ";
00284 } else {
00285 ss<<" ORDER BY id1, id2, id3";
00286 }
00287
00288 vector<EcalLogicID> result;
00289
00290 try {
00291 stmt->setSQL(ss.str());
00292
00293
00294 int j = 1;
00295 stmt->setString(j, name);
00296 j++;
00297
00298 for (int i=0; i<3; i++) {
00299 from = idArray[2*i];
00300 to = idArray[2*i + 1];
00301 if (from != EcalLogicID::NULLID) {
00302 stmt->setInt(j, from);
00303 j++;
00304 stmt->setInt(j, to);
00305 j++;
00306 }
00307 }
00308
00309 stmt->setString(j, mapsTo);
00310
00311
00312 stmt->setPrefetchRowCount(IDBObject::ECALDB_NROWS);
00313
00314 ResultSet* rset = stmt->executeQuery();
00315
00316 int id1, id2, id3, logicId;
00317
00318 while (rset->next()) {
00319 name = rset->getString(1);
00320 logicId = rset->getInt(2);
00321 id1 = rset->getInt(3);
00322 if (rset->isNull(3)) { id1 = EcalLogicID::NULLID; }
00323 id2 = rset->getInt(4);
00324 if (rset->isNull(4)) { id2 = EcalLogicID::NULLID; }
00325 id3 = rset->getInt(5);
00326 if (rset->isNull(5)) { id3 = EcalLogicID::NULLID; }
00327 mapsTo = rset->getString(6);
00328
00329 EcalLogicID ecid = EcalLogicID( name, logicId, id1, id2, id3, mapsTo );
00330 result.push_back(ecid);
00331 }
00332 stmt->setPrefetchRowCount(0);
00333
00334 } catch (SQLException &e) {
00335 throw(runtime_error("ERROR: Failure while getting EcalLogicID set: " + e.getMessage() ));
00336 }
00337
00338 return result;
00339 }
00340
00341
00342
00343 void EcalCondDBInterface::insertRunIOV(RunIOV* iov)
00344 throw(runtime_error)
00345 {
00346 try {
00347 iov->setConnection(env, conn);
00348 iov->writeDB();
00349 } catch(runtime_error &e) {
00350 conn->rollback();
00351 throw(e);
00352 }
00353 conn->commit();
00354 }
00355
00356 void EcalCondDBInterface::updateRunIOV(RunIOV* iov)
00357 throw(runtime_error)
00358 {
00359 try {
00360 iov->setConnection(env, conn);
00361 iov->updateEndTimeDB();
00362 } catch(runtime_error &e) {
00363 conn->rollback();
00364 throw(e);
00365 }
00366 conn->commit();
00367 }
00368
00369 void EcalCondDBInterface::updateRunConfig(ODRunConfigInfo* od)
00370 throw(runtime_error)
00371 {
00372 try {
00373 od->setConnection(env, conn);
00374 od->updateDefaultCycle();
00375 } catch(runtime_error &e) {
00376 conn->rollback();
00377 throw(e);
00378 }
00379 conn->commit();
00380 }
00381
00382
00383
00384 RunIOV EcalCondDBInterface::fetchRunIOV(RunTag* tag, run_t run)
00385 throw(runtime_error)
00386 {
00387 RunIOV iov;
00388 iov.setConnection(env, conn);
00389 iov.setByRun(tag, run);
00390 return iov;
00391 }
00392
00393
00394
00395 RunIOV EcalCondDBInterface::fetchRunIOV(std::string location, run_t run)
00396 throw(runtime_error)
00397 {
00398 RunIOV iov;
00399 iov.setConnection(env, conn);
00400 iov.setByRun(location, run);
00401 return iov;
00402 }
00403
00404
00405
00406
00407 MonRunIOV EcalCondDBInterface::fetchMonRunIOV(RunTag* runtag, MonRunTag* montag, run_t run, subrun_t subrun)
00408 throw(runtime_error)
00409 {
00410 RunIOV runiov = fetchRunIOV(runtag, run);
00411 MonRunIOV moniov;
00412 moniov.setConnection(env, conn);
00413 moniov.setByRun(montag, &runiov, subrun);
00414 return moniov;
00415 }
00416
00417
00418
00419 DCUIOV EcalCondDBInterface::fetchDCUIOV(DCUTag* tag, Tm eventTm)
00420 throw(runtime_error)
00421 {
00422 DCUIOV dcuiov;
00423 dcuiov.setConnection(env, conn);
00424 dcuiov.setByTm(tag, eventTm);
00425 return dcuiov;
00426 }
00427
00428
00429
00430 LMFRunIOV EcalCondDBInterface::fetchLMFRunIOV(RunTag* runtag, LMFRunTag* lmftag, run_t run, subrun_t subrun)
00431 throw(runtime_error)
00432 {
00433 RunIOV runiov = fetchRunIOV(runtag, run);
00434 LMFRunIOV lmfiov;
00435 lmfiov.setConnection(env, conn);
00436 lmfiov.setByRun(lmftag, &runiov, subrun);
00437 return lmfiov;
00438 }
00439
00440
00441
00442 CaliIOV EcalCondDBInterface::fetchCaliIOV(CaliTag* tag, Tm eventTm)
00443 throw(runtime_error)
00444 {
00445 CaliIOV caliiov;
00446 caliiov.setConnection(env, conn);
00447 caliiov.setByTm(tag, eventTm);
00448 return caliiov;
00449 }
00450
00451 DCSPTMTempList EcalCondDBInterface::fetchDCSPTMTempList(EcalLogicID ecid)
00452 throw(runtime_error)
00453 {
00454 DCSPTMTempList r;
00455 r.setConnection(env, conn);
00456 r.fetchValuesForECID(ecid);
00457 return r;
00458 }
00459
00460 DCSPTMTempList EcalCondDBInterface::fetchDCSPTMTempList(EcalLogicID ecid, Tm start, Tm end)
00461 throw(runtime_error)
00462 {
00463 DCSPTMTempList r;
00464 r.setConnection(env, conn);
00465 r.fetchValuesForECIDAndTime(ecid, start, end);
00466 return r;
00467 }
00468
00469 RunList EcalCondDBInterface::fetchRunList(RunTag tag)
00470 throw(runtime_error)
00471 {
00472 RunList r;
00473 r.setConnection(env, conn);
00474 r.setRunTag(tag);
00475 r.fetchRuns();
00476 return r;
00477 }
00478
00479 LMFRunList EcalCondDBInterface::fetchLMFRunList(RunTag tag, LMFRunTag lmfrunTag)
00480 throw(runtime_error)
00481 {
00482 LMFRunList r;
00483 r.setConnection(env, conn);
00484 r.setRunTag(tag);
00485 r.setLMFRunTag(lmfrunTag);
00486 r.fetchRuns();
00487 return r;
00488 }
00489
00490 LMFRunList EcalCondDBInterface::fetchLMFRunList(RunTag tag, LMFRunTag lmfrunTag,int min_run, int max_run)
00491 throw(runtime_error)
00492 {
00493 LMFRunList r;
00494 r.setConnection(env, conn);
00495 r.setRunTag(tag);
00496 r.setLMFRunTag(lmfrunTag);
00497 r.fetchRuns(min_run, max_run);
00498 return r;
00499 }
00500
00501 LMFRunList EcalCondDBInterface::fetchLMFRunListLastNRuns(RunTag tag, LMFRunTag lmfrunTag,int max_run, int n_runs )
00502 throw(runtime_error)
00503 {
00504 LMFRunList r;
00505 r.setConnection(env, conn);
00506 r.setRunTag(tag);
00507 r.setLMFRunTag(lmfrunTag);
00508 r.fetchLastNRuns(max_run, n_runs );
00509 return r;
00510 }
00511
00512
00513
00514 MonRunList EcalCondDBInterface::fetchMonRunList(RunTag tag, MonRunTag monrunTag)
00515 throw(runtime_error)
00516 {
00517 MonRunList r;
00518 r.setConnection(env, conn);
00519 r.setRunTag(tag);
00520 r.setMonRunTag(monrunTag);
00521 r.fetchRuns();
00522 return r;
00523 }
00524
00525 MonRunList EcalCondDBInterface::fetchMonRunList(RunTag tag, MonRunTag monrunTag,int min_run, int max_run)
00526 throw(runtime_error)
00527 {
00528 MonRunList r;
00529 r.setConnection(env, conn);
00530 r.setRunTag(tag);
00531 r.setMonRunTag(monrunTag);
00532 r.fetchRuns(min_run, max_run);
00533 return r;
00534 }
00535
00536 MonRunList EcalCondDBInterface::fetchMonRunListLastNRuns(RunTag tag, MonRunTag monrunTag,int max_run, int n_runs )
00537 throw(runtime_error)
00538 {
00539 MonRunList r;
00540 r.setConnection(env, conn);
00541 r.setRunTag(tag);
00542 r.setMonRunTag(monrunTag);
00543 r.fetchLastNRuns(max_run, n_runs );
00544 return r;
00545 }
00546
00547
00548
00549 void EcalCondDBInterface::dummy()
00550 {
00551 }