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

O3DBIObject.cpp

Go to the documentation of this file.
00001 
00012 #include "O3DBIException.h"
00013 
00014 #pragma warning(push, 4)
00015 
00016 // ----------------------------------------------------------------------------
00017 // O3DBIObject static data member initialization
00018 
00019 UINT O3DBIObject::_nRefCount = 0;
00020 TOCIEnvironmentHandle O3DBIObject::_hEnv = NULL;
00021 TOCIErrorHandle O3DBIObject::_hError = NULL;
00022 MemAllocFP O3DBIObject::_memallocfp = NULL;
00023 ReAllocFP O3DBIObject::_reallocfp = NULL;
00024 MemFreeFP O3DBIObject::_memfreefp = NULL;
00025 
00026 // ----------------------------------------------------------------------------
00027 // O3DBIObject static member functions
00028 
00029 void O3DBIObject::RegisterMemAllocCB(MemAllocFP memallocfp)
00030 {
00031     _memallocfp = memallocfp;
00032 }
00033 
00034 void O3DBIObject::RegisterReAllocCB(ReAllocFP reallocfp)
00035 {
00036     _reallocfp = reallocfp;
00037 }
00038 
00039 void O3DBIObject::RegisterMemFreeCB(MemFreeFP memfreefp)
00040 {
00041     _memfreefp = memfreefp;
00042 }
00043 
00044 // ----------------------------------------------------------------------------
00045 // O3DBIObject construction/destruction
00046 
00047 O3DBIObject::O3DBIObject()
00048 {
00049     if (_nRefCount == 0)
00050     {
00051         ub4 mode =
00052 #ifdef _UNICODE
00053             OCI_UTF16 | OCI_THREADED;
00054 #else
00055             OCI_DEFAULT | OCI_THREADED;
00056 #endif // _UNICODE
00057 
00058         // The first O3DBIObject is constructed: create and initialize an
00059         // default, thread-safety environment for OCI! OCIEnvCreate() must be
00060         // invoked before any other OCI call!
00061         TOCIResult result = OCIEnvCreate(&_hEnv, mode, (TOCIDvoidPtr)NULL,
00062             _memallocfp, _reallocfp, _memfreefp, 0, (TOCIDvoidPtr*)NULL);
00063 
00064         if (result == OCI_ERROR)
00065         {
00066             throw new O3DBIException(-1, _T("Could not create an environment: ")
00067                 _T("OCIEnvCreate returned OCI_ERROR!"),
00068                 _T("O3DBIObject::O3DBIObject"), this);
00069         }
00070 
00071         // Allocate error handle
00072         result = OCIHandleAlloc(_hEnv, reinterpret_cast<TOCIDvoidPtr*>(&_hError),
00073             OCI_HTYPE_ERROR, (size_t)0, (TOCIDvoidPtr*)NULL);
00074         if (result == OCI_INVALID_HANDLE)
00075         {
00076             throw new O3DBIException(-1, _T("Could not allocate error handle: ")
00077                 _T("OCIHandleAlloc returned OCI_INVALID_HANDLE!"),
00078                 _T("O3DBIObject::O3DBIObject"), this);
00079         }
00080     }
00081     ++_nRefCount;   // Increase reference counter
00082 
00083     // DbC postcondition check
00084     ENSURE(_hEnv != NULL);
00085     ENSURE(_hError != NULL);
00086 }
00087 
00088 O3DBIObject::~O3DBIObject()
00089 {
00090     --_nRefCount;   // Decrease reference counter
00091     if (_nRefCount == 0)
00092     {
00093         // The last instance of an O3DBIObject (or of a class derived from
00094         // O3DBIObject) is dying: do some cleanup work...
00095         if (_hError != NULL)
00096             OCIHandleFree(reinterpret_cast<TOCIDvoidPtr*>(&_hError),
00097             OCI_HTYPE_ERROR);
00098 
00099         // Shut down OCI
00100         OCITerminate(OCI_DEFAULT);
00101     }
00102 }
00103 
00104 TO3DBIString O3DBIObject::ToString() const
00105 {
00106     _TCHAR buffer[512];
00107     int p;
00108 
00109     p = _stprintf(buffer, _T("O3DBIObject at: 0x%x\n"), this);
00110     _stprintf(buffer + p, _T("\tOCI environment handle: %x\n"), _hEnv);
00111     return TO3DBIString(buffer);
00112 }
00113 
00114 // ----------------------------------------------------------------------------
00115 // O3DBIObject consistency-check
00116 
00117 #ifdef _DEBUG
00118 bool O3DBIObject::IsValid() const
00119 {
00120     return true;
00121 }
00122 #endif
00123 
00124 // ----------------------------------------------------------------------------
00125 // O3DBIObject internal implementation
00126 
00127 TOCIErrorcode O3DBIObject::GetLastError(TOCIResult result,
00128                                         TO3DBIString& errortext)
00129 {
00130     TOCIErrorcode errorcode = -1;
00131     OraText errorbuffer[512];
00132 
00133     switch (result)
00134     {
00135     case OCI_SUCCESS_WITH_INFO:
00136     case OCI_ERROR:
00137         OCIErrorGet((TOCIDvoidPtr)_hError, (ub4)1, (OraText*)NULL,
00138             &errorcode, errorbuffer, (ub4)sizeof(errorbuffer),
00139             (ub4)OCI_HTYPE_ERROR);
00140         errortext = reinterpret_cast<P_TCHAR>(errorbuffer);
00141         break;
00142 
00143     case OCI_NEED_DATA:
00144         errortext = _T("Error - OCI_NEED_DATA");
00145         break;
00146 
00147     case OCI_NO_DATA:
00148         errortext = _T("Error - OCI_NO_DATA");
00149         break;
00150 
00151     case OCI_INVALID_HANDLE:
00152         errortext = _T("Error - OCI_INVALID_HANDLE");
00153         break;
00154 
00155     case OCI_STILL_EXECUTING:
00156         errortext = _T("Error - OCI_STILL_EXECUTING");
00157         break;
00158 
00159     case OCI_CONTINUE:
00160         errortext = _T("Error - OCI_CONTINUE");
00161         break;
00162 
00163     default:
00164         P_TCHAR pBuffer = reinterpret_cast<P_TCHAR>(errorbuffer);
00165         ::_stprintf(pBuffer, _T("Error - return value:%d"), result);
00166         errortext = pBuffer;
00167         break;
00168     }
00169 
00170     return errorcode;
00171 }
00172 
00173 #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