A couple other notes:

- I know I could start with a query that will return everything, like
"db.person.id > 0", but I'm worried about how that will interact with
queries that are optimized by an index that doesn't include the id
column

- An old trick when building up SQL WHERE statements as strings is to
start with "WHERE 1=1" (which should get optimized away by the SQL
engine) and go from there. However, I couldn't find a way to make a
Query that evaluated to the equivalent of "1=1".

Cheers,
Kevin

On Oct 5, 1:39 pm, Kevin Ivarsen <kivar...@gmail.com> wrote:
> Hi everybody,
>
> I'm building a page that will search for rows in a database table
> based on optional URL parameters, or return all rows if no parameters
> are given. However, I'm having trouble finding a good way to build up
> the query without having an existing Query object to start with.
> Web2py currently lets me do something like this:
>
> q = (db.person.height > 5)
> q = q & (db.person.height < 6)
> db(q).select()
>
> But I'd like to be able to do something like this (in pseudocode):
>
> q = EmptyQuery(defaultTable=db.person)
> if request.vars.minheight:
>   q = q & (db.person.height > request.vars.minheight)
> if request.vars.maxheight:
>   q = q & (db.person.height < request.vars.maxheight)
>
> people = db(q).select()
>
> so that if no parameters are given, it will select all from db.person,
> but you can optionally filter by min and max height.
>
> Is there an easy way to do this in web2py?
>
> Thanks!
> Kevin

Reply via email to