CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/OnlineDB/EcalCondDB/src/MODRunIOV.cc

Go to the documentation of this file.
00001 #include <stdexcept>
00002 #include "OnlineDB/Oracle/interface/Oracle.h"
00003 
00004 #include "OnlineDB/EcalCondDB/interface/MODRunIOV.h"
00005 #include "OnlineDB/EcalCondDB/interface/RunIOV.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 MODRunIOV::MODRunIOV()
00013 {
00014   m_conn = NULL;
00015   m_ID = 0;
00016   m_runIOV = RunIOV();
00017   m_subRunNum = 0;
00018   m_subRunStart = Tm();
00019   m_subRunEnd = Tm();
00020 }
00021 
00022 
00023 
00024 MODRunIOV::~MODRunIOV()
00025 {
00026 }
00027 
00028 
00029 void MODRunIOV::setID(int id)
00030 {
00031     m_ID = id;
00032 }
00033 
00034 
00035 
00036 void MODRunIOV::setRunIOV(RunIOV iov)
00037 {
00038   if (iov != m_runIOV) {
00039     m_ID = 0;
00040     m_runIOV = iov;
00041   }
00042 }
00043 
00044 RunIOV MODRunIOV::getRunIOV() 
00045 { 
00046   return m_runIOV;
00047 }
00048 
00049 
00050 void MODRunIOV::setSubRunNumber(subrun_t subrun)
00051 {
00052   if (subrun != m_subRunNum) {
00053     m_ID = 0;
00054     m_subRunNum = subrun;
00055   }
00056 }
00057 
00058 
00059 
00060 run_t MODRunIOV::getSubRunNumber() const
00061 {
00062   return m_subRunNum;
00063 }
00064 
00065 
00066 
00067 void MODRunIOV::setSubRunStart(Tm start)
00068 {
00069   if (start != m_subRunStart) {
00070     m_ID = 0;
00071     m_subRunStart = start;
00072   }
00073 }
00074 
00075 
00076 
00077 Tm MODRunIOV::getSubRunStart() const
00078 {
00079   return m_subRunStart;
00080 }
00081 
00082 
00083 
00084 void MODRunIOV::setSubRunEnd(Tm end)
00085 {
00086   if (end != m_subRunEnd) {
00087     m_ID = 0;
00088     m_subRunEnd = end;
00089   }
00090 }
00091 
00092 
00093 
00094 Tm MODRunIOV::getSubRunEnd() const
00095 {
00096   return m_subRunEnd;
00097 }
00098 
00099 
00100 
00101 int MODRunIOV::fetchID()
00102   throw(std::runtime_error)
00103 {
00104   // Return from memory if available
00105   if (m_ID) {
00106     return m_ID;
00107   }
00108 
00109   this->checkConnection();
00110 
00111   // fetch the parent IDs
00112   int  runIOVID;
00113   this->fetchParentIDs(&runIOVID);
00114 
00115   if (!runIOVID) { 
00116     return 0;
00117   }
00118 
00119   DateHandler dh(m_env, m_conn);
00120 
00121   if (m_subRunEnd.isNull()) {
00122     m_subRunEnd = dh.getPlusInfTm();
00123   }
00124 
00125   try {
00126     Statement* stmt = m_conn->createStatement();
00127     stmt->setSQL("SELECT iov_id FROM OD_run_iov "
00128                  "WHERE "
00129                  "run_iov_id   = :1 AND "
00130                  "subrun_num   = :2 AND "
00131                  "subrun_start = :3 AND "
00132                  "subrun_end   = :4");
00133 
00134     stmt->setInt(1, runIOVID);
00135     stmt->setInt(2, m_subRunNum);
00136     stmt->setDate(3, dh.tmToDate(m_subRunStart));
00137     stmt->setDate(4, dh.tmToDate(m_subRunEnd));
00138   
00139     ResultSet* rset = stmt->executeQuery();
00140 
00141     if (rset->next()) {
00142       m_ID = rset->getInt(1);
00143     } else {
00144       m_ID = 0;
00145     }
00146     m_conn->terminateStatement(stmt);
00147   } catch (SQLException &e) {
00148     throw(std::runtime_error("MODRunIOV::fetchID:  "+e.getMessage()));
00149   }
00150 
00151   return m_ID;
00152 }
00153 
00154 
00155 
00156 void MODRunIOV::setByID(int id) 
00157   throw(std::runtime_error)
00158 {
00159    this->checkConnection();
00160 
00161    DateHandler dh(m_env, m_conn);
00162 
00163    try {
00164      Statement* stmt = m_conn->createStatement();
00165 
00166      stmt->setSQL("SELECT run_iov_id, subrun_num, subrun_start, subrun_end FROM OD_run_iov WHERE iov_id = :1");
00167      stmt->setInt(1, id);
00168      
00169      ResultSet* rset = stmt->executeQuery();
00170      if (rset->next()) {
00171        int runIOVID = rset->getInt(1);
00172        m_subRunNum = rset->getInt(2);
00173        Date startDate = rset->getDate(3);
00174        Date endDate = rset->getDate(4);
00175          
00176        m_subRunStart = dh.dateToTm( startDate );
00177        m_subRunEnd = dh.dateToTm( endDate );
00178 
00179        m_runIOV.setConnection(m_env, m_conn);
00180        m_runIOV.setByID(runIOVID);
00181 
00182        m_ID = id;
00183      } else {
00184        throw(std::runtime_error("MODRunIOV::setByID:  Given id is not in the database"));
00185      }
00186      
00187      m_conn->terminateStatement(stmt);
00188    } catch (SQLException &e) {
00189      throw(std::runtime_error("MODRunIOV::setByID:  "+e.getMessage()));
00190    }
00191 }
00192 
00193 
00194 
00195 int MODRunIOV::writeDB()
00196   throw(std::runtime_error)
00197 {
00198   this->checkConnection();
00199 
00200   // Check if this IOV has already been written
00201   if (this->fetchID()) {
00202     return m_ID;
00203   }
00204 
00205   // fetch Parent IDs
00206   int runIOVID;
00207   this->fetchParentIDs(&runIOVID);
00208                        
00209   // Validate the data, use infinity-till convention
00210   DateHandler dh(m_env, m_conn);
00211 
00212   if (m_subRunStart.isNull()) {
00213     throw(std::runtime_error("MODRunIOV::writeDB:  Must setSubRunStart before writing"));
00214   }
00215   
00216   if (m_subRunEnd.isNull()) {
00217     m_subRunEnd = dh.getPlusInfTm();
00218   }
00219 
00220   try {
00221     Statement* stmt = m_conn->createStatement();
00222     
00223     stmt->setSQL("INSERT INTO od_run_iov (iov_id,  run_iov_id, subrun_num, subrun_start, subrun_end) "
00224                  "VALUES (OD_run_iov_sq.NextVal, :1, :2, :3, :4)");
00225     stmt->setInt(1, runIOVID);
00226     stmt->setInt(2, m_subRunNum);
00227     stmt->setDate(3, dh.tmToDate(m_subRunStart));
00228     stmt->setDate(4, dh.tmToDate(m_subRunEnd));
00229 
00230     stmt->executeUpdate();
00231 
00232     m_conn->terminateStatement(stmt);
00233   } catch (SQLException &e) {
00234     throw(std::runtime_error("MODRunIOV::writeDB:  "+e.getMessage()));
00235   }
00236 
00237   // Now get the ID
00238   if (!this->fetchID()) {
00239     throw(std::runtime_error("MODRunIOV::writeDB:  Failed to write"));
00240   }
00241   
00242   return m_ID;
00243 }
00244 
00245 
00246 
00247 void MODRunIOV::fetchParentIDs( int* runIOVID)
00248   throw(std::runtime_error)
00249 {
00250   // get the RunIOV
00251   m_runIOV.setConnection(m_env, m_conn);
00252   *runIOVID = m_runIOV.fetchID();
00253 
00254   if (! *runIOVID) { 
00255     throw(std::runtime_error("MODRunIOV:  Given RunIOV does not exist in DB")); 
00256   }
00257 
00258 }
00259 
00260 
00261 
00262 void MODRunIOV::setByRun( RunIOV* runiov, subrun_t subrun)
00263   throw(std::runtime_error)
00264 {
00265   this->checkConnection();
00266   
00267   runiov->setConnection(m_env, m_conn);
00268   int runIOVID = runiov->fetchID();
00269 
00270   if (!runIOVID) {
00271     throw(std::runtime_error("MODRunIOV::setByRun:  Given RunIOV does not exist in DB"));
00272   }
00273 
00274   DateHandler dh(m_env, m_conn);
00275 
00276   try {
00277     Statement* stmt = m_conn->createStatement();
00278 
00279     stmt->setSQL("SELECT iov_id, subrun_start, subrun_end FROM OD_run_iov "
00280                  "WHERE run_iov_id = :1 AND subrun_num = :2");
00281     stmt->setInt(1, runIOVID);
00282     stmt->setInt(2, subrun);
00283 
00284     ResultSet* rset = stmt->executeQuery();
00285     if (rset->next()) {
00286       m_runIOV = *runiov;
00287       m_subRunNum = subrun;
00288       
00289       m_ID = rset->getInt(1);
00290       Date startDate = rset->getDate(2);
00291       Date endDate = rset->getDate(3);
00292          
00293       m_subRunStart = dh.dateToTm( startDate );
00294       m_subRunEnd = dh.dateToTm( endDate );
00295     } else {
00296       throw(std::runtime_error("MODRunIOV::setByRun:  Given subrun is not in the database"));
00297     }
00298      
00299     m_conn->terminateStatement(stmt);
00300   } catch (SQLException &e) {
00301     throw(std::runtime_error("MODRunIOV::setByRun:  "+e.getMessage()));
00302   }
00303   
00304 }