00001 #include <stdexcept>
00002 #include <string>
00003 #include <fstream>
00004 #include <iostream>
00005 #include <stdio.h>
00006
00007
00008 #include "OnlineDB/Oracle/interface/Oracle.h"
00009
00010 #include "OnlineDB/EcalCondDB/interface/ODDCCConfig.h"
00011
00012 using namespace std;
00013 using namespace oracle::occi;
00014
00015 ODDCCConfig::ODDCCConfig()
00016 {
00017 m_env = NULL;
00018 m_conn = NULL;
00019 m_writeStmt = NULL;
00020 m_readStmt = NULL;
00021 m_size=0;
00022 m_config_tag="";
00023 m_ID=0;
00024 clear();
00025
00026 }
00027
00028
00029
00030 ODDCCConfig::~ODDCCConfig()
00031 {
00032 }
00033
00034 int ODDCCConfig::fetchNextId() throw(std::runtime_error) {
00035
00036 int result=0;
00037 try {
00038 this->checkConnection();
00039
00040 m_readStmt = m_conn->createStatement();
00041 m_readStmt->setSQL("select ecal_dcc_config_sq.NextVal from dual");
00042 ResultSet* rset = m_readStmt->executeQuery();
00043 while (rset->next ()){
00044 result= rset->getInt(1);
00045 }
00046 m_conn->terminateStatement(m_readStmt);
00047 return result;
00048
00049 } catch (SQLException &e) {
00050 throw(runtime_error("ODDCCConfig::fetchNextId(): "+e.getMessage()));
00051 }
00052
00053 }
00054
00055
00056
00057
00058 void ODDCCConfig::prepareWrite()
00059 throw(runtime_error)
00060 {
00061 this->checkConnection();
00062
00063 int next_id=fetchNextId();
00064
00065 try {
00066 m_writeStmt = m_conn->createStatement();
00067 m_writeStmt->setSQL("INSERT INTO ECAL_DCC_CONFIGURATION (dcc_configuration_id, dcc_tag, "
00068 " DCC_CONFIGURATION_URL, TESTPATTERN_FILE_URL, "
00069 " N_TESTPATTERNS_TO_LOAD , SM_HALF, "
00070 " dcc_configuration) "
00071 "VALUES (:1, :2, :3, :4, :5, :6 , :7 )");
00072 m_writeStmt->setInt(1, next_id);
00073 m_writeStmt->setString(2, getConfigTag());
00074 m_writeStmt->setString(3, getDCCConfigurationUrl());
00075 m_writeStmt->setString(4, getTestPatternFileUrl());
00076 m_writeStmt->setInt(5, getNTestPatternsToLoad());
00077 m_writeStmt->setInt(6, getSMHalf());
00078
00079
00080 oracle::occi::Clob clob(m_conn);
00081 clob.setEmpty();
00082 m_writeStmt->setClob(7,clob);
00083 m_writeStmt->executeUpdate ();
00084 m_ID=next_id;
00085
00086 m_conn->terminateStatement(m_writeStmt);
00087 std::cout<<"DCC Clob inserted into CONFIGURATION with id="<<next_id<<std::endl;
00088
00089
00090 m_writeStmt = m_conn->createStatement();
00091 m_writeStmt->setSQL ("SELECT dcc_configuration FROM ECAL_DCC_CONFIGURATION WHERE"
00092 " dcc_configuration_id=:1 FOR UPDATE");
00093
00094 std::cout<<"updating the clob 0"<<std::endl;
00095
00096
00097
00098 } catch (SQLException &e) {
00099 throw(runtime_error("ODDCCConfig::prepareWrite(): "+e.getMessage()));
00100 }
00101
00102 std::cout<<"updating the clob 1 "<<std::endl;
00103
00104
00105 }
00106
00107
00108 void ODDCCConfig::setParameters(std::map<string,string> my_keys_map){
00109
00110
00111
00112
00113 for( std::map<std::string, std::string >::iterator ci=
00114 my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
00115
00116 if(ci->first== "DCC_CONFIGURATION_ID") setConfigTag(ci->second);
00117 if(ci->first== "TESTPATTERN_FILE_URL") setTestPatternFileUrl(ci->second );
00118 if(ci->first== "N_TESTPATTERNS_TO_LOAD") setNTestPatternsToLoad(atoi(ci->second.c_str() ));
00119 if(ci->first== "SM_HALF") setSMHalf(atoi(ci->second.c_str() ));
00120 if(ci->first== "DCC_CONFIGURATION_URL") {
00121 std::string fname=ci->second ;
00122 setDCCConfigurationUrl(fname );
00123
00124
00125 std::cout << "Going to read DCC file: " << fname << endl;
00126
00127 ifstream inpFile;
00128 inpFile.open(fname.c_str());
00129
00130
00131 int bufsize = 0;
00132 inpFile.seekg( 0,ios::end );
00133 bufsize = inpFile.tellg();
00134 std::cout <<" bufsize ="<<bufsize<< std::endl;
00135
00136 inpFile.seekg( 0,ios::beg );
00137
00138 m_size=bufsize;
00139
00140 inpFile.close();
00141
00142 }
00143 }
00144
00145 }
00146
00147
00148
00149
00150 void ODDCCConfig::writeDB()
00151 throw(runtime_error)
00152 {
00153
00154
00155 std::cout<<"updating the clob "<<std::endl;
00156
00157
00158 try {
00159
00160
00161 m_writeStmt->setInt(1, m_ID);
00162 ResultSet* rset = m_writeStmt->executeQuery();
00163
00164 rset->next ();
00165 oracle::occi::Clob clob = rset->getClob (1);
00166
00167 cout << "Opening the clob in read write mode" << endl;
00168
00169 std::cout << "Populating the clob" << endl;
00170
00171 populateClob (clob, getDCCConfigurationUrl(), m_size);
00172 int clobLength=clob.length ();
00173 cout << "Length of the clob is: " << clobLength << endl;
00174
00175 m_writeStmt->executeUpdate();
00176
00177 m_writeStmt->closeResultSet (rset);
00178
00179 } catch (SQLException &e) {
00180 throw(runtime_error("ODDCCConfig::writeDB(): "+e.getMessage()));
00181 }
00182
00183 if (!this->fetchID()) {
00184 throw(runtime_error("ODDCCConfig::writeDB: Failed to write"));
00185 }
00186
00187
00188 }
00189
00190
00191 void ODDCCConfig::clear(){
00192
00193 m_dcc_url="";
00194 m_test_url="";
00195 m_ntest=0;
00196 m_sm_half=0;
00197
00198 }
00199
00200
00201
00202
00203 void ODDCCConfig::fetchData(ODDCCConfig * result)
00204 throw(runtime_error)
00205 {
00206 this->checkConnection();
00207 result->clear();
00208 if(result->getId()==0 && (result->getConfigTag()=="") ){
00209 throw(runtime_error("ODDCCConfig::fetchData(): no Id defined for this ODDCCConfig "));
00210 }
00211
00212 try {
00213
00214 m_readStmt->setSQL("SELECT * "
00215 "FROM ECAL_DCC_CONFIGURATION "
00216 " where dcc_configuration_id = :1 or dcc_tag=:2 " );
00217 m_readStmt->setInt(1, result->getId());
00218 m_readStmt->setString(2, result->getConfigTag());
00219 ResultSet* rset = m_readStmt->executeQuery();
00220
00221 rset->next();
00222
00223
00224
00225 result->setId(rset->getInt(1));
00226 result->setConfigTag(rset->getString(2));
00227 result->setDCCConfigurationUrl(rset->getString(3));
00228 result->setTestPatternFileUrl(rset->getString(4));
00229 result->setNTestPatternsToLoad(rset->getInt(5));
00230 result->setSMHalf(rset->getInt(6));
00231
00232
00233 Clob clob = rset->getClob (7);
00234 cout << "Opening the clob in Read only mode" << endl;
00235 clob.open (OCCI_LOB_READONLY);
00236 int clobLength=clob.length ();
00237 cout << "Length of the clob is: " << clobLength << endl;
00238 m_size=clobLength;
00239 unsigned char* buffer = readClob (clob, m_size);
00240 clob.close ();
00241 cout<< "the clob buffer is:"<<endl;
00242 for (int i = 0; i < clobLength; ++i)
00243 cout << (char) buffer[i];
00244 cout << endl;
00245
00246
00247 result->setDCCClob(buffer );
00248
00249 } catch (SQLException &e) {
00250 throw(runtime_error("ODDCCConfig::fetchData(): "+e.getMessage()));
00251 }
00252 }
00253
00254
00255
00256 int ODDCCConfig::fetchID() throw(std::runtime_error)
00257 {
00258 if (m_ID!=0) {
00259 return m_ID;
00260 }
00261
00262 this->checkConnection();
00263
00264 try {
00265 Statement* stmt = m_conn->createStatement();
00266 stmt->setSQL("SELECT DCC_configuration_id FROM ecal_dcc_configuration "
00267 "WHERE dcc_tag=:dcc_tag "
00268 );
00269
00270 stmt->setString(1, getConfigTag() );
00271
00272
00273 ResultSet* rset = stmt->executeQuery();
00274
00275 if (rset->next()) {
00276 m_ID = rset->getInt(1);
00277 } else {
00278 m_ID = 0;
00279 }
00280 m_conn->terminateStatement(stmt);
00281 } catch (SQLException &e) {
00282 throw(runtime_error("ODDCCConfig::fetchID: "+e.getMessage()));
00283 }
00284
00285
00286 return m_ID;
00287 }