0

I'm trying to patch this bug and am running into problems. django-pyodbc is an SQL Server backend to django, and the bug causes any sql to be converted to a bytes type in python 3.x, which raises a TypeError when it is attempted to be executed (must be string or unicode).

Here is the code I have at my_app/pyodbc_patch/base.py (the only file in that folder):

import django_pyodbc.base

class CursorWrapper(django_pyodbc.base.CursorWrapper):
    def format_sql(self, sql, n_params=None):
        if n_params is not None:
            try:
                sql = sql % tuple('?' * n_params)
            except:
                pass
            else:
                if '%s' in sql:
                    sql = sql.replace('%s', '?')
        return sql

class DatabaseFeatures(django_pyodbc.base.DatabaseFeatures):
    pass

class DatabaseWrapper(django_pyodbc.base.DatabaseWrapper):
    pass

My settings.DATABASES:

DATABASES = {
    'default': {
        'ENGINE': 'my_app.pyodbc_patch',
        'HOST': '...',
        'NAME': '...',
        'OPTIONS': {
            'host_is_server': True
        }
    }
}

As far as I can tell from the django source, django.db.utils.ConnectionHandler gets the engine from DATABASES and lets django.db.utils.load_backend import its base module. However, the error I get is exactly the same as if I hadn't applied the patch. It makes completely no sense to me, because pyodbc_patch imports django_pyodbc itself, so I cannot imagine the faulty version of CursorWrapper.format_sql is being called even earlier.

The other option is to fix my local copy of django_pyodbc. Unfortunately I only have Python33/Lib/site-packages/django_pyodbc-0.2.3-py3.3.egg and I have no idea how to edit egg files. I really am at my wits' end. It's my first foray into python and django, and I would have absolutely stuck to the ruby and rails that I know if I'd known it would be this messy.

Any help greatly appreciated.

aidan
  • 33
  • 1
  • 5

1 Answers1

0

I don't have access to a SQL Server 2000 installation, but if I remember correctly, SQL Server 2000 encoded unicode with UCS-2 instead of UTF-8. You also need to use TDS version 7.1 with SQL Server 2000, IIRC. It has been a while since I connected over FreeTDS to SQL Server 2000, but perhaps, before changing the django-pyodbc code, try this?

'OPTIONS': {
    'host_is_server': True,
    'autocommit': True,
    'unicode_results': True,
    'extra_params': 'tds_version=7.1'
},

The autocommit is required for Django 1.6+, this will enable Unicode results (which should be converted to UCS-2 by SQL Server 2000), and set the correct TDS version. YMMV, but I would give it a shot.

FlipperPA
  • 13,607
  • 4
  • 39
  • 71