[sqlalchemy] Re: Dynamic where-statement with OR

2006-11-10 Thread Lee McFadden

On 11/7/06, DNeumann [EMAIL PROTECTED] wrote:

 Hello list,

 To build a dynamic where-statement in my script, i looked here for more
 info:
 http://www.sqlalchemy.org/docs/sqlconstruction.myt#sql_building

 statement = my_table.select()
 if id:
   statement.append_whereclause(my_table.c.id==id)
 if name:
   statement.append_whereclause(my_table.c.name==name)
 ...

 The where-statement is now generated with the AND-operator.

 now my question:
 Is it possible to get OR instead of AND?
 Or alternative Solutions?


Another option that I use quite a lot is to use:

query_args = []
if id:
query_args.append(my_table.c.id==id)
if name:
query_args.append(my_table.c.name==name)

statement = my_table.select(or_(*query_args))

You can build quite complex queries this way by nesting and_() and
or_() in the list.

Lee


-- 
Lee McFadden

blog: http://www.splee.co.uk
work: http://fireflisystems.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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Dynamic or-Statement

2006-11-08 Thread Michael Bayer

build the OR clause separately:

list = []
list.append(users.c.user_id==id)
list.append(users.c.user_name==name)

statement = user.select(or_(*list))

or

statement.append_whereclause(or_(*list))

alternate method:

critrerion = users.c.user_id==id
criterion = criterion.or(users.c.user_name==name)
crtierion = criterion.or(crit)
...

statement = user.select(criterion)

On Nov 8, 2006, at 3:33 AM, Dominik Neumann wrote:


 Hello list,

 i need to build a dynamic where-statement, linked by the
 OR-operator.
 In the docs there are only examples with AND.

 example (snippet from docs):
 statement = user.select()
 statement.append_whereclause(user.c.user_id == id)
 statement.append_whereclause(user.c.user_name == name)

 is it possible to get here the OR instead of the AND-operator?

 Best Regards,
 -dn


 


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