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(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(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(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(runtime_error("RunIOV::setByID: "+e.getMessage()));
00185 }
00186 }
00187
00188
00189
00190 int RunIOV::writeDB()
00191 throw(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(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(runtime_error("RunIOV::writeDB: "+e.getMessage()));
00229 }
00230
00231
00232 if (!this->fetchID()) {
00233 throw(runtime_error("RunIOV::writeDB: Failed to write"));
00234 }
00235
00236 return m_ID;
00237 }
00238
00239
00240 int RunIOV::updateEndTimeDB()
00241 throw(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 int tagID = m_runTag.writeDB();
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(runtime_error("RunIOV::writeDB: "+e.getMessage()));
00274 }
00275
00276
00277 if (!this->fetchID()) {
00278 throw(runtime_error("RunIOV::writeDB: Failed to write"));
00279 }
00280
00281 return m_ID;
00282 }
00283
00284
00285
00286 void RunIOV::setByRun(RunTag* tag, run_t run)
00287 throw(std::runtime_error)
00288 {
00289 this->checkConnection();
00290
00291 tag->setConnection(m_env, m_conn);
00292 int tagID = tag->fetchID();
00293 if (!tagID) {
00294 throw(runtime_error("RunIOV::setByRun: Given tag is not in the database"));
00295 }
00296
00297 DateHandler dh(m_env, m_conn);
00298
00299 try {
00300 Statement* stmt = m_conn->createStatement();
00301
00302 stmt->setSQL("SELECT iov_id, run_start, run_end FROM run_iov WHERE tag_id = :1 AND run_num = :2");
00303 stmt->setInt(1, tagID);
00304 stmt->setInt(2, run);
00305
00306 ResultSet* rset = stmt->executeQuery();
00307 if (rset->next()) {
00308 m_runTag = *tag;
00309 m_runNum = run;
00310
00311 m_ID = rset->getInt(1);
00312 Date startDate = rset->getDate(2);
00313 Date endDate = rset->getDate(3);
00314
00315 m_runStart = dh.dateToTm( startDate );
00316 m_runEnd = dh.dateToTm( endDate );
00317 } else {
00318 throw(runtime_error("RunIOV::setByRun: Given run is not in the database"));
00319 }
00320
00321 m_conn->terminateStatement(stmt);
00322 } catch (SQLException &e) {
00323 throw(runtime_error("RunIOV::setByRun: "+e.getMessage()));
00324 }
00325 }
00326
00327
00328
00329 void RunIOV::setByRun(std::string location, run_t run)
00330 throw(std::runtime_error)
00331 {
00332 this->checkConnection();
00333
00334 DateHandler dh(m_env, m_conn);
00335
00336 try {
00337 Statement* stmt = m_conn->createStatement();
00338
00339 stmt->setSQL("SELECT iov_id FROM run_iov riov "
00340 "JOIN run_tag rtag ON riov.tag_id = rtag.tag_id "
00341 "JOIN location_def loc ON rtag.location_id = loc.def_id "
00342 "WHERE loc.location = :1 AND riov.run_num = :2");
00343 stmt->setString(1, location);
00344 stmt->setInt(2, run);
00345
00346 ResultSet* rset = stmt->executeQuery();
00347 if (rset->next()) {
00348 int id = rset->getInt(1);
00349 this->setByID(id);
00350 } else {
00351 throw(runtime_error("RunIOV::setByRun(loc, run): Given run is not in the database"));
00352 }
00353
00354
00355 if (rset->next()) {
00356 throw(runtime_error("RunIOV::setByRun(loc, run): Run is nonunique for given location."));
00357 }
00358
00359 m_conn->terminateStatement(stmt);
00360 } catch (SQLException &e) {
00361 throw(runtime_error("RunIOV::setByRun(loc, run): "+e.getMessage()));
00362 }
00363 }
00364
00365
00366 void RunIOV::setByRecentData(std::string dataTable, RunTag* tag, run_t run)
00367 throw(std::runtime_error)
00368 {
00369 this->checkConnection();
00370
00371 tag->setConnection(m_env, m_conn);
00372 int tagID = tag->fetchID();
00373 if (!tagID) {
00374 throw(runtime_error("RunIOV::setByRecentData: Given tag is not in the database"));
00375 }
00376
00377 DateHandler dh(m_env, m_conn);
00378
00379 try {
00380 Statement* stmt = m_conn->createStatement();
00381
00382 stmt->setSQL("SELECT * FROM (SELECT riov.iov_id, riov.run_num, riov.run_start, riov.run_end "
00383 "FROM run_iov riov "
00384 "JOIN "+dataTable+" dat on dat.iov_id = riov.iov_id "
00385 "WHERE tag_id = :1 AND riov.run_num <= :run ORDER BY riov.run_num DESC) WHERE rownum = 1");
00386
00387 stmt->setInt(1, tagID);
00388 stmt->setInt(2, run);
00389
00390 ResultSet* rset = stmt->executeQuery();
00391 if (rset->next()) {
00392 m_runTag = *tag;
00393
00394 m_ID = rset->getInt(1);
00395 m_runNum = rset->getInt(2);
00396 Date startDate = rset->getDate(3);
00397 Date endDate = rset->getDate(4);
00398
00399 m_runStart = dh.dateToTm( startDate );
00400 m_runEnd = dh.dateToTm( endDate );
00401 } else {
00402 throw(runtime_error("RunIOV::setByRecentData: No data exists for given tag and run"));
00403 }
00404
00405 m_conn->terminateStatement(stmt);
00406 } catch (SQLException &e) {
00407 throw(runtime_error("RunIOV::setByRecentData: "+e.getMessage()));
00408 }
00409 }
00410
00411
00412
00413
00414
00415
00416 void RunIOV::setByRecentData(std::string dataTable, std::string location, run_t run)
00417 throw(std::runtime_error)
00418 {
00419 this->checkConnection();
00420
00421 DateHandler dh(m_env, m_conn);
00422
00423 try {
00424 Statement* stmt = m_conn->createStatement();
00425
00426 stmt->setSQL("SELECT * FROM (SELECT riov.iov_id, riov.run_num, riov.run_start, riov.run_end "
00427 "FROM run_iov riov "
00428 "JOIN "+dataTable+" dat on dat.iov_id = riov.iov_id "
00429 "JOIN run_tag rtag ON riov.tag_id = rtag.tag_id "
00430 "JOIN location_def loc ON rtag.location_id = loc.def_id "
00431 "WHERE loc.location = :1 AND riov.run_num <= :2 ORDER BY riov.run_num DESC ) WHERE rownum = 1");
00432
00433 stmt->setString(1, location);
00434 stmt->setInt(2, run);
00435
00436 ResultSet* rset = stmt->executeQuery();
00437
00438
00439 if (rset->next()) {
00440 int id = rset->getInt(1);
00441 this->setByID(id);
00442 } else {
00443 throw(runtime_error("RunIOV::setByRecentData(datatable, loc, run): Given run is not in the database"));
00444 }
00445
00446
00447 m_conn->terminateStatement(stmt);
00448 } catch (SQLException &e) {
00449 throw(runtime_error("RunIOV::setByRecentData: "+e.getMessage()));
00450 }
00451 }
00452