tape_ODBC/ODBCConnMgr.cpp

00001 /*  ODBCConnMgr.cpp
00002  *
00003  *  author: Matt Hauer, matthew.hauer@pnl.gov, 6/4/07 - ***
00004  */
00005 
00006 
00007 #include "ODBCConnMgr.h"
00008 
00009 ODBCConnMgr *ODBCConnMgr::mgr=0;
00010 
00011 ODBCConnMgr::ODBCConnMgr(){
00012     mgr=this;
00013     handles.clear();
00014 }
00015 
00016 ODBCConnMgr::~ODBCConnMgr(){
00017     mgr=0;
00018     while(!handles.empty()){
00019         delete handles.front();
00020         handles.pop_front();
00021     }
00022 }
00023 
00024 ODBCConnMgr *ODBCConnMgr::GetMgr(){
00025     if(mgr == 0) return new ODBCConnMgr();
00026     return mgr;
00027 }
00028 
00029 ODBCConnHandle *ODBCConnMgr::ConnectToHost(char *hostname, char *uid, char *pwd){
00030     if(!handles.empty()){
00031         list<ODBCConnHandle *>::iterator itr=handles.begin();
00032         do{
00033             if(0 == (*itr)->CheckName(hostname)) return *itr;
00034         } while(itr!=handles.end());
00035     }
00036     //  guess we aren't connected to that host.
00037 //  then again, let's give it a shot.  -MH
00038 //  if((uid == null) || (pwd == null)) return null; //  no uid or pwd, auto-fail.
00039     odbc::Connection *conn=odbc::DriverManager::getConnection(hostname, uid, pwd);
00040     if(conn != 0){
00041         handles.push_back(new ODBCConnHandle(conn, hostname));
00042     } else {
00043         printf("WARNING:\tODBCConnMgr::ConnectToHost: unable to connect to host %s with uid %s!\n", hostname, uid);
00044         return 0;
00045     }
00046     return handles.back();
00047 }
00048 
00049 int ODBCConnMgr::DisconnectFromHost(char *hostname){
00050     if(!handles.empty()){
00051         list<ODBCConnHandle *>::iterator itr=handles.begin();
00052         do{
00053             if(0 == (*itr)->CheckName(hostname)){
00054                 (*itr)->Disconnect();
00055                 handles.erase(itr);
00056                 return 1;
00057             }
00058         } while(itr!=handles.end());
00059     }
00060     printf("WARNING:\tODBCConnMgr::DisconnectFromHost(%s): we're not connected to that host!\n", hostname);
00061     return 0;
00062 }
00063 
00064 //  end of ODBCConnMgr.cpp

GridLAB-DTM Version 1.0
An open-source project initiated by the US Department of Energy