Go to the documentation of this file.00001 #include <stdexcept>
00002 #include "OnlineDB/Oracle/interface/Oracle.h"
00003
00004 #include "OnlineDB/EcalCondDB/interface/ODRunConfigSeqInfo.h"
00005 #include "OnlineDB/EcalCondDB/interface/Tm.h"
00006 #include "OnlineDB/EcalCondDB/interface/DateHandler.h"
00007
00008 using namespace std;
00009 using namespace oracle::occi;
00010
00011
00012 ODRunConfigSeqInfo::ODRunConfigSeqInfo()
00013 {
00014 m_env = NULL;
00015 m_conn = NULL;
00016 m_writeStmt = NULL;
00017 m_readStmt = NULL;
00018
00019 m_ID = 0;
00020
00021 m_ecal_config_id =0;
00022 m_seq_num =0;
00023 m_cycles =0;
00024 m_run_seq = RunSeqDef();
00025 m_description="";
00026 }
00027
00028 ODRunConfigSeqInfo::~ODRunConfigSeqInfo(){}
00029
00030
00031 RunSeqDef ODRunConfigSeqInfo::getRunSeqDef() const { return m_run_seq;}
00032 void ODRunConfigSeqInfo::setRunSeqDef(const RunSeqDef run_seq)
00033 {
00034 if (run_seq != m_run_seq) {
00035 m_run_seq = run_seq;
00036 }
00037 }
00038
00039
00040
00041
00042
00043 int ODRunConfigSeqInfo::fetchID()
00044 throw(std::runtime_error)
00045 {
00046
00047 if (m_ID>0) {
00048 return m_ID;
00049 }
00050
00051 this->checkConnection();
00052
00053
00054 DateHandler dh(m_env, m_conn);
00055
00056 try {
00057 Statement* stmt = m_conn->createStatement();
00058 stmt->setSQL("SELECT sequence_id from ECAL_sequence_DAT "
00059 "WHERE ecal_config_id = :id1 "
00060 " and sequence_num = :id2 " );
00061 stmt->setInt(1, m_ecal_config_id);
00062 stmt->setInt(2, m_seq_num);
00063
00064 ResultSet* rset = stmt->executeQuery();
00065
00066 if (rset->next()) {
00067 m_ID = rset->getInt(1);
00068 } else {
00069 m_ID = 0;
00070 }
00071 m_conn->terminateStatement(stmt);
00072 } catch (SQLException &e) {
00073 throw(std::runtime_error("ODRunConfigSeqInfo::fetchID: "+e.getMessage()));
00074 }
00075 setByID(m_ID);
00076 return m_ID;
00077 }
00078
00079
00080
00081 int ODRunConfigSeqInfo::fetchIDLast()
00082 throw(std::runtime_error)
00083 {
00084
00085 this->checkConnection();
00086
00087 DateHandler dh(m_env, m_conn);
00088
00089 try {
00090 Statement* stmt = m_conn->createStatement();
00091 stmt->setSQL("SELECT max(sequence_id) FROM ecal_sequence_dat " );
00092 ResultSet* rset = stmt->executeQuery();
00093
00094 if (rset->next()) {
00095 m_ID = rset->getInt(1);
00096 } else {
00097 m_ID = 0;
00098 }
00099 m_conn->terminateStatement(stmt);
00100 } catch (SQLException &e) {
00101 throw(std::runtime_error("ODRunConfigSeqInfo::fetchIDLast: "+e.getMessage()));
00102 }
00103
00104 setByID(m_ID);
00105 return m_ID;
00106 }
00107
00108
00109 void ODRunConfigSeqInfo::setByID(int id)
00110 throw(std::runtime_error)
00111 {
00112 this->checkConnection();
00113
00114 DateHandler dh(m_env, m_conn);
00115
00116 cout<< "ODRunConfigSeqInfo::setByID called for id "<<id<<endl;
00117
00118 try {
00119 Statement* stmt = m_conn->createStatement();
00120
00121 stmt->setSQL("SELECT ecal_config_id, sequence_num, num_of_cycles, sequence_type_def_id, description FROM ECAL_sequence_DAT WHERE sequence_id = :1 ");
00122 stmt->setInt(1, id);
00123
00124 ResultSet* rset = stmt->executeQuery();
00125 if (rset->next()) {
00126 m_ecal_config_id= rset->getInt(1);
00127 m_seq_num=rset->getInt(2);
00128 m_cycles=rset->getInt(3);
00129 int seq_def_id=rset->getInt(4);
00130 m_description= rset->getString(5);
00131 m_ID = id;
00132 m_run_seq.setConnection(m_env, m_conn);
00133 m_run_seq.setByID(seq_def_id);
00134 } else {
00135 throw(std::runtime_error("ODRunConfigSeqInfo::setByID: Given config_id is not in the database"));
00136 }
00137 m_conn->terminateStatement(stmt);
00138 } catch (SQLException &e) {
00139 throw(std::runtime_error("ODRunConfigSeqInfo::setByID: "+e.getMessage()));
00140 }
00141 }
00142
00143 void ODRunConfigSeqInfo::prepareWrite()
00144 throw(std::runtime_error)
00145 {
00146 this->checkConnection();
00147
00148 try {
00149 m_writeStmt = m_conn->createStatement();
00150 m_writeStmt->setSQL("INSERT INTO ECAL_SEQUENCE_DAT ( ecal_config_id, "
00151 "sequence_num, num_of_cycles, sequence_type_def_id, description ) "
00152 "VALUES (:1, :2, :3 , :4, :5 )");
00153 } catch (SQLException &e) {
00154 throw(std::runtime_error("ODRunConfigSeqInfo::prepareWrite(): "+e.getMessage()));
00155 }
00156 }
00157 void ODRunConfigSeqInfo::writeDB()
00158 throw(std::runtime_error)
00159 {
00160 this->checkConnection();
00161 this->checkPrepare();
00162
00163
00164
00165
00166 DateHandler dh(m_env, m_conn);
00167
00168 try {
00169
00170
00171
00172
00173
00174 m_run_seq.setConnection(m_env, m_conn);
00175 int seq_def_id = m_run_seq.writeDB();
00176
00177
00178 m_writeStmt->setInt(1, this->getEcalConfigId());
00179 m_writeStmt->setInt(2, this->getSequenceNumber());
00180 m_writeStmt->setInt(3, this->getNumberOfCycles());
00181 m_writeStmt->setInt(4,seq_def_id );
00182 m_writeStmt->setString(5,this->getDescription() );
00183
00184 m_writeStmt->executeUpdate();
00185
00186 } catch (SQLException &e) {
00187 throw(std::runtime_error("ODRunConfigSeqInfo::writeDB(): "+e.getMessage()));
00188 }
00189 if (!this->fetchID()) {
00190 throw(std::runtime_error("ODRunConfigSeqInfo::writeDB: Failed to write"));
00191 }
00192 cout<< "ODRunConfigSeqInfo::writeDB>> done inserting ODRunConfigSeqInfo with id="<<m_ID<<endl;
00193
00194 }
00195
00196
00197 void ODRunConfigSeqInfo::clear(){
00198
00199
00200 m_ID =0;
00201 m_cycles =0;
00202 m_run_seq = RunSeqDef();
00203 m_description="";
00204 }
00205
00206
00207 void ODRunConfigSeqInfo::fetchData(ODRunConfigSeqInfo * result)
00208 throw(std::runtime_error)
00209 {
00210 this->checkConnection();
00211
00212 if(result->getId()==0){
00213
00214 result->fetchID();
00215 }
00216
00217 try {
00218
00219 m_readStmt->setSQL("SELECT ecal_config_id, sequence_num, num_of_cycles, "
00220 "sequence_type_def_id, description FROM ECAL_sequence_DAT WHERE sequence_id = :1 ");
00221
00222 m_readStmt->setInt(1, result->getId());
00223 ResultSet* rset = m_readStmt->executeQuery();
00224
00225 rset->next();
00226
00227 result->setEcalConfigId( rset->getInt(1) );
00228 result->setSequenceNumber( rset->getInt(2) );
00229 result->setNumberOfCycles( rset->getInt(3) );
00230 int seq_def_id=rset->getInt(4);
00231
00232 m_run_seq.setConnection(m_env, m_conn);
00233 m_run_seq.setByID(seq_def_id);
00234 result->setDescription( rset->getString(5) );
00235
00236
00237 } catch (SQLException &e) {
00238 throw(std::runtime_error("ODRunConfigSeqInfo::fetchData(): "+e.getMessage()));
00239 }
00240 }