CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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  m_env = nullptr;
12  m_conn = nullptr;
13  m_writeStmt = nullptr;
14  m_readStmt = nullptr;
15  m_config_tag = "";
16  m_ID = 0;
17  clear();
18 }
19 
21 
23 
24 void ODDCUConfig::setParameters(const std::map<string, string>& my_keys_map) {
25  // parses the result of the XML parser that is a map of
26  // string string with variable name variable value
27 
28  for (std::map<std::string, std::string>::const_iterator ci = my_keys_map.begin(); ci != my_keys_map.end(); ci++) {
29  if (ci->first == "DCU_CONFIGURATION_ID")
30  setConfigTag(ci->second);
31  }
32 }
33 
35  int result = 0;
36  try {
37  this->checkConnection();
38 
39  m_readStmt = m_conn->createStatement();
40  m_readStmt->setSQL("select ecal_dcu_config_sq.NextVal from dual");
41  ResultSet* rset = m_readStmt->executeQuery();
42  while (rset->next()) {
43  result = rset->getInt(1);
44  }
45  m_conn->terminateStatement(m_readStmt);
46  return result;
47 
48  } catch (SQLException& e) {
49  throw(std::runtime_error(std::string("ODDCUConfig::fetchNextId(): ") + e.getMessage()));
50  }
51 }
52 
53 void ODDCUConfig::prepareWrite() noexcept(false) {
54  this->checkConnection();
55  int next_id = fetchNextId();
56 
57  try {
58  m_writeStmt = m_conn->createStatement();
59  m_writeStmt->setSQL(
60  "INSERT INTO ECAL_DCU_CONFIGURATION ( dcu_configuration_id, dcu_tag ) "
61  "VALUES ( "
62  ":1, :2 )");
63  m_writeStmt->setInt(1, next_id);
64  m_ID = next_id;
65 
66  } catch (SQLException& e) {
67  throw(std::runtime_error(std::string("ODDCUConfig::prepareWrite(): ") + e.getMessage()));
68  }
69 }
70 
71 void ODDCUConfig::writeDB() noexcept(false) {
72  this->checkConnection();
73  this->checkPrepare();
74 
75  try {
76  m_writeStmt->setString(2, this->getConfigTag());
77 
78  m_writeStmt->executeUpdate();
79 
80  } catch (SQLException& e) {
81  throw(std::runtime_error(std::string("ODDCUConfig::writeDB(): ") + e.getMessage()));
82  }
83  // Now get the ID
84  if (!this->fetchID()) {
85  throw(std::runtime_error("ODDCUConfig::writeDB: Failed to write"));
86  }
87 }
88 
89 void ODDCUConfig::fetchData(ODDCUConfig* result) noexcept(false) {
90  this->checkConnection();
91  result->clear();
92  if (result->getId() == 0 && (result->getConfigTag().empty())) {
93  throw(std::runtime_error("ODDCUConfig::fetchData(): no Id defined for this ODDCUConfig "));
94  }
95 
96  try {
97  m_readStmt->setSQL(
98  "SELECT * "
99  "FROM ECAL_DCU_CONFIGURATION "
100  " where ( dcu_configuration_id = :1 or dcu_tag=:2 ) ");
101  m_readStmt->setInt(1, result->getId());
102  m_readStmt->setString(2, result->getConfigTag());
103  ResultSet* rset = m_readStmt->executeQuery();
104 
105  rset->next();
106  // 1 is the id and 2 is the config tag
107  result->setId(rset->getInt(1));
108  result->setConfigTag(rset->getString(2));
109 
110  } catch (SQLException& e) {
111  throw(std::runtime_error(std::string("ODDCUConfig::fetchData(): ") + e.getMessage()));
112  }
113 }
114 
115 int ODDCUConfig::fetchID() noexcept(false) {
116  // Return from memory if available
117  if (m_ID != 0) {
118  return m_ID;
119  }
120 
121  this->checkConnection();
122 
123  try {
124  Statement* stmt = m_conn->createStatement();
125  stmt->setSQL(
126  "SELECT dcu_configuration_id FROM ecal_dcu_configuration "
127  "WHERE dcu_tag=:dcu_tag ");
128 
129  stmt->setString(1, getConfigTag());
130 
131  ResultSet* rset = stmt->executeQuery();
132 
133  if (rset->next()) {
134  m_ID = rset->getInt(1);
135  } else {
136  m_ID = 0;
137  }
138  m_conn->terminateStatement(stmt);
139  } catch (SQLException& e) {
140  throw(std::runtime_error(std::string("ODDCUConfig::fetchID: ") + e.getMessage()));
141  }
142 
143  return m_ID;
144 }
~ODDCUConfig() override
Definition: ODDCUConfig.cc:22
int fetchID() noexcept(false)
Definition: ODDCUConfig.cc:115
oracle::occi::Statement Statement
Definition: IODConfig.h:21
oracle::occi::SQLException SQLException
Definition: IODConfig.h:20
void writeDB() noexcept(false)
Definition: ODDCUConfig.cc:71
void clear()
Definition: ODDCUConfig.cc:20
void clear(EGIsoObj &c)
Definition: egamma.h:82
int fetchNextId() noexcept(false)
Definition: ODDCUConfig.cc:34
void prepareWrite() noexcept(false) override
Definition: ODDCUConfig.cc:53
void fetchData(ODDCUConfig *result) noexcept(false)
Definition: ODDCUConfig.cc:89
void setParameters(const std::map< std::string, std::string > &my_keys_map)
Definition: ODDCUConfig.cc:24