On Wednesday 15 October 2008 07:04:29 Jorge Vargas wrote: > On Tue, Oct 14, 2008 at 11:59 AM, <[EMAIL PROTECTED]> wrote: > > this is somewhat frequent question, lookup the group in the past > > if anyone has something usable. > > > > maybe something like > > query(cls).filter( > > or_(*[column == value for column in alternatives] )) > > if many values, use column.in_(values) instead of == > > > > alternatives can come from yourtable.columns or > > classmapper( yourclas).iterate_properties. > > > > things to check: > > - types, e.g. comparing strings and integers may fail or succeed > > - primary_key = set( c.key for c in class_mapper( > > cls).primary_key ) - relations/references maybe also be checked > > via .has/.any e.g. references = [ p for p in > > classmapper(yourclass).iterate_properties > > if (p not in primary_key > > and (not isinstance( p, PropertyLoader) #not a relation > > or p.use_list == False )) #or a singular reference > > ] > > these above are just for example, do your own filtering > > Thanks for this it got me going to where I wanted, now I'm a bit > lost on how to extract that into a function. Ideally I want a > function that will return a value I can put into the filter clause > but it seems to be expecting code not an object. How can I write a > function to take care of the branching you say below. Something > along the lines of. query(cls).filter( > or_(*[build_clause(column,value) for column in alternatives] > ))
simplest is: def build_clause(column,value): return column == value that gives sql expression to put into filters (or assemble in another way). use whatever operator instead of == if u also differentiate plain values from references from relations, that would be 3 functions, operating on the 3 separate subsets of attributes. or one function that does the differentiating by itself, so it would need the class and attr-name. but i guess this relation stuff is not very useful, as it is recursive and has to be level-limited. ciao svil > > On Tuesday 14 October 2008 20:26:16 Jorge Vargas wrote: > >> Hi, > >> > >> I'm trying to implement a "simple search" field in my > >> application and I was wondering which will be the best way to > >> implement it on SQLAlchemy. I have googled around and found > >> several partial solutions but none of them convince me of being > >> the right way. > >> > >> My use case is the following. I got a textfield search box which > >> will post a set of words, my webapp is to take those words and > >> do a query in *any* field of the corresponding table, and it is > >> to say within just one table. For example I got a User class > >> that has first_name,last_name,address_fields. And I ran a query > >> for "John Thompson", that will bring me back a list of user > >> objects where any of the following is valid, "John" in > >> first_name, "John" in last_name, "John" in address, "Thompson" > >> in first_name, "Thompson" in last_name, "Thompson" in > >> address_field. > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---