Hi

On Tue, Jul 28, 2009 at 3:14 PM, Ed Singleton <singleto...@gmail.com> wrote:

>
> On 26 Jul 2009, at 15:06, Michael Bayer wrote:
>
> >
> > i have freetds 0.82, pyodbc 2.1.4.   except for binary it mostly works
> > fine (with sqla 0.6).
> >
> Is that on Mac, Linux or both?
>
> Did you do any particular configuration of character encodings?
>

I've worked a lot recently in both of these environments. With Mac and Linux
I've experienced the same behavior; that is that you can't pass unicode
statements and you can't pass unicode parameters directly, like you can when
working just with pyodbc on Windows.  With Mac and Linux you need to ensure
that:

    engine.dialect.supports_unicode = False
    engine.dialect.supports_unicode_statements = False

Additional I've had to set convert_unicode to True and the encoding to
Latin1.

Finally one other import factor, pyodbc will not work properly as is on *nix
environments like Mac or Ubuntu, because it won't accept UCS2 like it will
directly on Windows.  To counter this you need to do something like:

class CustomString(MSString):
    """MSSQL VARCHAR type, for variable-length non-Unicode data with a
maximum
    of 8,000 characters."""

    def bind_processor(self, dialect):
        if self.convert_unicode or dialect.convert_unicode:
            if self.assert_unicode is None:
                assert_unicode = dialect.assert_unicode
            else:
                assert_unicode = self.assert_unicode
            def process(value):
                if isinstance(value, unicode):
                    return value.encode(dialect.encoding)
                elif assert_unicode and not isinstance(value, (unicode,
NoneType)):
                    if assert_unicode == 'warn':
                        util.warn("Unicode type received non-unicode bind "
                                  "param value %r" % value)
                        return value
                    else:
                        raise exc.InvalidRequestError("Unicode type received
non-unicode bind param value %r" % value)
                else:
                    return value
            return process
        else:
            return None
from sqlalchemy.databases.mssql import MSString, MSText
import sqlalchemy.util as util
from sqlalchemy import exc

class CustomText(MSText):
    """MSSQL TEXT type, for variable-length text up to 2^31 characters."""

    def bind_processor(self, dialect):
        if self.convert_unicode or dialect.convert_unicode:
            if self.assert_unicode is None:
                assert_unicode = dialect.assert_unicode
            else:
                assert_unicode = self.assert_unicode
            def process(value):
                if isinstance(value, unicode):
                    return value.encode(dialect.encoding)
                elif assert_unicode and not isinstance(value, (unicode,
NoneType)):
                    if assert_unicode == 'warn':
                        util.warn("Unicode type received non-unicode bind "
                                  "param value %r" % value)
                        return value
                    else:
                        raise exc.InvalidRequestError("Unicode type received
non-unicode bind param value %r" % value)
                else:
                    return value
            return process
        else:
            return None

This ensures that unicode gets converted properly.  By default we ignore the
convert_unicode when using pyodbc, but that won't work with freetds in the
mix. We plan to correct this in 0.6 with the ability to pass additional
dbapi information.

Finally my stack is:

Mac: SA -> iODBC -> FreeTDS -> pyodbc -> MSSQL
Ubuntu: SA -> unixODBC -> FreeTDS -> pyodbc -> MSSQL

Of course you can remove the xODBC part of the equation if you want, but the
results are the same.

-- 
Michael Trier
http://michaeltrier.com/
http://thisweekindjango.com/

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to