CMS 3D CMS Logo

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