CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/OnlineDB/EcalCondDB/src/CaliIOV.cc

Go to the documentation of this file.
00001 #include <stdexcept>
00002 #include "OnlineDB/Oracle/interface/Oracle.h"
00003 
00004 #include "OnlineDB/EcalCondDB/interface/CaliIOV.h"
00005 #include "OnlineDB/EcalCondDB/interface/CaliTag.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 CaliIOV::CaliIOV()
00013 {
00014   m_conn = NULL;
00015   m_ID = 0;
00016   m_since = Tm();
00017   m_till = Tm();
00018 }
00019 
00020 
00021 
00022 CaliIOV::~CaliIOV()
00023 {
00024 }
00025 
00026 
00027 
00028 void CaliIOV::setSince(Tm since)
00029 {
00030   if (since != m_since) {
00031     m_ID = 0;
00032     m_since = since;
00033   }
00034 }
00035 
00036 
00037 
00038 Tm CaliIOV::getSince() const
00039 {
00040   return m_since;
00041 }
00042 
00043 
00044 
00045 void CaliIOV::setTill(Tm till)
00046 {
00047   if (till != m_till) {
00048     m_ID = 0;
00049     m_till = till;
00050   }
00051 }
00052 
00053 
00054 
00055 Tm CaliIOV::getTill() const
00056 {
00057   return m_till;
00058 }
00059 
00060 
00061 
00062 void CaliIOV::setCaliTag(CaliTag tag)
00063 {
00064   if (tag != m_caliTag) {
00065     m_ID = 0;
00066     m_caliTag = tag;
00067   }
00068 }
00069 
00070 
00071 
00072 CaliTag CaliIOV::getCaliTag() const
00073 {
00074   return m_caliTag;
00075 }
00076 
00077 
00078 
00079 int CaliIOV::fetchID()
00080   throw(std::runtime_error)
00081 {
00082   // Return from memory if available
00083   if (m_ID) {
00084     return m_ID;
00085   }
00086 
00087   this->checkConnection();
00088 
00089   m_caliTag.setConnection(m_env, m_conn);
00090   int tagID = m_caliTag.fetchID();
00091   if (!tagID) { 
00092     return 0;
00093   }
00094 
00095   DateHandler dh(m_env, m_conn);
00096 
00097   if (m_till.isNull()) {
00098     m_till = dh.getPlusInfTm();
00099   }
00100 
00101   try {
00102     Statement* stmt = m_conn->createStatement();
00103     stmt->setSQL("SELECT iov_id FROM cali_iov "
00104                  "WHERE tag_id = :tag_id AND "
00105                  "since = :since AND "
00106                  "till = :till");
00107     stmt->setInt(1, tagID);
00108     stmt->setDate(2, dh.tmToDate(m_since));
00109     stmt->setDate(3, dh.tmToDate(m_till));
00110   
00111     ResultSet* rset = stmt->executeQuery();
00112 
00113     if (rset->next()) {
00114       m_ID = rset->getInt(1);
00115     } else {
00116       m_ID = 0;
00117     }
00118     m_conn->terminateStatement(stmt);
00119   } catch (SQLException &e) {
00120     throw(std::runtime_error("CaliIOV::fetchID:  "+e.getMessage()));
00121   }
00122 
00123   return m_ID;
00124 }
00125 
00126 
00127 
00128 void CaliIOV::setByID(int id) 
00129   throw(std::runtime_error)
00130 {
00131    this->checkConnection();
00132 
00133    DateHandler dh(m_env, m_conn);
00134 
00135    try {
00136      Statement* stmt = m_conn->createStatement();
00137 
00138      stmt->setSQL("SELECT tag_id, since, till FROM cali_iov WHERE iov_id = :1");
00139      stmt->setInt(1, id);
00140      
00141      ResultSet* rset = stmt->executeQuery();
00142      if (rset->next()) {
00143        int tagID = rset->getInt(1);
00144        Date since = rset->getDate(2);
00145        Date till = rset->getDate(3);
00146          
00147        m_since = dh.dateToTm( since );
00148        m_till = dh.dateToTm( till );
00149 
00150        m_caliTag.setConnection(m_env, m_conn);
00151        m_caliTag.setByID(tagID);
00152        m_ID = id;
00153      } else {
00154        throw(std::runtime_error("CaliTag::setByID:  Given tag_id is not in the database"));
00155      }
00156      
00157      m_conn->terminateStatement(stmt);
00158    } catch (SQLException &e) {
00159      throw(std::runtime_error("CaliTag::setByID:  "+e.getMessage()));
00160    }
00161 }
00162 
00163 
00164 
00165 int CaliIOV::writeDB()
00166   throw(std::runtime_error)
00167 {
00168   this->checkConnection();
00169 
00170   // Check if this IOV has already been written
00171   if (this->fetchID()) {
00172     return m_ID;
00173   }
00174 
00175   m_caliTag.setConnection(m_env, m_conn);
00176   int tagID = m_caliTag.writeDB();
00177   
00178   // Validate the data, use infinity-till convention
00179   DateHandler dh(m_env, m_conn);
00180 
00181   if (m_since.isNull()) {
00182     throw(std::runtime_error("CaliIOV::writeDB:  Must setSince before writing"));
00183   }
00184   
00185   if (m_till.isNull()) {
00186     m_till = dh.getPlusInfTm();
00187   }
00188 
00189   try {
00190     Statement* stmt = m_conn->createStatement();
00191     
00192     stmt->setSQL("INSERT INTO cali_iov (iov_id, tag_id, since, till) "
00193                  "VALUES (cali_iov_sq.NextVal, :1, :2, :3)");
00194     stmt->setInt(1, tagID);
00195     stmt->setDate(2, dh.tmToDate(m_since));
00196     stmt->setDate(3, dh.tmToDate(m_till));
00197 
00198     stmt->executeUpdate();
00199 
00200     m_conn->terminateStatement(stmt);
00201   } catch (SQLException &e) {
00202     throw(std::runtime_error("CaliIOV::writeDB:  "+e.getMessage()));
00203   }
00204 
00205   // Now get the ID
00206   if (!this->fetchID()) {
00207     throw(std::runtime_error("CaliIOV::writeDB:  Failed to write"));
00208   }
00209   
00210   return m_ID;
00211 }
00212 
00213 
00214 
00215 void CaliIOV::setByTm(CaliTag* tag, Tm eventTm)
00216   throw(std::runtime_error)
00217 {
00218   this->checkConnection();
00219   
00220   tag->setConnection(m_env, m_conn);
00221   int tagID = tag->fetchID();
00222   
00223   if (!tagID) {
00224     throw(std::runtime_error("CaliIOV::setByTm:  Given CaliTag does not exist in the DB"));
00225   }
00226 
00227   DateHandler dh(m_env, m_conn);
00228 
00229   Date eventDate = dh.tmToDate(eventTm);
00230 
00231   try {
00232     Statement* stmt = m_conn->createStatement();
00233 
00234 
00235     stmt->setSQL("SELECT iov_id, since, till FROM cali_iov "
00236                  "WHERE tag_id = :1 AND since <= :2 AND till > :3");
00237     stmt->setInt(1, tagID);
00238     stmt->setDate(2, eventDate);
00239     stmt->setDate(3, eventDate);
00240 
00241     ResultSet* rset = stmt->executeQuery();
00242     if (rset->next()) {
00243       m_caliTag = *tag;
00244       
00245       m_ID = rset->getInt(1);
00246       Date sinceDate = rset->getDate(2);
00247       Date tillDate = rset->getDate(3);
00248          
00249       m_since = dh.dateToTm( sinceDate );
00250       m_till = dh.dateToTm( tillDate );
00251     } else {
00252       throw(std::runtime_error("CaliIOV::setByTm:  Given subrun is not in the database"));
00253     }
00254      
00255     m_conn->terminateStatement(stmt);
00256   } catch (SQLException &e) {
00257     throw(std::runtime_error("CaliIOV::setByTm:  "+e.getMessage()));
00258   }
00259 }