00001 #include <stdexcept>
00002 #include "OnlineDB/Oracle/interface/Oracle.h"
00003
00004 #include "OnlineDB/EcalCondDB/interface/ODRunConfigInfo.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 ODRunConfigInfo::ODRunConfigInfo()
00013 {
00014 m_conn = NULL;
00015 m_ID = 0;
00016
00017 m_tag ="";
00018 m_version=0;
00019 m_num_seq=0;
00020 m_runTypeDef = RunTypeDef();
00021 m_runModeDef = RunModeDef();
00022 m_defaults=0;
00023 m_trigger_mode="";
00024 m_num_events=0;
00025 }
00026
00027
00028
00029 ODRunConfigInfo::~ODRunConfigInfo(){}
00030
00031
00032
00033
00034 RunTypeDef ODRunConfigInfo::getRunTypeDef() const { return m_runTypeDef;}
00035 void ODRunConfigInfo::setRunTypeDef(const RunTypeDef runTypeDef)
00036 {
00037 if (runTypeDef != m_runTypeDef) {
00038 m_ID = 0;
00039 m_runTypeDef = runTypeDef;
00040 }
00041 }
00042
00043 RunModeDef ODRunConfigInfo::getRunModeDef() const { return m_runModeDef;}
00044 void ODRunConfigInfo::setRunModeDef(const RunModeDef runModeDef)
00045 {
00046 if (runModeDef != m_runModeDef) {
00047 m_ID = 0;
00048 m_runModeDef = runModeDef;
00049 }
00050 }
00051
00052
00053 int ODRunConfigInfo::fetchNextId() throw(std::runtime_error) {
00054
00055 int result=0;
00056 try {
00057 this->checkConnection();
00058
00059 m_readStmt = m_conn->createStatement();
00060 m_readStmt->setSQL("select ecal_run_sq.NextVal from dual");
00061 ResultSet* rset = m_readStmt->executeQuery();
00062 while (rset->next ()){
00063 result= rset->getInt(1);
00064 }
00065 m_conn->terminateStatement(m_readStmt);
00066 return result;
00067
00068 } catch (SQLException &e) {
00069 throw(runtime_error("ODDCCConfig::fetchNextId(): "+e.getMessage()));
00070 }
00071
00072 }
00073
00074 int ODRunConfigInfo::fetchID()
00075 throw(runtime_error)
00076 {
00077
00078 if (m_ID>0) {
00079 return m_ID;
00080 }
00081
00082 this->checkConnection();
00083
00084
00085 DateHandler dh(m_env, m_conn);
00086
00087 try {
00088 Statement* stmt = m_conn->createStatement();
00089 stmt->setSQL("SELECT config_id from ECAL_RUN_CONFIGURATION_DAT "
00090 "WHERE tag = :tag "
00091 " and version = :version " );
00092 stmt->setString(1, m_tag);
00093 stmt->setInt(2, m_version);
00094
00095 ResultSet* rset = stmt->executeQuery();
00096
00097 if (rset->next()) {
00098 m_ID = rset->getInt(1);
00099 } else {
00100 m_ID = 0;
00101 }
00102 m_conn->terminateStatement(stmt);
00103 } catch (SQLException &e) {
00104 throw(runtime_error("ODRunConfigInfo::fetchID: "+e.getMessage()));
00105 }
00106 setByID(m_ID);
00107 return m_ID;
00108 }
00109
00110
00111
00112 int ODRunConfigInfo::fetchIDLast()
00113 throw(runtime_error)
00114 {
00115
00116 this->checkConnection();
00117
00118 DateHandler dh(m_env, m_conn);
00119
00120 try {
00121 Statement* stmt = m_conn->createStatement();
00122 stmt->setSQL("SELECT max(config_id) FROM ecal_run_configuration_dat " );
00123 ResultSet* rset = stmt->executeQuery();
00124
00125 if (rset->next()) {
00126 m_ID = rset->getInt(1);
00127 } else {
00128 m_ID = 0;
00129 }
00130 m_conn->terminateStatement(stmt);
00131 } catch (SQLException &e) {
00132 throw(runtime_error("ODRunConfigInfo::fetchIDLast: "+e.getMessage()));
00133 }
00134
00135 setByID(m_ID);
00136 return m_ID;
00137 }
00138
00139
00140 int ODRunConfigInfo::fetchIDFromTagAndVersion()
00141 throw(runtime_error)
00142 {
00143 fetchID();
00144 return m_ID;
00145 }
00146
00147
00148
00149 void ODRunConfigInfo::setByID(int id)
00150 throw(std::runtime_error)
00151 {
00152 this->checkConnection();
00153
00154 DateHandler dh(m_env, m_conn);
00155
00156 cout<< "ODRunConfigInfo::setByID called for id "<<id<<endl;
00157
00158 try {
00159 Statement* stmt = m_conn->createStatement();
00160
00161 stmt->setSQL("SELECT tag, version, run_type_def_id, run_mode_def_id, num_of_sequences, description, defaults,"
00162 " trg_mode,num_of_events, db_timestamp"
00163 " FROM ECAL_RUN_CONFIGURATION_DAT WHERE config_id = :1");
00164 stmt->setInt(1, id);
00165
00166 ResultSet* rset = stmt->executeQuery();
00167 if (rset->next()) {
00168 m_tag= rset->getString(1);
00169 m_version= rset->getInt(2);
00170 int run_type_id=rset->getInt(3);
00171 int run_mode_id=rset->getInt(4);
00172 m_num_seq=rset->getInt(5);
00173 m_description= rset->getString(6);
00174 m_defaults= rset->getInt(7);
00175 m_trigger_mode= rset->getString(8);
00176 m_num_events= rset->getInt(9);
00177 Date dbdate = rset->getDate(10);
00178 m_db_time = dh.dateToTm( dbdate );
00179 m_ID = id;
00180 m_runModeDef.setConnection(m_env, m_conn);
00181 m_runModeDef.setByID(run_mode_id);
00182 m_runTypeDef.setConnection(m_env, m_conn);
00183 m_runTypeDef.setByID(run_type_id);
00184
00185 } else {
00186 throw(runtime_error("ODRunConfigInfo::setByID: Given config_id is not in the database"));
00187 }
00188 m_conn->terminateStatement(stmt);
00189 } catch (SQLException &e) {
00190 throw(runtime_error("ODRunConfigInfo::setByID: "+e.getMessage()));
00191 }
00192 }
00193
00194 void ODRunConfigInfo::prepareWrite()
00195 throw(runtime_error)
00196 {
00197 this->checkConnection();
00198
00199 int next_id=fetchNextId();
00200
00201
00202
00203
00204 try {
00205 m_writeStmt = m_conn->createStatement();
00206 m_writeStmt->setSQL("INSERT INTO ECAL_RUN_CONFIGURATION_DAT (CONFIG_ID, tag, version, run_type_def_id, "
00207 " run_mode_def_id, num_of_sequences, defaults, trg_mode, num_of_events, description ) "
00208 " VALUES (:1, :2, :3 , :4, :5, :6 ,:7, :8, :9, :10 )");
00209
00210 m_writeStmt->setInt(1, next_id);
00211 m_ID=next_id;
00212
00213 } catch (SQLException &e) {
00214 throw(runtime_error("ODRunConfigInfo::prepareWrite(): "+e.getMessage()));
00215 }
00216 }
00217
00218
00219 void ODRunConfigInfo::writeDB()
00220 throw(runtime_error)
00221 {
00222 this->checkConnection();
00223 this->checkPrepare();
00224
00225
00226 DateHandler dh(m_env, m_conn);
00227
00228 try {
00229
00230
00231 m_runModeDef.setConnection(m_env, m_conn);
00232 int run_mode_id = m_runModeDef.fetchID();
00233
00234
00235 m_runTypeDef.setConnection(m_env, m_conn);
00236 int run_type_id = m_runTypeDef.fetchID();
00237
00238
00239
00240 m_writeStmt->setString(2, this->getTag());
00241 m_writeStmt->setInt(3, this->getVersion());
00242 m_writeStmt->setInt(4, run_type_id);
00243 m_writeStmt->setInt(5, run_mode_id);
00244 m_writeStmt->setInt(6, this->getNumberOfSequences());
00245 m_writeStmt->setInt(7, this->getDefaults());
00246 m_writeStmt->setString(8, this->getTriggerMode());
00247 m_writeStmt->setInt(9, this->getNumberOfEvents());
00248 m_writeStmt->setString(10, this->getDescription());
00249
00250 m_writeStmt->executeUpdate();
00251
00252 } catch (SQLException &e) {
00253 throw(runtime_error("ODRunConfigInfo::writeDB: "+e.getMessage()));
00254 }
00255
00256 if (!this->fetchID()) {
00257 throw(runtime_error("ODRunConfigInfo::writeDB Failed to write"));
00258 }
00259
00260 this->setByID(m_ID);
00261
00262 cout<< "ODRunConfigInfo::writeDB>> done inserting ODRunConfigInfo with id="<<m_ID<<endl;
00263 }
00264
00265
00266 int ODRunConfigInfo::updateDefaultCycle()
00267 throw(runtime_error)
00268 {
00269 this->checkConnection();
00270
00271
00272 if(!this->fetchID()){
00273 this->writeDB();
00274 }
00275
00276
00277 try {
00278 Statement* stmt = m_conn->createStatement();
00279
00280 stmt->setSQL("UPDATE ecal_run_configuration_dat set defaults=:1 where config_id=:2 " );
00281
00282 stmt->setInt(1, m_defaults);
00283 stmt->setInt(2, m_ID);
00284
00285 stmt->executeUpdate();
00286
00287 m_conn->terminateStatement(stmt);
00288 } catch (SQLException &e) {
00289 throw(runtime_error("ODRunConfigInfo::writeDB: "+e.getMessage()));
00290 }
00291
00292 return m_ID;
00293 }
00294