00001 #include <stdexcept>
00002 #include "OnlineDB/Oracle/interface/Oracle.h"
00003
00004 #include "OnlineDB/EcalCondDB/interface/FEConfigMainInfo.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 FEConfigMainInfo::FEConfigMainInfo()
00013 {
00014 m_env = NULL;
00015 m_conn = NULL;
00016 m_writeStmt = NULL;
00017 m_readStmt = NULL;
00018
00019 m_ID=0;
00020 m_version=0;
00021 clear();
00022
00023 }
00024
00025
00026
00027 FEConfigMainInfo::~FEConfigMainInfo(){}
00028
00029
00030 void FEConfigMainInfo::clear() {
00031
00032 m_description="";
00033 m_ped_id=0;
00034 m_lin_id=0;
00035 m_lut_id=0;
00036 m_sli_id=0;
00037 m_fgr_id=0;
00038 m_wei_id=0;
00039 m_bxt_id=0;
00040 m_btt_id=0;
00041 m_tim_id=0;
00042 m_spi_id=0;
00043 m_bst_id=0;
00044
00045 m_db_time=Tm();
00046
00047
00048 }
00049 int FEConfigMainInfo::fetchNextId() throw(std::runtime_error) {
00050
00051 int result=0;
00052 try {
00053 this->checkConnection();
00054
00055 m_readStmt = m_conn->createStatement();
00056 m_readStmt->setSQL("select fe_config_main_sq.NextVal from dual");
00057 ResultSet* rset = m_readStmt->executeQuery();
00058 while (rset->next ()){
00059 result= rset->getInt(1);
00060 }
00061 m_conn->terminateStatement(m_readStmt);
00062 return result;
00063
00064 } catch (SQLException &e) {
00065 throw(std::runtime_error("FEConfigMainInfo::fetchNextId(): "+e.getMessage()));
00066 }
00067
00068 }
00069
00070 int FEConfigMainInfo::fetchID()
00071 throw(std::runtime_error)
00072 {
00073
00074 if (m_ID>0) {
00075 return m_ID;
00076 }
00077
00078 this->checkConnection();
00079
00080
00081 DateHandler dh(m_env, m_conn);
00082
00083 std::cout << " tag/version " << getConfigTag() <<"/"<<getVersion() << std::endl;
00084
00085 try {
00086 Statement* stmt = m_conn->createStatement();
00087 if(m_version !=0){
00088 stmt->setSQL("SELECT conf_id from FE_CONFIG_MAIN "
00089 "WHERE tag = :tag "
00090 " and version = :version " );
00091 stmt->setString(1, m_config_tag);
00092 stmt->setInt(2, m_version);
00093 std::cout<<" using query with version " <<endl;
00094 } else {
00095
00096 stmt->setSQL("SELECT conf_id from FE_CONFIG_MAIN "
00097 "WHERE tag = :1 and version= (select max(version) from FE_CONFIG_MAIN where tag=:2) " );
00098 stmt->setString(1, m_config_tag);
00099 stmt->setString(2, m_config_tag);
00100 std::cout<<" using query WITHOUT version " <<endl;
00101 }
00102
00103 ResultSet* rset = stmt->executeQuery();
00104
00105 if (rset->next()) {
00106 m_ID = rset->getInt(1);
00107 } else {
00108 m_ID = 0;
00109 }
00110 std::cout<<m_ID<<endl;
00111 m_conn->terminateStatement(stmt);
00112 } catch (SQLException &e) {
00113 throw(std::runtime_error("FEConfigMainInfo::fetchID: "+e.getMessage()));
00114 }
00115 setByID(m_ID);
00116 return m_ID;
00117 }
00118
00119
00120
00121
00122 void FEConfigMainInfo::prepareWrite()
00123 throw(std::runtime_error)
00124 {
00125 this->checkConnection();
00126
00127
00128 int next_id=fetchNextId();
00129
00130 try {
00131 m_writeStmt = m_conn->createStatement();
00132 m_writeStmt->setSQL("INSERT INTO fe_config_main (conf_id, ped_conf_id, lin_conf_id, lut_conf_id, fgr_conf_id, sli_conf_id, wei_conf_id, spi_conf_id, tim_conf_id, bxt_conf_id, btt_conf_id, bst_conf_id, tag, version, description) "
00133 " VALUES (:1, :2, :3 , :4, :5, :6 ,:7, :8, :9, :10, :11, :12, :13, :14, :15 )");
00134
00135 m_writeStmt->setInt(1, next_id);
00136 m_ID=next_id;
00137
00138 } catch (SQLException &e) {
00139 throw(std::runtime_error("FEConfigMainInfo::prepareWrite(): "+e.getMessage()));
00140 }
00141
00142 }
00143
00144
00145 void FEConfigMainInfo::writeDB()
00146 throw(std::runtime_error)
00147 {
00148 this->checkConnection();
00149 this->checkPrepare();
00150
00151
00152 DateHandler dh(m_env, m_conn);
00153
00154 try {
00155
00156
00157 m_writeStmt->setInt(2, this->getPedId());
00158 m_writeStmt->setInt(3, this->getLinId());
00159 m_writeStmt->setInt(4, this->getLUTId());
00160 m_writeStmt->setInt(5, this->getFgrId());
00161 m_writeStmt->setInt(6, this->getSliId());
00162 m_writeStmt->setInt(7, this->getWeiId());
00163 m_writeStmt->setInt(8, this->getSpiId());
00164 m_writeStmt->setInt(9, this->getTimId());
00165 m_writeStmt->setInt(10, this->getBxtId());
00166 m_writeStmt->setInt(11, this->getBttId());
00167 m_writeStmt->setInt(12, this->getBstId());
00168 m_writeStmt->setString(13, this->getConfigTag());
00169 m_writeStmt->setInt(14, this->getVersion());
00170 m_writeStmt->setString(15, this->getDescription());
00171 m_writeStmt->executeUpdate();
00172
00173
00174 } catch (SQLException &e) {
00175 throw(std::runtime_error("FEConfigMainInfo::writeDB: "+e.getMessage()));
00176 }
00177
00178 if (!this->fetchID()) {
00179 throw(std::runtime_error("FEConfigMainInfo::writeDB: Failed to write"));
00180 }
00181 setByID(m_ID);
00182
00183 cout<< "FEConfigMainInfo::writeDB>> done inserting FEConfigMainInfo with id="<<m_ID<<endl;
00184
00185 }
00186
00187
00188
00189
00190 int FEConfigMainInfo::fetchIDLast()
00191 throw(std::runtime_error)
00192 {
00193
00194 this->checkConnection();
00195
00196 DateHandler dh(m_env, m_conn);
00197
00198 try {
00199 Statement* stmt = m_conn->createStatement();
00200 stmt->setSQL("SELECT max(conf_id) FROM fe_config_main ");
00201 ResultSet* rset = stmt->executeQuery();
00202
00203 if (rset->next()) {
00204 m_ID = rset->getInt(1);
00205 } else {
00206 m_ID = 0;
00207 }
00208 m_conn->terminateStatement(stmt);
00209 } catch (SQLException &e) {
00210 throw(std::runtime_error("ODRunConfigInfo::fetchIDLast: "+e.getMessage()));
00211 }
00212
00213 setByID(m_ID);
00214 return m_ID;
00215 }
00216
00217
00218 void FEConfigMainInfo::setByID(int id)
00219 throw(std::runtime_error)
00220 {
00221 this->checkConnection();
00222
00223 DateHandler dh(m_env, m_conn);
00224
00225 cout<< "FEConfigMainInfo::setByID called for id "<<id<<endl;
00226
00227 try {
00228 Statement* stmt = m_conn->createStatement();
00229
00230 stmt->setSQL("SELECT * FROM FE_CONFIG_MAIN WHERE conf_id = :1 ");
00231 stmt->setInt(1, id);
00232
00233 ResultSet* rset = stmt->executeQuery();
00234 if (rset->next()) {
00235 setId( rset->getInt(1) );
00236 setPedId( rset->getInt(2) );
00237 setLinId( rset->getInt(3) );
00238 setLUTId( rset->getInt(4) );
00239 setFgrId( rset->getInt(5) );
00240 setSliId( rset->getInt(6) );
00241 setWeiId( rset->getInt(7) );
00242 setSpiId( rset->getInt(8) );
00243 setTimId( rset->getInt(9) );
00244 setBxtId( rset->getInt(10) );
00245 setBttId( rset->getInt(11) );
00246 setBstId( rset->getInt(12) );
00247 setConfigTag( rset->getString(13) );
00248 setVersion( rset->getInt(14) );
00249 setDescription( rset->getString(15) );
00250 Date dbdate = rset->getDate(16);
00251 setDBTime( dh.dateToTm( dbdate ));
00252 m_ID = id;
00253 } else {
00254 throw(std::runtime_error("FEConfigMainInfo::setByID: Given cycle_id is not in the database"));
00255 }
00256 m_conn->terminateStatement(stmt);
00257 } catch (SQLException &e) {
00258 throw(std::runtime_error("FEConfigMainInfo::setByID: "+e.getMessage()));
00259 }
00260 }
00261
00262
00263 void FEConfigMainInfo::fetchData(FEConfigMainInfo * result)
00264 throw(std::runtime_error)
00265 { std::cout << " ### 1 getId from FEConfigMainInfo = " << result->getId() << std::endl;
00266 std::cout << " tag/version " << result->getConfigTag() <<"/"<<result->getVersion() << std::endl;
00267
00268 this->checkConnection();
00269 DateHandler dh(m_env, m_conn);
00270
00271
00272 int idid=0;
00273
00274 if(result->getId()==0){
00275
00276 idid=result->fetchID();
00277 result->setId(idid);
00278 }
00279
00280 try {
00281 m_readStmt->setSQL("SELECT * FROM FE_CONFIG_MAIN WHERE conf_id = :1 ");
00282
00283 std::cout << " ### 2 getId from FEConfigMainInfo = " << result->getId() << std::endl;
00284
00285
00286 m_readStmt->setInt(1, result->getId());
00287 ResultSet* rset = m_readStmt->executeQuery();
00288
00289 rset->next();
00290
00291 result->setId( rset->getInt(1) );
00292
00293 setPedId( rset->getInt(2) );
00294 setLinId( rset->getInt(3) );
00295 setLUTId( rset->getInt(4) );
00296 setFgrId( rset->getInt(5) );
00297 setSliId( rset->getInt(6) );
00298 setWeiId( rset->getInt(7) );
00299 setSpiId( rset->getInt(8) );
00300 setTimId( rset->getInt(9) );
00301 setBxtId( rset->getInt(10) );
00302 setBttId( rset->getInt(11) );
00303 setBstId( rset->getInt(12) );
00304
00305 result->setConfigTag( rset->getString(13) );
00306 result->setVersion( rset->getInt(14) );
00307 result->setDescription( rset->getString(15) );
00308 Date dbdate = rset->getDate(16);
00309 result->setDBTime( dh.dateToTm( dbdate ));
00310
00311 } catch (SQLException &e) {
00312 throw(std::runtime_error("FEConfigMainInfo::fetchData(): "+e.getMessage()));
00313 }
00314 }
00315
00316 void FEConfigMainInfo::insertConfig()
00317 throw(std::runtime_error)
00318 {
00319 try {
00320
00321 prepareWrite();
00322 writeDB();
00323 m_conn->commit();
00324 terminateWriteStatement();
00325 } catch (std::runtime_error &e) {
00326 m_conn->rollback();
00327 throw(e);
00328 } catch (...) {
00329 m_conn->rollback();
00330 throw(std::runtime_error("FEConfigMainInfo::insertConfig: Unknown exception caught"));
00331 }
00332 }
00333