CMS 3D CMS Logo

ODDCUConfig.cc
Go to the documentation of this file.
1 #include <stdexcept>
2 #include <string>
4 
6 
7 using namespace std;
8 using namespace oracle::occi;
9 
11 {
12  m_env = nullptr;
13  m_conn = nullptr;
14  m_writeStmt = nullptr;
15  m_readStmt = nullptr;
16  m_config_tag="";
17  m_ID=0;
18  clear();
19 
20 }
21 
23 }
24 
25 
26 
28 {
29 }
30 
31 void ODDCUConfig::setParameters(const std::map<string,string>& my_keys_map){
32 
33  // parses the result of the XML parser that is a map of
34  // string string with variable name variable value
35 
36  for( std::map<std::string, std::string >::const_iterator ci=
37  my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
38 
39  if(ci->first== "DCU_CONFIGURATION_ID") setConfigTag(ci->second);
40  }
41 
42 }
43 
45 
46  int result=0;
47  try {
48  this->checkConnection();
49 
50  m_readStmt = m_conn->createStatement();
51  m_readStmt->setSQL("select ecal_dcu_config_sq.NextVal from dual");
52  ResultSet* rset = m_readStmt->executeQuery();
53  while (rset->next ()){
54  result= rset->getInt(1);
55  }
56  m_conn->terminateStatement(m_readStmt);
57  return result;
58 
59  } catch (SQLException &e) {
60  throw(std::runtime_error(std::string("ODDCUConfig::fetchNextId(): ")+e.getMessage()));
61  }
62 
63 }
64 
65 
68 {
69  this->checkConnection();
70  int next_id=fetchNextId();
71 
72  try {
73  m_writeStmt = m_conn->createStatement();
74  m_writeStmt->setSQL("INSERT INTO ECAL_DCU_CONFIGURATION ( dcu_configuration_id, dcu_tag ) "
75  "VALUES ( "
76  ":1, :2 )");
77  m_writeStmt->setInt(1, next_id);
78  m_ID=next_id;
79 
80  } catch (SQLException &e) {
81  throw(std::runtime_error(std::string("ODDCUConfig::prepareWrite(): ")+e.getMessage()));
82  }
83 }
84 
85 
86 
89 {
90  this->checkConnection();
91  this->checkPrepare();
92 
93  try {
94 
95  m_writeStmt->setString(2, this->getConfigTag());
96 
97  m_writeStmt->executeUpdate();
98 
99 
100  } catch (SQLException &e) {
101  throw(std::runtime_error(std::string("ODDCUConfig::writeDB(): ")+e.getMessage()));
102  }
103  // Now get the ID
104  if (!this->fetchID()) {
105  throw(std::runtime_error("ODDCUConfig::writeDB: Failed to write"));
106  }
107 
108 
109 }
110 
111 
112 
114  noexcept(false)
115 {
116  this->checkConnection();
117  result->clear();
118  if(result->getId()==0 && (result->getConfigTag().empty()) ){
119  throw(std::runtime_error("ODDCUConfig::fetchData(): no Id defined for this ODDCUConfig "));
120  }
121 
122  try {
123 
124  m_readStmt->setSQL("SELECT * "
125  "FROM ECAL_DCU_CONFIGURATION "
126  " where ( dcu_configuration_id = :1 or dcu_tag=:2 ) " );
127  m_readStmt->setInt(1, result->getId());
128  m_readStmt->setString(2, result->getConfigTag());
129  ResultSet* rset = m_readStmt->executeQuery();
130 
131  rset->next();
132  // 1 is the id and 2 is the config tag
133  result->setId(rset->getInt(1));
134  result->setConfigTag(rset->getString(2));
135 
136 
137  } catch (SQLException &e) {
138  throw(std::runtime_error(std::string("ODDCUConfig::fetchData(): ")+e.getMessage()));
139  }
140 }
141 
143 {
144  // Return from memory if available
145  if (m_ID!=0) {
146  return m_ID;
147  }
148 
149  this->checkConnection();
150 
151  try {
152  Statement* stmt = m_conn->createStatement();
153  stmt->setSQL("SELECT dcu_configuration_id FROM ecal_dcu_configuration "
154  "WHERE dcu_tag=:dcu_tag " );
155 
156  stmt->setString(1, getConfigTag());
157 
158  ResultSet* rset = stmt->executeQuery();
159 
160  if (rset->next()) {
161  m_ID = rset->getInt(1);
162  } else {
163  m_ID = 0;
164  }
165  m_conn->terminateStatement(stmt);
166  } catch (SQLException &e) {
167  throw(std::runtime_error(std::string("ODDCUConfig::fetchID: ")+e.getMessage()));
168  }
169 
170  return m_ID;
171 }
~ODDCUConfig() override
Definition: ODDCUConfig.cc:27
int fetchID() noexcept(false)
Definition: ODDCUConfig.cc:142
void clear(CLHEP::HepGenMatrix &m)
Helper function: Reset all elements of a matrix to 0.
Definition: matutil.cc:167
#define noexcept
oracle::occi::Statement Statement
Definition: IODConfig.h:23
oracle::occi::SQLException SQLException
Definition: IODConfig.h:22
void writeDB() noexcept(false)
Definition: ODDCUConfig.cc:87
void clear()
Definition: ODDCUConfig.cc:22
int fetchNextId() noexcept(false)
Definition: ODDCUConfig.cc:44
void prepareWrite() noexcept(false) override
Definition: ODDCUConfig.cc:66
void fetchData(ODDCUConfig *result) noexcept(false)
Definition: ODDCUConfig.cc:113
void setParameters(const std::map< std::string, std::string > &my_keys_map)
Definition: ODDCUConfig.cc:31