Bug #30958 not null text fields containing empty strings return 0 records in queries
Submitted: 11 Sep 2007 18:11 Modified: 13 Nov 2007 11:54
Reporter: Stephen Wuebker Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / ODBC Severity:S2 (Serious)
Version:3.51.20, 5.1 OS:Windows
Assigned to: Jim Winstead CPU Architecture:Any
Tags: null, ODBC, text

[11 Sep 2007 18:11] Stephen Wuebker
Description:
Text fields that are marked as not null and contain empty strings will cause a query that includes those fields to return 0 records

How to repeat:
Setup the test in any query tool
CREATE DATABASE text_test;
CREATE TABLE test (tt_textfield TEXT NOT NULL, tt_varcharfield VARCHAR(50) NOT NULL);
INSERT INTO test (tt_textfield, tt_varcharfield) VALUES ('','');

I'm using VB6 SP6, ODBC/Connector 3.51.20, Server: 5.0.45-community-nt

Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
Dim sSQL As String
Dim oRS As ADODB.Recordset
Set oRS = New ADODB.Recordset

cn.CursorLocation = adUseClient
cn.Open "Provider=MSDASQL;Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=text_test;UID=user;PWD=;OPTION=16386;"

' This returns EOF
sSQL = "SELECT * FROM test"

' As does this
sSQL = "SELECT tt_textfield FROM test"

' This returns the row
sSQL = "SELECT tt_varcharfield FROM test"

oRS.Open sSQL, cn, adOpenStatic, adLockReadOnly

By setting the text field to allow null, the above queries will all return the row:
ALTER TABLE test MODIFY COLUMN tt_textfield TEXT NULL;
[13 Sep 2007 6:50] Jess Balint
Bug verified exactly as described. ODBC test case added in source repo in test/my_result.c (t_bug30958). The problem is we don't handle SQLGetData() correctly when the "out buffer space" parameter is 0. The varchar field works because it uses a bound parameter instead of SQLGetData(). The NULL case is odd because we treat it exactly the same way, but ADO decides it's ok. See test case for more details.
[23 Oct 2007 17:04] Bogdan Degtyariov
The bug never occurs if remove this string:
cn.CursorLocation = adUseClient
[2 Nov 2007 21:56] Jim Winstead
Fix handling of SQLGetData when given 0-sized buffer and result is empty string

Attachment: bug30958.patch (text/plain), 2.72 KiB.

[12 Nov 2007 21:16] Jim Winstead
The fix for this bug has been committed, and will be included in the next release (3.51.22). Thanks for the bug report.
[13 Nov 2007 11:54] MC Brown
A note has been added to the 3.51.22 changelog: 

The wrong result was returned by SQLGetData() when the data was an
    empty string and a zero-sized buffer was specified.