CMS 3D CMS Logo

LocationDef.cc
Go to the documentation of this file.
1 #include <string>
3 
5 
6 using namespace std;
7 using namespace oracle::occi;
8 
10  m_env = nullptr;
11  m_conn = nullptr;
12  m_ID = 0;
13  m_loc = "";
14 }
15 
17 
18 string LocationDef::getLocation() const { return m_loc; }
19 
20 void LocationDef::setLocation(string loc) {
21  if (loc != m_loc) {
22  m_ID = 0;
23  m_loc = loc;
24  }
25 }
26 
27 int LocationDef::fetchID() noexcept(false) {
28  // Return def from memory if available
29  if (m_ID) {
30  return m_ID;
31  }
32 
33  this->checkConnection();
34 
35  try {
36  Statement* stmt = m_conn->createStatement();
37  stmt->setSQL(
38  "SELECT def_id FROM location_def WHERE "
39  "location = :location");
40  stmt->setString(1, m_loc);
41 
42  ResultSet* rset = stmt->executeQuery();
43 
44  if (rset->next()) {
45  m_ID = rset->getInt(1);
46  } else {
47  m_ID = 0;
48  }
49  m_conn->terminateStatement(stmt);
50  } catch (SQLException& e) {
51  throw(std::runtime_error("LocationDef::fetchID: " + e.getMessage()));
52  }
53 
54  return m_ID;
55 }
56 
57 void LocationDef::setByID(int id) noexcept(false) {
58  this->checkConnection();
59 
60  try {
61  Statement* stmt = m_conn->createStatement();
62 
63  stmt->setSQL("SELECT location FROM location_def WHERE def_id = :1");
64  stmt->setInt(1, id);
65 
66  ResultSet* rset = stmt->executeQuery();
67  if (rset->next()) {
68  m_loc = rset->getString(1);
69  m_ID = id;
70  } else {
71  throw(std::runtime_error("LocationDef::setByID: Given def_id is not in the database"));
72  }
73 
74  m_conn->terminateStatement(stmt);
75  } catch (SQLException& e) {
76  throw(std::runtime_error("LocationDef::setByID: " + e.getMessage()));
77  }
78 }
79 
80 void LocationDef::fetchAllDefs(std::vector<LocationDef>* fillVec) noexcept(false) {
81  this->checkConnection();
82  try {
83  Statement* stmt = m_conn->createStatement();
84  stmt->setSQL("SELECT def_id FROM location_def ORDER BY def_id");
85  ResultSet* rset = stmt->executeQuery();
86 
87  LocationDef locationDef;
88  locationDef.setConnection(m_env, m_conn);
89 
90  while (rset->next()) {
91  locationDef.setByID(rset->getInt(1));
92  fillVec->push_back(locationDef);
93  }
94  } catch (SQLException& e) {
95  throw(std::runtime_error("LocationDef::fetchAllDefs: " + e.getMessage()));
96  }
97 }
void fetchAllDefs(std::vector< LocationDef > *fillVec) noexcept(false)
Definition: LocationDef.cc:80
std::string getLocation() const
Definition: LocationDef.cc:18
~LocationDef() override
Definition: LocationDef.cc:16
void setByID(int id) noexcept(false) override
Definition: LocationDef.cc:57
int fetchID() noexcept(false) override
Definition: LocationDef.cc:27
void setLocation(std::string loc)
Definition: LocationDef.cc:20
void setConnection(oracle::occi::Environment *env, oracle::occi::Connection *conn)
Definition: IDBObject.h:23