CMS 3D CMS Logo

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(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 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(runtime_error("DCUIOV::fetchID:  "+e.getMessage()));
00121   }
00122 
00123   return m_ID;
00124 }
00125 
00126 
00127 
00128 void DCUIOV::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 dcu_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_dcuTag.setConnection(m_env, m_conn);
00151        m_dcuTag.setByID(tagID);
00152        m_ID = id;
00153      } else {
00154        throw(runtime_error("DCUTag::setByID:  Given tag_id is not in the database"));
00155      }
00156      
00157      m_conn->terminateStatement(stmt);
00158    } catch (SQLException &e) {
00159      throw(runtime_error("DCUTag::setByID:  "+e.getMessage()));
00160    }
00161 }
00162 
00163 
00164 
00165 int DCUIOV::writeDB()
00166   throw(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_dcuTag.setConnection(m_env, m_conn);
00176   int tagID = m_dcuTag.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(runtime_error("DCUIOV::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 dcu_iov (iov_id, tag_id, since, till) "
00193                  "VALUES (dcu_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(runtime_error("DCUIOV::writeDB:  "+e.getMessage()));
00203   }
00204 
00205   // Now get the ID
00206   if (!this->fetchID()) {
00207     throw(runtime_error("DCUIOV::writeDB:  Failed to write"));
00208   }
00209   
00210   return m_ID;
00211 }
00212 
00213 
00214 
00215 void DCUIOV::setByTm(DCUTag* tag, Tm eventTm)
00216   throw(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(runtime_error("DCUIOV::setByTm:  Given DCUTag 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 dcu_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_dcuTag = *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(runtime_error("DCUIOV::setByTm:  Given subrun is not in the database"));
00253     }
00254      
00255     m_conn->terminateStatement(stmt);
00256   } catch (SQLException &e) {
00257     throw(runtime_error("DCUIOV::setByTm:  "+e.getMessage()));
00258   }
00259 }

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