CMS 3D CMS Logo

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