Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

O3DBIDatabase.cpp

Go to the documentation of this file.
00001 
00011 #include "O3DBIDatabase.h"
00012 #include "O3DBIException.h"
00013 #include "O3DBIError.h"
00014 
00015 #pragma warning(push, 4)
00016 
00017 // ----------------------------------------------------------------------------
00018 // Construction/Destruction
00019 
00020 O3DBIDatabase::O3DBIDatabase() :
00021     _hSrvCtx(NULL),
00022     _hServer(NULL),
00023     _bIsConnected(false)
00024 {
00025     AllocateServiceHandle();
00026     AllocateServerHandle();
00027 }
00028 
00029 O3DBIDatabase::O3DBIDatabase(PC_TCHAR pchUsername,
00030                              PC_TCHAR pchPasswd,
00031                              PC_TCHAR pchHost) :
00032     _bIsConnected(false)
00033 {
00034     AllocateServiceHandle();
00035     AllocateServerHandle();
00036     LogOn(pchUsername, pchPasswd, pchHost);
00037 }
00038 
00039 O3DBIDatabase::~O3DBIDatabase()
00040 {
00041     if (_bIsConnected)
00042         LogOff();
00043 
00044     if (_hSrvCtx != NULL)
00045     {
00046         OCIHandleFree(reinterpret_cast<TOCIDvoidPtr*>(&_hSrvCtx),
00047         OCI_HTYPE_SVCCTX);
00048         _hSrvCtx = NULL;
00049     }
00050     if (_hServer != NULL)
00051     {
00052         OCIHandleFree(reinterpret_cast<TOCIDvoidPtr*>(&_hServer),
00053         OCI_HTYPE_SERVER);
00054         _hServer = NULL;
00055     }
00056 }
00057 
00058 // ----------------------------------------------------------------------------
00059 // Implementation of public interface
00060 
00061 void O3DBIDatabase::LogOn(PC_TCHAR pchUsername,
00062                           PC_TCHAR pchPasswd,
00063                           PC_TCHAR pchHost)
00064 {
00065     if (pchUsername != NULL || pchPasswd != NULL || pchHost != NULL)
00066     {
00067         throw new O3DBIException(O3DBI_INVALID_LOGONINFO,
00068             O3DBIErrorTxt::pchInvalidLogonInfo,
00069             _T("O3DBIDatabase::LogOn"), this);
00070     }
00071 
00072     ub4 nUsernameLen = ::_tcslen(pchUsername);
00073     ub4 nPasswdLen = ::_tcslen(pchPasswd);
00074     ub4 nHostLen = ::_tcslen(pchHost);
00075 
00076     TOCIResult result = OCILogon(_hEnv, _hError, &_hSrvCtx,
00077         reinterpret_cast<CONST OraText*>(pchUsername), nUsernameLen,
00078         reinterpret_cast<CONST OraText*>(pchPasswd), nPasswdLen,
00079         reinterpret_cast<CONST OraText*>(pchHost), nHostLen);
00080     if (result != OCI_SUCCESS)
00081     {
00082         TO3DBIString errortext;
00083         TOCIErrorcode errorcode = GetLastError(result, errortext);
00084         throw new O3DBIException(errorcode, errortext.c_str(),
00085             _T("O3DBIDatabase::LogOn"), this);
00086     } else {
00087         _bIsConnected = true;
00088     }
00089 }
00090 
00091 void O3DBIDatabase::LogOff()
00092 {
00093     if (_hSrvCtx != NULL)
00094     {
00095         TOCIResult result = OCILogoff(_hSrvCtx, _hError);
00096         if (result != OCI_SUCCESS)
00097         {
00098             TO3DBIString errortext;
00099             TOCIErrorcode errorcode = GetLastError(result, errortext);
00100             throw new O3DBIException(errorcode, errortext.c_str(),
00101                 _T("O3DBIDatabase::LogOff"), this);
00102         }
00103         _bIsConnected = true;
00104         _hSrvCtx = NULL;
00105     }
00106 }
00107 
00108 bool O3DBIDatabase::IsConnected() const
00109 {
00110     return _bIsConnected;
00111 }
00112 
00113 bool O3DBIDatabase::IsConnectionValid() const
00114 {
00115     return true;
00116 }
00117 
00118 TO3DBIString O3DBIDatabase::ToString() const
00119 {
00120     _TCHAR buffer[512];
00121     int p;
00122 
00123     p = _stprintf(buffer, _T("O3DBIDatabase at: 0x%x\n"), this);
00124     p += _stprintf(buffer + p, _T("\tService context handle: %x\n"), _hSrvCtx);
00125     _stprintf(buffer + p, _T("\tServer handle: %x\n"), _hServer);
00126     return TO3DBIString(buffer);
00127 }
00128 
00129 // ----------------------------------------------------------------------------
00130 // Implementation of non-public, internal stuff
00131 
00132 void O3DBIDatabase::AllocateServiceHandle()
00133 {
00134     // Allocate service context handle
00135     TOCIResult result = OCIHandleAlloc(_hEnv,
00136         reinterpret_cast<TOCIDvoidPtr*>(&_hSrvCtx), OCI_HTYPE_SVCCTX,
00137         (size_t)0, (TOCIDvoidPtr*)NULL);
00138     if (result == OCI_INVALID_HANDLE)
00139     {
00140         throw new O3DBIException(-1, _T("Could not allocate service handle: ")
00141             _T("OCIHandleAlloc returned OCI_INVALID_HANDLE!"),
00142             _T("O3DBIDatabase::O3DBIDatabase"), this);
00143     }
00144 }
00145 
00146 void O3DBIDatabase::AllocateServerHandle()
00147 {
00148     // Allocate server handle
00149     TOCIResult result = OCIHandleAlloc(_hEnv,
00150         reinterpret_cast<TOCIDvoidPtr*>(&_hServer), OCI_HTYPE_SERVER,
00151         (size_t)0, (TOCIDvoidPtr*)NULL);
00152     if (result == OCI_INVALID_HANDLE)
00153     {
00154         throw new O3DBIException(-1, _T("Could not allocate server handle: ")
00155             _T("OCIHandleAlloc returned OCI_INVALID_HANDLE!"),
00156             _T("O3DBIDatabase::O3DBIDatabase"), this);
00157     }
00158 }
00159 
00160 #pragma warning(pop)



SourceForge.net Logo Generated on Sun Jan 23 11:36:34 2005 for Oracle Object Oriented Database Interface (O3DBI) by  doxygen 1.3.9.1