Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:11

0001 #include <stdexcept>
0002 #include "OnlineDB/Oracle/interface/Oracle.h"
0003 
0004 #include "OnlineDB/EcalCondDB/interface/ODRunConfigInfo.h"
0005 #include "OnlineDB/EcalCondDB/interface/Tm.h"
0006 #include "OnlineDB/EcalCondDB/interface/DateHandler.h"
0007 
0008 using namespace std;
0009 using namespace oracle::occi;
0010 
0011 ODRunConfigInfo::ODRunConfigInfo() {
0012   m_env = nullptr;
0013   m_conn = nullptr;
0014   m_ID = 0;
0015   //
0016   m_tag = "";
0017   m_version = 0;
0018   m_num_seq = 0;
0019   m_runTypeDef = RunTypeDef();
0020   m_runModeDef = RunModeDef();
0021   m_defaults = 0;
0022   m_trigger_mode = "";
0023   m_num_events = 0;
0024 }
0025 
0026 ODRunConfigInfo::~ODRunConfigInfo() {}
0027 
0028 //
0029 RunTypeDef ODRunConfigInfo::getRunTypeDef() const { return m_runTypeDef; }
0030 void ODRunConfigInfo::setRunTypeDef(const RunTypeDef& runTypeDef) {
0031   if (runTypeDef != m_runTypeDef) {
0032     m_ID = 0;
0033     m_runTypeDef = runTypeDef;
0034   }
0035 }
0036 //
0037 RunModeDef ODRunConfigInfo::getRunModeDef() const { return m_runModeDef; }
0038 void ODRunConfigInfo::setRunModeDef(const RunModeDef& runModeDef) {
0039   if (runModeDef != m_runModeDef) {
0040     m_ID = 0;
0041     m_runModeDef = runModeDef;
0042   }
0043 }
0044 //
0045 
0046 int ODRunConfigInfo::fetchNextId() noexcept(false) {
0047   int result = 0;
0048   try {
0049     this->checkConnection();
0050 
0051     m_readStmt = m_conn->createStatement();
0052     m_readStmt->setSQL("select ecal_run_sq.NextVal from dual");
0053     ResultSet* rset = m_readStmt->executeQuery();
0054     while (rset->next()) {
0055       result = rset->getInt(1);
0056     }
0057     m_conn->terminateStatement(m_readStmt);
0058     return result;
0059 
0060   } catch (SQLException& e) {
0061     throw(std::runtime_error("ODDCCConfig::fetchNextId():  " + e.getMessage()));
0062   }
0063 }
0064 
0065 int ODRunConfigInfo::fetchID() noexcept(false) {
0066   // Return from memory if available
0067   if (m_ID > 0) {
0068     return m_ID;
0069   }
0070 
0071   this->checkConnection();
0072 
0073   DateHandler dh(m_env, m_conn);
0074 
0075   try {
0076     Statement* stmt = m_conn->createStatement();
0077     stmt->setSQL(
0078         "SELECT config_id from ECAL_RUN_CONFIGURATION_DAT "
0079         "WHERE tag = :tag "
0080         " and version = :version ");
0081     stmt->setString(1, m_tag);
0082     stmt->setInt(2, m_version);
0083 
0084     ResultSet* rset = stmt->executeQuery();
0085     if (rset->next()) {
0086       m_ID = rset->getInt(1);
0087     } else {
0088       m_ID = 0;
0089     }
0090     m_conn->terminateStatement(stmt);
0091   } catch (SQLException& e) {
0092     throw(std::runtime_error("ODRunConfigInfo::fetchID:  " + e.getMessage()));
0093   }
0094   setByID(m_ID);
0095   return m_ID;
0096 }
0097 
0098 int ODRunConfigInfo::fetchIDLast() noexcept(false) {
0099   this->checkConnection();
0100 
0101   DateHandler dh(m_env, m_conn);
0102 
0103   try {
0104     Statement* stmt = m_conn->createStatement();
0105     stmt->setSQL("SELECT max(config_id) FROM ecal_run_configuration_dat ");
0106     ResultSet* rset = stmt->executeQuery();
0107 
0108     if (rset->next()) {
0109       m_ID = rset->getInt(1);
0110     } else {
0111       m_ID = 0;
0112     }
0113     m_conn->terminateStatement(stmt);
0114   } catch (SQLException& e) {
0115     throw(std::runtime_error("ODRunConfigInfo::fetchIDLast:  " + e.getMessage()));
0116   }
0117 
0118   setByID(m_ID);
0119   return m_ID;
0120 }
0121 
0122 //
0123 int ODRunConfigInfo::fetchIDFromTagAndVersion() noexcept(false) {
0124   fetchID();
0125   return m_ID;
0126 }
0127 
0128 void ODRunConfigInfo::setByID(int id) noexcept(false) {
0129   this->checkConnection();
0130 
0131   DateHandler dh(m_env, m_conn);
0132 
0133   try {
0134     Statement* stmt = m_conn->createStatement();
0135 
0136     stmt->setSQL(
0137         "SELECT tag, version, run_type_def_id, run_mode_def_id, num_of_sequences, description, defaults,"
0138         " trg_mode,num_of_events, db_timestamp, usage_status"
0139         " FROM ECAL_RUN_CONFIGURATION_DAT WHERE config_id = :1");
0140     stmt->setInt(1, id);
0141 
0142     ResultSet* rset = stmt->executeQuery();
0143     if (rset->next()) {
0144       m_tag = rset->getString(1);
0145       m_version = rset->getInt(2);
0146       int run_type_id = rset->getInt(3);
0147       int run_mode_id = rset->getInt(4);
0148       m_num_seq = rset->getInt(5);
0149       m_description = rset->getString(6);
0150       m_defaults = rset->getInt(7);
0151       m_trigger_mode = rset->getString(8);
0152       m_num_events = rset->getInt(9);
0153       Date dbdate = rset->getDate(10);
0154       m_db_time = dh.dateToTm(dbdate);
0155       m_ID = id;
0156       m_runModeDef.setConnection(m_env, m_conn);
0157       m_runModeDef.setByID(run_mode_id);
0158       m_runTypeDef.setConnection(m_env, m_conn);
0159       m_runTypeDef.setByID(run_type_id);
0160       m_usage_status = rset->getString(11);
0161     } else {
0162       throw(std::runtime_error("ODRunConfigInfo::setByID:  Given config_id is not in the database"));
0163     }
0164     m_conn->terminateStatement(stmt);
0165   } catch (SQLException& e) {
0166     throw(std::runtime_error("ODRunConfigInfo::setByID:  " + e.getMessage()));
0167   }
0168 }
0169 
0170 void ODRunConfigInfo::prepareWrite() noexcept(false) {
0171   this->checkConnection();
0172 
0173   int next_id = fetchNextId();
0174 
0175   try {
0176     m_writeStmt = m_conn->createStatement();
0177     m_writeStmt->setSQL(
0178         "INSERT INTO ECAL_RUN_CONFIGURATION_DAT (CONFIG_ID, tag, version, run_type_def_id, "
0179         " run_mode_def_id, num_of_sequences, defaults, trg_mode, num_of_events, description, usage_status ) "
0180         " VALUES (:1, :2, :3 , :4, :5, :6 ,:7, :8, :9, :10 , :11)");
0181 
0182     m_writeStmt->setInt(1, next_id);
0183     m_ID = next_id;
0184 
0185   } catch (SQLException& e) {
0186     throw(std::runtime_error("ODRunConfigInfo::prepareWrite():  " + e.getMessage()));
0187   }
0188 }
0189 
0190 void ODRunConfigInfo::writeDB() noexcept(false) {
0191   this->checkConnection();
0192   this->checkPrepare();
0193 
0194   // Validate the data, use infinity-till convention
0195   DateHandler dh(m_env, m_conn);
0196 
0197   try {
0198     // get the run mode
0199     m_runModeDef.setConnection(m_env, m_conn);
0200     int run_mode_id = m_runModeDef.fetchID();
0201 
0202     // get the run type
0203     m_runTypeDef.setConnection(m_env, m_conn);
0204     int run_type_id = m_runTypeDef.fetchID();
0205 
0206     // now insert
0207 
0208     m_writeStmt->setString(2, this->getTag());
0209     m_writeStmt->setInt(3, this->getVersion());
0210     m_writeStmt->setInt(4, run_type_id);
0211     m_writeStmt->setInt(5, run_mode_id);
0212     m_writeStmt->setInt(6, this->getNumberOfSequences());
0213     m_writeStmt->setInt(7, this->getDefaults());
0214     m_writeStmt->setString(8, this->getTriggerMode());
0215     m_writeStmt->setInt(9, this->getNumberOfEvents());
0216     m_writeStmt->setString(10, this->getDescription());
0217     m_writeStmt->setString(11, this->getUsageStatus());
0218 
0219     m_writeStmt->executeUpdate();
0220 
0221   } catch (SQLException& e) {
0222     throw(std::runtime_error("ODRunConfigInfo::writeDB:  " + e.getMessage()));
0223   }
0224   // Now get the ID
0225   if (!this->fetchID()) {
0226     throw(std::runtime_error("ODRunConfigInfo::writeDB  Failed to write"));
0227   }
0228 
0229   this->setByID(m_ID);
0230 
0231   cout << "ODRunConfigInfo::writeDB>> done inserting ODRunConfigInfo with id=" << m_ID << endl;
0232 }
0233 
0234 int ODRunConfigInfo::updateDefaultCycle() noexcept(false) {
0235   this->checkConnection();
0236 
0237   // Check if this has already been written
0238   if (!this->fetchID()) {
0239     this->writeDB();
0240   }
0241 
0242   try {
0243     Statement* stmt = m_conn->createStatement();
0244 
0245     stmt->setSQL("UPDATE ecal_run_configuration_dat set defaults=:1 where config_id=:2 ");
0246 
0247     stmt->setInt(1, m_defaults);
0248     stmt->setInt(2, m_ID);
0249 
0250     stmt->executeUpdate();
0251 
0252     m_conn->terminateStatement(stmt);
0253   } catch (SQLException& e) {
0254     throw(std::runtime_error("ODRunConfigInfo::writeDB:  " + e.getMessage()));
0255   }
0256 
0257   return m_ID;
0258 }
0259 
0260 void ODRunConfigInfo::clear() {
0261   m_num_seq = 0;
0262   m_runTypeDef = RunTypeDef();
0263   m_runModeDef = RunModeDef();
0264   m_defaults = 0;
0265   m_trigger_mode = "";
0266   m_num_events = 0;
0267 }
0268 
0269 void ODRunConfigInfo::fetchData(ODRunConfigInfo* result) noexcept(false) {
0270   this->checkConnection();
0271   DateHandler dh(m_env, m_conn);
0272   //  result->clear();
0273 
0274   if (result->getId() == 0) {
0275     //throw(std::runtime_error("FEConfigMainInfo::fetchData(): no Id defined for this FEConfigMainInfo "));
0276     result->fetchID();
0277   }
0278   try {
0279     m_readStmt->setSQL(
0280         "SELECT config_id, tag, version, run_type_def_id, run_mode_def_id, \
0281       num_of_sequences, description, defaults, trg_mode, num_of_events, db_timestamp, usage_status \
0282       FROM ECAL_RUN_CONFIGURATION_DAT WHERE config_id = :1 ");
0283     m_readStmt->setInt(1, result->getId());
0284 
0285     ResultSet* rset = m_readStmt->executeQuery();
0286     rset->next();
0287 
0288     result->setId(rset->getInt(1));
0289     result->setTag(rset->getString(2));
0290     result->setVersion(rset->getInt(3));
0291     //    RunTypeDef myRunType = rset->getInt(4);
0292     //    result->setRunTypeDef( myRunType );
0293     //    RunModeDef myRunMode = rset->getInt(5);
0294     //    result->setRunModeDef( myRunMode );
0295     result->setNumberOfSequences(rset->getInt(6));
0296     result->setDescription(rset->getString(7));
0297     result->setDefaults(rset->getInt(8));
0298     result->setTriggerMode(rset->getString(9));
0299     result->setNumberOfEvents(rset->getInt(10));
0300     Date dbdate = rset->getDate(11);
0301     result->setDBTime(dh.dateToTm(dbdate));
0302     result->setUsageStatus(rset->getString(12));
0303 
0304   } catch (SQLException& e) {
0305     cout << " ODRunConfigInfo::fetchData():  " << e.getMessage() << endl;
0306     throw(std::runtime_error("ODRunConfigInfo::fetchData():  " + e.getMessage()));
0307   }
0308 }