CMS 3D CMS Logo

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