CMS 3D CMS Logo

ODFEDAQConfig.cc
Go to the documentation of this file.
1 #include <stdexcept>
2 #include <cstdlib>
3 #include <string>
4 #include <cstring>
6 
8 
9 using namespace std;
10 using namespace oracle::occi;
11 
12 #define MY_NULL -1
13 #define SET_INT(statement, paramNum, paramVal) \
14  if (paramVal != MY_NULL) { \
15  statement->setInt(paramNum, paramVal); \
16  } else { \
17  statement->setNull(paramNum, OCCINUMBER); \
18  }
19 #define SET_STRING(statement, paramNum, paramVal) \
20  if (!paramVal.empty()) { \
21  statement->setString(paramNum, paramVal); \
22  } else { \
23  statement->setNull(paramNum, OCCICHAR); \
24  }
25 
26 int getInt(ResultSet* rset, int ipar) { return rset->isNull(ipar) ? MY_NULL : rset->getInt(ipar); }
27 
29  m_env = nullptr;
30  m_conn = nullptr;
31  m_writeStmt = nullptr;
32  m_readStmt = nullptr;
33  m_config_tag = "";
34  m_ID = 0;
35  clear();
36 }
37 
39  m_del = MY_NULL;
40  m_wei = MY_NULL;
41  m_ped = MY_NULL;
42 
43  m_bxt = MY_NULL;
44  m_btt = MY_NULL;
45  m_tbtt = MY_NULL;
46  m_tbxt = MY_NULL;
47 
48  m_version = 0;
49  m_com = "";
50 }
51 
53 
55  int result = 0;
56  try {
57  this->checkConnection();
58 
59  m_readStmt = m_conn->createStatement();
60  m_readStmt->setSQL("select fe_daq_conDfig_sq.nextVal from dual");
61  ResultSet* rset = m_readStmt->executeQuery();
62  while (rset->next()) {
63  result = rset->getInt(1);
64  }
65  m_conn->terminateStatement(m_readStmt);
66  return result;
67 
68  } catch (SQLException& e) {
69  throw(std::runtime_error(std::string("ODFEDAQConfig::fetchNextId(): ") + e.getMessage()));
70  }
71 }
72 
74  this->checkConnection();
75  int next_id = fetchNextId();
76 
77  try {
78  m_writeStmt = m_conn->createStatement();
79  m_writeStmt->setSQL(
80  "INSERT INTO FE_DAQ_CONFIG ( config_id, tag, version, ped_id, "
81  " del_id, wei_id,bxt_id, btt_id, tr_bxt_id, tr_btt_id, user_comment ) "
82  "VALUES ( :1, :2, :3, :4, :5, :6, :7 ,:8, :9, :10, :11 )");
83 
84  m_writeStmt->setInt(1, next_id);
85  m_ID = next_id;
86 
87  } catch (SQLException& e) {
88  throw(std::runtime_error(std::string("ODFEDAQConfig::prepareWrite(): ") + e.getMessage()));
89  }
90 }
91 
92 void ODFEDAQConfig::setParameters(const std::map<string, string>& my_keys_map) {
93  // parses the result of the XML parser that is a map of
94  // string string with variable name variable value
95 
96  for (std::map<std::string, std::string>::const_iterator ci = my_keys_map.begin(); ci != my_keys_map.end(); ci++) {
97  if (ci->first == "VERSION")
98  setVersion(atoi(ci->second.c_str()));
99  if (ci->first == "PED_ID")
100  setPedestalId(atoi(ci->second.c_str()));
101  if (ci->first == "DEL_ID")
102  setDelayId(atoi(ci->second.c_str()));
103  if (ci->first == "WEI_ID")
104  setWeightId(atoi(ci->second.c_str()));
105 
106  if (ci->first == "BXT_ID")
107  setBadXtId(atoi(ci->second.c_str()));
108  if (ci->first == "BTT_ID")
109  setBadTTId(atoi(ci->second.c_str()));
110  if (ci->first == "TRIG_BXT_ID")
111  setTriggerBadXtId(atoi(ci->second.c_str()));
112  if (ci->first == "TRIG_BTT_ID")
113  setTriggerBadTTId(atoi(ci->second.c_str()));
114 
115  if (ci->first == "COMMENT" || ci->first == "USER_COMMENT")
116  setComment(ci->second);
117  }
118 }
119 
120 void ODFEDAQConfig::writeDB() noexcept(false) {
121  this->checkConnection();
122  this->checkPrepare();
123 
124  try {
125  // number 1 is the id
126  m_writeStmt->setString(2, this->getConfigTag());
127  m_writeStmt->setInt(3, this->getVersion());
128  SET_INT(m_writeStmt, 4, this->getPedestalId());
129  SET_INT(m_writeStmt, 5, this->getDelayId());
130  SET_INT(m_writeStmt, 6, this->getWeightId());
131  SET_INT(m_writeStmt, 7, this->getBadXtId());
132  SET_INT(m_writeStmt, 8, this->getBadTTId());
133  SET_INT(m_writeStmt, 9, this->getTriggerBadXtId());
134  SET_INT(m_writeStmt, 10, this->getTriggerBadTTId());
135 
136  m_writeStmt->setString(11, this->getComment());
137 
138  m_writeStmt->executeUpdate();
139 
140  } catch (SQLException& e) {
141  throw(std::runtime_error(std::string("ODFEDAQConfig::writeDB(): ") + e.getMessage()));
142  }
143  // Now get the ID
144  if (!this->fetchID()) {
145  throw(std::runtime_error("ODFEDAQConfig::writeDB: Failed to write"));
146  }
147 }
148 
150  this->checkConnection();
151  result->clear();
152  if (result->getId() == 0 && (result->getConfigTag().empty())) {
153  throw(std::runtime_error("ODFEDAQConfig::fetchData(): no Id defined for this ODFEDAQConfig "));
154  }
155 
156  m_readStmt = m_conn->createStatement();
157  if (!result->getConfigTag().empty() && result->getVersion() == 0) {
158  int new_version = 0;
159  std::cout << "using new method : retrieving last version for this tag " << endl;
160  try {
161  this->checkConnection();
162 
163  m_readStmt->setSQL("select max(version) from " + getTable() + " where tag=:tag ");
164  m_readStmt->setString(1, result->getConfigTag());
165  std::cout << "Getting last ver" << std::endl << std::flush;
166  ResultSet* rset = m_readStmt->executeQuery();
167  while (rset->next()) {
168  new_version = rset->getInt(1);
169  }
170  m_conn->terminateStatement(m_readStmt);
171 
172  // m_readStmt = m_conn->createStatement();
173 
174  result->setVersion(new_version);
175 
176  } catch (SQLException& e) {
177  throw(std::runtime_error(std::string("ODFEDAQConfig::fetchData(): ") + e.getMessage()));
178  }
179  }
180 
181  try {
182  m_readStmt->setSQL("SELECT * FROM " + getTable() + " where ( config_id = :1 or (tag=:2 AND version=:3 ) )");
183  m_readStmt->setInt(1, result->getId());
184  m_readStmt->setString(2, result->getConfigTag());
185  m_readStmt->setInt(3, result->getVersion());
186  ResultSet* rset = m_readStmt->executeQuery();
187 
188  rset->next();
189 
190  // 1 is the id and 2 is the config tag and 3 is the version
191 
192  result->setId(rset->getInt(1));
193  result->setConfigTag(rset->getString(2));
194  result->setVersion(rset->getInt(3));
195 
196  result->setPedestalId(getInt(rset, 4));
197  result->setDelayId(getInt(rset, 5));
198  result->setWeightId(getInt(rset, 6));
199  result->setBadXtId(getInt(rset, 7));
200  result->setBadTTId(getInt(rset, 8));
201  result->setTriggerBadXtId(getInt(rset, 9));
202  result->setTriggerBadTTId(getInt(rset, 10));
203  result->setComment(rset->getString(11));
204 
205  } catch (SQLException& e) {
206  throw(std::runtime_error(std::string("ODFEDAQConfig::fetchData(): ") + e.getMessage()));
207  }
208 }
209 
210 int ODFEDAQConfig::fetchID() noexcept(false) {
211  // Return from memory if available
212  if (m_ID != 0) {
213  return m_ID;
214  }
215 
216  this->checkConnection();
217 
218  try {
219  Statement* stmt = m_conn->createStatement();
220  stmt->setSQL("SELECT config_id FROM " + getTable() + "WHERE tag=:1 and version=:2 ");
221 
222  stmt->setString(1, getConfigTag());
223  stmt->setInt(2, getVersion());
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("ODFEDAQConfig::fetchID: ") + e.getMessage()));
235  }
236 
237  return m_ID;
238 }
int fetchNextId() noexcept(false)
void setParameters(const std::map< std::string, std::string > &my_keys_map)
void prepareWrite() noexcept(false) override
int getInt(ResultSet *rset, int ipar)
~ODFEDAQConfig() override
int fetchID() noexcept(false)
#define SET_INT(statement, paramNum, paramVal)
oracle::occi::Statement Statement
Definition: IODConfig.h:21
void fetchData(ODFEDAQConfig *result) noexcept(false)
oracle::occi::SQLException SQLException
Definition: IODConfig.h:20
void clear(HadCaloObj &c)
Definition: data.h:124
void writeDB() noexcept(false)
#define MY_NULL