Go to the documentation of this file.00001 #include <string>
00002 #include "OnlineDB/Oracle/interface/Oracle.h"
00003
00004 #include "OnlineDB/EcalCondDB/interface/CaliTag.h"
00005
00006 using namespace std;
00007 using namespace oracle::occi;
00008
00009 CaliTag::CaliTag()
00010 {
00011 m_env = NULL;
00012 m_conn = NULL;
00013 m_ID = 0;
00014 m_genTag = "default";
00015 m_locDef = LocationDef();
00016 m_method = "default";
00017 m_version = "default";
00018 m_dataType = "default";
00019 }
00020
00021
00022
00023 CaliTag::~CaliTag()
00024 {
00025 }
00026
00027
00028
00029 string CaliTag::getGeneralTag() const
00030 {
00031 return m_genTag;
00032 }
00033
00034
00035
00036 void CaliTag::setGeneralTag(string genTag)
00037 {
00038 if (genTag != m_genTag) {
00039 m_ID = 0;
00040 m_genTag = genTag;
00041 }
00042 }
00043
00044
00045
00046 LocationDef CaliTag::getLocationDef() const
00047 {
00048 return m_locDef;
00049 }
00050
00051
00052
00053 void CaliTag::setLocationDef(const LocationDef locDef)
00054 {
00055 if (locDef != m_locDef) {
00056 m_ID = 0;
00057 m_locDef = locDef;
00058 }
00059 }
00060
00061
00062
00063 string CaliTag::getMethod() const
00064 {
00065 return m_method;
00066 }
00067
00068
00069
00070 void CaliTag::setMethod(string method)
00071 {
00072 if (method != m_method) {
00073 m_ID = 0;
00074 m_method = method;
00075 }
00076 }
00077
00078
00079
00080 string CaliTag::getVersion() const
00081 {
00082 return m_version;
00083 }
00084
00085
00086
00087 void CaliTag::setVersion(string version)
00088 {
00089 if (version != m_version) {
00090 m_ID = 0;
00091 m_version = version;
00092 }
00093 }
00094
00095
00096
00097 string CaliTag::getDataType() const
00098 {
00099 return m_dataType;
00100 }
00101
00102
00103
00104 void CaliTag::setDataType(string dataType)
00105 {
00106 if (dataType != m_dataType) {
00107 m_ID = 0;
00108 m_dataType = dataType;
00109 }
00110 }
00111
00112
00113
00114 int CaliTag::fetchID()
00115 throw(std::runtime_error)
00116 {
00117
00118 if (m_ID) {
00119 return m_ID;
00120 }
00121
00122 this->checkConnection();
00123
00124
00125 int locID;
00126 this->fetchParentIDs(&locID);
00127
00128
00129 try {
00130 Statement* stmt = m_conn->createStatement();
00131 stmt->setSQL("SELECT tag_id FROM cali_tag WHERE "
00132 "gen_tag = :1 AND "
00133 "location_id = :2 AND "
00134 "method = :3 AND "
00135 "version = :4 AND "
00136 "data_type = :5");
00137 stmt->setString(1, m_genTag);
00138 stmt->setInt(2, locID);
00139 stmt->setString(3, m_method);
00140 stmt->setString(4, m_version);
00141 stmt->setString(5, m_dataType);
00142
00143 ResultSet* rset = stmt->executeQuery();
00144
00145 if (rset->next()) {
00146 m_ID = rset->getInt(1);
00147 } else {
00148 m_ID = 0;
00149 }
00150 m_conn->terminateStatement(stmt);
00151 } catch (SQLException &e) {
00152 throw(std::runtime_error("CaliTag::fetchID: "+e.getMessage()));
00153 }
00154
00155 return m_ID;
00156 }
00157
00158
00159
00160 void CaliTag::setByID(int id)
00161 throw(std::runtime_error)
00162 {
00163 this->checkConnection();
00164
00165 try {
00166 Statement* stmt = m_conn->createStatement();
00167
00168 stmt->setSQL("SELECT gen_tag, location_id, method, version, data_type "
00169 "FROM cali_tag WHERE tag_id = :1");
00170 stmt->setInt(1, id);
00171
00172 ResultSet* rset = stmt->executeQuery();
00173 if (rset->next()) {
00174 m_genTag = rset->getString(1);
00175 int locID = rset->getInt(2);
00176 m_locDef.setConnection(m_env, m_conn);
00177 m_locDef.setByID(locID);
00178 m_method = rset->getString(3);
00179 m_version = rset->getString(4);
00180 m_dataType = rset->getString(5);
00181
00182 m_ID = id;
00183 } else {
00184 throw(std::runtime_error("CaliTag::setByID: Given tag_id is not in the database"));
00185 }
00186
00187 m_conn->terminateStatement(stmt);
00188 } catch (SQLException &e) {
00189 throw(std::runtime_error("CaliTag::setByID: "+e.getMessage()));
00190 }
00191 }
00192
00193
00194 int CaliTag::writeDB()
00195 throw(std::runtime_error)
00196 {
00197
00198 if (this->fetchID()) {
00199 return m_ID;
00200 }
00201
00202
00203 this->checkConnection();
00204
00205
00206 int locID;
00207 this->fetchParentIDs(&locID);
00208
00209
00210 try {
00211 Statement* stmt = m_conn->createStatement();
00212
00213 stmt->setSQL("INSERT INTO cali_tag (tag_id, gen_tag, location_id, method, version, data_type) "
00214 "VALUES (cali_tag_sq.NextVal, :1, :2, :3, :4, :5)");
00215 stmt->setString(1, m_genTag);
00216 stmt->setInt(2, locID);
00217 stmt->setString(3, m_method);
00218 stmt->setString(4, m_version);
00219 stmt->setString(5, m_dataType);
00220
00221 stmt->executeUpdate();
00222
00223 m_conn->terminateStatement(stmt);
00224 } catch (SQLException &e) {
00225 throw(std::runtime_error("CaliTag::writeDB: "+e.getMessage()));
00226 }
00227
00228
00229 if (!this->fetchID()) {
00230 throw(std::runtime_error("CaliTag::writeDB: Failed to write"));
00231 }
00232
00233 return m_ID;
00234 }
00235
00236
00237
00238 void CaliTag::fetchAllTags( std::vector<CaliTag>* fillVec)
00239 throw(std::runtime_error)
00240 {
00241 this->checkConnection();
00242 try {
00243 Statement* stmt = m_conn->createStatement();
00244 stmt->setSQL("SELECT tag_id FROM cali_tag ORDER BY tag_id");
00245 ResultSet* rset = stmt->executeQuery();
00246
00247 CaliTag dcutag;
00248 dcutag.setConnection(m_env, m_conn);
00249 while(rset->next()) {
00250 dcutag.setByID( rset->getInt(1) );
00251 fillVec->push_back( dcutag );
00252 }
00253 m_conn->terminateStatement(stmt);
00254 } catch (SQLException &e) {
00255 throw(std::runtime_error("CaliTag::fetchAllTags: "+e.getMessage()));
00256 }
00257 }
00258
00259
00260
00261 void CaliTag::fetchParentIDs(int* locID)
00262 throw(std::runtime_error)
00263 {
00264
00265 m_locDef.setConnection(m_env, m_conn);
00266 *locID = m_locDef.fetchID();
00267
00268 if (! *locID) {
00269 throw(std::runtime_error("CaliTag::writeDB: Given location does not exist in DB"));
00270 }
00271 }