CMS 3D CMS Logo

ODCond2ConfInfo.cc
Go to the documentation of this file.
1 #include <stdexcept>
2 #include <string>
3 #include <cstring>
6 
8 
9 using namespace std;
10 using namespace oracle::occi;
11 
13  m_env = nullptr;
14  m_conn = nullptr;
15  m_writeStmt = nullptr;
16  m_readStmt = nullptr;
17  m_config_tag = "";
18  m_ID = 0;
19  clear();
20 }
21 
23  m_type = "";
24  m_loc = "";
25  m_run = 0;
26  m_desc = "";
27  m_rec_time = Tm();
28  m_db_time = Tm();
29  m_typ_id = 0;
30  m_loc_id = 0;
31 }
32 
34 
36  int result = 0;
37  try {
38  this->checkConnection();
39 
40  m_readStmt = m_conn->createStatement();
41  m_readStmt->setSQL("select COND2CONF_INFO_SQ.NextVal from DUAL ");
42  ResultSet* rset = m_readStmt->executeQuery();
43  while (rset->next()) {
44  result = rset->getInt(1);
45  }
46  result++;
47  m_conn->terminateStatement(m_readStmt);
48  return result;
49 
50  } catch (SQLException& e) {
51  throw(std::runtime_error(std::string("ODCond2ConfInfo::fetchNextId(): ") + e.getMessage()));
52  }
53 }
54 
56  if (m_typ_id == 0) {
57  if (!getType().empty()) {
58  try {
59  this->checkConnection();
60  m_readStmt = m_conn->createStatement();
61  m_readStmt->setSQL("select def_id from COND2CONF_TYPE_DEF where rec_type=" + getType());
62  ResultSet* rset = m_readStmt->executeQuery();
63  while (rset->next()) {
64  m_typ_id = rset->getInt(1);
65  }
66  m_conn->terminateStatement(m_readStmt);
67 
68  } catch (SQLException& e) {
69  throw(std::runtime_error(std::string("ODCond2ConfInfo::fetchParents(): ") + e.getMessage()));
70  }
71  }
72  }
73  if (m_loc_id == 0) {
74  if (!getLocation().empty()) {
75  try {
76  this->checkConnection();
77  m_readStmt = m_conn->createStatement();
78  m_readStmt->setSQL("select def_id from location_def where location=" + getLocation());
79  ResultSet* rset = m_readStmt->executeQuery();
80  while (rset->next()) {
81  m_loc_id = rset->getInt(1);
82  }
83  m_conn->terminateStatement(m_readStmt);
84  } catch (SQLException& e) {
85  throw(std::runtime_error(std::string("ODCond2ConfInfo::fetchParents(): ") + e.getMessage()));
86  }
87  }
88  }
89 }
90 
92  this->checkConnection();
93 
94  int next_id = 0;
95  if (getId() == 0) {
96  next_id = fetchNextId();
97  }
98 
99  fetchParents();
100  try {
101  m_writeStmt = m_conn->createStatement();
102  m_writeStmt->setSQL("INSERT INTO " + getTable() +
103  " ( rec_id, rec_type_id, rec_date, "
104  "location_id, run_number, short_desc ) "
105  " VALUES ( :1, :2, :3 , :4, :5, :6 ) ");
106 
107  m_writeStmt->setInt(1, next_id);
108  m_writeStmt->setInt(3, m_typ_id);
109  m_writeStmt->setInt(4, m_loc_id);
110 
111  m_ID = next_id;
112 
113  } catch (SQLException& e) {
114  throw(std::runtime_error(std::string("ODCond2ConfInfo::prepareWrite(): ") + e.getMessage()));
115  }
116 }
117 
118 void ODCond2ConfInfo::setParameters(const std::map<string, string>& my_keys_map) {
119  // parses the result of the XML parser that is a map of
120  // string string with variable name variable value
121 
122  for (std::map<std::string, std::string>::const_iterator ci = my_keys_map.begin(); ci != my_keys_map.end(); ci++) {
123  // if(ci->first== "TAG") setConfigTag(ci->second);
124  }
125  std::cout << "method not yet implemented" << std::endl;
126 }
127 
128 void ODCond2ConfInfo::writeDB() noexcept(false) {
129  this->checkConnection();
130  this->checkPrepare();
131 
132  DateHandler dh(m_env, m_conn);
133  if (m_rec_time.isNull()) {
134  int very_old_time = 0;
135  m_rec_time = Tm(very_old_time);
136  }
137 
138  try {
139  m_writeStmt->setDate(3, dh.tmToDate(this->m_rec_time));
140  m_writeStmt->setInt(5, this->getRunNumber());
141  m_writeStmt->setString(6, this->getDescription());
142 
143  m_writeStmt->executeUpdate();
144 
145  } catch (SQLException& e) {
146  throw(std::runtime_error(std::string("ODCond2ConfInfo::writeDB(): ") + e.getMessage()));
147  }
148  // Now get the ID
149  if (!this->fetchID()) {
150  throw(std::runtime_error("ODCond2ConfInfo::writeDB: Failed to write"));
151  }
152 }
153 
155  this->checkConnection();
156  result->clear();
157 
158  DateHandler dh(m_env, m_conn);
159 
160  if (result->getId() == 0) {
161  throw(std::runtime_error("ODCond2ConfInfo::fetchData(): no Id defined for this ODCond2ConfInfo "));
162  }
163 
164  try {
165  m_readStmt->setSQL(
166  "SELECT rec_id, REC_TYPE, rec_date, location, "
167  "run_number, short_desc, db_timestamp FROM " +
168  getTable() +
169  " , COND2CONF_TYPE_DEF , location_def "
170  " where rec_id = :1 AND COND2CONF_TYPE_DEF.def_id=" +
171  getTable() + ".REC_TYPE_ID AND location_def.def_id=LOCATION_ID ");
172  m_readStmt->setInt(1, result->getId());
173  ResultSet* rset = m_readStmt->executeQuery();
174 
175  rset->next();
176 
177  // 1 is the id and 2 is the config tag and 3 is the version
178 
179  // result->setId(rset->getInt(1));
180 
181  result->setType(rset->getString(2));
182  Date startDate = rset->getDate(3);
183  result->setLocation(rset->getString(4));
184  result->setRunNumber(rset->getInt(5));
185  result->setDescription(rset->getString(6));
186  Date endDate = rset->getDate(7);
187 
188  m_rec_time = dh.dateToTm(startDate);
189  m_db_time = dh.dateToTm(endDate);
190 
191  } catch (SQLException& e) {
192  throw(std::runtime_error(std::string("ODCond2ConfInfo::fetchData(): ") + e.getMessage()));
193  }
194 }
195 
197  // Return from memory if available
198  if (m_ID != 0) {
199  return m_ID;
200  }
201 
202  this->checkConnection();
203 
204  fetchParents();
205 
206  try {
207  Statement* stmt = m_conn->createStatement();
208  stmt->setSQL("SELECT rec_id FROM " + getTable() +
209  "WHERE rec_type_id=:1 and (run_number=:2 or short_desc=:3 ) order by rec_id DESC ");
210 
211  stmt->setInt(1, m_typ_id);
212  stmt->setInt(2, getRunNumber());
213  stmt->setString(3, getDescription());
214 
215  ResultSet* rset = stmt->executeQuery();
216 
217  if (rset->next()) {
218  m_ID = rset->getInt(1);
219  } else {
220  m_ID = 0;
221  }
222  m_conn->terminateStatement(stmt);
223  } catch (SQLException& e) {
224  throw(std::runtime_error(std::string("ODCond2ConfInfo::fetchID: ") + e.getMessage()));
225  }
226 
227  return m_ID;
228 }
void setParameters(const std::map< std::string, std::string > &my_keys_map)
int fetchNextId() noexcept(false)
void writeDB() noexcept(false)
void prepareWrite() noexcept(false) override
void fetchParents() noexcept(false)
~ODCond2ConfInfo() override
oracle::occi::Statement Statement
Definition: IODConfig.h:21
def getRunNumber(filename)
static unsigned int getId()
oracle::occi::SQLException SQLException
Definition: IODConfig.h:20
void clear(HadCaloObj &c)
Definition: data.h:124
dh
Definition: cuy.py:354
Definition: Tm.h:13
int fetchID() noexcept(false)
void fetchData(ODCond2ConfInfo *result) noexcept(false)