std::string getOdbcError(unsigned int handletype, const SQLHANDLE& handle) { std::ostringstream err; SQLCHAR sqlstate[6]; SQLCHAR message[SQL_MAX_MESSAGE_LENGTH]; SQLINTEGER NativeError; SQLSMALLINT i, MsgLen; SQLRETURN rc2; // Get the status records. i = 1; while ((rc2 = SQLGetDiagRecA(handletype, handle, i, sqlstate, &NativeError, message, sizeof(message), &MsgLen)) != SQL_NO_DATA && rc2 >= 0) { err << "(" << i << ") " << "\nSQLState:" << (char*)&sqlstate[0]; err << "\nNativeError: " << (long)NativeError; err << "\nMessage: " << &message[0]; //err << "\nMsgLen: " + (long)MsgLen; i++; } return err.str(); }
void OdbcWrapper::get_error(unsigned int handletype, const SQLHANDLE &handle) { SQLCHAR sqlstate[1024]; SQLCHAR message[1024]; char err[2048] = {0}; if (SQL_SUCCESS == SQLGetDiagRecA(handletype, handle, 1, sqlstate, NULL, message, 1024, NULL)) { sprintf(err,"Message: %s\nSQLSTATE: %s", message, sqlstate); m_error.assign(err); } }
/*------------------------------------------------------------------------- * SQLWhatsWrong *------------------------------------------------------------------------- * Purpose: * Diagnostics for odbc calls * * Side Effects: * Breaks on anything but SQL_SUCCESS */ void SQLWhatsWrong(SQLSMALLINT ssiHandleType, SQLHANDLE sh) { SQLCHAR scState[5]; SQLINTEGER siNativeError; SQLCHAR scMsgText[512]; SQLSMALLINT cbMsgText; SQLRETURN sqlret = SQLGetDiagRecA(ssiHandleType, sh, 1, scState, &siNativeError, scMsgText, sizeof(scMsgText), &cbMsgText); ZDebugOutput((char *) scMsgText); // BIG hack to work with zone profile if (7312 == siNativeError) return; // If our query is deadlocked, let's try again if (1205 == siNativeError) { debugf("Query deadlocked. Retry #%d.", g_cSQLRetries); g_fSQLRetry = true; g_cSQLRetries++; SQLCloseCursor(g_hstmt_Last); // we get an invalid cursor state without this. Sleep(50 * g_cSQLRetries); return; } if(g_pSQLSite) // sometimes NULL because g_pSQLSite is sometimes constructed (and set to NULL) sometime after InitSql() has its way with it. g_pSQLSite->OnMessageBox((char *) scMsgText, "FedSrv SQL Error", MB_OK | MB_ICONINFORMATION); else { debugf("SQL Error %s\n", scMsgText); printf("SQL Error %s\n", scMsgText); } // if we can't access the database, we're screwed, so give up. // TODO: Make the exit more graceful //assert(0); // let's at least break in debug build //exit(EXIT_FAILURE); }