00001 #include <stdexcept>
00002 #include <sstream>
00003 #include <limits.h>
00004 #include "OnlineDB/EcalCondDB/interface/LMFSeqDat.h"
00005 #include "OnlineDB/EcalCondDB/interface/DateHandler.h"
00006
00007 using namespace std;
00008 using namespace oracle::occi;
00009
00010 LMFSeqDat::LMFSeqDat() : LMFUnique()
00011 {
00012 setClassName("LMFSeqDat");
00013
00014 m_runIOV = RunIOV();
00015 m_intFields["seq_num"] = 0;
00016 Tm t;
00017 t = t.plusInfinity();
00018 m_stringFields["seq_start"] = t.str();
00019 m_stringFields["seq_stop"] = t.str();
00020 m_intFields["vmin"] = 1;
00021 m_intFields["vmax"] = 0;
00022 }
00023
00024 LMFSeqDat::LMFSeqDat(EcalDBConnection *c) : LMFUnique(c) {
00025 setClassName("LMFSeqDat");
00026
00027 m_runIOV = RunIOV();
00028 m_intFields["seq_num"] = 0;
00029 Tm t;
00030 t = t.plusInfinity();
00031 m_stringFields["seq_start"] = t.str();
00032 m_stringFields["seq_stop"] = t.str();
00033 m_intFields["vmin"] = 1;
00034 m_intFields["vmax"] = 0;
00035 }
00036
00037 LMFSeqDat::LMFSeqDat(oracle::occi::Environment* env,
00038 oracle::occi::Connection* conn) : LMFUnique(env, conn) {
00039 setClassName("LMFSeqDat");
00040
00041 m_runIOV = RunIOV();
00042 m_intFields["seq_num"] = 0;
00043 Tm t;
00044 t = t.plusInfinity();
00045 m_stringFields["seq_start"] = t.str();
00046 m_stringFields["seq_stop"] = t.str();
00047 m_intFields["vmin"] = 1;
00048 m_intFields["vmax"] = 0;
00049 }
00050
00051 LMFSeqDat::~LMFSeqDat()
00052 {
00053 }
00054
00055 Tm LMFSeqDat::getSequenceStop() const {
00056 Tm t;
00057 t.setToString(getString("seq_stop"));
00058 return t;
00059 }
00060
00061 RunIOV LMFSeqDat::getRunIOV() const
00062 {
00063 return m_runIOV;
00064 }
00065
00066 LMFSeqDat& LMFSeqDat::setRunIOV(const RunIOV &iov)
00067 {
00068 if (iov != m_runIOV) {
00069 m_ID = 0;
00070 m_runIOV = iov;
00071 }
00072 return *this;
00073 }
00074
00075 std::string LMFSeqDat::fetchIdSql(Statement *stmt)
00076 {
00077 int runIOVID = m_runIOV.getID();
00078 if (!runIOVID) {
00079 if (m_debug) {
00080 std::cout << m_className << ": RunIOV not set" << endl;
00081 }
00082 return "";
00083 }
00084
00085 if (m_debug) {
00086 std::cout << "Run IOV ID: " << runIOVID << std::endl;
00087 std::cout << "SEQ # : " << getSequenceNumber() << std::endl;
00088 std::cout << "Versions : " << getVmin() << " - " << getVmax() << endl;
00089 }
00090 std::string sql = "SELECT SEQ_ID FROM CMS_ECAL_LASER_COND.LMF_SEQ_DAT "
00091 "WHERE "
00092 "RUN_IOV_ID = :1 AND "
00093 "SEQ_NUM = :2 AND "
00094 "VMIN = :3 ";
00095 if (getVmax() > 0) {
00096 sql += "AND VMAX = :4";
00097 } else {
00098 sql += "ORDER BY VMAX DESC";
00099 }
00100 stmt->setSQL(sql);
00101 stmt->setInt(1, runIOVID);
00102 stmt->setInt(2, getSequenceNumber());
00103 stmt->setInt(3, getVmin());
00104 if (getVmax() > 0) {
00105 stmt->setInt(4, getVmax());
00106 }
00107 return sql;
00108 }
00109
00110 std::string LMFSeqDat::setByIDSql(Statement *stmt, int id) {
00111 std::string sql = "SELECT RUN_IOV_ID, SEQ_NUM, SEQ_START, SEQ_STOP, "
00112 "VMIN, VMAX FROM CMS_ECAL_LASER_COND.LMF_SEQ_DAT WHERE SEQ_ID = :1";
00113 stmt->setSQL(sql);
00114 stmt->setInt(1, id);
00115 return sql;
00116 }
00117
00118 void LMFSeqDat::getParameters(ResultSet *rset) {
00119 DateHandler dh(m_env, m_conn);
00120 int runIOVID = rset->getInt(1);
00121 setInt("seq_num", rset->getInt(2));
00122 Date startDate = rset->getDate(3);
00123 Date endDate = rset->getDate(4);
00124 setInt("vmin", rset->getInt(5));
00125 setInt("vmax", rset->getInt(6));
00126
00127 setString("seq_start", dh.dateToTm( startDate ).str());
00128 setString("seq_stop", dh.dateToTm( endDate ).str());
00129
00130 m_runIOV.setConnection(m_env, m_conn);
00131 m_runIOV.setByID(runIOVID);
00132 }
00133
00134 bool LMFSeqDat::isValid() const {
00135 bool ret = true;
00136 if (getSequenceStart().isNull()) {
00137 ret = false;
00138 }
00139 if ((getSequenceStop().str().length() > 0) &&
00140 (getSequenceStop().microsTime() < getSequenceStart().microsTime())) {
00141 ret = false;
00142 }
00143 if (getSequenceStop() == Tm().plusInfinity()) {
00144 ret = false;
00145 }
00146 return ret;
00147 }
00148
00149 std::string LMFSeqDat::writeDBSql(Statement *stmt)
00150 {
00151
00152 DateHandler dh(m_env, m_conn);
00153
00154 if (!isValid()) {
00155 dump();
00156 throw(std::runtime_error("LMFSeqDat::writeDB: not valid"));
00157 }
00158
00159 if (getSequenceStop().str().length() == 0) {
00160 setSequenceStop(dh.getPlusInfTm());
00161 }
00162 int runIOVID = m_runIOV.getID();
00163 if (runIOVID == 0) {
00164 throw(std::runtime_error("LMFSeqDat::writeDB: RunIOV not set"));
00165 }
00166 std::string sp = sequencePostfix(getSequenceStart());
00167 std::string sql = "INSERT INTO LMF_SEQ_DAT (SEQ_ID, RUN_IOV_ID, SEQ_NUM, "
00168 "SEQ_START, SEQ_STOP, VMIN, VMAX) "
00169 "VALUES (SEQ_ID_" + sp + "_SQ.NextVal, :1, :2, :3, :4, :5, :6)";
00170 cout << sql << endl;
00171 stmt->setSQL(sql);
00172 stmt->setInt(1, runIOVID);
00173 stmt->setInt(2, getSequenceNumber());
00174 stmt->setDate(3, dh.tmToDate(getSequenceStart()));
00175 stmt->setDate(4, dh.tmToDate(getSequenceStop()));
00176 stmt->setInt(5, getVmin());
00177 stmt->setInt(6, getVmax());
00178 return sql;
00179 }
00180
00181 void LMFSeqDat::fetchParentIDs()
00182 throw(std::runtime_error)
00183 {
00184
00185 m_runIOV.setConnection(m_env, m_conn);
00186 int runIOVID = m_runIOV.getID();
00187 m_runIOV.setByID(runIOVID);
00188
00189 if (m_runIOV.getID() == 0) {
00190 throw(std::runtime_error("LMFSeqDat: Given RunIOV does not exist in DB"));
00191 }
00192
00193 }
00194
00195 std::map<int, LMFSeqDat> LMFSeqDat::fetchByRunIOV(std::string sql,
00196 std::string method)
00197 throw(std::runtime_error)
00198 {
00199 std::vector<std::string> pars;
00200 return fetchByRunIOV(pars, sql, method);
00201 }
00202
00203 std::map<int, LMFSeqDat> LMFSeqDat::fetchByRunIOV(int par,
00204 std::string sql,
00205 std::string method)
00206 throw(std::runtime_error)
00207 {
00208 std::vector<std::string> pars;
00209 std::stringstream ss;
00210 ss << "I" << par;
00211 pars.push_back(ss.str());
00212 return fetchByRunIOV(pars, sql, method);
00213 }
00214
00215 std::map<int, LMFSeqDat> LMFSeqDat::fetchByRunIOV(std::vector<std::string> pars,
00216 std::string sql,
00217 std::string method)
00218 throw(std::runtime_error)
00219 {
00220 std::map<int, LMFSeqDat> l;
00221 this->checkConnection();
00222 try {
00223 Statement *stmt = m_conn->createStatement();
00224 stmt->setSQL(sql);
00225 for (unsigned int i = 0; i < pars.size(); i++) {
00226 if (pars[i][0] == 'I') {
00227 stmt->setInt(i + 1, atoi(pars[i].c_str() + 1));
00228 } else if (pars[i][0] == 'S') {
00229 stmt->setString(i + 1, pars[i].c_str() + 1);
00230 } else {
00231 throw(std::runtime_error(m_className + "::" + method + ": " +
00232 "Invalid type"));
00233 }
00234 }
00235 ResultSet *rset = stmt->executeQuery();
00236 while (rset->next() != 0) {
00237 int seq_id = rset->getInt(1);
00238 LMFSeqDat s;
00239 s.setConnection(m_env, m_conn);
00240 s.setByID(seq_id);
00241 l[s.getSequenceNumber()] = s;
00242 }
00243 m_conn->terminateStatement(stmt);
00244 } catch (SQLException &e) {
00245 throw(std::runtime_error(m_className + "::" + method + ": " +
00246 e.getMessage()));
00247 }
00248 return l;
00249 }
00250
00251 LMFSeqDat LMFSeqDat::fetchLast() {
00252 LMFSeqDat ret;
00253 std::map<int, LMFSeqDat> m =
00254 fetchByRunIOV("SELECT SEQ_ID FROM CMS_ECAL_LASER_COND.LMF_SEQ_DAT "
00255 "WHERE SEQ_ID = "
00256 "(SELECT MAX(SEQ_ID) FROM CMS_ECAL_LASER_COND.LMF_SEQ_DAT)",
00257 "fetchLast");
00258 if (m.size() > 0) {
00259 ret = m.begin()->second;
00260 }
00261 return ret;
00262 }
00263
00264 RunIOV LMFSeqDat::fetchLastRun() {
00265 return fetchLast().getRunIOV();
00266 }
00267
00268 std::map<int, LMFSeqDat> LMFSeqDat::fetchByRunIOV(RunIOV &iov) {
00269 int runIOVID = iov.getID();
00270 return fetchByRunIOV(runIOVID,
00271 "SELECT SEQ_ID FROM CMS_ECAL_LASER_COND.LMF_SEQ_DAT "
00272 "WHERE RUN_IOV_ID = :1",
00273 "fetchByRunIOV");
00274 }
00275
00276 std::map<int, LMFSeqDat> LMFSeqDat::fetchByRunIOV(RunIOV &iov,
00277 const LMFColor &col) {
00278 int runIOVID = iov.getID();
00279 int colorId = col.getID();
00280 std::vector<std::string> pars;
00281 std::stringstream ss;
00282 ss << "I" << runIOVID;
00283 pars.push_back(ss.str());
00284 ss.str(std::string());
00285 ss << "I" << colorId;
00286 pars.push_back(ss.str());
00287 return fetchByRunIOV(pars,
00288 "SELECT S.SEQ_ID FROM CMS_ECAL_LASER_COND.LMF_SEQ_DAT "
00289 "S JOIN CMS_ECAL_LASER_COND.LMF_RUN_IOV R"
00290 " ON S.SEQ_ID = R.SEQ_ID WHERE RUN_IOV_ID = :1 AND "
00291 " COLOR_ID = :2",
00292 "fetchByRunIOVAndColor");
00293 }
00294
00295 std::map<int, LMFSeqDat> LMFSeqDat::fetchByRunNumber(int runno) {
00296 return fetchByRunIOV(runno,
00297 "SELECT SEQ_ID FROM CMS_ECAL_LASER_COND.LMF_SEQ_DAT D "
00298 "JOIN CMS_ECAL_COND.RUN_IOV R ON "
00299 "D.RUN_IOV_ID = R.IOV_ID WHERE RUN_NUM = :1",
00300 "fetchByRunNumber");
00301 }
00302
00303 LMFSeqDat LMFSeqDat::fetchByRunNumber(int runno, Tm taken_at) {
00304 return fetchByRunNumber(runno, taken_at.str());
00305 }
00306
00307 LMFSeqDat LMFSeqDat::fetchByRunNumber(int runno, std::string taken_at) {
00308 std::map<int, LMFSeqDat> l;
00309 std::vector<std::string> pars;
00310 std::stringstream ss;
00311 ss << "I" << runno;
00312 pars.push_back(ss.str());
00313 ss.str(std::string());
00314 ss << "S" << taken_at;
00315 pars.push_back(ss.str());
00316 std::string q = "SELECT SEQ_ID FROM CMS_ECAL_LASER_COND.LMF_SEQ_DAT D "
00317 "JOIN CMS_ECAL_COND.RUN_IOV R ON "
00318 "D.RUN_IOV_ID = R.IOV_ID WHERE RUN_NUM = :1 AND "
00319 "SEQ_START >= TO_DATE(:2, 'YYYY-MM-DD HH24:MI:SS') "
00320 "AND SEQ_STOP <= TO_DATE(:2, 'YYYY-MM-DD HH24:MI:SS')";
00321 l = fetchByRunIOV(pars, q, "fetchByRunNumberAt");
00322 LMFSeqDat ret;
00323 if (l.size() == 1) {
00324 std::map<int, LMFSeqDat>::const_iterator x = l.begin();
00325 ret = x->second;
00326 } else if (l.size() > 1) {
00327 std::cout << "WARNING: Your query returned more than one result. "
00328 << std::endl;
00329 std::cout << " This was not expected. Please check the DB!!!"
00330 << std::endl;
00331 std::cout << "Your query: " << std::endl << q << std::endl;
00332 std::cout << "Your parameters: " << std::endl;
00333 for (unsigned int i = 0; i < pars.size(); i++) {
00334 std::cout << i << ": " << pars[i] << std::endl;
00335 }
00336 }
00337 return ret;
00338 }
00339