Bug #16224 Calling SQLGetDiagField with RecNumber 0,DiagIdentifier NOT 0 returns SQL_ERROR
Submitted: 5 Jan 2006 13:32 Modified: 6 Jul 2007 2:40
Reporter: tom hindle Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / ODBC Severity:S3 (Non-critical)
Version:3.51.12 OS:IBM AIX (aix 64 5.2)
Assigned to: Jess Balint CPU Architecture:Any

[5 Jan 2006 13:32] tom hindle
Description:
Calling SQLGetDiagField with RecNumber 0,DiagIdentifier NOT 0 returns SQL_ERROR.

SQLGetDiagField returns SQL_ERROR when RecNumber is 0 and a valid DiagIdentifer is specified.

The Documetation I am using says:
"If the DiagIdentifier argument indicates any field of the diagnostics header, RecNumber is ignored."

Specifying RecNumber to 1 would solve the problem as a work around.

Other info:
platform: Aix 64 5.2
odbc manager: iodbc

How to repeat:
call SQLGetDiagField with RecNumber 0 and  set DiagIdentifier to any diagnositcs header field. 

Suggested fix:
Problem is in function SQLGetDiagField() in driver/error.c

About Line 495

Existing code:
if (RecNumber == 0 && DiagIdentifier > 0 )
   DBUG_RETURN_STATUS(SQL_ERROR)

what the programmer Intended?
  if (RecNumber == 0 && DiagIdentifier == 0 )
   DBUG_RETURN_STATUS(SQL_ERROR)

Changing it to this solves the problem. 
SQLGetDiagField then works with RecNumber as 0.
[9 Jan 2006 12:00] Vasily Kishkin
Sorry...I was not able to reproduce the bug. Could you please create and attach some test case ?
[9 Jan 2006 13:54] tom hindle
a Sample functions demonstrating this problem.

/* call this method to get the number of status records that are available for the specified handle.*/
/* 
int CheckError(SQLHANDLE handle){

SQLINTEGER diagnum=-1;
int htype=SQL_HANDLE_ENV;

/* this call will always return SQL_ERROR even though its a valid thing to do*/
SQLGetDiagField((SQLSMALLINT)htype,handle,
0,SQL_DIAG_NUMBER,&diagnum,SQL_IS_INTEGER,NULL);

return(diagnum);
}
[13 Jul 2006 9:56] Sveta Smirnova
test case

Attachment: bug16224.c (text/plain), 818 bytes.

[13 Jul 2006 18:27] Sveta Smirnova
Thank you for the report.

Verified using attached test case as described on Windows XP SP2 и Solaris 10.
[19 Jun 2007 19:01] Jess Balint
patch + test

Attachment: bug16224.diff (application/octet-stream, text), 1.26 KiB.

[19 Jun 2007 19:11] Jim Winstead
A more concise test (no need to alloc your own handles):

DECLARE_TEST(t_bug16624)
{
  SQLINTEGER diagcnt;

  expect_sql(hstmt, "This is an invalid Query! (odbc test)", SQL_ERROR);

  ok_stmt(hstmt, SQLGetDiagField(SQL_HANDLE_STMT, hstmt, 0,
                                 SQL_DIAG_NUMBER, &diagcnt,
                                 SQL_IS_INTEGER, NULL));
  is_num(diagcnt, 1);

  return OK;
}
[19 Jun 2007 19:12] Jim Winstead
the test needs a header, too:

/**
 Bug #16224: Calling SQLGetDiagField with RecNumber 0,DiagIdentifier NOT 0 returns SQL_ERROR
*/
[6 Jul 2007 2:40] Paul DuBois
Noted in 3.51.17 changelog.

Calling SQLGetDiagField with RecNumber 0, DiagIdentifier NOT 0 
returned SQL_ERROR, preventing access to diagnostic header fields.