CMS 3D CMS Logo

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