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

Add support for SQL Variant fields #356

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open

Conversation

akilude
Copy link

@akilude akilude commented Mar 6, 2024

Adds support for sql_variant field which causes the following error:
"ODBC SQL type -150 is not yet supported. column-index=0 type=-150', 'HY106'"

This commit converts these fields output to a string

akilude added 3 commits March 6, 2024 14:58
sql_variant fields in databases causes the following error 

"ODBC SQL type -150 is not yet supported.  column-index=0  type=-150', 'HY106'"

This commit converts these fields into a string and returns
@dauinsight
Copy link
Contributor

Hi @akilude, thank you for contributing. Could you provide a test case for this feature?

@akilude
Copy link
Author

akilude commented Mar 8, 2024

Hi @akilude, thank you for contributing. Could you provide a test case for this feature?

Hi,

Sure, will add it early next week.

@akilude
Copy link
Author

akilude commented Mar 12, 2024

@dauinsight, could you please give me a couple of pointers on how to provide a test case for this.

This feature will mainly be used with existing external databases. When we have an external mssql database with a field of type "sql_variant", this PR will convert the data to string and return it to django.

I think there are only test cases for database created with django?

@dauinsight
Copy link
Contributor

@dauinsight, could you please give me a couple of pointers on how to provide a test case for this.

This feature will mainly be used with existing external databases. When we have an external mssql database with a field of type "sql_variant", this PR will convert the data to string and return it to django.

I think there are only test cases for database created with django?

Good question, we can use Django's RunSQL function to make a custom migration with a sql_variant type:

https://docs.djangoproject.com/en/5.0/ref/migration-operations/#runsql

@dauinsight
Copy link
Contributor

dauinsight commented Mar 26, 2024

Hi @akilude, an example test might look like:

        connection = connections['default']
        with connection.cursor() as cursor:
            cursor.execute("CREATE TABLE tableA(colA sql_variant, colB INT)")
            cursor.execute("INSERT INTO tableA values (CAST(46279.1 as decimal(8,2)), 1689)")
            cursor.execute("SELECT colA FROM tableA WHERE colB=1689")

            rows = cursor.fetchall()

@akilude
Copy link
Author

akilude commented Mar 27, 2024

Thanks @dauinsight, bit busy with work, will add it in a day or two.

@akilude
Copy link
Author

akilude commented Mar 29, 2024

@dauinsight, apologies for the delay, have added the test case.

Please let me know if any changes are needed. Thanks.

testapp/tests/test_fields.py Outdated Show resolved Hide resolved
@@ -83,6 +83,9 @@ def encode_value(v):
return '{%s}' % (v.replace('}', '}}'),)
return v

def handle_sql_variant_as_string(value):
# SQL variant of type 150 is not supported, convert it to string and return
return value.decode('utf-16le')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too familiar with the sql_variant type but it looks like this method of decoding is likely to result in incorrect data.

Since sql_variant can contain multiple types, unpacking the data in a reliable manner seems difficult... We might be better off doing an explicit CAST in sql.

Copy link
Author

@akilude akilude Apr 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dauinsight We can find the base type of the field https://learn.microsoft.com/en-us/sql/t-sql/functions/sql-variant-property-transact-sql?view=sql-server-ver16

in the converter function, shall we convert whatever type to string and return.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dauinsight, please let me know the best way to proceed. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants