CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RunTag.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 = NULL;
12  m_conn = NULL;
13  m_ID = 0;
14  m_genTag = "default";
15  m_locDef = LocationDef();
16  m_runTypeDef = RunTypeDef();
17 }
18 
19 
20 
22 {
23 }
24 
25 
26 
27 string RunTag::getGeneralTag() const
28 {
29  return m_genTag;
30 }
31 
32 // User data methods
33 
34 
35 
36 void RunTag::setGeneralTag(string genTag)
37 {
38  if (genTag != m_genTag) {
39  m_ID = 0;
40  m_genTag = genTag;
41  }
42 }
43 
44 
45 
47 {
48  return m_locDef;
49 }
50 
51 
52 
54 {
55  if (locDef != m_locDef) {
56  m_ID = 0;
57  m_locDef = locDef;
58  }
59 }
60 
61 
62 
64 {
65  return m_runTypeDef;
66 }
67 
68 
69 
70 void RunTag::setRunTypeDef(const RunTypeDef runTypeDef)
71 {
72  if (runTypeDef != m_runTypeDef) {
73  m_ID = 0;
74  m_runTypeDef = runTypeDef;
75  }
76 }
77 
78 
79 
81  throw(std::runtime_error)
82 {
83 
84 
85 
86  // Return tag from memory if available
87  if (m_ID) {
88  return m_ID;
89  }
90 
91 
92  this->checkConnection();
93 
94 
95  // fetch the parent IDs
96  int locID, runTypeID;
97  this->fetchParentIDs(&locID, &runTypeID);
98 
99  // fetch this ID
100  try {
101  Statement* stmt = m_conn->createStatement();
102  stmt->setSQL("SELECT tag_id FROM run_tag WHERE "
103  "gen_tag = :1 AND "
104  "location_id = :2 AND "
105  "run_type_id = :3");
106  stmt->setString(1, m_genTag);
107  stmt->setInt(2, locID);
108  stmt->setInt(3, runTypeID);
109 
110  ResultSet* rset = stmt->executeQuery();
111 
112  if (rset->next()) {
113  m_ID = rset->getInt(1);
114  } else {
115  m_ID = 0;
116  }
117  m_conn->terminateStatement(stmt);
118  } catch (SQLException &e) {
119  throw(std::runtime_error("RunTag::fetchID: "+e.getMessage()));
120  }
121 
122  return m_ID;
123 }
124 
125 
126 
127 void RunTag::setByID(int id)
128  throw(std::runtime_error)
129 {
130  this->checkConnection();
131 
132  try {
133  Statement* stmt = m_conn->createStatement();
134 
135  stmt->setSQL("SELECT gen_tag, location_id, run_type_id FROM run_tag WHERE tag_id = :1");
136  stmt->setInt(1, id);
137 
138  ResultSet* rset = stmt->executeQuery();
139  if (rset->next()) {
140  m_genTag = rset->getString(1);
141  int locID = rset->getInt(2);
142  int runTypeID = rset->getInt(3);
143 
144  m_locDef.setConnection(m_env, m_conn);
145  m_locDef.setByID(locID);
146 
147  m_runTypeDef.setConnection(m_env, m_conn);
148  m_runTypeDef.setByID(runTypeID);
149 
150  m_ID = id;
151  } else {
152  throw(std::runtime_error("RunTag::setByID: Given tag_id is not in the database"));
153  }
154 
155  m_conn->terminateStatement(stmt);
156  } catch (SQLException &e) {
157  throw(std::runtime_error("RunTag::setByID: "+e.getMessage()));
158  }
159 }
160 
161 
163  throw(std::runtime_error)
164 {
165  // see if this data is already in the DB
166  if (this->fetchID()) {
167  return m_ID;
168  }
169 
170  // check the connectioin
171  this->checkConnection();
172 
173  // fetch the parent IDs
174  int locID, runTypeID;
175  this->fetchParentIDs(&locID, &runTypeID);
176 
177  // write new tag to the DB
178  try {
179  Statement* stmt = m_conn->createStatement();
180 
181  stmt->setSQL("INSERT INTO run_tag (tag_id, gen_tag, location_id, run_type_id) "
182  "VALUES (run_tag_sq.NextVal, :1, :2, :3)");
183  stmt->setString(1, m_genTag);
184  stmt->setInt(2, locID);
185  stmt->setInt(3, runTypeID);
186 
187  stmt->executeUpdate();
188 
189  m_conn->terminateStatement(stmt);
190  } catch (SQLException &e) {
191  throw(std::runtime_error("RunTag::writeDB: "+e.getMessage()));
192  }
193 
194  // now get the tag_id
195  if (!this->fetchID()) {
196  throw(std::runtime_error("RunTag::writeDB: Failed to write"));
197  }
198 
199  return m_ID;
200 }
201 
202 
203 
204 void RunTag::fetchAllTags( std::vector<RunTag>* fillVec)
205  throw(std::runtime_error)
206 {
207  this->checkConnection();
208  try {
209  Statement* stmt = m_conn->createStatement();
210  stmt->setSQL("SELECT tag_id FROM run_tag ORDER BY tag_id");
211  ResultSet* rset = stmt->executeQuery();
212 
213  RunTag runtag;
214  runtag.setConnection(m_env, m_conn);
215  while(rset->next()) {
216  runtag.setByID( rset->getInt(1) );
217  fillVec->push_back( runtag );
218  }
219  m_conn->terminateStatement(stmt);
220  } catch (SQLException &e) {
221  throw(std::runtime_error("RunTag::fetchAllTags: "+e.getMessage()));
222  }
223 }
224 
225 
226 
227 void RunTag::fetchParentIDs(int* locID, int* runTypeID)
228  throw(std::runtime_error)
229 {
230  // get the location
231  m_locDef.setConnection(m_env, m_conn);
232  *locID = m_locDef.fetchID();
233 
234  // get the run type
235  m_runTypeDef.setConnection(m_env, m_conn);
236  *runTypeID = m_runTypeDef.fetchID();
237 
238 
239 
240  if (! *locID) {
241  throw(std::runtime_error("RunTag::fetchparentids: Given location does not exist in DB"));
242  }
243 
244  if (! *runTypeID) {
245  throw(std::runtime_error("RunTag::fetchParentIDs: Given run type does not exist in DB"));
246  }
247 }
void setLocationDef(const LocationDef locDef)
Definition: RunTag.cc:53
LocationDef getLocationDef() const
Definition: RunTag.cc:46
RunTag()
Definition: RunTag.cc:9
Definition: RunTag.h:13
std::string getGeneralTag() const
Definition: RunTag.cc:27
#define NULL
Definition: scimark2.h:8
oracle::occi::SQLException SQLException
Definition: HcalDbOmds.cc:22
int writeDB()
Definition: RunTag.cc:162
~RunTag()
Definition: RunTag.cc:21
void fetchAllTags(std::vector< RunTag > *fillVec)
Definition: RunTag.cc:204
void setRunTypeDef(const RunTypeDef runTypeDef)
Definition: RunTag.cc:70
void fetchParentIDs(int *locId, int *runTypeID)
Definition: RunTag.cc:227
int fetchID()
Definition: RunTag.cc:80
void setByID(int id)
Definition: RunTag.cc:127
RunTypeDef getRunTypeDef() const
Definition: RunTag.cc:63
oracle::occi::ResultSet ResultSet
Definition: HcalDbOmds.cc:21
void setConnection(oracle::occi::Environment *env, oracle::occi::Connection *conn)
Definition: IDBObject.h:23
void setGeneralTag(std::string tag)
Definition: RunTag.cc:36