Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-15 01:25:28

0001 #include <stdexcept>
0002 #include "OnlineDB/Oracle/interface/Oracle.h"
0003 
0004 #include "OnlineDB/EcalCondDB/interface/FEConfigMainInfo.h"
0005 #include "OnlineDB/EcalCondDB/interface/Tm.h"
0006 #include "OnlineDB/EcalCondDB/interface/DateHandler.h"
0007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0008 
0009 using namespace std;
0010 using namespace oracle::occi;
0011 
0012 FEConfigMainInfo::FEConfigMainInfo() {
0013   m_env = nullptr;
0014   m_conn = nullptr;
0015   m_writeStmt = nullptr;
0016   m_readStmt = nullptr;
0017 
0018   m_ID = 0;
0019   m_version = 0;
0020   clear();
0021 }
0022 
0023 FEConfigMainInfo::~FEConfigMainInfo() {}
0024 
0025 void FEConfigMainInfo::clear() {
0026   m_description = "";
0027   m_ped_id = 0;
0028   m_lin_id = 0;
0029   m_lut_id = 0;
0030   m_sli_id = 0;
0031   m_fgr_id = 0;
0032   m_wei_id = 0;
0033   m_bxt_id = 0;
0034   m_btt_id = 0;
0035   m_tim_id = 0;
0036   m_spi_id = 0;
0037   m_bst_id = 0;
0038   m_coke_id = 0;
0039   m_wei2_id = 0;
0040 
0041   m_db_time = Tm();
0042 }
0043 int FEConfigMainInfo::fetchNextId() noexcept(false) {
0044   int result = 0;
0045   try {
0046     this->checkConnection();
0047 
0048     m_readStmt = m_conn->createStatement();
0049     m_readStmt->setSQL("select fe_config_main_sq.NextVal from dual");
0050     ResultSet* rset = m_readStmt->executeQuery();
0051     while (rset->next()) {
0052       result = rset->getInt(1);
0053     }
0054     m_conn->terminateStatement(m_readStmt);
0055     return result;
0056 
0057   } catch (SQLException& e) {
0058     throw cms::Exception("SQLException") << "FEConfigMainInfo::fetchNextId():  " << e.getMessage();
0059   }
0060 }
0061 
0062 int FEConfigMainInfo::fetchID() noexcept(false) {
0063   // Return from memory if available
0064   if (m_ID > 0) {
0065     return m_ID;
0066   }
0067 
0068   this->checkConnection();
0069 
0070   DateHandler dh(m_env, m_conn);
0071 
0072   std::cout << " tag/version " << getConfigTag() << "/" << getVersion() << std::endl;
0073 
0074   try {
0075     Statement* stmt = m_conn->createStatement();
0076     if (m_version != 0) {
0077       stmt->setSQL(
0078           "SELECT conf_id from FE_CONFIG_MAIN "
0079           "WHERE tag = :tag "
0080           " and version = :version ");
0081       stmt->setString(1, m_config_tag);
0082       stmt->setInt(2, m_version);
0083       std::cout << " using query with version " << endl;
0084     } else {
0085       // always select the last inserted one with a given tag
0086       stmt->setSQL(
0087           "SELECT conf_id from FE_CONFIG_MAIN "
0088           "WHERE tag = :1 and version= (select max(version) from FE_CONFIG_MAIN where tag=:2) ");
0089       stmt->setString(1, m_config_tag);
0090       stmt->setString(2, m_config_tag);
0091       std::cout << " using query WITHOUT version " << endl;
0092     }
0093 
0094     ResultSet* rset = stmt->executeQuery();
0095 
0096     if (rset->next()) {
0097       m_ID = rset->getInt(1);
0098     } else {
0099       m_ID = 0;
0100     }
0101     std::cout << m_ID << endl;
0102     m_conn->terminateStatement(stmt);
0103   } catch (SQLException& e) {
0104     throw cms::Exception("SQLException") << "FEConfigMainInfo::fetchID:  " << e.getMessage();
0105   }
0106   setByID(m_ID);
0107   return m_ID;
0108 }
0109 
0110 void FEConfigMainInfo::prepareWrite() noexcept(false) {
0111   this->checkConnection();
0112 
0113   int next_id = fetchNextId();
0114 
0115   try {
0116     m_writeStmt = m_conn->createStatement();
0117     m_writeStmt->setSQL(
0118         "INSERT INTO fe_config_main (conf_id, ped_conf_id, lin_conf_id, lut_conf_id, fgr_conf_id, sli_conf_id, "
0119         "wei_conf_id, spi_conf_id, tim_conf_id, bxt_conf_id, btt_conf_id, bst_conf_id, coke_conf_id, wei2_conf_id, "
0120         "tag, version, description) "
0121         " VALUES (:1, :2, :3 , :4, :5, :6 ,:7, :8, :9, :10, :11, :12, :13, :14, :15, :16, :17 )");
0122 
0123     m_writeStmt->setInt(1, next_id);
0124     m_ID = next_id;
0125 
0126   } catch (SQLException& e) {
0127     throw cms::Exception("SQLException") << "FEConfigMainInfo::prepareWrite():  " << e.getMessage();
0128   }
0129 }
0130 
0131 void FEConfigMainInfo::writeDB() noexcept(false) {
0132   this->checkConnection();
0133   this->checkPrepare();
0134 
0135   // Validate the data, use infinity-till convention
0136   DateHandler dh(m_env, m_conn);
0137 
0138   try {
0139     m_writeStmt->setInt(2, this->getPedId());
0140     m_writeStmt->setInt(3, this->getLinId());
0141     m_writeStmt->setInt(4, this->getLUTId());
0142     m_writeStmt->setInt(5, this->getFgrId());
0143     m_writeStmt->setInt(6, this->getSliId());
0144     m_writeStmt->setInt(7, this->getWeiId());
0145     m_writeStmt->setInt(8, this->getSpiId());
0146     m_writeStmt->setInt(9, this->getTimId());
0147     m_writeStmt->setInt(10, this->getBxtId());
0148     m_writeStmt->setInt(11, this->getBttId());
0149     m_writeStmt->setInt(12, this->getBstId());
0150     m_writeStmt->setInt(13, this->getCokeId());
0151     m_writeStmt->setInt(14, this->getWei2Id());
0152     m_writeStmt->setString(15, this->getConfigTag());
0153     m_writeStmt->setInt(16, this->getVersion());
0154     m_writeStmt->setString(17, this->getDescription());
0155     m_writeStmt->executeUpdate();
0156 
0157   } catch (SQLException& e) {
0158     throw cms::Exception("SQLException") << "FEConfigMainInfo::writeDB:  " << e.getMessage();
0159   }
0160   // Now get the ID
0161   if (!this->fetchID()) {
0162     throw(std::runtime_error("FEConfigMainInfo::writeDB:  Failed to write"));
0163   }
0164   setByID(m_ID);
0165 
0166   cout << "FEConfigMainInfo::writeDB>> done inserting FEConfigMainInfo with id=" << m_ID << endl;
0167 }
0168 
0169 int FEConfigMainInfo::fetchIDLast() noexcept(false) {
0170   this->checkConnection();
0171 
0172   DateHandler dh(m_env, m_conn);
0173 
0174   try {
0175     Statement* stmt = m_conn->createStatement();
0176     stmt->setSQL("SELECT max(conf_id) FROM fe_config_main ");
0177     ResultSet* rset = stmt->executeQuery();
0178 
0179     if (rset->next()) {
0180       m_ID = rset->getInt(1);
0181     } else {
0182       m_ID = 0;
0183     }
0184     m_conn->terminateStatement(stmt);
0185   } catch (SQLException& e) {
0186     throw cms::Exception("SQLException") << "ODRunConfigInfo::fetchIDLast:  " << e.getMessage();
0187   }
0188 
0189   setByID(m_ID);
0190   return m_ID;
0191 }
0192 
0193 void FEConfigMainInfo::setByID(int id) noexcept(false) {
0194   this->checkConnection();
0195 
0196   DateHandler dh(m_env, m_conn);
0197 
0198   cout << "FEConfigMainInfo::setByID called for id " << id << endl;
0199 
0200   try {
0201     Statement* stmt = m_conn->createStatement();
0202 
0203     stmt->setSQL(
0204         "SELECT conf_id, ped_conf_id, lin_conf_id, lut_conf_id, fgr_conf_id, sli_conf_id, wei_conf_id, spi_conf_id, "
0205         "tim_conf_id, bxt_conf_id, btt_conf_id, bst_conf_id, coke_conf_id, wei2_conf_id, tag, version, description, "
0206         "db_timestamp FROM FE_CONFIG_MAIN WHERE conf_id = :1 ");
0207     stmt->setInt(1, id);
0208 
0209     ResultSet* rset = stmt->executeQuery();
0210     if (rset->next()) {
0211       setId(rset->getInt(1));
0212       setPedId(rset->getInt(2));
0213       setLinId(rset->getInt(3));
0214       setLUTId(rset->getInt(4));
0215       setFgrId(rset->getInt(5));
0216       setSliId(rset->getInt(6));
0217       setWeiId(rset->getInt(7));
0218       setSpiId(rset->getInt(8));
0219       setTimId(rset->getInt(9));
0220       setBxtId(rset->getInt(10));
0221       setBttId(rset->getInt(11));
0222       setBstId(rset->getInt(12));
0223       setCokeId(rset->getInt(13));
0224       setWei2Id(rset->getInt(14));
0225       setConfigTag(rset->getString(15));
0226       setVersion(rset->getInt(16));
0227       setDescription(rset->getString(17));
0228       Date dbdate = rset->getDate(18);
0229       setDBTime(dh.dateToTm(dbdate));
0230       m_ID = id;
0231     } else {
0232       throw(std::runtime_error("FEConfigMainInfo::setByID:  Given cycle_id is not in the database"));
0233     }
0234     m_conn->terminateStatement(stmt);
0235   } catch (SQLException& e) {
0236     throw cms::Exception("SQLException") << "FEConfigMainInfo::setByID:  " << e.getMessage();
0237   }
0238 }
0239 
0240 void FEConfigMainInfo::fetchData(FEConfigMainInfo* result) noexcept(false) {
0241   std::cout << " ### 1 getId from FEConfigMainInfo = " << result->getId() << std::endl;
0242   std::cout << " tag/version " << result->getConfigTag() << "/" << result->getVersion() << std::endl;
0243 
0244   this->checkConnection();
0245   DateHandler dh(m_env, m_conn);
0246   //   result->clear();
0247 
0248   int idid = 0;
0249 
0250   if (result->getId() == 0) {
0251     //throw(std::runtime_error("FEConfigMainInfo::fetchData(): no Id defined for this FEConfigMainInfo "));
0252     idid = result->fetchID();
0253     result->setId(idid);
0254   }
0255 
0256   try {
0257     m_readStmt->setSQL(
0258         "SELECT conf_id, ped_conf_id, lin_conf_id, lut_conf_id, fgr_conf_id, sli_conf_id, wei_conf_id, spi_conf_id, "
0259         "tim_conf_id, bxt_conf_id, btt_conf_id, bst_conf_id, coke_conf_id, wei2_conf_id, tag, version, description, "
0260         "db_timestamp FROM FE_CONFIG_MAIN WHERE conf_id = :1 ");
0261 
0262     std::cout << " ### 2 getId from FEConfigMainInfo = " << result->getId() << std::endl;
0263 
0264     // good m_readStmt->setInt(1, result->getId());
0265     m_readStmt->setInt(1, result->getId());
0266     ResultSet* rset = m_readStmt->executeQuery();
0267 
0268     rset->next();
0269 
0270     result->setId(rset->getInt(1));
0271 
0272     setPedId(rset->getInt(2));
0273     setLinId(rset->getInt(3));
0274     setLUTId(rset->getInt(4));
0275     setFgrId(rset->getInt(5));
0276     setSliId(rset->getInt(6));
0277     setWeiId(rset->getInt(7));
0278     setSpiId(rset->getInt(8));
0279     setTimId(rset->getInt(9));
0280     setBxtId(rset->getInt(10));
0281     setBttId(rset->getInt(11));
0282     setBstId(rset->getInt(12));
0283     setCokeId(rset->getInt(13));
0284     setWei2Id(rset->getInt(14));
0285 
0286     result->setConfigTag(rset->getString(15));
0287     result->setVersion(rset->getInt(16));
0288     result->setDescription(rset->getString(17));
0289     Date dbdate = rset->getDate(18);
0290     result->setDBTime(dh.dateToTm(dbdate));
0291 
0292   } catch (SQLException& e) {
0293     throw cms::Exception("SQLException") << "FEConfigMainInfo::fetchData():  " << e.getMessage();
0294   }
0295 }
0296 
0297 void FEConfigMainInfo::insertConfig() noexcept(false) {
0298   try {
0299     prepareWrite();
0300     writeDB();
0301     m_conn->commit();
0302     terminateWriteStatement();
0303   } catch (std::runtime_error& e) {
0304     m_conn->rollback();
0305     throw cms::Exception("RuntimeError") << e.what();
0306   }
0307 }