Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected behavior when bult-in sql functioned invoked #317

Closed
zbychfish opened this issue Nov 19, 2016 · 8 comments
Closed

Unexpected behavior when bult-in sql functioned invoked #317

zbychfish opened this issue Nov 19, 2016 · 8 comments
Labels

Comments

@zbychfish
Copy link

zbychfish commented Nov 19, 2016

Output from LOGINPROPERTY builtin function is not correctly encoded

conn_params=['','sa','xxxx','192.168.0.20','1433','master']
conn=TinyTds::Client.new username: conn_params[1], password: conn_params[2],	host: conn_params[3], port: conn_params[4], database: conn_params[5], azure:false
sql="SELECT LOGINPROPERTY('sa', 'IsLocked') as param)"
puts conn.encoding
puts result=conn.execute(sql).each

returns:

UTF-8
{"param"=>"\u0000\u0000\u0000\u0000"}
[Finished in 1.236s]

It looks that something wrong with output encoding.

I found temporary solution but the reason of this behavior will be useful.
I have converted value to int internally on sql server:
SELECT CONVERT(int,(SELECT LOGINPROPERTY('sa', 'IsLocked') as param))
in this case returned value from tiny_tds is correctly displayed

@metaskills metaskills added the bug label Nov 27, 2016
@metaskills
Copy link
Member

Indeed! It seems other functions work. I wonder why this one does not.

v = @client.execute("SELECT LOGINPROPERTY('sa', 'IsLocked') as v").first['v']
=> "\u0000\u0000\u0000\u0000"

If I add a debug statement to the result file:

--- a/ext/tiny_tds/result.c
+++ b/ext/tiny_tds/result.c
@@ -205,6 +205,7 @@ static VALUE rb_tinytds_result_fetch_row(VALUE self, ID timezone, int symbolize_
     DBINT data_len = dbdatlen(rwrap->client, col);
     int null_val = ((data == NULL) && (data_len == 0));
     if (!null_val) {
+      rb_warn("coltype %i", coltype);
       switch(coltype) {
         case SYBINT1:
           val = INT2FIX(*(DBTINYINT *)data);

I get the following information:

@client.execute("SELECT LOGINPROPERTY('sa', 'IsLocked') as v").first['v']
(pry):1: warning: coltype 98
=> "\u0000\u0000\u0000\u0000"

And I do not see that in the https://github.com/FreeTDS/freetds/blob/master/include/sybdb.h#L153 file

@metaskills
Copy link
Member

I filed this issue with FreeTDS FreeTDS/freetds#86

@metaskills
Copy link
Member

Made this PR for us: #321

@metaskills
Copy link
Member

Odd that Ubuntu fails this new test but not locally on OS X or Appveyor.

Expected: 0
Actual: 140518445023232

@metaskills
Copy link
Member

Ah! I think it is a 4-byte integer. Testing that now...

@freddy77
Copy link

The data returned by dbdata is dependent on the type of SYBVARIANT (98). Basically every cell of the table can have a different type. I think currently there's no way to get the type of the SYBVARIANT cells in our dblib.
Microsoft dblib support up to 4.2 version of the protocol which does not support SYBVARIANT so SYBVARIANT is returned as VARCHAR or TEXT (not sure).

@metaskills
Copy link
Member

Thanks @freddy77 for dropping in. Have you seen other places where SYBVARIANT col types are returned? This is my first time seeing it.

I am going to move our SYBVARIANT case to the bottom of rb_tinytds_result_fetch_row and just check the length of the data. If == 4 then we will assume a DBINT. If not, we will just pass it thru as a binary string as is.

@metaskills
Copy link
Member

This is now in master and will be upcoming v1.0.6 release.

aharpervc pushed a commit to aharpervc/tiny_tds that referenced this issue Apr 9, 2020
…ixed rails-sqlserver#321.

Since we can not rely on determining the type from SYBVARIANT we rely on checking for a simple 4-byte (assumed integer) since this is the first time we have encountered it. Else, return binary/string representation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants