CMS 3D CMS Logo

ODScanConfig.cc
Go to the documentation of this file.
1 #include <stdexcept>
2 #include <cstdlib>
3 #include <string>
5 
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  clear();
19 }
20 
22 
24  m_type_id = 0;
25  m_type = "";
26  m_from_val = 0;
27  m_to_val = 0;
28  m_step = 0;
29 }
30 
32  int result = 0;
33  try {
34  this->checkConnection();
35 
36  m_readStmt = m_conn->createStatement();
37  m_readStmt->setSQL("select ecal_scan_config_sq.NextVal from dual");
38  ResultSet* rset = m_readStmt->executeQuery();
39  while (rset->next()) {
40  result = rset->getInt(1);
41  }
42  m_conn->terminateStatement(m_readStmt);
43  return result;
44 
45  } catch (SQLException& e) {
46  throw(std::runtime_error(std::string("ODScanConfig::fetchNextId(): ") + e.getMessage()));
47  }
48 }
49 
50 void ODScanConfig::setParameters(const std::map<string, string>& my_keys_map) {
51  // parses the result of the XML parser that is a map of
52  // string string with variable name variable value
53 
54  for (std::map<std::string, std::string>::const_iterator ci = my_keys_map.begin(); ci != my_keys_map.end(); ci++) {
55  if (ci->first == "SCAN_ID")
56  setConfigTag(ci->second);
57  if (ci->first == "TYPE_ID")
58  setTypeId(atoi(ci->second.c_str()));
59  if (ci->first == "TYPE" || ci->first == "SCAN_TYPE")
60  setScanType(ci->second);
61  if (ci->first == "FROM" || ci->first == "FROM_VAL")
62  setFromVal(atoi(ci->second.c_str()));
63  if (ci->first == "TO" || ci->first == "TO_VAL")
64  setToVal(atoi(ci->second.c_str()));
65  if (ci->first == "STEP")
66  setStep(atoi(ci->second.c_str()));
67  }
68 }
69 
71  this->checkConnection();
72  int next_id = fetchNextId();
73 
74  try {
75  m_writeStmt = m_conn->createStatement();
76  m_writeStmt->setSQL(
77  "INSERT INTO ECAL_scan_dat ( scan_id, scan_tag ,"
78  " type_id, scan_type , FROM_VAL , TO_VAL, STEP )"
79  " VALUES ( :1, :2, :3, :4, :5, :6, :7)");
80  m_writeStmt->setInt(1, next_id);
81  m_ID = next_id;
82 
83  } catch (SQLException& e) {
84  throw(std::runtime_error(std::string("ODScanConfig::prepareWrite(): ") + e.getMessage()));
85  }
86 }
87 
88 void ODScanConfig::writeDB() noexcept(false) {
89  this->checkConnection();
90  this->checkPrepare();
91 
92  try {
93  // number 1 is the id
94  m_writeStmt->setString(2, this->getConfigTag());
95 
96  m_writeStmt->setInt(3, this->getTypeId());
97  m_writeStmt->setString(4, this->getScanType());
98  m_writeStmt->setInt(5, this->getFromVal());
99  m_writeStmt->setInt(6, this->getToVal());
100  m_writeStmt->setInt(7, this->getStep());
101 
102  m_writeStmt->executeUpdate();
103 
104  } catch (SQLException& e) {
105  throw(std::runtime_error(std::string("ODScanConfig::writeDB(): ") + e.getMessage()));
106  }
107  // Now get the ID
108  if (!this->fetchID()) {
109  throw(std::runtime_error("ODScanConfig::writeDB: Failed to write"));
110  }
111 }
112 
114  this->checkConnection();
115  result->clear();
116  if (result->getId() == 0 && (result->getConfigTag().empty())) {
117  throw(std::runtime_error("ODScanConfig::fetchData(): no Id defined for this ODScanConfig "));
118  }
119 
120  try {
121  m_readStmt->setSQL(
122  "SELECT * "
123  "FROM ECAL_SCAN_DAT "
124  " where (scan_id = :1 or scan_tag=:2 )");
125  m_readStmt->setInt(1, result->getId());
126  m_readStmt->setString(2, result->getConfigTag());
127 
128  ResultSet* rset = m_readStmt->executeQuery();
129 
130  rset->next();
131 
132  // id 1 is the scan_id
133  result->setId(rset->getInt(1));
134  result->setConfigTag(rset->getString(2));
135  result->setTypeId(rset->getInt(3));
136  result->setScanType(rset->getString(4));
137  result->setFromVal(rset->getInt(5));
138  result->setToVal(rset->getInt(6));
139  result->setStep(rset->getInt(7));
140 
141  } catch (SQLException& e) {
142  throw(std::runtime_error(std::string("ODScanConfig::fetchData(): ") + e.getMessage()));
143  }
144 }
145 
146 int ODScanConfig::fetchID() noexcept(false) {
147  // Return from memory if available
148  if (m_ID != 0) {
149  return m_ID;
150  }
151 
152  this->checkConnection();
153 
154  try {
155  Statement* stmt = m_conn->createStatement();
156  stmt->setSQL(
157  "SELECT scan_id FROM ecal_scan_dat "
158  "WHERE scan_tag=:scan_tag ");
159 
160  stmt->setString(1, getConfigTag());
161 
162  ResultSet* rset = stmt->executeQuery();
163 
164  if (rset->next()) {
165  m_ID = rset->getInt(1);
166  } else {
167  m_ID = 0;
168  }
169  m_conn->terminateStatement(stmt);
170  } catch (SQLException& e) {
171  throw(std::runtime_error(std::string("ODScanConfig::fetchID: ") + e.getMessage()));
172  }
173 
174  return m_ID;
175 }
~ODScanConfig() override
Definition: ODScanConfig.cc:21
void setParameters(const std::map< std::string, std::string > &my_keys_map)
Definition: ODScanConfig.cc:50
int fetchNextId() noexcept(false)
Definition: ODScanConfig.cc:31
oracle::occi::Statement Statement
Definition: IODConfig.h:21
oracle::occi::SQLException SQLException
Definition: IODConfig.h:20
void fetchData(ODScanConfig *result) noexcept(false)
void clear(EGIsoObj &c)
Definition: egamma.h:82
void prepareWrite() noexcept(false) override
Definition: ODScanConfig.cc:70
int fetchID() noexcept(false)
void writeDB() noexcept(false)
Definition: ODScanConfig.cc:88