CMS 3D CMS Logo

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