CMS 3D CMS Logo

LMFRunIOV.cc

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

Generated on Tue Jun 9 17:40:48 2009 for CMSSW by  doxygen 1.5.4