00001 #include <stdexcept>
00002 #include <string>
00003 #include <string.h>
00004 #include "OnlineDB/Oracle/interface/Oracle.h"
00005
00006 #include "OnlineDB/EcalCondDB/interface/FEConfigLinInfo.h"
00007 #include "OnlineDB/EcalCondDB/interface/Tm.h"
00008 #include "OnlineDB/EcalCondDB/interface/DateHandler.h"
00009
00010 using namespace std;
00011 using namespace oracle::occi;
00012
00013 FEConfigLinInfo::FEConfigLinInfo()
00014 {
00015 m_env = NULL;
00016 m_conn = NULL;
00017 m_writeStmt = NULL;
00018 m_readStmt = NULL;
00019 m_config_tag="";
00020 m_version=0;
00021 m_ID=0;
00022 clear();
00023 }
00024
00025
00026 void FEConfigLinInfo::clear(){
00027 m_iov_id=0;
00028 }
00029
00030
00031
00032 FEConfigLinInfo::~FEConfigLinInfo()
00033 {
00034 }
00035
00036
00037
00038 int FEConfigLinInfo::fetchNextId() throw(std::runtime_error) {
00039
00040 int result=0;
00041 try {
00042 this->checkConnection();
00043
00044 m_readStmt = m_conn->createStatement();
00045 m_readStmt->setSQL("select FE_CONFIG_LIN_SQ.NextVal from DUAL ");
00046 ResultSet* rset = m_readStmt->executeQuery();
00047 while (rset->next ()){
00048 result= rset->getInt(1);
00049 }
00050 result++;
00051 m_conn->terminateStatement(m_readStmt);
00052 return result;
00053
00054 } catch (SQLException &e) {
00055 throw(runtime_error("FEConfigLinInfo::fetchNextId(): "+e.getMessage()));
00056 }
00057
00058 }
00059
00060 void FEConfigLinInfo::prepareWrite()
00061 throw(runtime_error)
00062 {
00063 this->checkConnection();
00064
00065 int next_id=0;
00066 if(getId()==0){
00067 next_id=fetchNextId();
00068 }
00069
00070 try {
00071 m_writeStmt = m_conn->createStatement();
00072 m_writeStmt->setSQL("INSERT INTO "+getTable()+" ( lin_conf_id, tag, version, iov_id) "
00073 " VALUES ( :1, :2, :3 , :4) " );
00074
00075 m_writeStmt->setInt(1, next_id);
00076 m_ID=next_id;
00077
00078 } catch (SQLException &e) {
00079 throw(runtime_error("FEConfigLinInfo::prepareWrite(): "+e.getMessage()));
00080 }
00081
00082 }
00083
00084 void FEConfigLinInfo::setParameters(std::map<string,string> my_keys_map){
00085
00086
00087
00088
00089 for( std::map<std::string, std::string >::iterator ci=
00090 my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
00091
00092 if(ci->first== "VERSION") setVersion(atoi(ci->second.c_str()) );
00093 if(ci->first== "TAG") setConfigTag(ci->second);
00094 if(ci->first== "IOV_ID") setIOVId(atoi(ci->second.c_str()) );
00095
00096 }
00097
00098 }
00099
00100 void FEConfigLinInfo::writeDB()
00101 throw(runtime_error)
00102 {
00103 this->checkConnection();
00104 this->checkPrepare();
00105
00106 try {
00107
00108
00109 m_writeStmt->setString(2, this->getConfigTag());
00110 m_writeStmt->setInt(3, this->getVersion());
00111 m_writeStmt->setInt(4, this->getIOVId());
00112
00113 m_writeStmt->executeUpdate();
00114
00115
00116 } catch (SQLException &e) {
00117 throw(runtime_error("FEConfigLinInfo::writeDB(): "+e.getMessage()));
00118 }
00119
00120 if (!this->fetchID()) {
00121 throw(runtime_error("FEConfigLinInfo::writeDB: Failed to write"));
00122 }
00123
00124
00125 }
00126
00127
00128 void FEConfigLinInfo::fetchData(FEConfigLinInfo * result)
00129 throw(runtime_error)
00130 {
00131 this->checkConnection();
00132 result->clear();
00133 if(result->getId()==0 && (result->getConfigTag()=="") ){
00134 throw(runtime_error("FEConfigLinInfo::fetchData(): no Id defined for this FEConfigLinInfo "));
00135 }
00136
00137 try {
00138 DateHandler dh(m_env, m_conn);
00139
00140 m_readStmt->setSQL("SELECT * FROM " + getTable() +
00141 " where ( lin_conf_id= :1 or (tag=:2 AND version=:3 ) )" );
00142 m_readStmt->setInt(1, result->getId());
00143 m_readStmt->setString(2, result->getConfigTag());
00144 m_readStmt->setInt(3, result->getVersion());
00145 ResultSet* rset = m_readStmt->executeQuery();
00146
00147 rset->next();
00148
00149
00150
00151 result->setId(rset->getInt(1));
00152 result->setConfigTag(rset->getString(2));
00153 result->setVersion(rset->getInt(3));
00154 result->setIOVId(rset->getInt(4));
00155 Date dbdate = rset->getDate(5);
00156 result->setDBTime( dh.dateToTm( dbdate ));
00157
00158 } catch (SQLException &e) {
00159 throw(runtime_error("FEConfigLinInfo::fetchData(): "+e.getMessage()));
00160 }
00161 }
00162
00163 void FEConfigLinInfo::fetchLastData(FEConfigLinInfo * result)
00164 throw(runtime_error)
00165 {
00166 this->checkConnection();
00167 result->clear();
00168 try {
00169 DateHandler dh(m_env, m_conn);
00170
00171 m_readStmt->setSQL("SELECT * FROM " + getTable() +
00172 " where lin_conf_id = ( select max( lin_conf_id) from "+ getTable() +" ) " );
00173 ResultSet* rset = m_readStmt->executeQuery();
00174
00175 rset->next();
00176
00177 result->setId(rset->getInt(1));
00178 result->setConfigTag(rset->getString(2));
00179 result->setVersion(rset->getInt(3));
00180 result->setIOVId(rset->getInt(4));
00181 Date dbdate = rset->getDate(5);
00182 result->setDBTime( dh.dateToTm( dbdate ));
00183
00184 } catch (SQLException &e) {
00185 throw(runtime_error("FEConfigLinInfo::fetchData(): "+e.getMessage()));
00186 }
00187 }
00188
00189 int FEConfigLinInfo::fetchID() throw(std::runtime_error)
00190 {
00191
00192 if (m_ID!=0) {
00193 return m_ID;
00194 }
00195
00196 this->checkConnection();
00197
00198 try {
00199 Statement* stmt = m_conn->createStatement();
00200 stmt->setSQL("SELECT lin_conf_id FROM "+ getTable()+
00201 " WHERE tag=:1 and version=:2 " );
00202
00203 stmt->setString(1, getConfigTag() );
00204 stmt->setInt(2, getVersion() );
00205
00206 ResultSet* rset = stmt->executeQuery();
00207
00208 if (rset->next()) {
00209 m_ID = rset->getInt(1);
00210 } else {
00211 m_ID = 0;
00212 }
00213 m_conn->terminateStatement(stmt);
00214 } catch (SQLException &e) {
00215 throw(runtime_error("FEConfigLinInfo::fetchID: "+e.getMessage()));
00216 }
00217
00218 return m_ID;
00219 }
00220
00221
00222
00223 void FEConfigLinInfo::setByID(int id)
00224 throw(std::runtime_error)
00225 {
00226 this->checkConnection();
00227
00228 DateHandler dh(m_env, m_conn);
00229
00230 try {
00231 Statement* stmt = m_conn->createStatement();
00232
00233 stmt->setSQL("SELECT * FROM fe_config_lin_info WHERE lin_conf_id = :1");
00234 stmt->setInt(1, id);
00235
00236 ResultSet* rset = stmt->executeQuery();
00237 if (rset->next()) {
00238 this->setId(rset->getInt(1));
00239 this->setConfigTag(rset->getString(2));
00240 this->setVersion(rset->getInt(3));
00241 this->setIOVId(rset->getInt(4));
00242 Date dbdate = rset->getDate(5);
00243 this->setDBTime( dh.dateToTm( dbdate ));
00244 } else {
00245 throw(runtime_error("FEConfigLinInfo::setByID: Given config_id is not in the database"));
00246 }
00247
00248 m_conn->terminateStatement(stmt);
00249 } catch (SQLException &e) {
00250 throw(runtime_error("FEConfigLinInfo::setByID: "+e.getMessage()));
00251 }
00252 }
00253
00254
00255