On May 30, 2007, at 4:50 PM, Eric Ongerth wrote:
> > So far I've been having a great time with generative query-building > methods... build a query, filter it, order it, etc... > > Is there a way to, uh, de-generate a query? E.g., > > q0 = session.query(someclass) > q1 = q.filter(col == value) > q2 = q.filter(othercol == othervalue) > # all of the above works great, of course > # but how about if we could: > q3 = q.remove_filter(othercol == othervalue) > q4 = q.remove_filter(col == value) > oh, come on :) just use q0. > > As it stands now, this is not actually difficult to do: I just store a > set of filters that have been added so far, and when the user clears > one of the search field boxes, the query is rebuilt from scratch with > all of the other stored filters, just without the one corresponding to > the newly unrestricted column. thats the way it should be done. dont try to make SA, which is decidedly a library, the framework for your application. query() is roughly a command object built via the builder pattern, but its not a mutable datastructure. Python gives you [] for that. remove_filter () is just one little element which might solve your problem at the moment, but then tomorrow youre going to need 4 other mutation/ inspection operations on the list of filters and then the whole "piggyback onto SA" idea goes out the window again. > > It would be nicer/cleaner/straightforward-er if I didn't have to > rebuild the query at that point; if I could just de-filter or de- > select by parts. Am I barking up an odd tree here? I understand that > selects/filters/etc. can't be assumed to be commutative; i.e. that you > can't necessarily expect query.filter(one).filter(two).filter(three) > == query.filter(two).filter(one).filter(three). ah well, each filter adds the given criterion via AND so in the case of just using filter(), i think it is commutative. but using joins and various filter_bys(), not as much since they build off existing state within the query. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---