RETCODE SQL_API SQLGetDiagRec_Internal ( SQLSMALLINT HandleType, SQLHANDLE Handle, SQLSMALLINT RecNumber, SQLPOINTER Sqlstate, SQLINTEGER * NativeErrorPtr, SQLPOINTER MessageText, SQLSMALLINT BufferLength, SQLSMALLINT * TextLengthPtr, SQLCHAR waMode) { sqlerr_t *curr_err = NULL; HERR err = NULL; int nRecs; HPROC hproc2 = SQL_NULL_HPROC; HPROC hproc3 = SQL_NULL_HPROC; HDBC hdbc = SQL_NULL_HDBC; RETCODE retcode = SQL_SUCCESS; SQLHANDLE dhandle = SQL_NULL_HANDLE; SWORD unicode_driver = 0; SQLUINTEGER dodbc_ver = SQL_OV_ODBC3; SQLUINTEGER odbc_ver = SQL_OV_ODBC3; wchar_t _sqlState[6] = {L"\0"}; void *_MessageText = NULL; void *messageTextOut = MessageText; void *SqlstateOut = Sqlstate; if (RecNumber < 1) return SQL_ERROR; if (BufferLength < 0) return SQL_ERROR; switch (HandleType) { case SQL_HANDLE_ENV: if (!IS_VALID_HENV (Handle)) { return SQL_INVALID_HANDLE; } err = ((GENV_t *) Handle)->herr; break; case SQL_HANDLE_DBC: if (!IS_VALID_HDBC (Handle)) { return SQL_INVALID_HANDLE; } err = ((DBC_t *) Handle)->herr; dhandle = ((DBC_t *) Handle)->dhdbc; hdbc = Handle; break; case SQL_HANDLE_STMT: if (!IS_VALID_HSTMT (Handle)) { return SQL_INVALID_HANDLE; } err = ((STMT_t *) Handle)->herr; dhandle = ((STMT_t *) Handle)->dhstmt; hdbc = ((STMT_t *) Handle)->hdbc; break; case SQL_HANDLE_DESC: if (!IS_VALID_HDESC (Handle)) { return SQL_INVALID_HANDLE; } err = ((DESC_t *) Handle)->herr; dhandle = ((DESC_t *) Handle)->dhdesc; hdbc = ((DESC_t *) Handle)->hdbc; break; default: return SQL_INVALID_HANDLE; } nRecs = error_rec_count (err); if (nRecs >= RecNumber) { /* DM error range */ curr_err = get_nth_error (err, RecNumber - 1); if (!curr_err) { return (SQL_NO_DATA_FOUND); } retcode = SQL_SUCCESS; if (Sqlstate != NULL) { int len; char *ststr = (char *) _iodbcdm_getsqlstate (curr_err, (void *) sqlerrmsg_tab); if (ststr == NULL) { len = 0; } else { len = (int) STRLEN (ststr); } /* buffer size of szSqlstate is not checked. Applications * suppose provide enough ( not less than 6 bytes ) buffer * or NULL for it. */ if (waMode != 'W') { STRNCPY (Sqlstate, ststr, len); ((char*)Sqlstate)[len] = 0; } else { dm_StrCopyOut2_A2W ((SQLCHAR *) ststr, (SQLWCHAR *) Sqlstate, 6, NULL); ((wchar_t*)Sqlstate)[len] = 0; } } if (MessageText == NULL || BufferLength == 0) { if (TextLengthPtr != NULL) { *TextLengthPtr = (SWORD) 0; } } else { int len; char msgbuf[256] = { '\0' }; char *errmsg; /* get sql state message */ errmsg = _iodbcdm_getsqlerrmsg (curr_err, (void *) sqlerrmsg_tab); if (errmsg == NULL) { errmsg = (char *) ""; } #if defined(HAVE_SNPRINTF) snprintf (msgbuf, sizeof (msgbuf), "%s%s", sqlerrhd, errmsg); #else sprintf (msgbuf, "%s%s", sqlerrhd, errmsg); #endif len = STRLEN (msgbuf); if (len < BufferLength - 1) { retcode = SQL_SUCCESS; } else { len = BufferLength - 1; retcode = SQL_SUCCESS_WITH_INFO; /* and not posts error for itself */ } if (waMode != 'W') { STRNCPY ((char *) MessageText, msgbuf, len); ((char*)MessageText)[len] = 0; if (TextLengthPtr != NULL) *TextLengthPtr = (SWORD) len; } else { dm_StrCopyOut2_A2W ((SQLCHAR *) msgbuf, (SQLWCHAR *) MessageText, BufferLength, TextLengthPtr); } } return retcode; } else { /* Driver errors */ if (hdbc == SQL_NULL_HDBC) { return SQL_NO_DATA_FOUND; } RecNumber -= nRecs; if (hdbc && ((DBC_t *)hdbc)->henv) { unicode_driver = ((ENV_t *) ((DBC_t *)hdbc)->henv)->unicode_driver; dodbc_ver = ((ENV_t *) ((DBC_t *)hdbc)->henv)->dodbc_ver; } if (hdbc && ((DBC_t *)hdbc)->genv) odbc_ver = ((GENV_t *) ((DBC_t *)hdbc)->genv)->odbc_ver; if ((unicode_driver && waMode != 'W') || (!unicode_driver && waMode == 'W')) { if (waMode != 'W') { /* ansi=>unicode*/ if ((_MessageText = malloc((BufferLength + 1) * sizeof(wchar_t))) == NULL) { return SQL_ERROR; } } else { /* unicode=>ansi*/ if ((_MessageText = malloc(BufferLength + 1)) == NULL) { return SQL_ERROR; } } messageTextOut = _MessageText; SqlstateOut = _sqlState; } /* call driver */ if (unicode_driver) { /* SQL_XXX_W */ hproc2 = _iodbcdm_getproc (hdbc, en_ErrorW); #if (ODBCVER >= 0x300) hproc3 = _iodbcdm_getproc (hdbc, en_GetDiagRecW); #endif } else { /* SQL_XXX */ /* SQL_XXX_A */ hproc2 = _iodbcdm_getproc (hdbc, en_Error); if (hproc2 == SQL_NULL_HPROC) hproc2 = _iodbcdm_getproc (hdbc, en_ErrorA); #if (ODBCVER >= 0x300) hproc3 = _iodbcdm_getproc (hdbc, en_GetDiagRec); if (hproc3 == SQL_NULL_HPROC) hproc3 = _iodbcdm_getproc (hdbc, en_GetDiagRecA); #endif } if (odbc_ver == SQL_OV_ODBC2 && ( dodbc_ver == SQL_OV_ODBC2 || (dodbc_ver == SQL_OV_ODBC3 && hproc2 != SQL_NULL_HPROC))) hproc3 = SQL_NULL_HPROC; #if (ODBCVER >= 0x300) if (hproc3 != SQL_NULL_HPROC) { CALL_DRIVER (hdbc, Handle, retcode, hproc3, ( HandleType, dhandle, RecNumber, SqlstateOut, NativeErrorPtr, messageTextOut, BufferLength, TextLengthPtr)); } else #endif { if (hproc2 == SQL_NULL_HPROC) { MEM_FREE(_MessageText); return SQL_ERROR; } if (RecNumber > 1 || HandleType == SQL_HANDLE_DESC) { MEM_FREE(_MessageText); return SQL_NO_DATA_FOUND; } CALL_DRIVER (hdbc, Handle, retcode, hproc2, ( SQL_NULL_HENV, HandleType == SQL_HANDLE_DBC ? dhandle : SQL_NULL_HDBC, HandleType == SQL_HANDLE_STMT ? dhandle : SQL_NULL_HSTMT, SqlstateOut, NativeErrorPtr, messageTextOut, BufferLength, TextLengthPtr)); } if (MessageText && SQL_SUCCEEDED (retcode) && ((unicode_driver && waMode != 'W') || (!unicode_driver && waMode == 'W'))) { if (waMode != 'W') { /* ansi<=unicode*/ dm_StrCopyOut2_W2A ((SQLWCHAR *)messageTextOut, (SQLCHAR *)MessageText, BufferLength, NULL); dm_StrCopyOut2_W2A ((SQLWCHAR *)SqlstateOut, (SQLCHAR *)Sqlstate, 6, NULL); } else { /* unicode<=ansi*/ dm_StrCopyOut2_A2W ((SQLCHAR *) messageTextOut, (SQLWCHAR *) MessageText, BufferLength, NULL); dm_StrCopyOut2_A2W ((SQLCHAR *) SqlstateOut, (SQLWCHAR *) Sqlstate, 6, NULL); } } MEM_FREE(_MessageText); return retcode; } }
RETCODE SQL_API SQLGetDiagField_Internal ( SQLSMALLINT nHandleType, SQLHANDLE Handle, SQLSMALLINT nRecNumber, SQLSMALLINT nDiagIdentifier, SQLPOINTER pDiagInfoPtr, SQLSMALLINT nBufferLength, SQLSMALLINT * pnStringLengthPtr, SQLCHAR waMode) { GENV (genv, Handle); CONN (con, Handle); STMT (stmt, Handle); DESC (desc, Handle); HERR err; HPROC hproc = SQL_NULL_HPROC; RETCODE retcode = SQL_SUCCESS; SQLHANDLE dhandle = SQL_NULL_HANDLE; SWORD unicode_driver = 0; void *_DiagInfoPtr = NULL; void *diagInfoPtr = pDiagInfoPtr; switch (nHandleType) { case SQL_HANDLE_ENV: if (!IS_VALID_HENV (Handle)) { return SQL_INVALID_HANDLE; } err = genv->herr; con = NULL; stmt = NULL; desc = NULL; break; case SQL_HANDLE_DBC: if (!IS_VALID_HDBC (Handle)) { return SQL_INVALID_HANDLE; } err = con->herr; genv = (GENV_t *) con->genv; stmt = NULL; desc = NULL; dhandle = con->dhdbc; break; case SQL_HANDLE_STMT: if (!IS_VALID_HSTMT (Handle)) { return SQL_INVALID_HANDLE; } err = stmt->herr; con = (DBC_t *) stmt->hdbc; genv = (GENV_t *) con->genv; desc = NULL; dhandle = stmt->dhstmt; break; case SQL_HANDLE_DESC: if (!IS_VALID_HDESC (Handle)) { return SQL_INVALID_HANDLE; } err = desc->herr; stmt = (STMT_t *) desc->hstmt; con = (DBC_t *) desc->hdbc; genv = (GENV_t *) con->genv; dhandle = desc->dhdesc; break; default: return SQL_INVALID_HANDLE; } if (con != NULL && con->henv != SQL_NULL_HENV) unicode_driver = ((ENV_t *) con->henv)->unicode_driver; switch (nRecNumber) { case 0: /* Header record */ switch (nDiagIdentifier) { case SQL_DIAG_ROW_COUNT: { if (nHandleType != SQL_HANDLE_STMT || !stmt) { return SQL_ERROR; } if (stmt->state != en_stmt_executed_with_info && stmt->state != en_stmt_executed && stmt->state != en_stmt_cursoropen) { return SQL_ERROR; } if (!con) { return SQL_INVALID_HANDLE; } CALL_UDRIVER(con, stmt, retcode, hproc, unicode_driver, en_GetDiagField, (SQL_HANDLE_DBC, stmt->dhstmt, nRecNumber, nDiagIdentifier, pDiagInfoPtr, nBufferLength, pnStringLengthPtr )); if (hproc == SQL_NULL_HPROC) { if (!con) { return SQL_INVALID_HANDLE; } hproc = _iodbcdm_getproc (con, en_RowCount); if (!hproc) { return SQL_ERROR; } CALL_DRIVER (stmt->hdbc, stmt, retcode, hproc, (stmt->dhstmt, pDiagInfoPtr)); } return retcode; } case SQL_DIAG_CURSOR_ROW_COUNT: case SQL_DIAG_DYNAMIC_FUNCTION: case SQL_DIAG_DYNAMIC_FUNCTION_CODE: { if (nHandleType != SQL_HANDLE_STMT || !stmt) { return SQL_ERROR; } if (stmt->state != en_stmt_executed_with_info && stmt->state != en_stmt_executed && stmt->state != en_stmt_cursoropen) { return SQL_ERROR; } if (!con) { return SQL_INVALID_HANDLE; } CALL_UDRIVER(con, stmt, retcode, hproc, unicode_driver, en_GetDiagField, (SQL_HANDLE_DBC, stmt->dhstmt, nRecNumber, nDiagIdentifier, pDiagInfoPtr, nBufferLength, pnStringLengthPtr )); if (hproc == SQL_NULL_HPROC) return SQL_ERROR; else return retcode; } case SQL_DIAG_RETURNCODE: if (pDiagInfoPtr) *((SQLRETURN *) pDiagInfoPtr) = ((GENV_t *) Handle)->rc; { return SQL_SUCCESS; } case SQL_DIAG_NUMBER: if (pDiagInfoPtr) { (*(SQLINTEGER *) pDiagInfoPtr) = 0; /* get the number from the driver */ if (con) { CALL_UDRIVER(con, Handle, retcode, hproc, unicode_driver, en_GetDiagField, (nHandleType, dhandle, 0, nDiagIdentifier, pDiagInfoPtr, nBufferLength, pnStringLengthPtr )); if (hproc != SQL_NULL_HPROC) { if (retcode != SQL_SUCCESS) { return retcode; } /* and add the DM's value */ (*(SQLINTEGER *) pDiagInfoPtr) += error_rec_count (err); } else if (((ENV_t *) con->henv)->dodbc_ver == SQL_OV_ODBC2 && ((GENV_t *) Handle)->rc) { /* ODBC2 drivers can only have one error */ (*(SQLINTEGER *) pDiagInfoPtr) = 1; } } else if (genv) { (*(SQLINTEGER *) pDiagInfoPtr) = error_rec_count (err); } } break; default: return SQL_ERROR; } break; default: /* status records */ { int nRecs = 0; if (nRecNumber < 1) { return SQL_ERROR; } nRecs = error_rec_count (err); if (nRecNumber <= nRecs) { /* DM Errors */ char *szval = ""; int ival = 0; int isInt = 0; sqlerr_t *rec = NULL; rec = get_nth_error (err, nRecNumber - 1); if (!rec) { return (SQL_NO_DATA_FOUND); } switch (nDiagIdentifier) { case SQL_DIAG_SUBCLASS_ORIGIN: case SQL_DIAG_CLASS_ORIGIN: isInt = 0; szval = (rec->code >= en_HY001 && rec->code <= en_IM014) ? (char *) "ODBC 3.0" : (char *) "ISO 9075"; break; case SQL_DIAG_COLUMN_NUMBER: if (nHandleType != SQL_HANDLE_STMT || !stmt) { return SQL_ERROR; } if (!con) { return SQL_INVALID_HANDLE; } if (pDiagInfoPtr) *((SQLINTEGER *) pDiagInfoPtr) = SQL_COLUMN_NUMBER_UNKNOWN; return SQL_SUCCESS; case SQL_DIAG_CONNECTION_NAME: case SQL_DIAG_SERVER_NAME: isInt = 0; if (con) { if (waMode != 'W') retcode = SQLGetInfo (con, SQL_DATA_SOURCE_NAME, pDiagInfoPtr, nBufferLength, pnStringLengthPtr); else retcode = SQLGetInfoW (con, SQL_DATA_SOURCE_NAME, pDiagInfoPtr, nBufferLength, pnStringLengthPtr); return retcode; } else break; case SQL_DIAG_MESSAGE_TEXT: isInt = 0; szval = _iodbcdm_getsqlerrmsg (rec, (void *) sqlerrmsg_tab); break; case SQL_DIAG_NATIVE: isInt = 1; ival = 0; break; case SQL_DIAG_ROW_NUMBER: isInt = 1; if (nHandleType != SQL_HANDLE_STMT || !stmt) { return SQL_ERROR; } if (!con) { return SQL_INVALID_HANDLE; } CALL_UDRIVER(con, Handle, retcode, hproc, unicode_driver, en_GetDiagField, (nHandleType, dhandle, nRecNumber, nDiagIdentifier, pDiagInfoPtr, nBufferLength, pnStringLengthPtr )); if (hproc != SQL_NULL_HPROC) { return retcode; } else { ival = SQL_ROW_NUMBER_UNKNOWN; break; } case SQL_DIAG_SQLSTATE: isInt = 0; szval = _iodbcdm_getsqlstate (rec, (void *) sqlerrmsg_tab); break; default: return SQL_ERROR; } if (isInt) { if (pDiagInfoPtr) *((SQLINTEGER *) pDiagInfoPtr) = ival; } else { if (waMode != 'W') { int len = strlen (szval), len1; len1 = len > nBufferLength ? nBufferLength : len; if (pnStringLengthPtr) *pnStringLengthPtr = len; if (pDiagInfoPtr) { STRNCPY (pDiagInfoPtr, szval, len1); *(((SQLCHAR *) pDiagInfoPtr) + len1) = 0; } } else { dm_StrCopyOut2_A2W((SQLCHAR *) szval, (SQLWCHAR *) pDiagInfoPtr, nBufferLength, pnStringLengthPtr); } } break; } else { /* Driver's errors */ nRecNumber -= nRecs; if (!con) { return SQL_NO_DATA_FOUND; } if ((unicode_driver && waMode != 'W') || (!unicode_driver && waMode == 'W')) { switch(nDiagIdentifier) { case SQL_DIAG_DYNAMIC_FUNCTION: case SQL_DIAG_CLASS_ORIGIN: case SQL_DIAG_CONNECTION_NAME: case SQL_DIAG_MESSAGE_TEXT: case SQL_DIAG_SERVER_NAME: case SQL_DIAG_SQLSTATE: case SQL_DIAG_SUBCLASS_ORIGIN: if (waMode != 'W') { /* ansi=>unicode*/ if ((_DiagInfoPtr = malloc((nBufferLength + 1) * sizeof(wchar_t))) == NULL) { return SQL_ERROR; } } else { /* unicode=>ansi*/ if ((_DiagInfoPtr = malloc(nBufferLength + 1)) == NULL) { return SQL_ERROR; } } diagInfoPtr = _DiagInfoPtr; break; } } CALL_UDRIVER(con, Handle, retcode, hproc, unicode_driver, en_GetDiagField, (nHandleType, dhandle, nRecNumber, nDiagIdentifier, diagInfoPtr, nBufferLength, pnStringLengthPtr )); if (hproc != SQL_NULL_HPROC) { if (pDiagInfoPtr && SQL_SUCCEEDED (retcode) && ((unicode_driver && waMode != 'W') || (!unicode_driver && waMode == 'W'))) { switch(nDiagIdentifier) { case SQL_DIAG_DYNAMIC_FUNCTION: case SQL_DIAG_CLASS_ORIGIN: case SQL_DIAG_CONNECTION_NAME: case SQL_DIAG_MESSAGE_TEXT: case SQL_DIAG_SERVER_NAME: case SQL_DIAG_SQLSTATE: case SQL_DIAG_SUBCLASS_ORIGIN: if (waMode != 'W') { /* ansi<=unicode*/ dm_StrCopyOut2_W2A ((SQLWCHAR *) diagInfoPtr, (SQLCHAR *) pDiagInfoPtr, nBufferLength, pnStringLengthPtr); } else { /* unicode<=ansi*/ dm_StrCopyOut2_A2W ((SQLCHAR *)diagInfoPtr, (SQLWCHAR *) pDiagInfoPtr, nBufferLength, pnStringLengthPtr); } } } MEM_FREE(_DiagInfoPtr); return retcode; } else { /* an ODBC2->ODBC3 translation */ char *szval = ""; wchar_t szState[6]; SQLINTEGER nNative; if (nRecNumber > 1) { MEM_FREE(_DiagInfoPtr); return SQL_NO_DATA_FOUND; } if (nHandleType == SQL_HANDLE_DESC) { MEM_FREE(_DiagInfoPtr); return SQL_INVALID_HANDLE; } if (nDiagIdentifier != SQL_DIAG_MESSAGE_TEXT) MEM_FREE(_DiagInfoPtr); switch (nDiagIdentifier) { case SQL_DIAG_SUBCLASS_ORIGIN: case SQL_DIAG_CLASS_ORIGIN: CALL_UDRIVER (con, Handle, retcode, hproc, unicode_driver, en_Error, (SQL_NULL_HENV, nHandleType == SQL_HANDLE_DBC ? dhandle : SQL_NULL_HDBC, nHandleType == SQL_HANDLE_STMT ? dhandle : SQL_NULL_HSTMT, szState, &nNative, NULL, 0, NULL)); if (hproc == SQL_NULL_HPROC) { return SQL_INVALID_HANDLE; } if (retcode != SQL_SUCCESS) { return SQL_NO_DATA_FOUND; } if (waMode != 'W') { szval = !STRNEQ (szState, "IM", 2) ? (char *) "ODBC 3.0" : (char *) "ISO 9075"; } else { if (szState[0] != L'I' && szState[1] != L'M') szval = (char *) "ODBC 3.0"; else szval = (char *) "ISO 9075"; } break; case SQL_DIAG_ROW_NUMBER: case SQL_DIAG_COLUMN_NUMBER: if (nHandleType != SQL_HANDLE_STMT || !stmt) { return SQL_ERROR; } if (!con) { return SQL_INVALID_HANDLE; } if (pDiagInfoPtr) *((SQLINTEGER *) pDiagInfoPtr) = SQL_COLUMN_NUMBER_UNKNOWN; { return SQL_SUCCESS; } case SQL_DIAG_SERVER_NAME: case SQL_DIAG_CONNECTION_NAME: break; case SQL_DIAG_MESSAGE_TEXT: CALL_UDRIVER (con, Handle, retcode, hproc, unicode_driver, en_Error, (SQL_NULL_HENV, nHandleType == SQL_HANDLE_DBC ? dhandle : SQL_NULL_HDBC, nHandleType == SQL_HANDLE_STMT ? dhandle : SQL_NULL_HSTMT, szState, &nNative, diagInfoPtr, nBufferLength, pnStringLengthPtr)); if (hproc == SQL_NULL_HPROC) { MEM_FREE(_DiagInfoPtr); return SQL_INVALID_HANDLE; } if (pDiagInfoPtr && SQL_SUCCEEDED (retcode) && ((unicode_driver && waMode != 'W') || (!unicode_driver && waMode == 'W'))) { if (waMode != 'W') { /* ansi<=unicode*/ dm_StrCopyOut2_W2A ((SQLWCHAR *) diagInfoPtr, (SQLCHAR *) pDiagInfoPtr, nBufferLength, pnStringLengthPtr); } else { /* unicode<=ansi*/ dm_StrCopyOut2_A2W ((SQLCHAR *)diagInfoPtr, (SQLWCHAR *) pDiagInfoPtr, nBufferLength, pnStringLengthPtr); } } MEM_FREE(_DiagInfoPtr); return retcode; case SQL_DIAG_NATIVE: CALL_UDRIVER (con, Handle, retcode, hproc, unicode_driver, en_Error, (SQL_NULL_HENV, nHandleType == SQL_HANDLE_DBC ? dhandle : SQL_NULL_HDBC, nHandleType == SQL_HANDLE_STMT ? dhandle : SQL_NULL_HSTMT, szState, &nNative, NULL, 0, NULL)); if (hproc == SQL_NULL_HPROC) { return SQL_INVALID_HANDLE; } if (pDiagInfoPtr) *((SQLINTEGER *) pDiagInfoPtr) = nNative; return retcode; case SQL_DIAG_SQLSTATE: CALL_UDRIVER (con, Handle, retcode, hproc, unicode_driver, en_Error, (SQL_NULL_HENV, nHandleType == SQL_HANDLE_DBC ? dhandle : SQL_NULL_HDBC, nHandleType == SQL_HANDLE_STMT ? dhandle : SQL_NULL_HSTMT, szState, &nNative, NULL, 0, NULL)); if (hproc == SQL_NULL_HPROC) { return SQL_INVALID_HANDLE; } if (pDiagInfoPtr && SQL_SUCCEEDED (retcode) && ((unicode_driver && waMode != 'W') || (!unicode_driver && waMode == 'W'))) { if (waMode != 'W') { /* ansi<=unicode*/ dm_StrCopyOut2_W2A ((SQLWCHAR *) szState, (SQLCHAR *) pDiagInfoPtr, nBufferLength, pnStringLengthPtr); } else { /* unicode<=ansi*/ dm_StrCopyOut2_A2W ((SQLCHAR *)szState, (SQLWCHAR *) pDiagInfoPtr, nBufferLength, pnStringLengthPtr); } } return retcode; default: return SQL_ERROR; } if (waMode != 'W') { if (pDiagInfoPtr) { int len = strlen (szval); if (len > nBufferLength) len = nBufferLength; if (len) _iodbcdm_strlcpy ((char *) pDiagInfoPtr, szval, len); } if (pnStringLengthPtr) *pnStringLengthPtr = strlen (szval); } else { dm_StrCopyOut2_A2W((SQLCHAR *) szval, (SQLWCHAR *) pDiagInfoPtr, nBufferLength, pnStringLengthPtr); } } /* ODBC3->ODBC2 */ } /* driver's errors */ } /* status records */ } /* switch (nRecNumber */ return (SQL_SUCCESS); }
SQLRETURN SQL_API _iodbcdm_sqlerror ( SQLHENV henv, SQLHDBC hdbc, SQLHSTMT hstmt, SQLPOINTER szSqlstate, SQLINTEGER * pfNativeError, SQLPOINTER szErrorMsg, SQLSMALLINT cbErrorMsgMax, SQLSMALLINT * pcbErrorMsg, int bDelete, SQLCHAR waMode) { GENV (genv, henv); CONN (pdbc, hdbc); STMT (pstmt, hstmt); HDBC thdbc = SQL_NULL_HDBC; HENV dhenv = SQL_NULL_HENV; HDBC dhdbc = SQL_NULL_HDBC; HSTMT dhstmt = SQL_NULL_HSTMT; HERR herr = SQL_NULL_HERR; HPROC hproc2 = SQL_NULL_HPROC; HPROC hproc3 = SQL_NULL_HPROC; #if (ODBCVER >= 0x0300) SQLINTEGER handleType = 0; SQLHANDLE handle3; SQLHANDLE dhandle3 = 0; SQLSMALLINT *perr_rec = NULL; #endif SWORD unicode_driver = 0; SQLUINTEGER dodbc_ver = SQL_OV_ODBC2; SQLUINTEGER odbc_ver = SQL_OV_ODBC2; wchar_t _sqlState[6] = {L"\0"}; void *SqlstateOut = szSqlstate; void *_ErrorMsg = NULL; void *errorMsgOut = szErrorMsg; void *errmsg = NULL; void *ststr = NULL; int handle = 0; SQLRETURN retcode = SQL_SUCCESS; if (IS_VALID_HSTMT (hstmt)) /* retrieve stmt err */ { herr = pstmt->herr; thdbc = pstmt->hdbc; if (thdbc == SQL_NULL_HDBC) { return SQL_INVALID_HANDLE; } #if (ODBCVER >= 0x0300) handleType = SQL_HANDLE_STMT; handle3 = hstmt; dhandle3 = pstmt->dhstmt; perr_rec = &((STMT_t *)pstmt)->err_rec; #endif dhstmt = pstmt->dhstmt; handle = 3; } else if (IS_VALID_HDBC (hdbc)) /* retrieve dbc err */ { herr = pdbc->herr; thdbc = pdbc; if (thdbc == SQL_NULL_HDBC) { return SQL_INVALID_HANDLE; } #if (ODBCVER >= 0x0300) handleType = SQL_HANDLE_DBC; handle3 = hdbc; dhandle3 = pdbc->dhdbc; perr_rec = &((DBC_t *)pdbc)->err_rec; #endif dhdbc = pdbc->dhdbc; handle = 2; if (herr == SQL_NULL_HERR && pdbc->henv == SQL_NULL_HENV) { return SQL_NO_DATA_FOUND; } } else if (IS_VALID_HENV (henv)) /* retrieve env err */ { herr = genv->herr; /* Drivers shouldn't push error message * on environment handle */ if (herr == SQL_NULL_HERR) { return SQL_NO_DATA_FOUND; } handle = 1; } else { return SQL_INVALID_HANDLE; } if (szErrorMsg != NULL) { if (cbErrorMsgMax < 0) { return SQL_ERROR; /* SQLError() doesn't post error for itself */ } } if (herr == SQL_NULL_HERR) /* no err on drv mng */ { if (thdbc && ((DBC_t *)thdbc)->genv) odbc_ver = ((GENV_t *) ((DBC_t *)thdbc)->genv)->odbc_ver; if (thdbc && ((DBC_t *)thdbc)->henv) { unicode_driver = ((ENV_t *) ((DBC_t *)thdbc)->henv)->unicode_driver; dodbc_ver = ((ENV_t *) ((DBC_t *)thdbc)->henv)->dodbc_ver; } /* call driver */ if ((unicode_driver && waMode != 'W') || (!unicode_driver && waMode == 'W')) { if (waMode != 'W') { /* ansi=>unicode*/ if ((_ErrorMsg = malloc(cbErrorMsgMax * sizeof(wchar_t) + 1)) == NULL) return SQL_ERROR; } else { /* unicode=>ansi*/ if ((_ErrorMsg = malloc(cbErrorMsgMax + 1)) == NULL) return SQL_ERROR; } errorMsgOut = _ErrorMsg; SqlstateOut = _sqlState; } /* call driver */ if (unicode_driver) { /* SQL_XXX_W */ hproc2 = _iodbcdm_getproc (thdbc, en_ErrorW); #if (ODBCVER >= 0x300) hproc3 = _iodbcdm_getproc (thdbc, en_GetDiagRecW); #endif } else { /* SQL_XXX */ /* SQL_XXX_A */ hproc2 = _iodbcdm_getproc (thdbc, en_Error); if (hproc2 == SQL_NULL_HPROC) hproc2 = _iodbcdm_getproc (thdbc, en_ErrorA); #if (ODBCVER >= 0x300) hproc3 = _iodbcdm_getproc (thdbc, en_GetDiagRec); if (hproc3 == SQL_NULL_HPROC) hproc3 = _iodbcdm_getproc (thdbc, en_GetDiagRecA); #endif } if (odbc_ver == SQL_OV_ODBC2 && ( dodbc_ver == SQL_OV_ODBC2 || (dodbc_ver == SQL_OV_ODBC3 && hproc2 != SQL_NULL_HPROC))) hproc3 = SQL_NULL_HPROC; #if (ODBCVER >= 0x300) if (hproc3 != SQL_NULL_HPROC) { (*perr_rec) = (*perr_rec) + 1; CALL_DRIVER (thdbc, NULL, retcode, hproc3, ( handleType, dhandle3, (*perr_rec), SqlstateOut, pfNativeError, errorMsgOut, cbErrorMsgMax, pcbErrorMsg)); } else #endif { if (hproc2 == SQL_NULL_HPROC) { MEM_FREE(_ErrorMsg); return SQL_NO_DATA_FOUND; } CALL_DRIVER (thdbc, NULL, retcode, hproc2, ( dhenv, dhdbc, dhstmt, SqlstateOut, pfNativeError, errorMsgOut, cbErrorMsgMax, pcbErrorMsg)); } if (szErrorMsg && SQL_SUCCEEDED (retcode) && ((unicode_driver && waMode != 'W') || (!unicode_driver && waMode == 'W'))) { if (waMode != 'W') { /* ansi<=unicode*/ dm_StrCopyOut2_W2A ((SQLWCHAR *)errorMsgOut, (SQLCHAR *)szErrorMsg, cbErrorMsgMax, NULL); dm_StrCopyOut2_W2A ((SQLWCHAR *)SqlstateOut, (SQLCHAR *)szSqlstate, 6, NULL); } else { /* unicode<=ansi*/ dm_StrCopyOut2_A2W ((SQLCHAR *)errorMsgOut, (SQLWCHAR *)szErrorMsg, cbErrorMsgMax, NULL); dm_StrCopyOut2_A2W ((SQLCHAR *)SqlstateOut, (SQLWCHAR *)szSqlstate, 6, NULL); } } MEM_FREE(_ErrorMsg); return retcode; } if (szSqlstate != NULL) { int len; /* get sql state string */ ststr = (char *) _iodbcdm_getsqlstate (herr, (void *) sqlerrmsg_tab); if (ststr == NULL) { len = 0; } else { len = (int) STRLEN (ststr); } /* buffer size of szSqlstate is not checked. Applications * suppose provide enough ( not less than 6 bytes ) buffer * or NULL for it. */ if (waMode != 'W') { STRNCPY (szSqlstate, ststr, len); ((char*)szSqlstate)[len] = 0; } else { dm_StrCopyOut2_A2W ((SQLCHAR *)ststr, (SQLWCHAR *)szSqlstate, 6, NULL); ((wchar_t*)szSqlstate)[len] = 0; } } if (pfNativeError != NULL) { /* native error code is specific to data source */ *pfNativeError = (SDWORD) 0L; } if (szErrorMsg == NULL || cbErrorMsgMax == 0) { if (pcbErrorMsg != NULL) { *pcbErrorMsg = (SWORD) 0; } } else { int len; char msgbuf[256] = {'\0'}; /* get sql state message */ errmsg = _iodbcdm_getsqlerrmsg (herr, (void *) sqlerrmsg_tab); if (errmsg == NULL) { errmsg = (char *) ""; } #if defined(HAVE_SNPRINTF) snprintf (msgbuf, sizeof(msgbuf), "%s%s", sqlerrhd, (char*)errmsg); #else sprintf (msgbuf, "%s%s", sqlerrhd, (char*)errmsg); #endif len = STRLEN (msgbuf); if (len < cbErrorMsgMax - 1) { retcode = SQL_SUCCESS; } else { len = cbErrorMsgMax - 1; retcode = SQL_SUCCESS_WITH_INFO; /* and not posts error for itself */ } if (waMode != 'W') { STRNCPY ((char *) szErrorMsg, msgbuf, len); ((char*)szErrorMsg)[len] = 0; if (pcbErrorMsg != NULL) *pcbErrorMsg = (SWORD) len; } else { dm_StrCopyOut2_A2W ((SQLCHAR *) msgbuf, (SQLWCHAR *) szErrorMsg, cbErrorMsgMax, pcbErrorMsg); } } if (bDelete) switch (handle) /* free this err */ { case 1: genv->herr = _iodbcdm_popsqlerr (genv->herr); break; case 2: pdbc->herr = _iodbcdm_popsqlerr (pdbc->herr); break; case 3: pstmt->herr = _iodbcdm_popsqlerr (pstmt->herr); break; default: break; } return retcode; }
SQLRETURN SQL_API SQLGetInfo_Internal ( SQLHDBC hdbc, SQLUSMALLINT fInfoType, SQLPOINTER rgbInfoValue, SQLSMALLINT cbInfoValueMax, SQLSMALLINT * pcbInfoValue, SQLCHAR waMode) { CONN (pdbc, hdbc); ENVR (penv, pdbc->henv); STMT (pstmt, NULL); STMT (tpstmt, NULL); HPROC hproc = SQL_NULL_HPROC; SQLRETURN retcode = SQL_SUCCESS; void * _InfoValue = NULL; void * infoValueOut = rgbInfoValue; DWORD dword = 0; int size = 0, len = 0, ret = 0; wchar_t buf[20] = {'\0'}; if (cbInfoValueMax < 0) { PUSHSQLERR (pdbc->herr, en_S1090); return SQL_ERROR; } #if (ODBCVER < 0x0300) if ( /* fInfoType < SQL_INFO_FIRST || */ (fInfoType > SQL_INFO_LAST && fInfoType < SQL_INFO_DRIVER_START)) { PUSHSQLERR (pdbc->herr, en_S1096); return SQL_ERROR; } #endif if (fInfoType == SQL_ODBC_VER #if (ODBCVER >= 0x0300) || fInfoType == SQL_DM_VER #endif ) { #if (ODBCVER >= 0x0300) if (fInfoType == SQL_DM_VER) sprintf ((char*)buf, "%02d.%02d.%04d.%04d", SQL_SPEC_MAJOR, SQL_SPEC_MINOR, IODBC_BUILD / 10000, IODBC_BUILD % 10000); else #endif sprintf ((char*)buf, "%02d.%02d.0000", SQL_SPEC_MAJOR, SQL_SPEC_MINOR); if(waMode == 'W') { SQLWCHAR *prov = dm_SQL_U8toW((SQLCHAR *)buf, SQL_NTS); if(prov) { WCSNCPY(buf, prov, sizeof(buf)/sizeof(wchar_t)); free(prov); } else buf[0] = L'\0'; } if (rgbInfoValue != NULL && cbInfoValueMax > 0) { len = (waMode != 'W' ? STRLEN (buf) : WCSLEN(buf)); if (len > cbInfoValueMax - 1) { len = cbInfoValueMax - 1; PUSHSQLERR (pdbc->herr, en_01004); retcode = SQL_SUCCESS_WITH_INFO; } if (waMode != 'W') { STRNCPY (rgbInfoValue, buf, len); ((char *) rgbInfoValue)[len] = '\0'; } else { WCSNCPY (rgbInfoValue, buf, len); ((wchar_t *) rgbInfoValue)[len] = L'\0'; } } if (pcbInfoValue != NULL) { *pcbInfoValue = (SWORD) len; } return retcode; } if (pdbc->state == en_dbc_allocated || pdbc->state == en_dbc_needdata) { PUSHSQLERR (pdbc->herr, en_08003); return SQL_ERROR; } switch (fInfoType) { case SQL_DRIVER_HDBC: dword = (DWORD) (pdbc->dhdbc); size = sizeof (dword); break; case SQL_DRIVER_HENV: penv = (ENV_t *) (pdbc->henv); dword = (DWORD) (penv->dhenv); size = sizeof (dword); break; case SQL_DRIVER_HLIB: penv = (ENV_t *) (pdbc->henv); dword = (DWORD) (penv->hdll); size = sizeof (dword); break; case SQL_DRIVER_HSTMT: if (rgbInfoValue != NULL) { pstmt = *((STMT_t **) rgbInfoValue); } for (tpstmt = (STMT_t *) (pdbc->hstmt); tpstmt != NULL; tpstmt = tpstmt->next) { if (tpstmt == pstmt) { break; } } if (tpstmt == NULL) { PUSHSQLERR (pdbc->herr, en_S1009); return SQL_ERROR; } dword = (DWORD) (pstmt->dhstmt); size = sizeof (dword); break; case SQL_DRIVER_NAME: case SQL_DRIVER_ODBC_VER: case SQL_DRIVER_VER: case SQL_ODBC_INTERFACE_CONFORMANCE: break; default: /* NOTE : this was before the switch, just move here to let some informations going through */ if (pdbc->state == en_dbc_allocated || pdbc->state == en_dbc_needdata) { PUSHSQLERR (pdbc->herr, en_08003); return SQL_ERROR; } } if (size) { if (rgbInfoValue != NULL) { *((DWORD *) rgbInfoValue) = dword; } if (pcbInfoValue != NULL) { *(pcbInfoValue) = (SWORD) size; } return SQL_SUCCESS; } #if (ODBCVER >= 0x0300) /* * This was a temp value in ODBC 2 */ if (((ENV_t *) pdbc->henv)->dodbc_ver == SQL_OV_ODBC2 && fInfoType == SQL_OJ_CAPABILITIES) fInfoType = 65003; #endif /* ODBCVER >= 0x0300 */ if ((penv->unicode_driver && waMode != 'W') || (!penv->unicode_driver && waMode == 'W')) { switch(fInfoType) { case SQL_ACCESSIBLE_PROCEDURES: case SQL_ACCESSIBLE_TABLES: case SQL_CATALOG_NAME: case SQL_CATALOG_NAME_SEPARATOR: case SQL_CATALOG_TERM: case SQL_COLLATION_SEQ: case SQL_COLUMN_ALIAS: case SQL_DATA_SOURCE_NAME: case SQL_DATA_SOURCE_READ_ONLY: case SQL_DATABASE_NAME: case SQL_DBMS_NAME: case SQL_DBMS_VER: case SQL_DESCRIBE_PARAMETER: case SQL_DRIVER_NAME: case SQL_DRIVER_ODBC_VER: case SQL_DRIVER_VER: case SQL_ODBC_VER: case SQL_EXPRESSIONS_IN_ORDERBY: case SQL_IDENTIFIER_QUOTE_CHAR: case SQL_INTEGRITY: case SQL_KEYWORDS: case SQL_LIKE_ESCAPE_CLAUSE: case SQL_MAX_ROW_SIZE_INCLUDES_LONG: case SQL_MULT_RESULT_SETS: case SQL_MULTIPLE_ACTIVE_TXN: case SQL_NEED_LONG_DATA_LEN: case SQL_ORDER_BY_COLUMNS_IN_SELECT: case SQL_PROCEDURE_TERM: case SQL_PROCEDURES: case SQL_ROW_UPDATES: case SQL_SCHEMA_TERM: case SQL_SEARCH_PATTERN_ESCAPE: case SQL_SERVER_NAME: case SQL_SPECIAL_CHARACTERS: case SQL_TABLE_TERM: case SQL_USER_NAME: case SQL_XOPEN_CLI_YEAR: case SQL_OUTER_JOINS: if (waMode != 'W') { /* ansi=>unicode*/ if ((_InfoValue = malloc(cbInfoValueMax * sizeof(wchar_t) + 1)) == NULL) { PUSHSQLERR (pdbc->herr, en_HY001); return SQL_ERROR; } cbInfoValueMax *= sizeof(wchar_t); } else { /* unicode=>ansi*/ if ((_InfoValue = malloc(cbInfoValueMax + 1)) == NULL) { PUSHSQLERR (pdbc->herr, en_HY001); return SQL_ERROR; } cbInfoValueMax /= sizeof(wchar_t); } infoValueOut = _InfoValue; break; } } CALL_UDRIVER(hdbc, pdbc, retcode, hproc, penv->unicode_driver, en_GetInfo, (pdbc->dhdbc, fInfoType, infoValueOut, cbInfoValueMax, pcbInfoValue)); if (hproc == SQL_NULL_HPROC) { PUSHSQLERR (pdbc->herr, en_IM001); return SQL_ERROR; } if (retcode == SQL_ERROR && fInfoType == SQL_DRIVER_ODBC_VER) { if (waMode != 'W') { STRCPY (buf, "01.00"); if (rgbInfoValue != NULL && cbInfoValueMax > 0) { len = STRLEN (buf); if (len > cbInfoValueMax - 1) { len = cbInfoValueMax - 1; ret = -1; } else { ret = 0; } STRNCPY (rgbInfoValue, buf, len); ((char *) rgbInfoValue)[len] = '\0'; } if (pcbInfoValue != NULL) *pcbInfoValue = (SWORD) len; } else { ret = dm_StrCopyOut2_A2W ((SQLCHAR *) "01.00", (SQLWCHAR *) rgbInfoValue, cbInfoValueMax / sizeof(wchar_t), pcbInfoValue); if (pcbInfoValue) *pcbInfoValue = *pcbInfoValue * sizeof(wchar_t); } if (ret == -1) { PUSHSQLERR (pdbc->herr, en_01004); retcode = SQL_SUCCESS_WITH_INFO; } else { retcode = SQL_SUCCESS; } } else if (rgbInfoValue && (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) && ((penv->unicode_driver && waMode != 'W') || (!penv->unicode_driver && waMode == 'W'))) { switch(fInfoType) { case SQL_ACCESSIBLE_PROCEDURES: case SQL_ACCESSIBLE_TABLES: case SQL_CATALOG_NAME: case SQL_CATALOG_NAME_SEPARATOR: case SQL_CATALOG_TERM: case SQL_COLLATION_SEQ: case SQL_COLUMN_ALIAS: case SQL_DATA_SOURCE_NAME: case SQL_DATA_SOURCE_READ_ONLY: case SQL_DATABASE_NAME: case SQL_DBMS_NAME: case SQL_DBMS_VER: case SQL_DESCRIBE_PARAMETER: case SQL_DRIVER_NAME: case SQL_DRIVER_ODBC_VER: case SQL_DRIVER_VER: case SQL_ODBC_VER: case SQL_EXPRESSIONS_IN_ORDERBY: case SQL_IDENTIFIER_QUOTE_CHAR: case SQL_INTEGRITY: case SQL_KEYWORDS: case SQL_LIKE_ESCAPE_CLAUSE: case SQL_MAX_ROW_SIZE_INCLUDES_LONG: case SQL_MULT_RESULT_SETS: case SQL_MULTIPLE_ACTIVE_TXN: case SQL_NEED_LONG_DATA_LEN: case SQL_ORDER_BY_COLUMNS_IN_SELECT: case SQL_PROCEDURE_TERM: case SQL_PROCEDURES: case SQL_ROW_UPDATES: case SQL_SCHEMA_TERM: case SQL_SEARCH_PATTERN_ESCAPE: case SQL_SERVER_NAME: case SQL_SPECIAL_CHARACTERS: case SQL_TABLE_TERM: case SQL_USER_NAME: case SQL_XOPEN_CLI_YEAR: case SQL_OUTER_JOINS: if (waMode != 'W') { /* ansi<=unicode*/ ret = dm_StrCopyOut2_W2A ((SQLWCHAR *) infoValueOut, (SQLCHAR *) rgbInfoValue, cbInfoValueMax / sizeof(wchar_t), pcbInfoValue); } else { /* unicode<=ansi*/ ret = dm_StrCopyOut2_A2W ((SQLCHAR *) infoValueOut, (SQLWCHAR *) rgbInfoValue, cbInfoValueMax, pcbInfoValue); if (pcbInfoValue) *pcbInfoValue = *pcbInfoValue * sizeof(wchar_t); } if (ret == -1) { PUSHSQLERR (pdbc->herr, en_01004); retcode = SQL_SUCCESS_WITH_INFO; } break; } } MEM_FREE(_InfoValue); return retcode; }