static void extract_error(SQLHANDLE handle, SQLSMALLINT type) { SQLINTEGER i = 0; SQLINTEGER native; SQLWCHAR state[7]; SQLWCHAR text[256]; SQLSMALLINT len; SQLRETURN ret; fprintf(stderr, "\n" "The driver reported the following diagnostics\n"); do { ret = SQLGetDiagRecW(type, handle, ++i, state, &native, text, 256, &len); state[5] = 0; if (SQL_SUCCEEDED(ret)) printf("%s:%ld:%ld:%s\n", odbc_get_sqlchar(&odbc_buf, state), (long) i, (long) native, odbc_get_sqlchar(&odbc_buf, text)); } while (ret == SQL_SUCCESS); }
void StatementObject::StoreError(SWORD fHandleType, SQLHANDLE handle) { SQLWCHAR szErrState[SQL_SQLSTATE_SIZE+1]; // SQL Error State string SQLINTEGER dwErrCode; // Native Error code SQLWCHAR szErrText[SQL_MAX_MESSAGE_LENGTH+1];// SQL Error Text string SWORD wErrMsgLen; // Error message length SQLWCHAR szBuffer[1024]; //SQL_SQLSTATE_SIZE+SQL_MAX_MESSAGE_LENGTH+1]; szBuffer[0] = 0; SQLRETURN nErrResult; // Return Code from SQLGetDiagRec SWORD sMsgNum = 1; // no of records m_errMessage[0] = 0; m_errState[0] = 0; m_errCode = 0; nErrResult = SQLGetDiagRecW(fHandleType, handle, sMsgNum, szErrState, &dwErrCode, szErrText, SQL_MAX_MESSAGE_LENGTH-1, &wErrMsgLen); if (nErrResult != SQL_NO_DATA) { if(nErrResult == SQL_ERROR || nErrResult == SQL_INVALID_HANDLE){ wcscpy(m_errMessage, L"\nError in executing SQLGetDiagRecW function"); } else { #ifdef XP_WIN _snwprintf((wchar_t *)m_errMessage, sizeof(m_errMessage)/sizeof(wchar_t), L"\nSQL Error State : %s\nNative Error Code: %lX\nODBC Error : %s",(LPWSTR) szErrState, dwErrCode, (LPWSTR)szErrText); #else swprintf((wchar_t *)m_errMessage, sizeof(m_errMessage)/sizeof(wchar_t), L"\nSQL Error State : %ls\nNative Error Code: %lX\nODBC Error : %ls",(LPWSTR) szErrState, dwErrCode, (LPWSTR)szErrText); #endif m_errCode = (dwErrCode?dwErrCode:1); wcscpy(m_errState, szErrState); } NPN_SetException(this, "$DB$"); } }
static void save_err_msgW( odbcdr_context_def *context, SQLSMALLINT plm_handle_type, // Handle type SQLHANDLE plm_handle, // Handle name int ConnInd) // Connection indicator { RETCODE plm_retcode = SQL_SUCCESS; SQLWCHAR plm_szSqlState[ODBCDR_MAX_BUFF_SIZE]; SQLWCHAR plm_szErrorMsg[ODBCDR_MAX_BUFF_SIZE]; SDWORD plm_pfNativeError = 0L; SWORD plm_pcbErrorMsg = 0; SQLSMALLINT plm_cRecNmbr = 1; SDWORD plm_SS_MsgState = 0; SDWORD plm_SS_Severity = 0; SQLINTEGER plm_Rownumber = 0; USHORT plm_SS_Line; SQLSMALLINT plm_cbSS_Procname, plm_cbSS_Srvname; SQLWCHAR plm_SS_Procname[555]; SQLWCHAR plm_SS_Srvname[555]; plm_szSqlState[0] = L'\0'; plm_szErrorMsg[0] = L'\0'; debug_on("odbcdr_xlt_status: save_err_msg"); context->odbcdr_last_err_msgW[0] = L'\0'; while (plm_retcode != SQL_NO_DATA_FOUND) { plm_retcode = SQLGetDiagRecW(plm_handle_type, plm_handle, plm_cRecNmbr, plm_szSqlState, &plm_pfNativeError, plm_szErrorMsg, ODBCDR_MAX_BUFF_SIZE - 1, &plm_pcbErrorMsg); // Note that if the application has not yet made a // successful connection, the SQLGetDiagField // information has not yet been cached by ODBC // Driver Manager and these calls to SQLGetDiagField // will fail. if (plm_retcode != SQL_NO_DATA_FOUND) { if (ConnInd) { plm_retcode = SQLGetDiagFieldW( plm_handle_type, plm_handle, plm_cRecNmbr, SQL_DIAG_ROW_NUMBER, &plm_Rownumber, SQL_IS_INTEGER, NULL); #ifdef _WIN32 plm_retcode = SQLGetDiagFieldW( plm_handle_type, plm_handle, plm_cRecNmbr, SQL_DIAG_SS_LINE, &plm_SS_Line, SQL_IS_INTEGER, NULL); plm_retcode = SQLGetDiagFieldW( plm_handle_type, plm_handle, plm_cRecNmbr, SQL_DIAG_SS_MSGSTATE, &plm_SS_MsgState, SQL_IS_INTEGER, NULL); plm_retcode = SQLGetDiagFieldW( plm_handle_type, plm_handle, plm_cRecNmbr, SQL_DIAG_SS_SEVERITY, &plm_SS_Severity, SQL_IS_INTEGER, NULL); plm_retcode = SQLGetDiagFieldW( plm_handle_type, plm_handle, plm_cRecNmbr, SQL_DIAG_SS_PROCNAME, &plm_SS_Procname, sizeof(plm_SS_Procname), &plm_cbSS_Procname); plm_retcode = SQLGetDiagFieldW( plm_handle_type, plm_handle, plm_cRecNmbr, SQL_DIAG_SS_SRVNAME, &plm_SS_Srvname, sizeof(plm_SS_Srvname), &plm_cbSS_Srvname); #endif } // if ConnInd // display error messages, filtering out expected "errors" if ((plm_pfNativeError == 5701) || // Change database context error code (plm_pfNativeError == 5703) ) { // Change language setting error code break;// out of while loop, do not display error messages } else { // Save the message. wcscpy( context->odbcdr_last_err_msgW, (wchar_t*)plm_szErrorMsg ); debug1("szSqlState = %ls",plm_szSqlState); debug1("pfNativeError = %d",plm_pfNativeError); debug1("szErrorMsg = %ls",plm_szErrorMsg); debug1("pcbErrorMsg = %d",plm_pcbErrorMsg); if (ConnInd) { debug1("ODBCRowNumber = %d", plm_Rownumber); debug1("SSrvrLine = %d", plm_Rownumber); debug1("SSrvrMsgState = %d",plm_SS_MsgState); debug1("SSrvrSeverity = %d",plm_SS_Severity); debug1("SSrvrProcname = %ls",plm_SS_Procname); debug1("SSrvrSrvname = %ls",plm_SS_Srvname); } break; } //else } //if plm_retcode plm_cRecNmbr++; //Increment to next diagnostic record. } // End while. debug_return_void(NULL); }
bool core_sqlsrv_get_odbc_error( sqlsrv_context& ctx, int record_number, sqlsrv_error_auto_ptr& error, logging_severity severity TSRMLS_DC ) { SQLHANDLE h = ctx.handle(); SQLSMALLINT h_type = ctx.handle_type(); if( h == NULL ) { return false; } zval* ssphp_z = NULL; int zr = SUCCESS; zval* temp = NULL; SQLRETURN r = SQL_SUCCESS; SQLSMALLINT wmessage_len = 0; SQLWCHAR wsqlstate[ SQL_SQLSTATE_BUFSIZE ]; SQLWCHAR wnative_message[ SQL_MAX_MESSAGE_LENGTH + 1 ]; SQLSRV_ENCODING enc = ctx.encoding(); switch( h_type ) { case SQL_HANDLE_STMT: { sqlsrv_stmt* stmt = static_cast<sqlsrv_stmt*>( &ctx ); if( stmt->current_results != NULL ) { error = stmt->current_results->get_diag_rec( record_number ); // don't use the CHECK* macros here since it will trigger reentry into the error handling system if( error == NULL ) { return false; } break; } // convert the error into the encoding of the context if( enc == SQLSRV_ENCODING_DEFAULT ) { enc = stmt->conn->encoding(); } } default: error = new ( sqlsrv_malloc( sizeof( sqlsrv_error ))) sqlsrv_error(); r = SQLGetDiagRecW( h_type, h, record_number, wsqlstate, &error->native_code, wnative_message, SQL_MAX_MESSAGE_LENGTH + 1, &wmessage_len ); // don't use the CHECK* macros here since it will trigger reentry into the error handling system if( !SQL_SUCCEEDED( r ) || r == SQL_NO_DATA ) { return false; } SQLINTEGER sqlstate_len = 0; convert_string_from_utf16(enc, wsqlstate, sizeof(wsqlstate), (char**)&error->sqlstate, sqlstate_len); SQLINTEGER message_len = 0; convert_string_from_utf16(enc, wnative_message, wmessage_len, (char**)&error->native_message, message_len); break; } // log the error first LOG( severity, "%1!s!: SQLSTATE = %2!s!", ctx.func(), error->sqlstate ); LOG( severity, "%1!s!: error code = %2!d!", ctx.func(), error->native_code ); LOG( severity, "%1!s!: message = %2!s!", ctx.func(), error->native_message ); error->format = false; return true; }
static int get_error_from_diag_rec( odbcdr_context_def *context, SQLSMALLINT handle_type, SQLHANDLE handle) { int rdbi_status = RDBI_SUCCESS; int crit_err_found = FALSE; RETCODE rec_retcode = SQL_SUCCESS; RETCODE field_retcode = SQL_SUCCESS; SQLWCHAR szSqlState[ODBCDR_MAX_BUFF_SIZE]; SQLWCHAR szErrorMsg[ODBCDR_MAX_BUFF_SIZE]; SDWORD pfNativeError = 0L; SWORD pcbErrorMsg = 0; SQLSMALLINT cRecNmbr = 1; SDWORD SS_Severity = 0; SQLINTEGER Rownumber = 0; SQLINTEGER Colnumber = 0; szSqlState[0] = L'\0'; szErrorMsg[0] = L'\0'; /* ** Loop through the diagnostic records until there are no records ** left or a critical error is found. ** For now, we will only deal with the first critical error, and ** ignore the rest. */ while ((rec_retcode != SQL_NO_DATA_FOUND) && !crit_err_found) { rec_retcode = (context->odbcdr_UseUnicode) ? SQLGetDiagRecW(handle_type, handle, cRecNmbr, szSqlState, &pfNativeError, szErrorMsg, ODBCDR_MAX_BUFF_SIZE, &pcbErrorMsg) : SQLGetDiagRec(handle_type, handle, cRecNmbr, (SQLCHAR*)szSqlState, &pfNativeError, (SQLCHAR*)szErrorMsg, ODBCDR_MAX_BUFF_SIZE, &pcbErrorMsg); if (rec_retcode != SQL_NO_DATA_FOUND) { field_retcode = (context->odbcdr_UseUnicode) ? SQLGetDiagFieldW( handle_type, handle, cRecNmbr, SQL_DIAG_ROW_NUMBER, &Rownumber, SQL_IS_INTEGER, NULL) : SQLGetDiagField( handle_type, handle, cRecNmbr, SQL_DIAG_ROW_NUMBER, &Rownumber, SQL_IS_INTEGER, NULL); if (Rownumber != SQL_NO_ROW_NUMBER && Rownumber != SQL_ROW_NUMBER_UNKNOWN) { field_retcode = (context->odbcdr_UseUnicode) ? SQLGetDiagFieldW( handle_type, handle, cRecNmbr, SQL_DIAG_COLUMN_NUMBER , &Colnumber, SQL_IS_INTEGER, NULL) : SQLGetDiagField( handle_type, handle, cRecNmbr, SQL_DIAG_COLUMN_NUMBER , &Colnumber, SQL_IS_INTEGER, NULL); } #ifdef _WIN32 field_retcode = (context->odbcdr_UseUnicode) ? SQLGetDiagFieldW( handle_type, handle, cRecNmbr, SQL_DIAG_SS_SEVERITY, &SS_Severity, SQL_IS_INTEGER, NULL): SQLGetDiagField(handle_type, handle, cRecNmbr, SQL_DIAG_SS_SEVERITY, &SS_Severity, SQL_IS_INTEGER,NULL); #endif switch( pfNativeError ) { case 208 : rdbi_status = RDBI_NO_SUCH_TABLE; crit_err_found = TRUE; break; case 916 : rdbi_status = RDBI_NOT_VALID_USER_IN_DATABASE; crit_err_found = TRUE; break; case 1555 : rdbi_status = RDBI_RESOURCE_BUSY; crit_err_found = TRUE; break; case 18456 : rdbi_status = RDBI_INVLD_USER_PSWD; crit_err_found = TRUE; break; case 913: // "Could not find database ID 52. Database may not be activated yet or may be in transition." // Severity 16. This is rather a bug related to updating views. rdbi_status = RDBI_GENERIC_ERROR; break; case 2601: case 2627: // Duplicate index found. rdbi_status = RDBI_DUPLICATE_INDEX; crit_err_found = TRUE; break; case 2714: // There is already an object named '%.*ls' in the database. rdbi_status = RDBI_OBJECT_EXISTS; crit_err_found = TRUE; break; case 3701: // No such object rdbi_status = RDBI_NO_SUCH_TABLE; crit_err_found = TRUE; break; case 229: // insufficient privileges rdbi_status = RDBI_INSUFFICIENT_PRIVS; crit_err_found = TRUE; break; default : // If we did not identify any specific errors in the // diagnostic record, use the severity level to // determine if a error occurred // *** DS doesn't agree. //if (SS_Severity > 10) { rdbi_status = RDBI_GENERIC_ERROR; crit_err_found = TRUE; //} else { // rdbi_status = RDBI_SUCCESS; //} } //switch cRecNmbr++; //Increment to next diagnostic record. } //if plm_retcode } // End while. if (context->odbcdr_UseUnicode) save_err_msgW( context, handle_type, handle, 1); // 1 = assuming a connection has already been made else save_err_msg( context, handle_type, handle, 1); // 1 = assuming a connection has already been made return rdbi_status; }