CMS 3D CMS Logo

ODGolBiasCurrentInfo.cc
Go to the documentation of this file.
1 #include <stdexcept>
2 #include <string>
3 #include <cstring>
5 #include <cstdlib>
7 
8 using namespace std;
9 using namespace oracle::occi;
10 
12 {
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  m_version=0;
20  clear();
21 }
22 
23 
25 
26 }
27 
28 
29 
31 {
32 }
33 
34 
35 
37 
38  int result=0;
39  try {
40  this->checkConnection();
41 
42  m_readStmt = m_conn->createStatement();
43  m_readStmt->setSQL("select COND2CONF_INFO_SQ.NextVal from DUAL ");
44  ResultSet* rset = m_readStmt->executeQuery();
45  while (rset->next ()){
46  result= rset->getInt(1);
47  }
48  result++;
49  m_conn->terminateStatement(m_readStmt);
50  return result;
51 
52  } catch (SQLException &e) {
53  throw(std::runtime_error(std::string("ODGolBiasCurrentInfo::fetchNextId(): ")+e.getMessage()));
54  }
55 
56 }
57 
60 {
61  this->checkConnection();
62 
63  int next_id=0;
64  if(getId()==0){
65  next_id=fetchNextId();
66  }
67 
68  try {
69  m_writeStmt = m_conn->createStatement();
70  m_writeStmt->setSQL("INSERT INTO "+getTable()+" ( rec_id, tag, version) "
71  " VALUES ( :1, :2, :3 ) " );
72 
73  m_writeStmt->setInt(1, next_id);
74  m_ID=next_id;
75 
76  } catch (SQLException &e) {
77  throw(std::runtime_error(std::string("ODGolBiasCurrentInfo::prepareWrite(): ")+e.getMessage()));
78  }
79 
80 }
81 
82 void ODGolBiasCurrentInfo::setParameters(const std::map<string,string>& my_keys_map){
83 
84  // parses the result of the XML parser that is a map of
85  // string string with variable name variable value
86 
87  for( std::map<std::string, std::string >::const_iterator ci=
88  my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
89 
90  if(ci->first== "VERSION") setVersion(atoi(ci->second.c_str()) );
91  if(ci->first== "TAG") setConfigTag(ci->second);
92 
93  }
94 
95 }
96 
99 {
100  this->checkConnection();
101  this->checkPrepare();
102 
103  try {
104 
105  // number 1 is the id
106  m_writeStmt->setString(2, this->getConfigTag());
107  m_writeStmt->setInt(3, this->getVersion());
108 
109  m_writeStmt->executeUpdate();
110 
111 
112  } catch (SQLException &e) {
113  throw(std::runtime_error(std::string("ODGolBiasCurrentInfo::writeDB(): ")+e.getMessage()));
114  }
115  // Now get the ID
116  if (!this->fetchID()) {
117  throw(std::runtime_error("ODGolBiasCurrentInfo::writeDB: Failed to write"));
118  } else {
119  int old_version=this->getVersion();
120  m_readStmt = m_conn->createStatement();
121  this->fetchData (this);
122  m_conn->terminateStatement(m_readStmt);
123  if(this->getVersion()!=old_version) std::cout << "ODGolBiasCurrentInfo>>WARNING version is "<< getVersion()<< endl;
124  }
125 
126 
127 }
128 
129 
131  noexcept(false)
132 {
133  this->checkConnection();
134  result->clear();
135  if(result->getId()==0 && (result->getConfigTag().empty()) ){
136  throw(std::runtime_error("ODGolBiasCurrentInfo::fetchData(): no Id defined for this ODGolBiasCurrentInfo "));
137  }
138 
139 
140 
141  try {
142  if(result->getId()!=0) {
143  m_readStmt->setSQL("SELECT * FROM " + getTable() +
144  " where rec_id = :1 ");
145  m_readStmt->setInt(1, result->getId());
146  } else if (!result->getConfigTag().empty()) {
147 
148  if(result->getVersion() !=0){
149  m_readStmt->setSQL("SELECT * FROM " + getTable() +
150  " WHERE tag = :tag "
151  " and version = :version " );
152  m_readStmt->setString(1, result->getConfigTag());
153  m_readStmt->setInt(2, result->getVersion());
154  } else {
155  // always select the last inserted one with a given tag
156  m_readStmt->setSQL("SELECT * FROM " + getTable() +
157  " WHERE tag = :1 and version= (select max(version) from "+getTable() +" where tag=:2) " );
158  m_readStmt->setString(1, result->getConfigTag());
159  m_readStmt->setString(2, result->getConfigTag());
160  }
161 
162  } else {
163  // we should never pass here
164  throw(std::runtime_error("ODGolBiasCurrentInfo::fetchData(): no Id defined for this record "));
165  }
166 
167 
168  ResultSet* rset = m_readStmt->executeQuery();
169 
170  rset->next();
171 
172  // 1 is the id and 2 is the config tag and 3 is the version
173 
174  result->setId(rset->getInt(1));
175  result->setConfigTag(rset->getString(2));
176  result->setVersion(rset->getInt(3));
177 
178  } catch (SQLException &e) {
179  throw(std::runtime_error(std::string("ODGolBiasCurrentInfo::fetchData(): ")+e.getMessage()));
180  }
181 }
182 
184 {
185  // Return from memory if available
186  if (m_ID!=0) {
187  return m_ID;
188  }
189 
190  this->checkConnection();
191 
192  try {
193  Statement* stmt = m_conn->createStatement();
194  stmt->setSQL("SELECT rec_id FROM "+ getTable()+
195  " WHERE tag=:1 and version=:2 " );
196 
197  stmt->setString(1, getConfigTag() );
198  stmt->setInt(2, getVersion() );
199 
200  ResultSet* rset = stmt->executeQuery();
201 
202  if (rset->next()) {
203  m_ID = rset->getInt(1);
204  } else {
205  m_ID = 0;
206  }
207  m_conn->terminateStatement(stmt);
208  } catch (SQLException &e) {
209  throw(std::runtime_error(std::string("ODGolBiasCurrentInfo::fetchID: ")+e.getMessage()));
210  }
211 
212  return m_ID;
213 }
static unsigned int getId()
void setParameters(const std::map< std::string, std::string > &my_keys_map)
void fetchData(ODGolBiasCurrentInfo *result) noexcept(false)
int fetchNextId() noexcept(false)
void writeDB() noexcept(false)
void clear(CLHEP::HepGenMatrix &m)
Helper function: Reset all elements of a matrix to 0.
Definition: matutil.cc:167
void prepareWrite() noexcept(false) override
#define noexcept
oracle::occi::Statement Statement
Definition: IODConfig.h:23
int fetchID() noexcept(false)
oracle::occi::SQLException SQLException
Definition: IODConfig.h:22