Stupidly forgot to attach the files.

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

# -*- coding: utf-8 -*-

import sqlalchemy as sa
from sqlalchemy import Table, Column, MetaData, String
from sqlalchemy.orm import mapper, relation
from sqlalchemy.dialects.mssql.base import MSNText, MSNVarchar

metadata = MetaData()

thing_table = Table("ThingTable", metadata,
    Column("id", String(5), primary_key=True),
    Column("text_field", MSNText()),
)

class ThingTable(object):
    pass

mapper(ThingTable, thing_table)

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

uri = "mssql://sa:passwo...@satestdsn2"
engine = create_engine(uri)
# engine.echo = True
metadata.bind = engine
Session = sessionmaker(bind=engine)
session = Session()

metadata.drop_all()
session.commit()
metadata.create_all()
session.commit()

thing = ThingTable()
thing.id = "flib"
thing.text_field = "test£"

session.save(thing)
session.commit()
  
# -*- coding: latin-1 -*-
encoding = 'latin-1'

import sqlalchemy as sa
from sqlalchemy import Table, Column, MetaData, String, text, create_engine
from sqlalchemy.orm import mapper, relation, sessionmaker

def get_mapped_table(column_type):
    metadata = MetaData()

    uri = "mssql://sa:passwo...@satestdsn2"
    engine = create_engine(uri)
    engine.dialect.supports_unicode = True
    engine.dialect.supports_unicode_statements = True
    engine.dialect.convert_unicode = False
    engine.dialect.encoding = encoding
    # engine.echo = True
    metadata.bind = engine
    Session = sessionmaker(bind=engine)
    session = Session()


    thing_table = Table("ThingTable", metadata,
        Column("id", String(5), primary_key=True),
        Column("text_field", getattr(sa.dialects.mssql.base, column_type)()),
    )

    class ThingTable(object):
        pass

    mapper(ThingTable, thing_table)
    
    metadata.drop_all()
    metadata.create_all()
    session.commit()

    return session, ThingTable


def plain_query(column_type, query_data_type, ascii_or_non_ascii_):
    session, ThingTable = get_mapped_table(column_type)

    q = u"INSERT INTO [ThingTableUnicode] (id, text_field) VALUES ('flib', '%s')" % ascii_or_non_ascii_
    if query_data_type == str:
        q = q.encode(encoding)
    q = text(q)
    
    r = session.execute(q)
    session.rollback()



def mapped_query(column_type, query_data_type, ascii_or_non_ascii_):
    session, ThingTable = get_mapped_table(column_type)

    thing = ThingTable()
    thing.id = "flib"
    thing.text_field = ascii_or_non_ascii_
    session.save(thing)
    session.commit()

    session.rollback()


def test_run_lots_of_tests():
    for ascii_or_non_ascii_ in ["ascii", "non_ascii£", u"ascii", u"non_ascii£"]:
        for query_data_type in [unicode, str]:
            for column_type in ["MSText", "MSNText", "MSVarchar", "MSNVarchar"]:
                for query_f in [plain_query, mapped_query]:
                    yield query_f, column_type, query_data_type, ascii_or_non_ascii_


On 29 Jul 2009, at 18:27, Ed Singleton wrote:

> On 25 Jul 2009, at 03:17, mtrier wrote:
>> On Jul 23, 8:30 am, Ed Singleton <singleto...@gmail.com> wrote:
>>> I've managed to get SA (0.6 branch) and pyodbc connecting to anMSSQL
>>> db on Mac OS X, but I've recently been trying to get it working on
>>> linux (Debian Lenny) and have been hitting some problems.
>>>
>>> It's definitely working to some degree.  Adding "TDS_Version =  
>>> 8.0" to
>>> my odbc.ini fixed some unicode problems, and so now simple pyodbc
>>> stuff seems to work.  Querying seems to be fine, but sometimes  
>>> adding
>>> lots of data using the orm fails with the message: DBAPIError:  
>>> (Error)
>>> ('HY000', 'The driver did not supply an error!')
>>>
>>> The same error occurs in both SA 0.5.5 and SA 0.6
>>>
>>> I'm going to try a few more things to narrow down a bit more where  
>>> the
>>> problem is, but if any has any ideas of what it could be or how I
>>> could debug it, I'd be very grateful.
>>
>> Any luck on this? I'm using both with OSX and Ubuntu without
>> differences in behavior.  Do you have an isolated test case that
>> duplicates this behavior?
>
> I don't yet have an isolated test case that causes the same error,  
> but I do now have an isolated test case that doesn't work, and that  
> I'd like to get working.
>
> There's two attached files with reasonably simple cases in.  The  
> first, simple_thing.py contains a single example.  I've tried  
> tweaking various things in it to various effect.
>
> The second, complex_thing.py, contains a test generator that  
> generates 64 tests (for now), testing all the different combinations  
> of things that might affect the success of the test.  This one also  
> clearly shows the difference between Mac and Linux on my setup, as  
> on the Mac several of the 'non_ascii£' tests pass.
>
> Note that it isn't expected for all the tests to pass.  Some of the  
> combinations are wrong.  It just shows what works and what doesn't.
>
> On both the Mac and Linux, my setup is happy with some unicode  
> queries and/or params.  Sometimes it will raise a warning if you are  
> inserting unicode into a non-unicode field, and sometimes it will  
> fail for the same reason.
>
> What I'm trying to achieve is:
>
> 1) Ability to insert non-ascii chars into the db on Linux
> 2) The above but for all column types (varchar, text, nvarchar, ntext)
>
> Absolutely any ideas will be appreciated.  I'm not sure what to try  
> next.  For now I'm going to document how I set up Linux.
>
> Thanks
>
> Ed
>

Reply via email to