00001 #include <stdexcept>
00002 #include <string>
00003 #include "OnlineDB/Oracle/interface/Oracle.h"
00004
00005 #include "OnlineDB/EcalCondDB/interface/ODTTCFConfig.h"
00006
00007 using namespace std;
00008 using namespace oracle::occi;
00009
00010 ODTTCFConfig::ODTTCFConfig()
00011 {
00012 m_env = NULL;
00013 m_conn = NULL;
00014 m_writeStmt = NULL;
00015 m_readStmt = NULL;
00016 m_config_tag="";
00017 m_ID=0;
00018 clear();
00019 m_size=0;
00020 }
00021
00022
00023 void ODTTCFConfig::clear(){
00024
00025 }
00026
00027
00028 ODTTCFConfig::~ODTTCFConfig()
00029 {
00030 }
00031
00032 int ODTTCFConfig::fetchNextId() throw(std::runtime_error) {
00033
00034 int result=0;
00035 try {
00036 this->checkConnection();
00037 std::cout<< "going to fetch new id for TTCF 1"<<endl;
00038 m_readStmt = m_conn->createStatement();
00039 m_readStmt->setSQL("select ecal_ttcf_config_sq.NextVal from dual");
00040 ResultSet* rset = m_readStmt->executeQuery();
00041 while (rset->next ()){
00042 result= rset->getInt(1);
00043 }
00044 std::cout<< "id is : "<< result<<endl;
00045
00046 m_conn->terminateStatement(m_readStmt);
00047 return result;
00048
00049 } catch (SQLException &e) {
00050 throw(runtime_error("ODTTCFConfig::fetchNextId(): "+e.getMessage()));
00051 }
00052
00053 }
00054
00055
00056
00057
00058 void ODTTCFConfig::prepareWrite()
00059 throw(runtime_error)
00060 {
00061 this->checkConnection();
00062 int next_id=fetchNextId();
00063
00064 try {
00065 m_writeStmt = m_conn->createStatement();
00066 m_writeStmt->setSQL("INSERT INTO ECAL_TTCF_CONFIGURATION (ttcf_configuration_id, ttcf_tag, "
00067 " ttcf_configuration_file , configuration ) "
00068 "VALUES (:1, :2, :3 , :4 )");
00069 m_writeStmt->setInt(1, next_id);
00070 m_writeStmt->setString(2, getConfigTag());
00071 m_writeStmt->setString(3, getTTCFConfigurationFile());
00072
00073 oracle::occi::Clob clob(m_conn);
00074 clob.setEmpty();
00075 m_writeStmt->setClob(4,clob);
00076 m_writeStmt->executeUpdate ();
00077 m_ID=next_id;
00078
00079 m_conn->terminateStatement(m_writeStmt);
00080 std::cout<<"inserted into CONFIGURATION with id="<<next_id<<std::endl;
00081
00082
00083 m_writeStmt = m_conn->createStatement();
00084 m_writeStmt->setSQL ("SELECT configuration FROM ECAL_TTCF_CONFIGURATION WHERE"
00085 " ttcf_configuration_id=:1 FOR UPDATE");
00086
00087 std::cout<<"updating the clob 0"<<std::endl;
00088
00089
00090 } catch (SQLException &e) {
00091 throw(runtime_error("ODTTCFConfig::prepareWrite(): "+e.getMessage()));
00092 }
00093
00094 std::cout<<"updating the clob 1 "<<std::endl;
00095
00096 }
00097
00098 void ODTTCFConfig::writeDB()
00099 throw(runtime_error)
00100 {
00101
00102 std::cout<<"updating the clob 2"<<std::endl;
00103
00104 try {
00105 m_writeStmt->setInt(1, m_ID);
00106 ResultSet* rset = m_writeStmt->executeQuery();
00107
00108 rset->next ();
00109
00110 oracle::occi::Clob clob = rset->getClob (1);
00111 cout << "Opening the clob in read write mode" << endl;
00112 populateClob (clob, getTTCFConfigurationFile(), m_size);
00113 int clobLength=clob.length ();
00114 cout << "Length of the clob is: " << clobLength << endl;
00115
00116 m_writeStmt->executeUpdate();
00117 m_writeStmt->closeResultSet (rset);
00118
00119 } catch (SQLException &e) {
00120 throw(runtime_error("ODTTCFConfig::writeDB(): "+e.getMessage()));
00121 }
00122
00123 if (!this->fetchID()) {
00124 throw(runtime_error("ODTTCFConfig::writeDB: Failed to write"));
00125 }
00126
00127
00128 }
00129
00130
00131
00132
00133 void ODTTCFConfig::fetchData(ODTTCFConfig * result)
00134 throw(runtime_error)
00135 {
00136 this->checkConnection();
00137 result->clear();
00138
00139 if(result->getId()==0 && (result->getConfigTag()=="") ){
00140 throw(runtime_error("ODTTCFConfig::fetchData(): no Id defined for this ODTTCFConfig "));
00141 }
00142
00143 try {
00144
00145 m_readStmt->setSQL("SELECT * "
00146 "FROM ECAL_TTCF_CONFIGURATION "
00147 " where (ttcf_configuration_id = :1 or ttcf_tag= :2) " );
00148 m_readStmt->setInt(1, result->getId());
00149 m_readStmt->setString(2, result->getConfigTag());
00150 ResultSet* rset = m_readStmt->executeQuery();
00151
00152 rset->next();
00153
00154 result->setId(rset->getInt(1));
00155 result->setConfigTag(rset->getString(2));
00156 result->setTTCFConfigurationFile(rset->getString(3));
00157 Clob clob = rset->getClob (4);
00158 cout << "Opening the clob in Read only mode" << endl;
00159 clob.open (OCCI_LOB_READONLY);
00160 int clobLength=clob.length ();
00161 cout << "Length of the clob is: " << clobLength << endl;
00162 m_size=clobLength;
00163 unsigned char* buffer = readClob (clob, m_size);
00164 clob.close ();
00165 result->setTTCFClob((unsigned char*) buffer );
00166
00167 } catch (SQLException &e) {
00168 throw(runtime_error("ODTTCFConfig::fetchData(): "+e.getMessage()));
00169 }
00170 }
00171
00172
00173
00174 int ODTTCFConfig::fetchID() throw(std::runtime_error)
00175 {
00176 if (m_ID!=0) {
00177 return m_ID;
00178 }
00179
00180 this->checkConnection();
00181
00182 try {
00183 Statement* stmt = m_conn->createStatement();
00184 stmt->setSQL("SELECT ttcf_configuration_id FROM ecal_ttcf_configuration "
00185 "WHERE ttcf_tag=:ttcf_tag "
00186 );
00187
00188 stmt->setString(1, getConfigTag() );
00189
00190 ResultSet* rset = stmt->executeQuery();
00191
00192 if (rset->next()) {
00193 m_ID = rset->getInt(1);
00194 } else {
00195 m_ID = 0;
00196 }
00197 m_conn->terminateStatement(stmt);
00198 } catch (SQLException &e) {
00199 throw(runtime_error("ODTTCFConfig::fetchID: "+e.getMessage()));
00200 }
00201
00202 return m_ID;
00203 }
00204
00205 void ODTTCFConfig::setParameters(std::map<string,string> my_keys_map){
00206
00207
00208
00209
00210 for( std::map<std::string, std::string >::iterator ci=
00211 my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
00212
00213 if(ci->first== "TTCF_CONFIGURATION_ID") setConfigTag(ci->second);
00214 if(ci->first== "Configuration") {
00215 std::string fname=ci->second ;
00216 string str3;
00217 size_t pos, pose;
00218
00219 pos = fname.find("=");
00220 pose = fname.size();
00221 str3 = fname.substr (pos+1, pose-pos-2);
00222
00223 cout << "fname="<<fname<< " and reduced is: "<<str3 << endl;
00224 setTTCFConfigurationFile(str3 );
00225
00226
00227
00228 std::cout << "Going to read file: " << str3 << endl;
00229
00230 ifstream inpFile;
00231 inpFile.open(str3.c_str());
00232
00233
00234 int bufsize = 0;
00235 inpFile.seekg( 0,ios::end );
00236 bufsize = inpFile.tellg();
00237 std::cout <<" bufsize ="<<bufsize<< std::endl;
00238
00239 inpFile.seekg( 0,ios::beg );
00240
00241 inpFile.close();
00242 m_size=bufsize;
00243
00244 }
00245 }
00246
00247 }
00248