00001 #include <string>
00002 #include "OnlineDB/Oracle/interface/Oracle.h"
00003
00004
00005 #include "OnlineDB/EcalCondDB/interface/LocationDef.h"
00006
00007 using namespace std;
00008 using namespace oracle::occi;
00009
00010 LocationDef::LocationDef()
00011 {
00012 m_env = NULL;
00013 m_conn = NULL;
00014 m_ID = 0;
00015 m_loc = "";
00016 }
00017
00018
00019
00020 LocationDef::~LocationDef()
00021 {
00022 }
00023
00024
00025
00026 string LocationDef::getLocation() const
00027 {
00028 return m_loc;
00029 }
00030
00031
00032
00033 void LocationDef::setLocation(string loc)
00034 {
00035 if (loc != m_loc) {
00036 m_ID = 0;
00037 m_loc = loc;
00038 }
00039 }
00040
00041
00042
00043 int LocationDef::fetchID()
00044 throw(std::runtime_error)
00045 {
00046
00047 if (m_ID) {
00048 return m_ID;
00049 }
00050
00051 this->checkConnection();
00052
00053 try {
00054 Statement* stmt = m_conn->createStatement();
00055 stmt->setSQL("SELECT def_id FROM location_def WHERE "
00056 "location = :location");
00057 stmt->setString(1, m_loc);
00058
00059 ResultSet* rset = stmt->executeQuery();
00060
00061 if (rset->next()) {
00062 m_ID = rset->getInt(1);
00063 } else {
00064 m_ID = 0;
00065 }
00066 m_conn->terminateStatement(stmt);
00067 } catch (SQLException &e) {
00068 throw(runtime_error("LocationDef::fetchID: "+e.getMessage()));
00069 }
00070
00071 return m_ID;
00072 }
00073
00074
00075
00076 void LocationDef::setByID(int id)
00077 throw(runtime_error)
00078 {
00079 this->checkConnection();
00080
00081 try {
00082 Statement* stmt = m_conn->createStatement();
00083
00084 stmt->setSQL("SELECT location FROM location_def WHERE def_id = :1");
00085 stmt->setInt(1, id);
00086
00087 ResultSet* rset = stmt->executeQuery();
00088 if (rset->next()) {
00089 m_loc = rset->getString(1);
00090 m_ID = id;
00091 } else {
00092 throw(runtime_error("LocationDef::setByID: Given def_id is not in the database"));
00093 }
00094
00095 m_conn->terminateStatement(stmt);
00096 } catch (SQLException &e) {
00097 throw(runtime_error("LocationDef::setByID: "+e.getMessage()));
00098 }
00099 }
00100
00101
00102
00103 void LocationDef::fetchAllDefs( std::vector<LocationDef>* fillVec)
00104 throw(std::runtime_error)
00105 {
00106 this->checkConnection();
00107 try {
00108 Statement* stmt = m_conn->createStatement();
00109 stmt->setSQL("SELECT def_id FROM location_def ORDER BY def_id");
00110 ResultSet* rset = stmt->executeQuery();
00111
00112 LocationDef locationDef;
00113 locationDef.setConnection(m_env, m_conn);
00114
00115 while(rset->next()) {
00116 locationDef.setByID( rset->getInt(1) );
00117 fillVec->push_back( locationDef );
00118 }
00119 } catch (SQLException &e) {
00120 throw(runtime_error("LocationDef::fetchAllDefs: "+e.getMessage()));
00121 }
00122 }