On Thursday 11 October 2007 05:37:41 Diez B. Roggisch wrote:
>             if not typeClauses:
>                 typeClauses.append(NOT(SQLTrueClause))
>             if filterClause:
>                 filterClause = [AND(*(filterClause + [OR(*typeClauses)]))]
>             else:
>                 filterClause = OR(*typeClauses)
>         else:
>             # we have no filtering, so remove the features from
>             # this query.
>             typeClause = AND(m.BaseBookable.q.childName != 'Feature')
>             if filterClause:
>                 filterClause = [AND(filterClause, typeClause)]
>             else:
>                 filterClause = [typeClause]
>
>         if not self['show_deleted']:
>             filterClause = AND(filterClause, m.BaseBookable.q.deleted ==
> False)
>         clauses = [name_join]
>         if filterClause:
>             clauses.append(filterClause)
>         clause = AND(*clauses)
>
>         self._the_result = m.BaseBookable.select(clause,
>                                               join=joins,
>                                               orderBy=orderBy)
>
> Obviously, this doesn't make much sense to you object-model-wise - but you
> should get an idea how to work with SO.


SQLObject is "lazy" to evaluate results, so you might benefit from a more 
straighforward approach:

        results = model.Books.select()

        if only_read:
                results = results.filter(onlyRead = True)
        if not_borrowed:
                results = results.filter(notBorrowed = True)

        return (books=results)


This will prepare to retrieve all books.  If any of the two filters is true, 
then it will be applied to the query (as you can see, both can be true at 
once).

The query will be performed only when you iterate over the results or when you 
ask to make it a list, for example.

I consider this a much cleaner approach to build "AND" filters.  You can 
expand this into "OR" as well.


-- 
Jorge Godoy      <[EMAIL PROTECTED]>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to