CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
33 void LocationDef::setLocation(string loc)
34 {
35  if (loc != m_loc) {
36  m_ID = 0;
37  m_loc = loc;
38  }
39 }
40 
41 
42 
44  throw(std::runtime_error)
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  throw(std::runtime_error)
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  throw(std::runtime_error)
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 }
#define NULL
Definition: scimark2.h:8
oracle::occi::SQLException SQLException
Definition: HcalDbOmds.cc:27
virtual ~LocationDef()
Definition: LocationDef.cc:20
void fetchAllDefs(std::vector< LocationDef > *fillVec)
Definition: LocationDef.cc:103
std::string getLocation() const
Definition: LocationDef.cc:26
oracle::occi::ResultSet ResultSet
Definition: HcalDbOmds.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
int fetchID()
Definition: LocationDef.cc:43
void setByID(int id)
Definition: LocationDef.cc:76