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