CMS 3D CMS Logo

DCUTag.cc
Go to the documentation of this file.
1 #include <string>
3 
5 
6 using namespace std;
7 using namespace oracle::occi;
8 
10 {
11  m_env = nullptr;
12  m_conn = nullptr;
13  m_ID = 0;
14  m_genTag = "default";
15  m_locDef = LocationDef();
16 }
17 
18 
19 
21 {
22 }
23 
24 
25 
26 string DCUTag::getGeneralTag() const
27 {
28  return m_genTag;
29 }
30 
31 // User data methods
32 
33 
34 
35 void DCUTag::setGeneralTag(string genTag)
36 {
37  if (genTag != m_genTag) {
38  m_ID = 0;
39  m_genTag = genTag;
40  }
41 }
42 
43 
44 
46 {
47  return m_locDef;
48 }
49 
50 
51 
53 {
54  if (locDef != m_locDef) {
55  m_ID = 0;
56  m_locDef = locDef;
57  }
58 }
59 
60 
61 
64 {
65  // Return tag from memory if available
66  if (m_ID) {
67  return m_ID;
68  }
69 
70  this->checkConnection();
71 
72  // fetch the parent IDs
73  int locID;
74  this->fetchParentIDs(&locID);
75 
76  // fetch this ID
77  try {
78  Statement* stmt = m_conn->createStatement();
79  stmt->setSQL("SELECT tag_id FROM dcu_tag WHERE "
80  "gen_tag = :1 AND "
81  "location_id = :2");
82  stmt->setString(1, m_genTag);
83  stmt->setInt(2, locID);
84 
85  ResultSet* rset = stmt->executeQuery();
86 
87  if (rset->next()) {
88  m_ID = rset->getInt(1);
89  } else {
90  m_ID = 0;
91  }
92  m_conn->terminateStatement(stmt);
93  } catch (SQLException &e) {
94  throw(std::runtime_error("DCUTag::fetchID: "+e.getMessage()));
95  }
96 
97  return m_ID;
98 }
99 
100 
101 
102 void DCUTag::setByID(int id)
103  noexcept(false)
104 {
105  this->checkConnection();
106 
107  try {
108  Statement* stmt = m_conn->createStatement();
109 
110  stmt->setSQL("SELECT gen_tag, location_id FROM dcu_tag WHERE tag_id = :1");
111  stmt->setInt(1, id);
112 
113  ResultSet* rset = stmt->executeQuery();
114  if (rset->next()) {
115  m_genTag = rset->getString(1);
116  int locID = rset->getInt(2);
117 
118  m_locDef.setConnection(m_env, m_conn);
119  m_locDef.setByID(locID);
120 
121  m_ID = id;
122  } else {
123  throw(std::runtime_error("DCUTag::setByID: Given tag_id is not in the database"));
124  }
125 
126  m_conn->terminateStatement(stmt);
127  } catch (SQLException &e) {
128  throw(std::runtime_error("DCUTag::setByID: "+e.getMessage()));
129  }
130 }
131 
132 
134  noexcept(false)
135 {
136  // see if this data is already in the DB
137  if (this->fetchID()) {
138  return m_ID;
139  }
140 
141  // check the connectioin
142  this->checkConnection();
143 
144  // fetch the parent IDs
145  int locID;
146  this->fetchParentIDs(&locID);
147 
148  // write new tag to the DB
149  try {
150  Statement* stmt = m_conn->createStatement();
151 
152  stmt->setSQL("INSERT INTO dcu_tag (tag_id, gen_tag, location_id) "
153  "VALUES (dcu_tag_sq.NextVal, :1, :2)");
154  stmt->setString(1, m_genTag);
155  stmt->setInt(2, locID);
156 
157  stmt->executeUpdate();
158 
159  m_conn->terminateStatement(stmt);
160  } catch (SQLException &e) {
161  throw(std::runtime_error("DCUTag::writeDB: "+e.getMessage()));
162  }
163 
164  // now get the tag_id
165  if (!this->fetchID()) {
166  throw(std::runtime_error("DCUTag::writeDB: Failed to write"));
167  }
168 
169  return m_ID;
170 }
171 
172 
173 
174 void DCUTag::fetchAllTags( std::vector<DCUTag>* fillVec)
175  noexcept(false)
176 {
177  this->checkConnection();
178  try {
179  Statement* stmt = m_conn->createStatement();
180  stmt->setSQL("SELECT tag_id FROM dcu_tag ORDER BY tag_id");
181  ResultSet* rset = stmt->executeQuery();
182 
183  DCUTag dcutag;
184  dcutag.setConnection(m_env, m_conn);
185  while(rset->next()) {
186  dcutag.setByID( rset->getInt(1) );
187  fillVec->push_back( dcutag );
188  }
189  m_conn->terminateStatement(stmt);
190  } catch (SQLException &e) {
191  throw(std::runtime_error("DCUTag::fetchAllTags: "+e.getMessage()));
192  }
193 }
194 
195 
196 
197 void DCUTag::fetchParentIDs(int* locID)
198  noexcept(false)
199 {
200  // get the location
201  m_locDef.setConnection(m_env, m_conn);
202  *locID = m_locDef.fetchID();
203 
204  if (! *locID) {
205  throw(std::runtime_error("DCUTag::writeDB: Given location does not exist in DB"));
206  }
207 }
void setLocationDef(const LocationDef &locDef)
Definition: DCUTag.cc:52
~DCUTag() override
Definition: DCUTag.cc:20
DCUTag()
Definition: DCUTag.cc:9
LocationDef getLocationDef() const
Definition: DCUTag.cc:45
void fetchParentIDs(int *locId) noexcept(false)
Definition: DCUTag.cc:197
void setGeneralTag(std::string tag)
Definition: DCUTag.cc:35
int writeDB() noexcept(false)
Definition: DCUTag.cc:133
void fetchAllTags(std::vector< DCUTag > *fillVec) noexcept(false)
Definition: DCUTag.cc:174
#define noexcept
int fetchID() noexcept(false) override
Definition: DCUTag.cc:62
void setByID(int id) noexcept(false) override
Definition: DCUTag.cc:102
std::string getGeneralTag() const
Definition: DCUTag.cc:26
void setConnection(oracle::occi::Environment *env, oracle::occi::Connection *conn)
Definition: IDBObject.h:23
Definition: DCUTag.h:13