Go to the documentation of this file.00001 #include <fstream>
00002 #include <iostream>
00003 #include <stdio.h>
00004 #include <stdexcept>
00005 #include <string>
00006 #include "OnlineDB/Oracle/interface/Oracle.h"
00007
00008 #include "OnlineDB/EcalCondDB/interface/ODLTCConfig.h"
00009
00010 using namespace std;
00011 using namespace oracle::occi;
00012
00013 ODLTCConfig::ODLTCConfig()
00014 {
00015 m_env = NULL;
00016 m_conn = NULL;
00017 m_writeStmt = NULL;
00018 m_readStmt = NULL;
00019 m_config_tag="";
00020 m_size=0;
00021
00022 m_ID=0;
00023 clear();
00024
00025 }
00026
00027
00028
00029 ODLTCConfig::~ODLTCConfig()
00030 {
00031
00032 }
00033
00034 int ODLTCConfig::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_ltc_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(std::runtime_error("ODLTCConfig::fetchNextId(): "+e.getMessage()));
00051 }
00052
00053 }
00054
00055
00056
00057
00058 void ODLTCConfig::prepareWrite()
00059 throw(std::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_LTC_CONFIGURATION (ltc_configuration_id, ltc_tag, "
00068 " LTC_CONFIGURATION_file, "
00069 " Configuration ) "
00070 "VALUES (:1, :2, :3, :4 )");
00071 m_writeStmt->setInt(1, next_id);
00072 m_writeStmt->setString(2, this->getConfigTag());
00073 m_writeStmt->setString(3, getLTCConfigurationFile());
00074
00075
00076
00077 oracle::occi::Clob clob(m_conn);
00078 clob.setEmpty();
00079 m_writeStmt->setClob(4,clob);
00080 m_writeStmt->executeUpdate ();
00081 m_ID=next_id;
00082
00083 m_conn->terminateStatement(m_writeStmt);
00084 std::cout<<"LTC Clob inserted into CONFIGURATION with id="<<next_id<<std::endl;
00085
00086
00087 m_writeStmt = m_conn->createStatement();
00088 m_writeStmt->setSQL ("SELECT Configuration FROM ECAL_LTC_CONFIGURATION WHERE"
00089 " ltc_configuration_id=:1 FOR UPDATE");
00090
00091 std::cout<<"updating the clob 0"<<std::endl;
00092
00093
00094 } catch (SQLException &e) {
00095 throw(std::runtime_error("ODLTCConfig::prepareWrite(): "+e.getMessage()));
00096 }
00097
00098 std::cout<<"updating the clob 1 "<<std::endl;
00099
00100 }
00101
00102 void ODLTCConfig::setParameters(std::map<string,string> my_keys_map){
00103
00104
00105
00106
00107 for( std::map<std::string, std::string >::iterator ci=
00108 my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
00109
00110 if(ci->first== "LTC_CONFIGURATION_ID") setConfigTag(ci->second);
00111 if(ci->first== "Configuration") {
00112 std::string fname=ci->second ;
00113 string str3;
00114 size_t pos, pose;
00115
00116 pos = fname.find("=");
00117 pose = fname.size();
00118 str3 = fname.substr (pos+1, pose-pos-2);
00119
00120 cout << "fname="<<fname<< " and reduced is: "<<str3 << endl;
00121 setLTCConfigurationFile(str3 );
00122
00123
00124
00125 std::cout << "Going to read LTC file: " << fname << endl;
00126
00127 ifstream inpFile;
00128 inpFile.open(str3.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 void ODLTCConfig::writeDB()
00149 throw(std::runtime_error)
00150 {
00151
00152 std::cout<<"updating the clob "<<std::endl;
00153
00154
00155
00156 try {
00157
00158
00159 m_writeStmt->setInt(1, m_ID);
00160 ResultSet* rset = m_writeStmt->executeQuery();
00161
00162 rset->next ();
00163 oracle::occi::Clob clob = rset->getClob (1);
00164
00165 cout << "Opening the clob in read write mode" << endl;
00166
00167 std::cout << "Populating the clob" << endl;
00168
00169 populateClob (clob, getLTCConfigurationFile(), m_size);
00170 int clobLength=clob.length ();
00171 cout << "Length of the clob is: " << clobLength << endl;
00172
00173
00174 m_writeStmt->executeUpdate();
00175
00176 m_writeStmt->closeResultSet (rset);
00177
00178 } catch (SQLException &e) {
00179 throw(std::runtime_error("ODLTCConfig::writeDB(): "+e.getMessage()));
00180 }
00181
00182 if (!this->fetchID()) {
00183 throw(std::runtime_error("ODLTCConfig::writeDB: Failed to write"));
00184 }
00185
00186
00187 }
00188
00189
00190 void ODLTCConfig::clear(){
00191
00192
00193 m_ltc_file="";
00194
00195 }
00196
00197
00198
00199 void ODLTCConfig::fetchData(ODLTCConfig * result)
00200 throw(std::runtime_error)
00201 {
00202 this->checkConnection();
00203 result->clear();
00204 if(result->getId()==0 && result->getConfigTag()==""){
00205 throw(std::runtime_error("ODLTCConfig::fetchData(): no Id defined for this ODLTCConfig "));
00206 }
00207
00208 try {
00209
00210 m_readStmt->setSQL("SELECT * "
00211 "FROM ECAL_LTC_CONFIGURATION "
00212 " where (ltc_configuration_id = :1 or LTC_tag=:2 )" );
00213 m_readStmt->setInt(1, result->getId());
00214 m_readStmt->setString(2, result->getConfigTag());
00215 ResultSet* rset = m_readStmt->executeQuery();
00216
00217 rset->next();
00218
00219
00220 result->setId(rset->getInt(1));
00221 result->setConfigTag(rset->getString(2));
00222 result->setLTCConfigurationFile(rset->getString(3));
00223
00224
00225 Clob clob = rset->getClob (4);
00226 cout << "Opening the clob in Read only mode" << endl;
00227 clob.open (OCCI_LOB_READONLY);
00228 int clobLength=clob.length ();
00229 cout << "Length of the clob is: " << clobLength << endl;
00230 m_size=clobLength;
00231 unsigned char* buffer = readClob (clob, clobLength);
00232 clob.close ();
00233 cout<< "the clob buffer is:"<<endl;
00234 for (int i = 0; i < clobLength; ++i)
00235 cout << (char) buffer[i];
00236 cout << endl;
00237
00238
00239 result->setLTCClob(buffer );
00240
00241 } catch (SQLException &e) {
00242 throw(std::runtime_error("ODLTCConfig::fetchData(): "+e.getMessage()));
00243 }
00244 }
00245
00246
00247
00248 int ODLTCConfig::fetchID() throw(std::runtime_error)
00249 {
00250 if (m_ID!=0) {
00251 return m_ID;
00252 }
00253
00254 this->checkConnection();
00255
00256 try {
00257 Statement* stmt = m_conn->createStatement();
00258 stmt->setSQL("SELECT ltc_configuration_id FROM ecal_ltc_configuration "
00259 "WHERE ltc_tag=:ltc_tag "
00260 );
00261
00262 stmt->setString(1, getConfigTag() );
00263
00264 ResultSet* rset = stmt->executeQuery();
00265
00266 if (rset->next()) {
00267 m_ID = rset->getInt(1);
00268 } else {
00269 m_ID = 0;
00270 }
00271 m_conn->terminateStatement(stmt);
00272 } catch (SQLException &e) {
00273 throw(std::runtime_error("ODLTCConfig::fetchID: "+e.getMessage()));
00274 }
00275
00276 return m_ID;
00277 }
00278