rails-sqlserver / tiny_tds

TinyTDS - Simple and fast FreeTDS bindings for Ruby using DB-Library.
Other
606 stars 190 forks source link

Unexpected SQL_VARIANT casting #478

Open pkuykendall opened 4 years ago

pkuykendall commented 4 years ago

Environment

Operating System

AWS Lambda Ruby Docker container: lambci/lambda:build-ruby2.7

TinyTDS Version and Information

                           tiny_tds: 2.1.2
                            Version: freetds v1.1.20
             freetds.conf directory: /etc
     MS db-lib source compatibility: yes
        Sybase binary compatibility: yes
                      Thread safety: yes
                      iconv library: yes
                        TDS version: auto
                              iODBC: no
                           unixodbc: yes
              SSPI "trusted" logins: no
                           Kerberos: yes
                            OpenSSL: no
                             GnuTLS: yes
                               MARS: yes

FreeTDS Version

v1.1.20

Description

After updating tiny_tds to 2.1.2 I've noticed some weirdness with the sql_variant data type. I have an extended property with a value of true that is being converted into 1702195828. This doesn't seem to happen in v1.0.5 or in Azure Data Studio.

I believe it might be related to this logic: https://github.com/rails-sqlserver/tiny_tds/blob/master/ext/tiny_tds/result.c#L329, but I'm not completely positive. I don't know if I have a solution, but I did want to raise the issue.

I am able to work around this issue by casting the column as a VARCHAR(2000). Here is a little example:

EXEC sp_addextendedproperty @name='is_json', @value='true'
    ,@level0type = N'Schema', @level0name = 'dbo'  
    ,@level1type = N'Table',  @level1name =  'table_name'
    ,@level2type = N'Column', @level2name = 'column_name';
result = client.execute("SELECT *, cast(value as VARCHAR(2000)) cast_val FROM FN_LISTEXTENDEDPROPERTY(NULL, 'schema', 'dbo', 'table', 'table_name', 'column', DEFAULT)")

result.each { |row| puts row }

#=> {"objtype"=>"COLUMN", "objname"=>"column_name", "name"=>"is_json", "value"=>1702195828, "cast_val"=>"true"}