File indexing completed on 2024-04-06 12:23:06
0001 #include <string>
0002 #include "OnlineDB/Oracle/interface/Oracle.h"
0003
0004 #include "OnlineDB/EcalCondDB/interface/LocationDef.h"
0005
0006 using namespace std;
0007 using namespace oracle::occi;
0008
0009 LocationDef::LocationDef() {
0010 m_env = nullptr;
0011 m_conn = nullptr;
0012 m_ID = 0;
0013 m_loc = "";
0014 }
0015
0016 LocationDef::~LocationDef() {}
0017
0018 string LocationDef::getLocation() const { return m_loc; }
0019
0020 void LocationDef::setLocation(string loc) {
0021 if (loc != m_loc) {
0022 m_ID = 0;
0023 m_loc = loc;
0024 }
0025 }
0026
0027 int LocationDef::fetchID() noexcept(false) {
0028
0029 if (m_ID) {
0030 return m_ID;
0031 }
0032
0033 this->checkConnection();
0034
0035 try {
0036 Statement* stmt = m_conn->createStatement();
0037 stmt->setSQL(
0038 "SELECT def_id FROM location_def WHERE "
0039 "location = :location");
0040 stmt->setString(1, m_loc);
0041
0042 ResultSet* rset = stmt->executeQuery();
0043
0044 if (rset->next()) {
0045 m_ID = rset->getInt(1);
0046 } else {
0047 m_ID = 0;
0048 }
0049 m_conn->terminateStatement(stmt);
0050 } catch (SQLException& e) {
0051 throw(std::runtime_error("LocationDef::fetchID: " + e.getMessage()));
0052 }
0053
0054 return m_ID;
0055 }
0056
0057 void LocationDef::setByID(int id) noexcept(false) {
0058 this->checkConnection();
0059
0060 try {
0061 Statement* stmt = m_conn->createStatement();
0062
0063 stmt->setSQL("SELECT location FROM location_def WHERE def_id = :1");
0064 stmt->setInt(1, id);
0065
0066 ResultSet* rset = stmt->executeQuery();
0067 if (rset->next()) {
0068 m_loc = rset->getString(1);
0069 m_ID = id;
0070 } else {
0071 throw(std::runtime_error("LocationDef::setByID: Given def_id is not in the database"));
0072 }
0073
0074 m_conn->terminateStatement(stmt);
0075 } catch (SQLException& e) {
0076 throw(std::runtime_error("LocationDef::setByID: " + e.getMessage()));
0077 }
0078 }
0079
0080 void LocationDef::fetchAllDefs(std::vector<LocationDef>* fillVec) noexcept(false) {
0081 this->checkConnection();
0082 try {
0083 Statement* stmt = m_conn->createStatement();
0084 stmt->setSQL("SELECT def_id FROM location_def ORDER BY def_id");
0085 ResultSet* rset = stmt->executeQuery();
0086
0087 LocationDef locationDef;
0088 locationDef.setConnection(m_env, m_conn);
0089
0090 while (rset->next()) {
0091 locationDef.setByID(rset->getInt(1));
0092 fillVec->push_back(locationDef);
0093 }
0094 } catch (SQLException& e) {
0095 throw(std::runtime_error("LocationDef::fetchAllDefs: " + e.getMessage()));
0096 }
0097 }