00001 #include <stdexcept>
00002 #include "OnlineDB/Oracle/interface/Oracle.h"
00003
00004 #include "OnlineDB/EcalCondDB/interface/RunIOV.h"
00005 #include "OnlineDB/EcalCondDB/interface/RunTag.h"
00006 #include "OnlineDB/EcalCondDB/interface/Tm.h"
00007 #include "OnlineDB/EcalCondDB/interface/DateHandler.h"
00008
00009 using namespace std;
00010 using namespace oracle::occi;
00011
00012 RunIOV::RunIOV()
00013 {
00014 m_conn = NULL;
00015 m_ID = 0;
00016 m_runNum = 0;
00017 m_runStart = Tm();
00018 m_runEnd = Tm();
00019 }
00020
00021
00022
00023 RunIOV::~RunIOV()
00024 {
00025 }
00026
00027
00028
00029 void RunIOV::setRunNumber(run_t run)
00030 {
00031 if ( run != m_runNum) {
00032 m_ID = 0;
00033 m_runNum = run;
00034 }
00035 }
00036
00037 void RunIOV::setID(int id)
00038 {
00039 m_ID = id;
00040 }
00041
00042
00043
00044
00045 run_t RunIOV::getRunNumber() const
00046 {
00047 return m_runNum;
00048 }
00049
00050
00051
00052 void RunIOV::setRunStart(Tm start)
00053 {
00054 if (start != m_runStart) {
00055 m_ID = 0;
00056 m_runStart = start;
00057 }
00058 }
00059
00060
00061
00062 Tm RunIOV::getRunStart() const
00063 {
00064 return m_runStart;
00065 }
00066
00067
00068
00069 void RunIOV::setRunEnd(Tm end)
00070 {
00071 if (end != m_runEnd) {
00072 m_ID = 0;
00073 m_runEnd = end;
00074 }
00075 }
00076
00077
00078
00079 Tm RunIOV::getRunEnd() const
00080 {
00081 return m_runEnd;
00082 }
00083
00084
00085
00086 void RunIOV::setRunTag(RunTag tag)
00087 {
00088 if (tag != m_runTag) {
00089 m_ID = 0;
00090 m_runTag = tag;
00091 }
00092 }
00093
00094
00095
00096 RunTag RunIOV::getRunTag() const
00097 {
00098 return m_runTag;
00099 }
00100
00101
00102
00103 int RunIOV::fetchID()
00104 throw(std::runtime_error)
00105 {
00106
00107 if (m_ID) {
00108 return m_ID;
00109 }
00110
00111 this->checkConnection();
00112
00113 m_runTag.setConnection(m_env, m_conn);
00114 int tagID = m_runTag.fetchID();
00115 if (!tagID) {
00116 return 0;
00117 }
00118
00119 DateHandler dh(m_env, m_conn);
00120
00121 if (m_runEnd.isNull()) {
00122 m_runEnd = dh.getPlusInfTm();
00123 }
00124
00125 try {
00126 Statement* stmt = m_conn->createStatement();
00127 stmt->setSQL("SELECT iov_id FROM run_iov "
00128 "WHERE tag_id = :tag_id AND "
00129 "run_num = :run_num AND "
00130 "run_start = :run_start " );
00131 stmt->setInt(1, tagID);
00132 stmt->setInt(2, m_runNum);
00133 stmt->setDate(3, dh.tmToDate(m_runStart));
00134
00135 ResultSet* rset = stmt->executeQuery();
00136
00137 if (rset->next()) {
00138 m_ID = rset->getInt(1);
00139 } else {
00140 m_ID = 0;
00141 }
00142 m_conn->terminateStatement(stmt);
00143 } catch (SQLException &e) {
00144 throw(std::runtime_error("RunIOV::fetchID: "+e.getMessage()));
00145 }
00146
00147 return m_ID;
00148 }
00149
00150
00151
00152 void RunIOV::setByID(int id)
00153 throw(std::runtime_error)
00154 {
00155 this->checkConnection();
00156
00157 DateHandler dh(m_env, m_conn);
00158
00159 try {
00160 Statement* stmt = m_conn->createStatement();
00161
00162 stmt->setSQL("SELECT tag_id, run_num, run_start, run_end FROM run_iov WHERE iov_id = :1");
00163 stmt->setInt(1, id);
00164
00165 ResultSet* rset = stmt->executeQuery();
00166 if (rset->next()) {
00167 int tagID = rset->getInt(1);
00168 m_runNum = rset->getInt(2);
00169 Date startDate = rset->getDate(3);
00170 Date endDate = rset->getDate(4);
00171
00172 m_runStart = dh.dateToTm( startDate );
00173 m_runEnd = dh.dateToTm( endDate );
00174
00175 m_runTag.setConnection(m_env, m_conn);
00176 m_runTag.setByID(tagID);
00177 m_ID = id;
00178 } else {
00179 throw(std::runtime_error("RunIOV::setByID: Given tag_id is not in the database"));
00180 }
00181
00182 m_conn->terminateStatement(stmt);
00183 } catch (SQLException &e) {
00184 throw(std::runtime_error("RunIOV::setByID: "+e.getMessage()));
00185 }
00186 }
00187
00188
00189
00190 int RunIOV::writeDB()
00191 throw(std::runtime_error)
00192 {
00193 this->checkConnection();
00194
00195
00196 if (this->fetchID()) {
00197 return m_ID;
00198 }
00199
00200 m_runTag.setConnection(m_env, m_conn);
00201 int tagID = m_runTag.writeDB();
00202
00203
00204 DateHandler dh(m_env, m_conn);
00205
00206 if (m_runStart.isNull()) {
00207 throw(std::runtime_error("RunIOV::writeDB: Must setRunStart before writing"));
00208 }
00209
00210 if (m_runEnd.isNull()) {
00211 m_runEnd = dh.getPlusInfTm();
00212 }
00213
00214 try {
00215 Statement* stmt = m_conn->createStatement();
00216
00217 stmt->setSQL("INSERT INTO run_iov (iov_id, tag_id, run_num, run_start, run_end) "
00218 "VALUES (run_iov_sq.NextVal, :1, :2, :3, :4)");
00219 stmt->setInt(1, tagID);
00220 stmt->setInt(2, m_runNum);
00221 stmt->setDate(3, dh.tmToDate(m_runStart));
00222 stmt->setDate(4, dh.tmToDate(m_runEnd));
00223
00224 stmt->executeUpdate();
00225
00226 m_conn->terminateStatement(stmt);
00227 } catch (SQLException &e) {
00228 throw(std::runtime_error("RunIOV::writeDB: "+e.getMessage()));
00229 }
00230
00231
00232 if (!this->fetchID()) {
00233 throw(std::runtime_error("RunIOV::writeDB: Failed to write"));
00234 }
00235
00236 return m_ID;
00237 }
00238
00239
00240 int RunIOV::updateEndTimeDB()
00241 throw(std::runtime_error)
00242 {
00243 this->checkConnection();
00244
00245
00246 if(!this->fetchID()){
00247 this->writeDB();
00248 }
00249
00250
00251 m_runTag.setConnection(m_env, m_conn);
00252
00253
00254
00255 DateHandler dh(m_env, m_conn);
00256
00257
00258 if (m_runEnd.isNull()) {
00259 m_runEnd = dh.getPlusInfTm();
00260 }
00261
00262 try {
00263 Statement* stmt = m_conn->createStatement();
00264
00265 stmt->setSQL("UPDATE run_iov set run_end=:1 where iov_id=:2 " );
00266 stmt->setDate(1, dh.tmToDate(m_runEnd));
00267 stmt->setInt(2, m_ID);
00268
00269 stmt->executeUpdate();
00270
00271 m_conn->terminateStatement(stmt);
00272 } catch (SQLException &e) {
00273 throw(std::runtime_error("RunIOV::writeDB: "+e.getMessage()));
00274 }
00275
00276
00277 if (!this->fetchID()) {
00278 throw(std::runtime_error("RunIOV::writeDB: Failed to write"));
00279 }
00280
00281 return m_ID;
00282 }
00283
00284 int RunIOV::fetchIDByRunAndTag()
00285 throw(std::runtime_error)
00286 {
00287
00288 if (m_ID) {
00289 return m_ID;
00290 }
00291
00292 this->checkConnection();
00293
00294 m_runTag.setConnection(m_env, m_conn);
00295 int tagID = m_runTag.fetchID();
00296 if (!tagID) {
00297 return 0;
00298 }
00299
00300 DateHandler dh(m_env, m_conn);
00301
00302 if (m_runEnd.isNull()) {
00303 m_runEnd = dh.getPlusInfTm();
00304 }
00305
00306 try {
00307 Statement* stmt = m_conn->createStatement();
00308 stmt->setSQL("SELECT iov_id FROM run_iov "
00309 "WHERE tag_id = :tag_id AND "
00310 "run_num = :run_num " );
00311 stmt->setInt(1, tagID);
00312 stmt->setInt(2, m_runNum);
00313
00314 ResultSet* rset = stmt->executeQuery();
00315
00316 if (rset->next()) {
00317 m_ID = rset->getInt(1);
00318 } else {
00319 m_ID = 0;
00320 }
00321 m_conn->terminateStatement(stmt);
00322 } catch (SQLException &e) {
00323 throw(std::runtime_error("RunIOV::fetchID: "+e.getMessage()));
00324 }
00325
00326 return m_ID;
00327 }
00328
00329
00330 int RunIOV::updateStartTimeDB()
00331 throw(std::runtime_error)
00332 {
00333 this->checkConnection();
00334
00335
00336 if(!this->fetchIDByRunAndTag()){
00337 this->writeDB();
00338 }
00339
00340
00341
00342
00343
00344
00345 DateHandler dh(m_env, m_conn);
00346
00347
00348 if (m_runEnd.isNull()) {
00349 m_runEnd = dh.getPlusInfTm();
00350 }
00351
00352 try {
00353 Statement* stmt = m_conn->createStatement();
00354
00355 stmt->setSQL("UPDATE run_iov set run_start=:1 where iov_id=:2 " );
00356 stmt->setDate(1, dh.tmToDate(m_runStart));
00357 stmt->setInt(2, m_ID);
00358
00359 stmt->executeUpdate();
00360
00361 m_conn->terminateStatement(stmt);
00362 } catch (SQLException &e) {
00363 throw(std::runtime_error("RunIOV::writeDB: "+e.getMessage()));
00364 }
00365
00366
00367 if (!this->fetchID()) {
00368 throw(std::runtime_error("RunIOV::writeDB: Failed to write"));
00369 }
00370
00371 return m_ID;
00372 }
00373
00374
00375
00376 void RunIOV::setByRun(RunTag* tag, run_t run)
00377 throw(std::runtime_error)
00378 {
00379 this->checkConnection();
00380
00381 tag->setConnection(m_env, m_conn);
00382 int tagID = tag->fetchID();
00383 if (!tagID) {
00384 throw(std::runtime_error("RunIOV::setByRun: Given tag is not in the database"));
00385 }
00386
00387 DateHandler dh(m_env, m_conn);
00388
00389 try {
00390 Statement* stmt = m_conn->createStatement();
00391
00392 stmt->setSQL("SELECT iov_id, run_start, run_end FROM run_iov WHERE tag_id = :1 AND run_num = :2");
00393 stmt->setInt(1, tagID);
00394 stmt->setInt(2, run);
00395
00396 ResultSet* rset = stmt->executeQuery();
00397 if (rset->next()) {
00398 m_runTag = *tag;
00399 m_runNum = run;
00400
00401 m_ID = rset->getInt(1);
00402 Date startDate = rset->getDate(2);
00403 Date endDate = rset->getDate(3);
00404
00405 m_runStart = dh.dateToTm( startDate );
00406 m_runEnd = dh.dateToTm( endDate );
00407 } else {
00408 throw(std::runtime_error("RunIOV::setByRun: Given run is not in the database"));
00409 }
00410
00411 m_conn->terminateStatement(stmt);
00412 } catch (SQLException &e) {
00413 throw(std::runtime_error("RunIOV::setByRun: "+e.getMessage()));
00414 }
00415 }
00416
00417 void RunIOV::setByTime(std::string location, const Tm &t)
00418 throw(std::runtime_error)
00419 {
00420 this->checkConnection();
00421
00422 DateHandler dh(m_env, m_conn);
00423
00424 try {
00425 Statement* stmt = m_conn->createStatement();
00426
00427 stmt->setSQL("SELECT iov_id FROM (SELECT iov_id FROM run_iov riov "
00428 "JOIN run_tag rtag ON riov.tag_id = rtag.tag_id "
00429 "JOIN location_def loc ON rtag.location_id = loc.def_id "
00430 "WHERE loc.location = :1 AND "
00431 "run_start <= to_date(:2, 'YYYY-MM-DD HH24:MI:SS') AND "
00432 "run_end >= to_date(:3, 'YYYY-MM-DD HH24:MI:SS') "
00433 "AND rtag.gen_tag != 'INVALID' ORDER BY iov_id DESC) "
00434 "where ROWNUM <=1");
00435 stmt->setString(1, location);
00436 stmt->setString(2, t.str());
00437 stmt->setString(3, t.str());
00438
00439 ResultSet* rset = stmt->executeQuery();
00440 if (rset->next()) {
00441 int id = rset->getInt(1);
00442 this->setByID(id);
00443 } else {
00444 throw(std::runtime_error("RunIOV::setByTime(loc, run): Given run is not in the database"));
00445 }
00446
00447
00448 if (rset->next()) {
00449 throw(std::runtime_error("RunIOV::setByTime(loc, run): Run is nonunique for given location."));
00450 }
00451
00452 m_conn->terminateStatement(stmt);
00453 } catch (SQLException &e) {
00454 throw(std::runtime_error("RunIOV::setByTime(loc, run): " + e.getMessage()));
00455 }
00456 }
00457
00458 void RunIOV::setByRun(std::string location, run_t run)
00459 throw(std::runtime_error)
00460 {
00461 this->checkConnection();
00462
00463 DateHandler dh(m_env, m_conn);
00464
00465 try {
00466 Statement* stmt = m_conn->createStatement();
00467
00468 stmt->setSQL("SELECT iov_id FROM run_iov riov "
00469 "JOIN run_tag rtag ON riov.tag_id = rtag.tag_id "
00470 "JOIN location_def loc ON rtag.location_id = loc.def_id "
00471 "WHERE loc.location = :1 AND riov.run_num = :2 "
00472 "AND rtag.gen_tag != 'INVALID'");
00473 stmt->setString(1, location);
00474 stmt->setInt(2, run);
00475
00476 ResultSet* rset = stmt->executeQuery();
00477 if (rset->next()) {
00478 int id = rset->getInt(1);
00479 this->setByID(id);
00480 } else {
00481 throw(std::runtime_error("RunIOV::setByRun(loc, run): Given run is not in the database"));
00482 }
00483
00484
00485 if (rset->next()) {
00486 throw(std::runtime_error("RunIOV::setByRun(loc, run): Run is nonunique for given location."));
00487 }
00488
00489 m_conn->terminateStatement(stmt);
00490 } catch (SQLException &e) {
00491 throw(std::runtime_error("RunIOV::setByRun(loc, run): "+e.getMessage()));
00492 }
00493 }
00494
00495
00496 void RunIOV::setByRecentData(std::string dataTable, RunTag* tag, run_t run)
00497 throw(std::runtime_error)
00498 {
00499 this->checkConnection();
00500
00501 tag->setConnection(m_env, m_conn);
00502 int tagID = tag->fetchID();
00503 if (!tagID) {
00504 throw(std::runtime_error("RunIOV::setByRecentData: Given tag is not in the database"));
00505 }
00506
00507 DateHandler dh(m_env, m_conn);
00508
00509 try {
00510 Statement* stmt = m_conn->createStatement();
00511
00512 stmt->setSQL("SELECT * FROM (SELECT riov.iov_id, riov.run_num, riov.run_start, riov.run_end "
00513 "FROM run_iov riov "
00514 "JOIN "+dataTable+" dat on dat.iov_id = riov.iov_id "
00515 "WHERE tag_id = :1 AND riov.run_num <= :run ORDER BY riov.run_num DESC) WHERE rownum = 1");
00516
00517 stmt->setInt(1, tagID);
00518 stmt->setInt(2, run);
00519
00520 ResultSet* rset = stmt->executeQuery();
00521 if (rset->next()) {
00522 m_runTag = *tag;
00523
00524 m_ID = rset->getInt(1);
00525 m_runNum = rset->getInt(2);
00526 Date startDate = rset->getDate(3);
00527 Date endDate = rset->getDate(4);
00528
00529 m_runStart = dh.dateToTm( startDate );
00530 m_runEnd = dh.dateToTm( endDate );
00531 } else {
00532 throw(std::runtime_error("RunIOV::setByRecentData: No data exists for given tag and run"));
00533 }
00534
00535 m_conn->terminateStatement(stmt);
00536 } catch (SQLException &e) {
00537 throw(std::runtime_error("RunIOV::setByRecentData: "+e.getMessage()));
00538 }
00539 }
00540
00541
00542
00543
00544
00545
00546 void RunIOV::setByRecentData(std::string dataTable, std::string location, run_t run)
00547 throw(std::runtime_error)
00548 {
00549 this->checkConnection();
00550
00551 DateHandler dh(m_env, m_conn);
00552
00553 try {
00554 Statement* stmt = m_conn->createStatement();
00555
00556 stmt->setSQL("SELECT * FROM (SELECT riov.iov_id, riov.run_num, riov.run_start, riov.run_end "
00557 "FROM run_iov riov "
00558 "JOIN "+dataTable+" dat on dat.iov_id = riov.iov_id "
00559 "JOIN run_tag rtag ON riov.tag_id = rtag.tag_id "
00560 "JOIN location_def loc ON rtag.location_id = loc.def_id "
00561 "WHERE loc.location = :1 AND riov.run_num <= :2 ORDER BY riov.run_num DESC ) WHERE rownum = 1");
00562
00563 stmt->setString(1, location);
00564 stmt->setInt(2, run);
00565
00566 ResultSet* rset = stmt->executeQuery();
00567
00568
00569 if (rset->next()) {
00570 int id = rset->getInt(1);
00571 this->setByID(id);
00572 } else {
00573 throw(std::runtime_error("RunIOV::setByRecentData(datatable, loc, run): Given run is not in the database"));
00574 }
00575
00576
00577 m_conn->terminateStatement(stmt);
00578 } catch (SQLException &e) {
00579 throw(std::runtime_error("RunIOV::setByRecentData: "+e.getMessage()));
00580 }
00581 }
00582