File indexing completed on 2024-04-06 12:23:11
0001 #include <stdexcept>
0002 #include <cstdlib>
0003 #include <string>
0004 #include "OnlineDB/Oracle/interface/Oracle.h"
0005
0006 #include "OnlineDB/EcalCondDB/interface/ODTCCConfig.h"
0007
0008 using namespace std;
0009 using namespace oracle::occi;
0010
0011 ODTCCConfig::ODTCCConfig() {
0012 m_env = nullptr;
0013 m_conn = nullptr;
0014 m_writeStmt = nullptr;
0015 m_readStmt = nullptr;
0016 m_config_tag = "";
0017
0018 m_ID = 0;
0019 clear();
0020 m_size = 0;
0021 }
0022
0023 void ODTCCConfig::clear() {
0024 m_tcc_file = "";
0025 m_lut_file = "";
0026 m_slb_file = "";
0027 m_test_url = "";
0028 m_ntest = 0;
0029 }
0030
0031 ODTCCConfig::~ODTCCConfig() {}
0032
0033 int ODTCCConfig::fetchNextId() noexcept(false) {
0034 int result = 0;
0035 try {
0036 this->checkConnection();
0037
0038 m_readStmt = m_conn->createStatement();
0039 m_readStmt->setSQL("select ecal_tcc_config_sq.NextVal from dual");
0040 ResultSet* rset = m_readStmt->executeQuery();
0041 while (rset->next()) {
0042 result = rset->getInt(1);
0043 }
0044 m_conn->terminateStatement(m_readStmt);
0045 return result;
0046
0047 } catch (SQLException& e) {
0048 throw(std::runtime_error(std::string("ODTCCConfig::fetchNextId(): ") + e.getMessage()));
0049 }
0050 }
0051
0052 void ODTCCConfig::setParameters(const std::map<string, string>& my_keys_map) {
0053
0054
0055
0056 for (std::map<std::string, std::string>::const_iterator ci = my_keys_map.begin(); ci != my_keys_map.end(); ci++) {
0057 if (ci->first == "TCC_CONFIGURATION_ID")
0058 setConfigTag(ci->second);
0059 if (ci->first == "N_TESTPATTERNS_TO_LOAD")
0060 setNTestPatternsToLoad(atoi(ci->second.c_str()));
0061 if (ci->first == "LUT_CONFIGURATION_FILE")
0062 setLUTConfigurationFile(ci->second);
0063 if (ci->first == "CONFIGURATION_FILE")
0064 setTCCConfigurationFile(ci->second);
0065 if (ci->first == "SLB_CONFIGURATION_FILE")
0066 setSLBConfigurationFile(ci->second);
0067 if (ci->first == "TESTPATTERNFILE_URL")
0068 setTestPatternFileUrl(ci->second);
0069 }
0070 }
0071
0072 void ODTCCConfig::prepareWrite() noexcept(false) {
0073 this->checkConnection();
0074
0075 int next_id = fetchNextId();
0076
0077 try {
0078 m_writeStmt = m_conn->createStatement();
0079 m_writeStmt->setSQL(
0080 "INSERT INTO ECAL_TCC_CONFIGURATION (tcc_configuration_id, tcc_tag, "
0081 "Configuration_file, LUT_CONFIGURATION_FILE, SLB_CONFIGURATION_FILE, "
0082 "TESTPATTERNFILE_URL , N_TESTPATTERNS_TO_LOAD, "
0083 "tcc_configuration, lut_configuration, slb_configuration ) "
0084 "VALUES (:1, :2, :3, :4, :5, :6, :7, :8 , :9, :10)");
0085 m_writeStmt->setInt(1, next_id);
0086 m_writeStmt->setString(2, getConfigTag());
0087 m_writeStmt->setString(3, getTCCConfigurationFile());
0088 m_writeStmt->setString(4, getLUTConfigurationFile());
0089 m_writeStmt->setString(5, getSLBConfigurationFile());
0090 m_writeStmt->setString(6, getTestPatternFileUrl());
0091 m_writeStmt->setInt(7, getNTestPatternsToLoad());
0092
0093 oracle::occi::Clob clob1(m_conn);
0094 clob1.setEmpty();
0095 m_writeStmt->setClob(8, clob1);
0096
0097 oracle::occi::Clob clob2(m_conn);
0098 clob2.setEmpty();
0099 m_writeStmt->setClob(9, clob2);
0100
0101 oracle::occi::Clob clob3(m_conn);
0102 clob3.setEmpty();
0103 m_writeStmt->setClob(10, clob3);
0104
0105 m_writeStmt->executeUpdate();
0106 m_ID = next_id;
0107
0108 m_conn->terminateStatement(m_writeStmt);
0109 std::cout << "TCC 3 empty Clobs inserted into CONFIGURATION with id=" << next_id << std::endl;
0110
0111
0112 m_writeStmt = m_conn->createStatement();
0113 m_writeStmt->setSQL(
0114 "SELECT tcc_configuration, lut_configuration, slb_configuration FROM ECAL_TCC_CONFIGURATION WHERE"
0115 " tcc_configuration_id=:1 FOR UPDATE");
0116
0117 std::cout << "updating the clobs 0" << std::endl;
0118
0119 } catch (SQLException& e) {
0120 throw(std::runtime_error(std::string("ODTCCConfig::prepareWrite(): ") + e.getMessage()));
0121 }
0122
0123 std::cout << "updating the clob 1 " << std::endl;
0124 }
0125
0126 void ODTCCConfig::writeDB() noexcept(false) {
0127 std::cout << "updating the clob 2" << std::endl;
0128
0129 try {
0130 m_writeStmt->setInt(1, m_ID);
0131 ResultSet* rset = m_writeStmt->executeQuery();
0132
0133 while (rset->next()) {
0134 oracle::occi::Clob clob1 = rset->getClob(1);
0135 oracle::occi::Clob clob2 = rset->getClob(2);
0136 oracle::occi::Clob clob3 = rset->getClob(3);
0137 cout << "Opening the clob in read write mode" << endl;
0138 cout << "Populating the clobs" << endl;
0139 populateClob(clob1, getTCCConfigurationFile(), m_size);
0140 populateClob(clob2, getLUTConfigurationFile(), m_size);
0141 populateClob(clob3, getSLBConfigurationFile(), m_size);
0142 }
0143
0144 m_writeStmt->executeUpdate();
0145 m_writeStmt->closeResultSet(rset);
0146
0147 } catch (SQLException& e) {
0148 throw(std::runtime_error(std::string("ODTCCConfig::writeDB(): ") + e.getMessage()));
0149 }
0150
0151 if (!this->fetchID()) {
0152 throw(std::runtime_error("ODTCCConfig::writeDB: Failed to write"));
0153 }
0154 }
0155
0156 void ODTCCConfig::fetchData(ODTCCConfig* result) noexcept(false) {
0157 this->checkConnection();
0158 result->clear();
0159 if (result->getId() == 0 && (result->getConfigTag().empty())) {
0160 throw(std::runtime_error("ODTCCConfig::fetchData(): no Id defined for this ODTCCConfig "));
0161 }
0162
0163 try {
0164 m_readStmt->setSQL(
0165 "SELECT * "
0166 "FROM ECAL_TCC_CONFIGURATION d "
0167 " where (tcc_configuration_id = :1 or tcc_tag=:2 )");
0168 m_readStmt->setInt(1, result->getId());
0169 m_readStmt->setString(2, result->getConfigTag());
0170 ResultSet* rset = m_readStmt->executeQuery();
0171
0172 rset->next();
0173
0174 result->setId(rset->getInt(1));
0175 result->setConfigTag(rset->getString(2));
0176
0177 result->setTCCConfigurationFile(rset->getString(3));
0178 result->setLUTConfigurationFile(rset->getString(4));
0179 result->setSLBConfigurationFile(rset->getString(5));
0180 result->setTestPatternFileUrl(rset->getString(6));
0181 result->setNTestPatternsToLoad(rset->getInt(7));
0182
0183
0184 Clob clob1 = rset->getClob(8);
0185 cout << "Opening the clob in Read only mode" << endl;
0186 clob1.open(OCCI_LOB_READONLY);
0187 int clobLength = clob1.length();
0188 cout << "Length of the clob1 is: " << clobLength << endl;
0189 unsigned char* buffer = readClob(clob1, clobLength);
0190 clob1.close();
0191 cout << "the clob buffer is:" << endl;
0192 for (int i = 0; i < clobLength; ++i)
0193 cout << (char)buffer[i];
0194 cout << endl;
0195 result->setTCCClob(buffer);
0196
0197 Clob clob2 = rset->getClob(9);
0198 cout << "Opening the clob in Read only mode" << endl;
0199 clob2.open(OCCI_LOB_READONLY);
0200 clobLength = clob2.length();
0201 cout << "Length of the clob2 is: " << clobLength << endl;
0202 unsigned char* buffer2 = readClob(clob2, clobLength);
0203 clob2.close();
0204 cout << "the clob buffer is:" << endl;
0205 for (int i = 0; i < clobLength; ++i)
0206 cout << (char)buffer2[i];
0207 cout << endl;
0208 result->setLUTClob(buffer2);
0209
0210 Clob clob3 = rset->getClob(10);
0211 cout << "Opening the clob in Read only mode" << endl;
0212 clob3.open(OCCI_LOB_READONLY);
0213 clobLength = clob3.length();
0214 cout << "Length of the clob3 is: " << clobLength << endl;
0215 unsigned char* buffer3 = readClob(clob3, clobLength);
0216 clob3.close();
0217 cout << "the clob buffer is:" << endl;
0218 for (int i = 0; i < clobLength; ++i)
0219 cout << (char)buffer3[i];
0220 cout << endl;
0221 result->setSLBClob(buffer3);
0222
0223 } catch (SQLException& e) {
0224 throw(std::runtime_error(std::string("ODTCCConfig::fetchData(): ") + e.getMessage()));
0225 }
0226 }
0227
0228 int ODTCCConfig::fetchID() noexcept(false) {
0229 if (m_ID != 0) {
0230 return m_ID;
0231 }
0232
0233 this->checkConnection();
0234
0235 try {
0236 Statement* stmt = m_conn->createStatement();
0237 stmt->setSQL(
0238 "SELECT tcc_configuration_id FROM ecal_tcc_configuration "
0239 "WHERE tcc_tag=:tcc_tag ");
0240
0241 stmt->setString(1, getConfigTag());
0242
0243 ResultSet* rset = stmt->executeQuery();
0244
0245 if (rset->next()) {
0246 m_ID = rset->getInt(1);
0247 } else {
0248 m_ID = 0;
0249 }
0250 m_conn->terminateStatement(stmt);
0251 } catch (SQLException& e) {
0252 throw(std::runtime_error(std::string("ODTCCConfig::fetchID: ") + e.getMessage()));
0253 }
0254
0255 return m_ID;
0256 }