CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/OnlineDB/EcalCondDB/src/LMFLmrSubIOV.cc

Go to the documentation of this file.
00001 #include "OnlineDB/EcalCondDB/interface/LMFLmrSubIOV.h"
00002 
00003 void LMFLmrSubIOV::init() {
00004   m_className = "LMFLmrSubIOV";
00005 
00006   m_lmfIOV = 0;
00007   m_t[0] = Tm();
00008   m_t[1] = Tm();
00009   m_t[2] = Tm();
00010 }
00011 
00012 LMFLmrSubIOV::LMFLmrSubIOV() {
00013   init();
00014 }
00015 
00016 LMFLmrSubIOV::LMFLmrSubIOV(EcalDBConnection *c): LMFUnique(c) {
00017   init();
00018 }
00019 
00020 LMFLmrSubIOV::LMFLmrSubIOV(oracle::occi::Environment* env,
00021                            oracle::occi::Connection* conn): LMFUnique(env, conn)
00022 {
00023   init();
00024 }
00025 
00026 LMFLmrSubIOV::~LMFLmrSubIOV() {
00027 }
00028 
00029 LMFLmrSubIOV& LMFLmrSubIOV::setLMFIOV(const LMFIOV &iov) {
00030   if (m_debug) {
00031     std::cout << "[LMFLmrSubIOV] Setting IOV_ID as " << iov.getID() 
00032               << std::endl << std::flush; 
00033   }
00034   m_lmfIOV = iov.getID();
00035   return *this;
00036 }
00037 
00038 LMFLmrSubIOV& LMFLmrSubIOV::setTimes(Tm *t) {
00039   m_t[0] = t[0];
00040   m_t[1] = t[1];
00041   m_t[2] = t[2];
00042   return *this;
00043 }
00044 
00045 LMFLmrSubIOV& LMFLmrSubIOV::setTimes(Tm t1, Tm t2, Tm t3) {
00046   m_t[0] = t1;
00047   m_t[1] = t2;
00048   m_t[2] = t3;
00049   return *this;
00050 }
00051 
00052 std::string LMFLmrSubIOV::fetchIdSql(Statement *stmt) {
00053   if (!m_lmfIOV) {
00054     if (m_debug) {
00055       std::cout << m_className << ": LMFIOV not set" << std::endl;
00056     }
00057     return "";
00058   }
00059 
00060   std::string sql = "SELECT LMR_SUB_IOV_ID FROM LMF_LMR_SUB_IOV "
00061     "WHERE "
00062     "IOV_ID  = :1 AND "
00063     "T1      = :2 AND "
00064     "T2      = :3 AND "
00065     "T3      = :4";
00066   stmt->setSQL(sql);
00067   stmt->setInt(1, m_lmfIOV);
00068   DateHandler dh(m_env, m_conn);
00069   for (int i = 0; i < 3; i++) {
00070     oracle::occi::Date t = dh.tmToDate(m_t[i]);
00071     stmt->setDate(i + 2, t);
00072   }
00073   return sql;
00074 
00075 }
00076 
00077 std::string LMFLmrSubIOV::setByIDSql(Statement *stmt, int id) {
00078   std::string sql = "SELECT IOV_ID, T1, T2, T3 FROM LMF_LMR_SUB_IOV "
00079     "WHERE LMR_SUB_IOV_ID = :1";
00080   stmt->setSQL(sql);
00081   stmt->setInt(1, id);
00082   return sql;
00083 }
00084 
00085 std::string LMFLmrSubIOV::writeDBSql(Statement *stmt) {
00086   // Validate the data, use infinity-till convention
00087   DateHandler dh(m_env, m_conn);
00088 
00089   for (int i = 0; i < 3; i++) {
00090     if (m_t[i].isNull()) {
00091       m_t[i] = dh.getPlusInfTm();
00092     }
00093   }
00094 
00095   if (m_lmfIOV == 0) {
00096     throw(std::runtime_error(m_className + "::writeDB: LMFIOV not set"));
00097   }
00098   std::string sp = sequencePostfix(m_t[0]);
00099   std::string sql = "INSERT INTO LMF_LMR_SUB_IOV (LMR_SUB_IOV_ID, "
00100     "IOV_ID, T1, T2, T3) "
00101     "VALUES (LMF_LMR_SUB_IOV_ID_" + sp + "_SQ.NextVal, :1, :2, :3, :4)";
00102   stmt->setSQL(sql);
00103   stmt->setInt(1, m_lmfIOV);
00104   for (int i = 0; i < 3; i++) {
00105     stmt->setDate(i + 2, dh.tmToDate(m_t[i]));
00106   }
00107   return sql;
00108 }
00109 
00110 void LMFLmrSubIOV::getParameters(ResultSet *rset) {
00111   m_lmfIOV = rset->getInt(1);
00112   for (int i = 0; i < 3; i++) {
00113     oracle::occi::Date t = rset->getDate(i + 2);
00114     m_t[i].setToString(t.toText("YYYY-MM-DD HH24:MI:SS"));
00115   }
00116 }
00117 
00118 std::list<int> LMFLmrSubIOV::getIOVIDsLaterThan(const Tm &t) 
00119   throw(std::runtime_error) {
00120   Tm tinf;
00121   tinf.setToString("9999-12-31 23:59:59");
00122   return getIOVIDsLaterThan(t, tinf, 0);
00123 }
00124 
00125 std::list<int> LMFLmrSubIOV::getIOVIDsLaterThan(const Tm &t, int howmany) 
00126   throw(std::runtime_error) {
00127   Tm tinf;
00128   tinf.setToString("9999-12-31 23:59:59");
00129   return getIOVIDsLaterThan(t, tinf, howmany);
00130 }
00131 
00132 std::list<int> LMFLmrSubIOV::getIOVIDsLaterThan(const Tm &tmin,
00133                                                 const Tm &tmax) 
00134   throw(std::runtime_error) {
00135   return getIOVIDsLaterThan(tmin, tmax, 0);
00136 }
00137 
00138 std::list<int> LMFLmrSubIOV::getIOVIDsLaterThan(const Tm &tmin, const Tm &tmax,
00139                                                 int howMany) 
00140   throw(std::runtime_error) {
00141   Tm tinf;
00142   tinf.setToString("9999-12-31 23:59:59");
00143   std::string sql = "SELECT * FROM (SELECT LMR_SUB_IOV_ID "  
00144     "FROM LMF_LMR_SUB_IOV WHERE T3 > :1 ";
00145   if (tmax != tinf) {
00146     sql += "AND T3 < :2 ORDER BY T3 ASC) ";
00147     if (howMany > 0) {
00148       sql += "WHERE ROWNUM <= :3";
00149     }
00150   } else {
00151     sql += "ORDER BY T3 ASC) ";
00152     if (howMany > 0) {
00153       sql += "WHERE ROWNUM <= :2";
00154     }
00155   }
00156   if (m_debug) {
00157     std::cout << "Executing query: " << std::endl << sql << std::endl;
00158   }
00159   std::list<int> ret; 
00160   if (m_conn != NULL) {
00161     try {
00162       DateHandler dh(m_env, m_conn);
00163       Statement *stmt = m_conn->createStatement();
00164       stmt->setPrefetchRowCount(131072);
00165       stmt->setSQL(sql);
00166       stmt->setDate(1, dh.tmToDate(tmin));
00167       if (tmax != tinf) {
00168         stmt->setDate(2, dh.tmToDate(tmax));
00169         if (howMany > 0) {
00170           stmt->setInt(3, howMany);
00171         }
00172       } else {
00173         if (howMany > 0) {
00174           stmt->setInt(2, howMany);
00175         }
00176       }
00177       ResultSet *rset = stmt->executeQuery();
00178       int row = 1;
00179       while (rset->next()) {
00180         if (m_debug) {
00181           std::cout << "Getting row " << row++ << std::endl;
00182         }
00183         ret.push_back(rset->getInt(1));
00184       }
00185       stmt->setPrefetchRowCount(0);
00186       m_conn->terminateStatement(stmt);
00187     }
00188     catch (oracle::occi::SQLException e) {
00189       throw(std::runtime_error(m_className + "::getLmrSubIOVLaterThan: " +
00190                                e.getMessage()));
00191     }
00192   } else {
00193     throw(std::runtime_error(m_className + "::getLmrSubIOVLaterThan: " +
00194                              "Connection not set"));
00195   }
00196   if (m_debug) {
00197     std::cout << "Sorting..." << std::flush;
00198   }
00199   ret.sort();
00200   if (m_debug) {
00201     std::cout << "Done!" << std::endl << std::flush;
00202   }
00203   return ret;
00204 }
00205 
00206