CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/OnlineDB/EcalCondDB/src/DCUIOV.cc

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