RETCODE SQL_API SQLGetDiagRecW(SQLSMALLINT fHandleType, SQLHANDLE handle, SQLSMALLINT iRecord, SQLWCHAR *szSqlState, SQLINTEGER *pfNativeError, SQLWCHAR *szErrorMsg, SQLSMALLINT cbErrorMsgMax, SQLSMALLINT *pcbErrorMsg) { CSTR func = "SQLGetDiagRecW"; RETCODE ret; SQLSMALLINT buflen, tlen; char *qstr = NULL, *mtxt = NULL; mylog("[%s]", func); if (szSqlState) qstr = malloc(8); buflen = 0; if (szErrorMsg && cbErrorMsgMax > 0) { buflen = cbErrorMsgMax; mtxt = malloc(buflen); } ret = PGAPI_GetDiagRec(fHandleType, handle, iRecord, qstr, pfNativeError, mtxt, buflen, &tlen); if (SQL_SUCCEEDED(ret)) { if (qstr) utf8_to_ucs2(qstr, strlen(qstr), szSqlState, 6); if (mtxt && tlen <= cbErrorMsgMax) { SQLULEN ulen = utf8_to_ucs2_lf1(mtxt, tlen, FALSE, szErrorMsg, cbErrorMsgMax); if (ulen == (SQLULEN) -1) { tlen = (SQLSMALLINT) msgtowstr(NULL, mtxt, (int) tlen, (LPWSTR) szErrorMsg, (int) cbErrorMsgMax); } else tlen = (SQLSMALLINT) ulen; if (tlen >= cbErrorMsgMax) ret = SQL_SUCCESS_WITH_INFO; } if (pcbErrorMsg) *pcbErrorMsg = tlen; } if (qstr) free(qstr); if (mtxt) free(mtxt); return ret; }
void test_connection(HANDLE hwnd, ConnInfo *ci, BOOL withDTC) { int errnum; char out_conn[MAX_CONNECT_STRING_LEN]; SQLRETURN ret; SQLHENV env = SQL_NULL_HANDLE; SQLHDBC conn = SQL_NULL_HANDLE; SQLSMALLINT str_len; char dsn_1st; BOOL connected = FALSE; #ifdef UNICODE_SUPPORT SQLWCHAR wout_conn[MAX_CONNECT_STRING_LEN]; SQLWCHAR szMsg[SQL_MAX_MESSAGE_LENGTH]; const SQLWCHAR *ermsg = NULL; SQLWCHAR *conn_str; #else SQLCHAR szMsg[SQL_MAX_MESSAGE_LENGTH]; const SQLCHAR *ermsg = NULL; SQLCHAR *conn_str; #endif /* UNICODE_SUPPORT */ dsn_1st = ci->dsn[0]; ci->dsn[0] = '\0'; makeConnectString(out_conn, ci, sizeof(out_conn)); mylog("conn_string=%s\n", out_conn); #ifdef UNICODE_SUPPORT msgtowstr(out_conn, wout_conn, _countof(wout_conn)); conn_str = wout_conn; #else conn_str = out_conn; #endif /* UNICODE_SUPPORT */ ci->dsn[0] = dsn_1st; if (!SQL_SUCCEEDED(ret = SQLALLOCHANDLEFUNC(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env))) { ermsg = _T("SQLAllocHandle for env error"); goto cleanup; } if (!SQL_SUCCEEDED(ret = SQLSETENVATTRFUNC(env, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0))) { snprintf(szMsg, _countof(szMsg), _T("SQLAllocHandle for env error=%d"), ret); goto cleanup; } if (!SQL_SUCCEEDED(ret = SQLALLOCHANDLEFUNC(SQL_HANDLE_DBC, env, &conn))) { SQLGETDIAGRECFUNC(SQL_HANDLE_ENV, env, 1, NULL, &errnum, szMsg, _countof(szMsg), &str_len); ermsg = szMsg; goto cleanup; } if (!SQL_SUCCEEDED(ret = SQLDRIVERCONNECTFUNC(conn, hwnd, conn_str, SQL_NTS, NULL, MAX_CONNECT_STRING_LEN, &str_len, SQL_DRIVER_NOPROMPT))) { SQLGETDIAGRECFUNC(SQL_HANDLE_DBC, conn, 1, NULL, &errnum, szMsg, _countof(szMsg), &str_len); ermsg = szMsg; goto cleanup; } connected = TRUE; ermsg = _T("Connection successful"); if (withDTC) { #ifdef _HANDLE_ENLIST_IN_DTC_ HRESULT res; void *pObj = NULL; pObj = CALL_GetTransactionObject(&res); if (NULL != pObj) { SQLRETURN ret = SQLSETCONNECTATTRFUNC(conn, SQL_ATTR_ENLIST_IN_DTC, (SQLPOINTER) pObj, 0); if (SQL_SUCCEEDED(ret)) { SQLSETCONNECTATTRFUNC(conn, SQL_ATTR_ENLIST_IN_DTC, SQL_DTC_DONE, 0); snprintf(szMsg, _countof(szMsg), _T("%s\nenlistment was successful\n"), ermsg); ermsg = szMsg; } else { SQLGETDIAGRECFUNC(SQL_HANDLE_DBC, conn, 1, NULL, &errnum, szMsg, _countof(szMsg), &str_len); if (szMsg[0] != 0) { snprintf(szMsg, _countof(szMsg), _T("%s\nMSDTC error:%s"), ermsg, szMsg); ermsg = szMsg; } } CALL_ReleaseTransactionObject(pObj); } else if (FAILED(res)) { snprintf(szMsg, _countof(szMsg), _T("%s\nDistibuted Transaction enlistment error %x"), ermsg, res); ermsg = szMsg; } #else /* _HANDLE_ENLIST_IN_DTC_ */ snprintf(szMsg, _countof(szMsg), _T("%s\nDistibuted Transaction enlistment not supported by this driver"), ermsg); ermsg = szMsg; #endif } cleanup: if (NULL != ermsg && NULL != hwnd) { MESSAGEBOXFUNC(hwnd, ermsg, _T("Connection Test"), MB_ICONEXCLAMATION | MB_OK); } #undef _T #undef snprintf #define snprintf _snprintf if (NULL != conn) { if (connected) SQLDISCONNECTFUNC(conn); SQLFREEHANDLEFUNC(SQL_HANDLE_DBC, conn); } if (env) SQLFREEHANDLEFUNC(SQL_HANDLE_ENV, env); return; }