CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ODLTSConfig.cc
Go to the documentation of this file.
1 #include <stdexcept>
2 #include <cstdlib>
3 #include <string>
5 
7 
8 using namespace std;
9 using namespace oracle::occi;
10 
12 {
13  m_env = NULL;
14  m_conn = NULL;
15  m_writeStmt = NULL;
16  m_readStmt = NULL;
17  m_config_tag="";
18  m_ID=0;
19  clear();
20 
21 }
22 
24  m_trg_type="";
25  m_num=0;
26  m_rate=0;
27  m_delay=0;
28 }
29 
30 
31 
33 {
34 }
35 
36 void ODLTSConfig::setParameters(std::map<string,string> my_keys_map){
37 
38  // parses the result of the XML parser that is a map of
39  // string string with variable name variable value
40 
41  for( std::map<std::string, std::string >::iterator ci=
42  my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
43 
44  if(ci->first== "LTS_CONFIGURATION_ID") setConfigTag(ci->second);
45  if(ci->first== "NUM_OF_EVENTS") setNumberOfEvents(atoi(ci->second.c_str()) );
46  if(ci->first== "RATE") setRate(atoi(ci->second.c_str() ));
47  if(ci->first== "TRIGGER_TYPE") setTriggerType(ci->second );
48  if(ci->first== "TRIG_LOC_L1_DELAY") setTrigLocL1Delay(atoi(ci->second.c_str() ));
49  }
50 
51 }
52 
53 int ODLTSConfig::fetchNextId() throw(std::runtime_error) {
54 
55  int result=0;
56  try {
57  this->checkConnection();
58 
59  m_readStmt = m_conn->createStatement();
60  m_readStmt->setSQL("select ecal_lts_config_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("ODLTSConfig::fetchNextId(): "+e.getMessage()));
70  }
71 
72 }
73 
74 
76  throw(std::runtime_error)
77 {
78  this->checkConnection();
79  int next_id=fetchNextId();
80 
81  try {
82  m_writeStmt = m_conn->createStatement();
83  m_writeStmt->setSQL("INSERT INTO ECAL_LTS_CONFIGURATION ( lts_configuration_id, lts_tag, "
84  "trigger_type, num_of_events, rate, trig_loc_l1_delay ) "
85  "VALUES ( "
86  ":1, :2, :3, :4 , :5, :6 )");
87  m_writeStmt->setInt(1, next_id);
88  m_ID=next_id;
89 
90  } catch (SQLException &e) {
91  throw(std::runtime_error("ODLTSConfig::prepareWrite(): "+e.getMessage()));
92  }
93 }
94 
95 
96 
98  throw(std::runtime_error)
99 {
100  this->checkConnection();
101  this->checkPrepare();
102 
103  try {
104 
105  m_writeStmt->setString(2, this->getConfigTag());
106  m_writeStmt->setString(3, this->getTriggerType());
107  m_writeStmt->setInt(4, this->getNumberOfEvents());
108  m_writeStmt->setInt(5, this->getRate());
109  m_writeStmt->setInt(6, this->getTrigLocL1Delay());
110 
111  m_writeStmt->executeUpdate();
112 
113 
114  } catch (SQLException &e) {
115  throw(std::runtime_error("ODLTSConfig::writeDB(): "+e.getMessage()));
116  }
117  // Now get the ID
118  if (!this->fetchID()) {
119  throw(std::runtime_error("ODLTSConfig::writeDB: Failed to write"));
120  }
121 
122 
123 }
124 
125 
126 
128  throw(std::runtime_error)
129 {
130  this->checkConnection();
131  result->clear();
132  if(result->getId()==0 && (result->getConfigTag()=="") ){
133  throw(std::runtime_error("ODLTSConfig::fetchData(): no Id defined for this ODLTSConfig "));
134  }
135 
136  try {
137 
138  m_readStmt->setSQL("SELECT * "
139  "FROM ECAL_LTS_CONFIGURATION "
140  " where ( lts_configuration_id = :1 or lts_tag=:2 ) " );
141  m_readStmt->setInt(1, result->getId());
142  m_readStmt->setString(2, result->getConfigTag());
143  ResultSet* rset = m_readStmt->executeQuery();
144 
145  rset->next();
146  // 1 is the id and 2 is the config tag
147  result->setId(rset->getInt(1));
148  result->setConfigTag(rset->getString(2));
149 
150  result->setTriggerType( rset->getString(3) );
151  result->setNumberOfEvents( rset->getInt(4) );
152  result->setRate( rset->getInt(5) );
153  result->setTrigLocL1Delay( rset->getInt(6) );
154 
155 
156  } catch (SQLException &e) {
157  throw(std::runtime_error("ODLTSConfig::fetchData(): "+e.getMessage()));
158  }
159 }
160 
161 int ODLTSConfig::fetchID() throw(std::runtime_error)
162 {
163  // Return from memory if available
164  if (m_ID!=0) {
165  return m_ID;
166  }
167 
168  this->checkConnection();
169 
170  try {
171  Statement* stmt = m_conn->createStatement();
172  stmt->setSQL("SELECT lts_configuration_id FROM ecal_lts_configuration "
173  "WHERE lts_tag=:lts_tag " );
174 
175  stmt->setString(1, getConfigTag());
176 
177  ResultSet* rset = stmt->executeQuery();
178 
179  if (rset->next()) {
180  m_ID = rset->getInt(1);
181  } else {
182  m_ID = 0;
183  }
184  m_conn->terminateStatement(stmt);
185  } catch (SQLException &e) {
186  throw(std::runtime_error("ODLTSConfig::fetchID: "+e.getMessage()));
187  }
188 
189  return m_ID;
190 }
void prepareWrite()
Definition: ODLTSConfig.cc:75
void writeDB()
Definition: ODLTSConfig.cc:97
void clear()
Definition: ODLTSConfig.cc:23
#define NULL
Definition: scimark2.h:8
void fetchData(ODLTSConfig *result)
Definition: ODLTSConfig.cc:127
void setParameters(std::map< std::string, std::string > my_keys_map)
Definition: ODLTSConfig.cc:36
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
int fetchNextId()
Definition: ODLTSConfig.cc:53
oracle::occi::ResultSet ResultSet
Definition: HcalDbOmds.cc:21
oracle::occi::SQLException SQLException
Definition: IODConfig.h:22