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

Reply via email to