Fantastic, it works just the way I was hoping. Thanks. I ended up using the mapper rather than taking the declarative route (especially since I'm extracting table structures via metadata), but as far as I can tell it all works fine - just in case anyone wants to see an example of it (missing some of the detail around metdata & mapper):
from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.orm.properties import ColumnProperty mytable = Table('mytable', metadata, autoload=True) class MyClass(object): def __init__(self, name): # using '_' column prefix, see mapper call below self._name = name class MyComparator(ColumnProperty.ColumnComparator): def __eq__(self, other): return func.lower(self.__clause_element__()) == func.lower (other) mapper(MyClass, mytable, column_prefix='_', properties={ 'name':synonym('_name', map_column=True, comparator_factory=MyComparator) }) On Nov 18, 1:41 am, Michael Bayer <[EMAIL PROTECTED]> wrote: > On Nov 17, 2008, at 7:50 AM, Iain wrote: > > > > > > > I was wondering if anyone is aware of a way that I can customize the > > way query filters are written - possibly by some sort of customization > > of table columns? > > > The issue is that I have some odd data/column types that can't be > > compared directly. That is, "SELECT * WHERE column1=value1" will not > > work - instead, I need to do "SELECT * WHERE user_func(column1) > > =user_func(value1)" or in some instances I want to do "SELECT * WHERE > > column2=user_func(value2)". > > > So rather than everyone who writes queries having to remember to write > > these out as text or such, it'd be good if I could tell sqlalchemy to > > always re-write the queries that way whenever a user tests for > > equivalence on the relevant columns. > > > Hope that makes sense! > > you would implement this using a custom comparator class. Its a very > new feature that these are fully customizable, so this is how it looks > right now: > > from sqlalchemy import * > from sqlalchemy.orm import * > from sqlalchemy.ext.declarative import declarative_base > from sqlalchemy.orm.properties import ColumnProperty > > Base = declarative_base() > > class MyComparator(ColumnProperty.ColumnComparator): > def __eq__(self, other): > return func.lower(self.__clause_element__()) == > func.lower(other) > > class MyClass(Base): > __tablename__ = 'mytable' > id = Column(Integer, primary_key=True) > name = column_property(Column(String(50)), > comparator_factory=MyComparator) > > sess = sessionmaker()() > > print sess.query(MyClass).filter_by(name='SomeName').statement --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---