00001 #include <stdexcept>
00002 #include <string>
00003 #include "OnlineDB/Oracle/interface/Oracle.h"
00004
00005 #include "OnlineDB/EcalCondDB/interface/ODDCUConfig.h"
00006
00007 using namespace std;
00008 using namespace oracle::occi;
00009
00010 ODDCUConfig::ODDCUConfig()
00011 {
00012 m_env = NULL;
00013 m_conn = NULL;
00014 m_writeStmt = NULL;
00015 m_readStmt = NULL;
00016 m_config_tag="";
00017 m_ID=0;
00018 clear();
00019
00020 }
00021
00022 void ODDCUConfig::clear(){
00023 }
00024
00025
00026
00027 ODDCUConfig::~ODDCUConfig()
00028 {
00029 }
00030
00031 void ODDCUConfig::setParameters(std::map<string,string> my_keys_map){
00032
00033
00034
00035
00036 for( std::map<std::string, std::string >::iterator ci=
00037 my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
00038
00039 if(ci->first== "DCU_CONFIGURATION_ID") setConfigTag(ci->second);
00040 }
00041
00042 }
00043
00044 int ODDCUConfig::fetchNextId() throw(std::runtime_error) {
00045
00046 int result=0;
00047 try {
00048 this->checkConnection();
00049
00050 m_readStmt = m_conn->createStatement();
00051 m_readStmt->setSQL("select ecal_dcu_config_sq.NextVal from dual");
00052 ResultSet* rset = m_readStmt->executeQuery();
00053 while (rset->next ()){
00054 result= rset->getInt(1);
00055 }
00056 m_conn->terminateStatement(m_readStmt);
00057 return result;
00058
00059 } catch (SQLException &e) {
00060 throw(runtime_error("ODDCUConfig::fetchNextId(): "+e.getMessage()));
00061 }
00062
00063 }
00064
00065
00066 void ODDCUConfig::prepareWrite()
00067 throw(runtime_error)
00068 {
00069 this->checkConnection();
00070 int next_id=fetchNextId();
00071
00072 try {
00073 m_writeStmt = m_conn->createStatement();
00074 m_writeStmt->setSQL("INSERT INTO ECAL_DCU_CONFIGURATION ( dcu_configuration_id, dcu_tag ) "
00075 "VALUES ( "
00076 ":1, :2 )");
00077 m_writeStmt->setInt(1, next_id);
00078 m_ID=next_id;
00079
00080 } catch (SQLException &e) {
00081 throw(runtime_error("ODDCUConfig::prepareWrite(): "+e.getMessage()));
00082 }
00083 }
00084
00085
00086
00087 void ODDCUConfig::writeDB()
00088 throw(runtime_error)
00089 {
00090 this->checkConnection();
00091 this->checkPrepare();
00092
00093 try {
00094
00095 m_writeStmt->setString(2, this->getConfigTag());
00096
00097 m_writeStmt->executeUpdate();
00098
00099
00100 } catch (SQLException &e) {
00101 throw(runtime_error("ODDCUConfig::writeDB(): "+e.getMessage()));
00102 }
00103
00104 if (!this->fetchID()) {
00105 throw(runtime_error("ODDCUConfig::writeDB: Failed to write"));
00106 }
00107
00108
00109 }
00110
00111
00112
00113 void ODDCUConfig::fetchData(ODDCUConfig * result)
00114 throw(runtime_error)
00115 {
00116 this->checkConnection();
00117 result->clear();
00118 if(result->getId()==0 && (result->getConfigTag()=="") ){
00119 throw(runtime_error("ODDCUConfig::fetchData(): no Id defined for this ODDCUConfig "));
00120 }
00121
00122 try {
00123
00124 m_readStmt->setSQL("SELECT * "
00125 "FROM ECAL_DCU_CONFIGURATION "
00126 " where ( dcu_configuration_id = :1 or dcu_tag=:2 ) " );
00127 m_readStmt->setInt(1, result->getId());
00128 m_readStmt->setString(2, result->getConfigTag());
00129 ResultSet* rset = m_readStmt->executeQuery();
00130
00131 rset->next();
00132
00133 result->setId(rset->getInt(1));
00134 result->setConfigTag(rset->getString(2));
00135
00136
00137 } catch (SQLException &e) {
00138 throw(runtime_error("ODDCUConfig::fetchData(): "+e.getMessage()));
00139 }
00140 }
00141
00142 int ODDCUConfig::fetchID() throw(std::runtime_error)
00143 {
00144
00145 if (m_ID!=0) {
00146 return m_ID;
00147 }
00148
00149 this->checkConnection();
00150
00151 try {
00152 Statement* stmt = m_conn->createStatement();
00153 stmt->setSQL("SELECT dcu_configuration_id FROM ecal_dcu_configuration "
00154 "WHERE dcu_tag=:dcu_tag " );
00155
00156 stmt->setString(1, getConfigTag());
00157
00158 ResultSet* rset = stmt->executeQuery();
00159
00160 if (rset->next()) {
00161 m_ID = rset->getInt(1);
00162 } else {
00163 m_ID = 0;
00164 }
00165 m_conn->terminateStatement(stmt);
00166 } catch (SQLException &e) {
00167 throw(runtime_error("ODDCUConfig::fetchID: "+e.getMessage()));
00168 }
00169
00170 return m_ID;
00171 }