CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/CaloOnlineTools/HcalOnlineDb/src/ConnectionManager.cc

Go to the documentation of this file.
00001 
00002 #include <cstring>
00003 #include <cstdio>
00004 
00005 #include "OnlineDB/Oracle/interface/Oracle.h"
00006 #include "CaloOnlineTools/HcalOnlineDb/interface/ConnectionManager.h"
00007 #include "ctype.h"
00008 
00009 ConnectionManager::ConnectionManager() : env(0), conn(0) {
00010 }
00011 
00012 static const std::string keyFile("/nfshome0/hcalsw/.ReadOMDSKey");
00013 
00014 static void clean(char* s) {
00015   for (int x=strlen(s)-1; x>=0; x--) {
00016     if (isspace(s[x])) s[x]=0;
00017   }
00018 }
00019 
00020 bool ConnectionManager::connect() {
00021   if (env!=0) return true;
00022   std::string username,password,database;
00023 
00024   char s[100];
00025   FILE* f=fopen(keyFile.c_str(),"r");
00026   s[0]=0; fgets(s,100,f); clean(s); username=s;
00027   s[0]=0; fgets(s,100,f); clean(s); password=s;
00028   s[0]=0; fgets(s,100,f); clean(s); database=s;
00029   fclose(f);
00030 
00031   //  printf("'%s' '%s' '%s'\n",username.c_str(),password.c_str(),database.c_str());
00032   try {
00033     env = oracle::occi::Environment::createEnvironment (oracle::occi::Environment::DEFAULT);
00034     conn = env->createConnection (username, password, database);
00035   } catch (...) {
00036     return false;
00037   }
00038   return true;
00039 }
00040 oracle::occi::Statement* ConnectionManager::getStatement(const std::string& query) {
00041   if (env==0) return 0;
00042   return conn->createStatement(query);
00043 }
00044 void ConnectionManager::disconnect() {
00045   if (env==0) return;
00046   env->terminateConnection(conn);
00047   oracle::occi::Environment::terminateEnvironment(env);
00048   env=0; conn=0;
00049 }