zillow / ctds

Python DB-API 2.0 library for MS SQL Server
MIT License
83 stars 12 forks source link

Connection messages lost in presence of preceding result set #9

Open craigahobbs opened 6 years ago

craigahobbs commented 6 years ago

Run the following code and notice the output:

import ctds

connection = ctds.connect(
    'localhost',
    user='sa',
    password='password',
    database='Mortgage'
)
with connection.cursor() as cursor:
    cursor.execute('''\
SELECT N'Before';

DECLARE @Cmd NVARCHAR(MAX);
SET @Cmd = N'RAISERROR(@Msg,@Severity,10) WITH NOWAIT;';
EXEC sp_executesql @Cmd,
                   N'@Msg NVARCHAR(MAX),@Severity INT',
                   N'Hello!,'
                   0;

SELECT N'After';
''')
    for message in connection.messages:
        print('=== msg ===', message['description'])
    while cursor.description:
        row = cursor.fetchone()
        if row:
            print('=== row ===', row[0])
        else:
            print('=== row === weird, null row in result set???')
        cursor.nextset()

Actual output:

=== row === Before
=== row === weird, null row in result set???
=== row === After

Expected output:

=== msg === Hello!
=== row === Before
=== row === After

Notice the message is lost in the actual case and also the unexpected null row. If you comment out the "Before" select you get the message and you don't get the unexpected null row.

craigahobbs commented 6 years ago

Behavior unchanged in libfreetds dev.1.00.304

joshuahlang commented 6 years ago

This is due to: https://github.com/FreeTDS/freetds/issues/156