CMS 3D CMS Logo

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