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

O3DBIStatement.cpp

Go to the documentation of this file.
00001 
00011 #include "O3DBIStatement.h"
00012 #include "O3DBIError.h"
00013 #include "O3DBIException.h"
00014 
00015 // ----------------------------------------------------------------------------
00016 // O3DBIStatement construction/destruction
00017 
00018 O3DBIStatement::O3DBIStatement() :
00019     _pchStmnt(NULL),
00020     _hStmnt(NULL)
00021 {
00022     AllocateStmntHandle();
00023 }
00024 
00025 O3DBIStatement::O3DBIStatement(PC_TCHAR pchStmnt) :
00026     _pchStmnt(NULL),
00027     _hStmnt(NULL)
00028 {
00029     AllocateStmntHandle();
00030     if (! AllocateAndSetStmnt(pchStmnt))
00031         throw new O3DBIException(O3DBI_INVALID_STATEMENT,
00032             O3DBIErrorTxt::pchInvalidStatement,
00033             _T("O3DBIStatement::O3DBIStatement"), this);
00034 }
00035 
00036 O3DBIStatement::O3DBIStatement(const O3DBIStatement& other) :
00037     _pchStmnt(NULL),
00038     _hStmnt(NULL)
00039 {
00040     AllocateStmntHandle();
00041     (*this) = other;
00042 }
00043 
00044 O3DBIStatement::~O3DBIStatement()
00045 {
00046     // Free statement string
00047     if (_pchStmnt != NULL)
00048     {
00049         ::free(reinterpret_cast<void*>(_pchStmnt));
00050         _pchStmnt = NULL;
00051     }
00052 
00053     // Free statement handle
00054     if (_hStmnt != NULL)
00055         OCIHandleFree(reinterpret_cast<TOCIDvoidPtr*>(&_hStmnt),
00056         OCI_HTYPE_STMT);
00057 }
00058 
00059 // ----------------------------------------------------------------------------
00060 // O3DBIStatement operations
00061 
00062 O3DBIStatement& O3DBIStatement::operator=(const O3DBIStatement& other)
00063 {
00064     if (! AllocateAndSetStmnt(other._pchStmnt))
00065         throw new O3DBIException(O3DBI_INVALID_STATEMENT,
00066             O3DBIErrorTxt::pchInvalidStatement,
00067             _T("O3DBIStatement::operator="), this);
00068     return (*this);
00069 }
00070 
00071 O3DBIStatement& O3DBIStatement::operator=(PC_TCHAR pchStmnt)
00072 {
00073     if (! AllocateAndSetStmnt(pchStmnt))
00074         throw new O3DBIException(O3DBI_INVALID_STATEMENT,
00075             O3DBIErrorTxt::pchInvalidStatement,
00076             _T("O3DBIStatement::operator="), this);
00077     return (*this);
00078 }
00079 
00080 bool O3DBIStatement::operator==(const O3DBIStatement& other) const
00081 {
00082     return (::_tcscmp(_pchStmnt, other._pchStmnt) == 0);
00083 }
00084 
00085 void O3DBIStatement::SetStatement(PC_TCHAR pchStmnt)
00086 {
00087     if (! AllocateAndSetStmnt(pchStmnt))
00088         throw new O3DBIException(O3DBI_INVALID_STATEMENT,
00089             O3DBIErrorTxt::pchInvalidStatement,
00090             _T("O3DBIStatement::SetStatement"), this);
00091 }
00092 
00093 PC_TCHAR O3DBIStatement::GetStatement() const
00094 {
00095     return _pchStmnt;
00096 }
00097 
00098 void O3DBIStatement::SetBindVariable(PC_TCHAR pchPlaceholderName, int iValue)
00099 {
00100     PreBindCheck(pchPlaceholderName);
00101 
00102 }
00103 
00104 TO3DBIResult O3DBIStatement::Execute(O3DBIDatabase& db,
00105                                      O3DBIRecordSet& recordset)
00106 {
00107     PrepareStatement();
00108 //  BindInputParams();
00109 //  DefineOutputParams();
00110     return OSUCCESS;
00111 }
00112 
00113 TO3DBIResult O3DBIStatement::ExecuteAsync(O3DBIDatabase& db,
00114                                           O3DBIRecordSet& recordset)
00115 {
00116     PrepareStatement();
00117 //  BindInputParams();
00118 //  DefineOutputParams();
00119     return OSUCCESS;
00120 }
00121 
00122 bool O3DBIStatement::IsStillExecuting()
00123 {
00124     return true;
00125 }
00126 
00127 bool O3DBIStatement::Terminate()
00128 {
00129     return true;
00130 }
00131 
00132 TO3DBIString O3DBIStatement::ToString() const
00133 {
00134     _TCHAR buffer[512];
00135     int p;
00136 
00137     p = _stprintf(buffer, _T("O3DBIStatement at: 0x%x\n"), this);
00138     p += _stprintf(buffer + p, _T("\tStatement handle: %x\n"), _hStmnt);
00139     _stprintf(buffer + p, _T("\tStatement: %s\n"), GetStatement());
00140     return TO3DBIString(buffer);
00141 }
00142 
00143 // ----------------------------------------------------------------------------
00144 // O3DBIStatement internal implementation
00145 
00146 bool O3DBIStatement::AllocateAndSetStmnt(PC_TCHAR pchStmnt)
00147 {
00148     if (pchStmnt == NULL)
00149         return false;   // NULL pointers not allowed!
00150     unsigned int nLength = ::_tcslen(pchStmnt);
00151     if (nLength == 0)
00152         return false;   // Zero-length strings are not allowed!
00153     if (_pchStmnt != NULL)
00154     {
00155         // Free previous allocated memory first
00156         ::free(reinterpret_cast<void*>(_pchStmnt));
00157     }
00158     _pchStmnt =
00159         reinterpret_cast<P_TCHAR>(::calloc(nLength + sizeof(_TCHAR),
00160         sizeof(_TCHAR)));
00161     if (_pchStmnt == NULL)
00162         return false;
00163     // Copy string
00164     ::_tcsncpy(_pchStmnt, pchStmnt, nLength);
00165     return true;
00166 }
00167 
00168 void O3DBIStatement::AllocateStmntHandle()
00169 {
00170     // Allocate server handle
00171     TOCIResult result = OCIHandleAlloc(_hEnv,
00172         reinterpret_cast<TOCIDvoidPtr*>(&_hStmnt), OCI_HTYPE_STMT,
00173         (size_t)0, (TOCIDvoidPtr*)NULL);
00174 
00175     if (result == OCI_INVALID_HANDLE)
00176     {
00177         throw new O3DBIException(-1, _T("Could not allocate statement handle: ")
00178             _T("OCIHandleAlloc returned OCI_INVALID_HANDLE!"),
00179             _T("O3DBIStatement::O3DBIStatement"), this);
00180     }
00181 }
00182 
00183 void O3DBIStatement::PrepareStatement()
00184 {
00185     // Prepare the statement for execution
00186     ub4 nLength = static_cast<ub4>(::_tcslen(_pchStmnt));
00187     TOCIResult result = OCIStmtPrepare(_hStmnt, _hError,
00188         reinterpret_cast<TOCITextPtr>(_pchStmnt), nLength,
00189         static_cast<ub4>(OCI_NTV_SYNTAX), static_cast<ub4>(OCI_DEFAULT));
00190     if (result != OCI_SUCCESS)
00191     {
00192         TO3DBIString errortext;
00193         TOCIErrorcode errorcode = GetLastError(result, errortext);
00194         throw new O3DBIException(errorcode, errortext.c_str(),
00195             _T("O3DBIStatement::PrepareStatement"), this);
00196     }
00197 }
00198 
00199 void O3DBIStatement::BindInputParams()
00200 {
00201 
00202 }
00203 
00204 void O3DBIStatement::DefineOutputParams()
00205 {
00206 
00207 }
00208 
00209 void O3DBIStatement::PreBindCheck(PC_TCHAR pchPlaceholderName) const
00210 {
00211     // Called by SetBindVariable() with placeholder name.
00212     if (_hStmnt == NULL)
00213         throw new O3DBIException(O3DBI_INVALID_STATEMENTHANDLE,
00214         O3DBIErrorTxt::pchInvalidStatementhandle,
00215         _T("O3DBIStatement::SetBindVariable"), this);
00216     if (pchPlaceholderName == NULL || ::_tcslen(pchPlaceholderName) == 0)
00217         throw new O3DBIException(O3DBI_INVALID_PLACEHOLDER,
00218         O3DBIErrorTxt::pchInvalidPlaceholder,
00219         _T("O3DBIStatement::SetBindVariable"), this);
00220 }



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