CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FEConfigSpikeInfo.cc
Go to the documentation of this file.
1 #include <stdexcept>
2 #include <string>
3 #include <string.h>
5 #include <cstdlib>
9 
10 using namespace std;
11 using namespace oracle::occi;
12 
14 {
15  m_env = NULL;
16  m_conn = NULL;
17  m_writeStmt = NULL;
18  m_readStmt = NULL;
19  m_config_tag="";
20  m_version=0;
21  m_ID=0;
22  clear();
23 }
24 
25 
27 }
28 
29 
30 
32 {
33 }
34 
35 
36 
37 int FEConfigSpikeInfo::fetchNextId() throw(std::runtime_error) {
38 
39  int result=0;
40  try {
41  this->checkConnection();
42 
43  m_readStmt = m_conn->createStatement();
44  m_readStmt->setSQL("select FE_CONFIG_SPI_SQ.NextVal from DUAL ");
45  ResultSet* rset = m_readStmt->executeQuery();
46  while (rset->next ()){
47  result= rset->getInt(1);
48  }
49  result++;
50  m_conn->terminateStatement(m_readStmt);
51  return result;
52 
53  } catch (SQLException &e) {
54  throw(std::runtime_error("FEConfigSpikeInfo::fetchNextId(): "+e.getMessage()));
55  }
56 
57 }
58 
60  throw(std::runtime_error)
61 {
62  this->checkConnection();
63 
64  int next_id=0;
65  if(getId()==0){
66  next_id=fetchNextId();
67  }
68 
69  try {
70  m_writeStmt = m_conn->createStatement();
71  m_writeStmt->setSQL("INSERT INTO "+getTable()+" ( spi_conf_id, tag, version ) "
72  " VALUES ( :1, :2, :3 ) " );
73 
74  m_writeStmt->setInt(1, next_id);
75  m_ID=next_id;
76 
77  } catch (SQLException &e) {
78  throw(std::runtime_error("FEConfigSpikeInfo::prepareWrite(): "+e.getMessage()));
79  }
80 
81 }
82 
83 void FEConfigSpikeInfo::setParameters(std::map<string,string> my_keys_map){
84 
85  // parses the result of the XML parser that is a map of
86  // string string with variable name variable value
87 
88  for( std::map<std::string, std::string >::iterator ci=
89  my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
90 
91  if(ci->first== "VERSION") setVersion(atoi(ci->second.c_str()) );
92  if(ci->first== "TAG") setConfigTag(ci->second);
93 
94  }
95 
96 }
97 
99  throw(std::runtime_error)
100 {
101  this->checkConnection();
102  this->checkPrepare();
103 
104  try {
105 
106  // number 1 is the id
107  m_writeStmt->setString(2, this->getConfigTag());
108  m_writeStmt->setInt(3, this->getVersion());
109 
110  m_writeStmt->executeUpdate();
111 
112 
113  } catch (SQLException &e) {
114  throw(std::runtime_error("FEConfigSpikeInfo::writeDB(): "+e.getMessage()));
115  }
116  // Now get the ID
117  if (!this->fetchID()) {
118  throw(std::runtime_error("FEConfigSpikeInfo::writeDB: Failed to write"));
119  }
120 
121 
122 }
123 
124 
126  throw(std::runtime_error)
127 {
128  this->checkConnection();
129  result->clear();
130  if(result->getId()==0 && (result->getConfigTag()=="") ){
131  throw(std::runtime_error("FEConfigSpikeInfo::fetchData(): no Id defined for this FEConfigSpikeInfo "));
132  }
133 
134  try {
135  DateHandler dh(m_env, m_conn);
136 
137  m_readStmt->setSQL("SELECT * FROM " + getTable() +
138  " where ( spi_conf_id= :1 or (tag=:2 AND version=:3 ) )" );
139  m_readStmt->setInt(1, result->getId());
140  m_readStmt->setString(2, result->getConfigTag());
141  m_readStmt->setInt(3, result->getVersion());
142  ResultSet* rset = m_readStmt->executeQuery();
143 
144  rset->next();
145 
146  // 1 is the id and 2 is the config tag and 3 is the version
147 
148  result->setId(rset->getInt(1));
149  result->setConfigTag(rset->getString(2));
150  result->setVersion(rset->getInt(3));
151  Date dbdate = rset->getDate(4);
152  result->setDBTime( dh.dateToTm( dbdate ));
153 
154  } catch (SQLException &e) {
155  throw(std::runtime_error("FEConfigSpikeInfo::fetchData(): "+e.getMessage()));
156  }
157 }
158 
160  throw(std::runtime_error)
161 {
162  this->checkConnection();
163  result->clear();
164  try {
165  DateHandler dh(m_env, m_conn);
166 
167  m_readStmt->setSQL("SELECT * FROM " + getTable() +
168  " where spi_conf_id = ( select max( spi_conf_id) from "+ getTable() +" ) " );
169  ResultSet* rset = m_readStmt->executeQuery();
170 
171  rset->next();
172 
173  result->setId(rset->getInt(1));
174  result->setConfigTag(rset->getString(2));
175  result->setVersion(rset->getInt(3));
176  Date dbdate = rset->getDate(4);
177  result->setDBTime( dh.dateToTm( dbdate ));
178 
179  } catch (SQLException &e) {
180  throw(std::runtime_error("FEConfigSpikeInfo::fetchData(): "+e.getMessage()));
181  }
182 }
183 
184 int FEConfigSpikeInfo::fetchID() throw(std::runtime_error)
185 {
186  // Return from memory if available
187  if (m_ID!=0) {
188  return m_ID;
189  }
190 
191  this->checkConnection();
192 
193  try {
194  Statement* stmt = m_conn->createStatement();
195  stmt->setSQL("SELECT spi_conf_id FROM "+ getTable()+
196  " WHERE tag=:1 and version=:2 " );
197 
198  stmt->setString(1, getConfigTag() );
199  stmt->setInt(2, getVersion() );
200 
201  ResultSet* rset = stmt->executeQuery();
202 
203  if (rset->next()) {
204  m_ID = rset->getInt(1);
205  } else {
206  m_ID = 0;
207  }
208  m_conn->terminateStatement(stmt);
209  } catch (SQLException &e) {
210  throw(std::runtime_error("FEConfigSpikeInfo::fetchID: "+e.getMessage()));
211  }
212 
213  return m_ID;
214 }
215 
216 
217 
219  throw(std::runtime_error)
220 {
221  this->checkConnection();
222 
223  DateHandler dh(m_env, m_conn);
224 
225  try {
226  Statement* stmt = m_conn->createStatement();
227 
228  stmt->setSQL("SELECT * FROM "+ getTable()+" WHERE spi_conf_id = :1");
229  stmt->setInt(1, id);
230 
231  ResultSet* rset = stmt->executeQuery();
232  if (rset->next()) {
233  this->setId(rset->getInt(1));
234  this->setConfigTag(rset->getString(2));
235  this->setVersion(rset->getInt(3));
236  Date dbdate = rset->getDate(4);
237  this->setDBTime( dh.dateToTm( dbdate ));
238  } else {
239  throw(std::runtime_error("FEConfigSpikeInfo::setByID: Given spi_conf_id is not in the database"));
240  }
241 
242  m_conn->terminateStatement(stmt);
243  } catch (SQLException &e) {
244  throw(std::runtime_error("FEConfigSpikeInfo::setByID: "+e.getMessage()));
245  }
246 }
247 
248 
249 
static unsigned int getId(void)
#define NULL
Definition: scimark2.h:8
void setParameters(std::map< std::string, std::string > my_keys_map)
void fetchLastData(FEConfigSpikeInfo *result)
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
void fetchData(FEConfigSpikeInfo *result)
oracle::occi::Statement Statement
Definition: IODConfig.h:23
oracle::occi::ResultSet ResultSet
Definition: HcalDbOmds.cc:21
oracle::occi::SQLException SQLException
Definition: IODConfig.h:22
Tm dateToTm(oracle::occi::Date &date) const
Definition: DateHandler.cc:31