CMS 3D CMS Logo

ODLTCConfig.cc
Go to the documentation of this file.
1 #include <fstream>
2 #include <iostream>
3 #include <cstdio>
4 #include <stdexcept>
5 #include <string>
7 
9 
10 using namespace std;
11 using namespace oracle::occi;
12 
14  m_env = nullptr;
15  m_conn = nullptr;
16  m_writeStmt = nullptr;
17  m_readStmt = nullptr;
18  m_config_tag = "";
19  m_size = 0;
20 
21  m_ID = 0;
22  clear();
23 }
24 
26  // delete [] m_ltc_clob;
27 }
28 
30  int result = 0;
31  try {
32  this->checkConnection();
33 
34  m_readStmt = m_conn->createStatement();
35  m_readStmt->setSQL("select ecal_ltc_config_sq.NextVal from dual");
36  ResultSet* rset = m_readStmt->executeQuery();
37  while (rset->next()) {
38  result = rset->getInt(1);
39  }
40  m_conn->terminateStatement(m_readStmt);
41  return result;
42 
43  } catch (SQLException& e) {
44  throw(std::runtime_error(std::string("ODLTCConfig::fetchNextId(): ") + e.getMessage()));
45  }
46 }
47 
48 void ODLTCConfig::prepareWrite() noexcept(false) {
49  this->checkConnection();
50 
51  int next_id = fetchNextId();
52 
53  try {
54  m_writeStmt = m_conn->createStatement();
55  m_writeStmt->setSQL(
56  "INSERT INTO ECAL_LTC_CONFIGURATION (ltc_configuration_id, ltc_tag, "
57  " LTC_CONFIGURATION_file, "
58  " Configuration ) "
59  "VALUES (:1, :2, :3, :4 )");
60  m_writeStmt->setInt(1, next_id);
61  m_writeStmt->setString(2, this->getConfigTag());
62  m_writeStmt->setString(3, getLTCConfigurationFile());
63 
64  // and now the clob
65  oracle::occi::Clob clob(m_conn);
66  clob.setEmpty();
67  m_writeStmt->setClob(4, clob);
68  m_writeStmt->executeUpdate();
69  m_ID = next_id;
70 
71  m_conn->terminateStatement(m_writeStmt);
72  std::cout << "LTC Clob inserted into CONFIGURATION with id=" << next_id << std::endl;
73 
74  // now we read and update it
75  m_writeStmt = m_conn->createStatement();
76  m_writeStmt->setSQL(
77  "SELECT Configuration FROM ECAL_LTC_CONFIGURATION WHERE"
78  " ltc_configuration_id=:1 FOR UPDATE");
79 
80  std::cout << "updating the clob 0" << std::endl;
81 
82  } catch (SQLException& e) {
83  throw(std::runtime_error(std::string("ODLTCConfig::prepareWrite(): ") + e.getMessage()));
84  }
85 
86  std::cout << "updating the clob 1 " << std::endl;
87 }
88 
89 void ODLTCConfig::setParameters(const std::map<string, string>& my_keys_map) {
90  // parses the result of the XML parser that is a map of
91  // string string with variable name variable value
92 
93  for (std::map<std::string, std::string>::const_iterator ci = my_keys_map.begin(); ci != my_keys_map.end(); ci++) {
94  if (ci->first == "LTC_CONFIGURATION_ID")
95  setConfigTag(ci->second);
96  if (ci->first == "Configuration") {
97  std::string fname = ci->second;
98  string str3;
99  size_t pos, pose;
100 
101  pos = fname.find('='); // position of "live" in str
102  pose = fname.size(); // position of "]" in str
103  str3 = fname.substr(pos + 1, pose - pos - 2);
104 
105  cout << "fname=" << fname << " and reduced is: " << str3 << endl;
106  setLTCConfigurationFile(str3);
107 
108  // here we must open the file and read the LTC Clob
109  std::cout << "Going to read LTC file: " << fname << endl;
110 
111  ifstream inpFile;
112  inpFile.open(str3.c_str());
113 
114  // tell me size of file
115  int bufsize = 0;
116  inpFile.seekg(0, ios::end);
117  bufsize = inpFile.tellg();
118  std::cout << " bufsize =" << bufsize << std::endl;
119  // set file pointer to start again
120  inpFile.seekg(0, ios::beg);
121 
122  m_size = bufsize;
123 
124  inpFile.close();
125  }
126  }
127 }
128 
129 void ODLTCConfig::writeDB() noexcept(false) {
130  std::cout << "updating the clob " << std::endl;
131 
132  try {
133  m_writeStmt->setInt(1, m_ID);
134  ResultSet* rset = m_writeStmt->executeQuery();
135 
136  rset->next();
137  oracle::occi::Clob clob = rset->getClob(1);
138 
139  cout << "Opening the clob in read write mode" << endl;
140 
141  std::cout << "Populating the clob" << endl;
142 
143  populateClob(clob, getLTCConfigurationFile(), m_size);
144  int clobLength = clob.length();
145  cout << "Length of the clob is: " << clobLength << endl;
146  // clob.close ();
147 
148  m_writeStmt->executeUpdate();
149 
150  m_writeStmt->closeResultSet(rset);
151 
152  } catch (SQLException& e) {
153  throw(std::runtime_error(std::string("ODLTCConfig::writeDB(): ") + e.getMessage()));
154  }
155  // Now get the ID
156  if (!this->fetchID()) {
157  throw(std::runtime_error("ODLTCConfig::writeDB: Failed to write"));
158  }
159 }
160 
162  // strcpy((char *)m_ltc_clob, "");
163 
164  m_ltc_file = "";
165 }
166 
167 void ODLTCConfig::fetchData(ODLTCConfig* result) noexcept(false) {
168  this->checkConnection();
169  result->clear();
170  if (result->getId() == 0 && result->getConfigTag().empty()) {
171  throw(std::runtime_error("ODLTCConfig::fetchData(): no Id defined for this ODLTCConfig "));
172  }
173 
174  try {
175  m_readStmt->setSQL(
176  "SELECT * "
177  "FROM ECAL_LTC_CONFIGURATION "
178  " where (ltc_configuration_id = :1 or LTC_tag=:2 )");
179  m_readStmt->setInt(1, result->getId());
180  m_readStmt->setString(2, result->getConfigTag());
181  ResultSet* rset = m_readStmt->executeQuery();
182 
183  rset->next();
184  // 1 is the id and 2 is the config tag
185 
186  result->setId(rset->getInt(1));
187  result->setConfigTag(rset->getString(2));
188  result->setLTCConfigurationFile(rset->getString(3));
189 
190  Clob clob = rset->getClob(4);
191  cout << "Opening the clob in Read only mode" << endl;
192  clob.open(OCCI_LOB_READONLY);
193  int clobLength = clob.length();
194  cout << "Length of the clob is: " << clobLength << endl;
195  m_size = clobLength;
196  unsigned char* buffer = readClob(clob, clobLength);
197  clob.close();
198  cout << "the clob buffer is:" << endl;
199  for (int i = 0; i < clobLength; ++i)
200  cout << (char)buffer[i];
201  cout << endl;
202 
203  result->setLTCClob(buffer);
204 
205  } catch (SQLException& e) {
206  throw(std::runtime_error(std::string("ODLTCConfig::fetchData(): ") + e.getMessage()));
207  }
208 }
209 
210 int ODLTCConfig::fetchID() noexcept(false) {
211  if (m_ID != 0) {
212  return m_ID;
213  }
214 
215  this->checkConnection();
216 
217  try {
218  Statement* stmt = m_conn->createStatement();
219  stmt->setSQL(
220  "SELECT ltc_configuration_id FROM ecal_ltc_configuration "
221  "WHERE ltc_tag=:ltc_tag ");
222 
223  stmt->setString(1, getConfigTag());
224 
225  ResultSet* rset = stmt->executeQuery();
226 
227  if (rset->next()) {
228  m_ID = rset->getInt(1);
229  } else {
230  m_ID = 0;
231  }
232  m_conn->terminateStatement(stmt);
233  } catch (SQLException& e) {
234  throw(std::runtime_error(std::string("ODLTCConfig::fetchID: ") + e.getMessage()));
235  }
236 
237  return m_ID;
238 }
void prepareWrite() noexcept(false) override
Definition: ODLTCConfig.cc:48
void writeDB() noexcept(false)
Definition: ODLTCConfig.cc:129
void fetchData(ODLTCConfig *result) noexcept(false)
Definition: ODLTCConfig.cc:167
~ODLTCConfig() override
Definition: ODLTCConfig.cc:25
int fetchID() noexcept(false)
Definition: ODLTCConfig.cc:210
oracle::occi::Statement Statement
Definition: IODConfig.h:21
oracle::occi::Clob Clob
Definition: IODConfig.h:23
void clear()
Definition: ODLTCConfig.cc:161
string fname
main script
oracle::occi::SQLException SQLException
Definition: IODConfig.h:20
void clear(HadCaloObj &c)
Definition: data.h:124
int fetchNextId() noexcept(false)
Definition: ODLTCConfig.cc:29
void setParameters(const std::map< std::string, std::string > &my_keys_map)
Definition: ODLTCConfig.cc:89