123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480 |
- //#include <windows.h>
- #include "pch.h"
- #include "SQLWrap.h"
- CSQLWrap::CSQLWrap()
- {
- mhSQLEnv = NULL;
- mhSQLConn = SQL_NULL_HDBC;
- }
- CSQLWrap::~CSQLWrap()
- {
- this->Terminate();
- }
- SQLRETURN CSQLWrap::InitializeEnvironment()
- {
- SQLRETURN SQLResult;
- if (NULL == mhSQLEnv)
- {
- SQLResult = SQLAllocHandle(SQL_HANDLE_ENV,
- SQL_NULL_HANDLE,
- &mhSQLEnv);
- if (SQL_SUCCESS == SQLResult)
- {
- //
- // Exhibit ODBC 2.x behavior. Not sure why,
- // but this is what dev is doing.
- //
- SQLResult = SQLSetEnvAttr(mhSQLEnv,
- SQL_ATTR_ODBC_VERSION,
- (SQLPOINTER) SQL_OV_ODBC2,
- 0);
- if (SQL_SUCCESS != SQLResult)
- {
- SQLFreeHandle(SQL_HANDLE_ENV, mhSQLEnv);
- mhSQLEnv = NULL;
- }
- } else
- mhSQLEnv = NULL;
- }
- return(SQLResult);
- }
- SQLRETURN CSQLWrap::Initialize(PCHAR szDSN)
- {
- SQLRETURN SQLResult;
- SQLResult = this->InitializeEnvironment();
- //
- // Open a connection to the ODBC DSN.
- //
- if ((SQL_NULL_HDBC == mhSQLConn) && SQL_OK(SQLResult))
- {
- SQLResult = SQLAllocHandle(SQL_HANDLE_DBC,
- mhSQLEnv,
- &mhSQLConn);
- if (SQL_OK(SQLResult))
- {
- SQLResult = SQLConnectA(mhSQLConn,
- (SQLCHAR *) szDSN,
- SQL_NTS,
- (SQLCHAR *) "",
- SQL_NTS,
- (SQLCHAR *) "",
- SQL_NTS);
- if (!SQL_OK(SQLResult))
- {
- this->TrapError(mhSQLEnv, mhSQLConn, SQL_NULL_HSTMT);
- SQLFreeHandle(SQL_HANDLE_DBC, mhSQLConn);
- mhSQLConn = SQL_NULL_HDBC;
- }
- } else
- mhSQLConn = SQL_NULL_HDBC;
- }
- return(SQLResult);
- }
- SQLRETURN CSQLWrap::Initialize(PWCHAR szDSN)
- {
- SQLRETURN SQLResult;
- SQLResult = this->InitializeEnvironment();
- //
- // Open a connection to the ODBC DSN.
- //
- if ((SQL_NULL_HDBC == mhSQLConn) && (SQL_SUCCESS == SQLResult))
- {
- SQLResult = SQLAllocHandle(SQL_HANDLE_DBC,
- mhSQLEnv,
- &mhSQLConn);
- if (SQL_OK(SQLResult))
- {
- SQLResult = SQLConnectW(mhSQLConn,
- (SQLWCHAR *) szDSN,
- SQL_NTS,
- (SQLWCHAR *) L"",
- SQL_NTS,
- (SQLWCHAR *) L"",
- SQL_NTS);
- if (!SQL_OK(SQLResult))
- {
- this->TrapError(mhSQLEnv, mhSQLConn, SQL_NULL_HSTMT);
- SQLFreeHandle(SQL_HANDLE_DBC, mhSQLConn);
- mhSQLConn = SQL_NULL_HDBC;
- }
- } else
- mhSQLConn = SQL_NULL_HDBC;
- }
- return(SQLResult);
- }
- SQLRETURN CSQLWrap::Terminate()
- {
- SQLRETURN SQLResult;
- SQLResult = SQL_SUCCESS;
- if (SQL_NULL_HDBC != mhSQLConn)
- {
- SQLResult = SQLDisconnect(mhSQLConn);
- if (SQL_OK(SQLResult))
- {
- SQLResult = SQLFreeHandle(SQL_HANDLE_DBC,
- mhSQLConn);
- if (SQL_OK(SQLResult))
- mhSQLConn = SQL_NULL_HDBC;
- else
- this->TrapError(mhSQLEnv, mhSQLConn, SQL_NULL_HSTMT);
- } else
- this->TrapError(mhSQLEnv, mhSQLConn, SQL_NULL_HSTMT);
- }
- if ((NULL != mhSQLEnv) && SQL_OK(SQLResult))
- {
- SQLResult = SQLFreeHandle(SQL_HANDLE_ENV,
- mhSQLEnv);
- if (SQL_OK(SQLResult))
- mhSQLEnv = NULL;
- else
- this->TrapError(mhSQLEnv, SQL_NULL_HDBC, SQL_NULL_HSTMT);
- }
- return(SQLResult);
- }
- SQLRETURN CSQLWrap::BindOutputParameter(SQLHSTMT hStatement,
- SQL_PARAMETER_LIST *pParameter,
- SQLUSMALLINT *pusmiColumns)
- {
- SQLRETURN SQLResult;
- SQLResult = SQLBindCol(
- hStatement,
- ++(*pusmiColumns),
- pParameter->ValueType,
- pParameter->ParameterValuePtr,
- pParameter->BufferLength,
- pParameter->StrLen_or_IndPtr);
- return(SQLResult);
- }
- SQLRETURN CSQLWrap::FinishExecute(SQLHSTMT hStatement,
- SQL_PARAMETER_LIST *pParameter,
- BOOL *pfReturnParameters)
- {
- SQLUSMALLINT usmiParameters, usmiColumns;
- SQLRETURN SQLResult;
- DWORD dwIndex;
- *pfReturnParameters = FALSE;
- if (NULL != pParameter)
- {
- usmiParameters = usmiColumns = 0;
- for(dwIndex = 0;
- NULL != pParameter[dwIndex].ParameterValuePtr;
- dwIndex++)
- {
- switch(pParameter[dwIndex].InputOutputType)
- {
- case SQL_PARAM_INPUT:
- SQLResult = SQLBindParameter(
- hStatement,
- ++usmiParameters,
- SQL_PARAM_INPUT,
- pParameter[dwIndex].ValueType,
- pParameter[dwIndex].ParameterType,
- pParameter[dwIndex].ColumnDigits,
- (SQLSMALLINT) pParameter[dwIndex].ColumnDigits,
- pParameter[dwIndex].ParameterValuePtr,
- pParameter[dwIndex].BufferLength,
- pParameter[dwIndex].StrLen_or_IndPtr);
- break;
- case SQL_PARAM_OUTPUT:
- SQLResult = this->BindOutputParameter(
- hStatement,
- &pParameter[dwIndex],
- &usmiColumns);
- *pfReturnParameters = TRUE;
- break;
- default:
- //
- // BUGBUG: Set an error here.
- //
- break;
- }
- if (!SQL_OK(SQLResult))
- goto END_LABEL;
- }
- }
- //
- // Execute the statement.
- //
- SQLResult = SQLExecute(hStatement);
- END_LABEL:
- if (!SQL_OK(SQLResult))
- this->TrapError(mhSQLEnv, mhSQLConn, hStatement);
- return(SQLResult);
- }
- SQLRETURN CSQLWrap::Execute(PCHAR szStatement, SQL_PARAMETER_LIST *pParameter,
- SQLHSTMT * phStatement)
- {
- SQLRETURN SQLResult;
- SQLHSTMT hStatement;
- BOOL fReturnParameters;
- if (NULL != phStatement)
- *phStatement = SQL_NULL_HSTMT;
- hStatement = SQL_NULL_HSTMT;
- //
- // Need to create a statement.
- //
- SQLResult = SQLAllocHandle(SQL_HANDLE_STMT, mhSQLConn, &hStatement);
- if (!SQL_OK(SQLResult))
- goto ERROR_LABEL;
- //
- // Need to prepare the statement.
- //
- SQLResult = SQLPrepareA(hStatement,
- (SQLCHAR *) szStatement,
- SQL_NTS);
- if (!SQL_OK(SQLResult))
- goto ERROR_LABEL;
- //
- // Time to add parameters to the statement.
- //
- SQLResult = this->FinishExecute(hStatement,
- pParameter,
- &fReturnParameters);
- if (!SQL_OK(SQLResult))
- goto ERROR_LABEL;
- goto END_LABEL;
- ERROR_LABEL:
- this->TrapError(mhSQLEnv, mhSQLConn, hStatement);
- if (SQL_NULL_HSTMT != hStatement)
- {
- SQLFreeHandle(SQL_HANDLE_STMT, hStatement);
- hStatement = SQL_NULL_HSTMT;
- }
- END_LABEL:
- if (NULL != phStatement)
- *phStatement = hStatement;
- else if (SQL_NULL_HSTMT != hStatement)
- SQLFreeHandle(SQL_HANDLE_STMT, hStatement);
- return(SQLResult);
- }
- SQLRETURN CSQLWrap::Execute(PWCHAR szStatement, SQL_PARAMETER_LIST *pParameter,
- SQLHSTMT * phStatement)
- {
- SQLRETURN SQLResult;
- SQLHSTMT hStatement;
- BOOL fReturnParameters;
- if (NULL != phStatement)
- *phStatement = SQL_NULL_HSTMT;
- hStatement = SQL_NULL_HSTMT;
- //
- // Need to create a statement.
- //
- SQLResult = SQLAllocHandle(SQL_HANDLE_STMT, mhSQLConn, &hStatement);
- if (!SQL_OK(SQLResult))
- goto ERROR_LABEL;
- //
- // Need to prepare the statement.
- //
- SQLResult = SQLPrepareW(hStatement,
- (SQLWCHAR *) szStatement,
- SQL_NTS);
- if (!SQL_OK(SQLResult))
- goto ERROR_LABEL;
- //
- // Time to add parameters to the statement.
- //
- SQLResult = this->FinishExecute(hStatement,
- pParameter,
- &fReturnParameters);
- if (!SQL_OK(SQLResult))
- goto ERROR_LABEL;
- goto END_LABEL;
- ERROR_LABEL:
- this->TrapError(mhSQLEnv, mhSQLConn, hStatement);
- if (SQL_NULL_HSTMT != hStatement)
- {
- SQLFreeHandle(SQL_HANDLE_STMT, hStatement);
- hStatement = SQL_NULL_HSTMT;
- }
- END_LABEL:
- if (NULL != phStatement)
- *phStatement = hStatement;
- else if (SQL_NULL_HSTMT != hStatement)
- SQLFreeHandle(SQL_HANDLE_STMT, hStatement);
- return(SQLResult);
- }
- SQLRETURN CSQLWrap::BindReturnParameters(SQLHSTMT hStatement,
- SQL_PARAMETER_LIST *pParameter)
- {
- SQLRETURN SQLResult;
- DWORD dwIndex;
- SQLUSMALLINT usmiColumns;
-
- SQLResult = SQL_SUCCESS;
- usmiColumns = 0;
- for(dwIndex = 0;
- NULL != pParameter[dwIndex].ParameterValuePtr;
- dwIndex++)
- {
- if (pParameter[dwIndex].InputOutputType != SQL_PARAM_OUTPUT)
- {
- SQLResult = SQL_ERROR;
- goto END_LABEL;
- }
- SQLResult = this->BindOutputParameter(hStatement,
- &pParameter[dwIndex],
- &usmiColumns);
- if (!SQL_OK(SQLResult))
- goto END_LABEL;
- }
- END_LABEL:
- if (!SQL_OK(SQLResult))
- this->TrapError(mhSQLEnv, mhSQLConn, hStatement);
- return(SQLResult);
- }
- SQLRETURN CSQLWrap::FetchRow(SQLHSTMT hStatement)
- {
- SQLRETURN SQLResult;
- SQLResult = SQLFetch(hStatement);
- if (!SQL_OK(SQLResult))
- this->TrapError(mhSQLEnv, mhSQLConn, hStatement);
- return(SQLResult);
- }
- SQLRETURN CSQLWrap::EndStatement(SQLHSTMT hStatement)
- {
- SQLRETURN SQLResult;
- SQLResult = SQLFreeHandle(SQL_HANDLE_STMT, hStatement);
- return(SQLResult);
- }
- VOID CSQLWrap::TrapError(SQLHENV hEnv, SQLHDBC hDbc, SQLHSTMT hStmt)
- {
- SWORD swLen;
- SQLErrorW(hEnv,
- hDbc,
- hStmt,
- mwszErrorState,
- &msdwErrorCode,
- mwszErrorMessage,
- SQLWRAP_ERROR_MSG_LENGTH,
- &swLen);
- }
- VOID CSQLWrap::GetError(WCHAR * szError, DWORD cbLength)
- {
- DWORD cchLength;
- cchLength = (cbLength / sizeof(WCHAR)) - 1;
- wcsncpy(szError, mwszErrorMessage, cchLength);
- szError[cchLength] = L'\0';
- }
- VOID CSQLWrap::GetError(CHAR * szError, DWORD cbLength)
- {
- INT i;
- i = WideCharToMultiByte(CP_ACP,
- 0,
- mwszErrorMessage,
- -1,
- szError,
- cbLength,
- NULL,
- NULL);
- szError[cbLength - 1] = '\0';
- }
- INT CSQLWrap::TerminateString(CHAR * szString, INT iLength)
- {
- if (iLength <= 0)
- iLength = 0;
- szString[iLength] = '\0';
- return(iLength + 1);
- }
- INT CSQLWrap::TerminateString(WCHAR * szString, INT iLength)
- {
- if (iLength <= 0)
- iLength = 0;
- szString[iLength] = L'\0';
- return(iLength + 1);
- }
|