Re: Building queries on the fly

[email protected] Sun, 17 Sep 2006 15:48:28 -0400
Newsgroups gmane.comp.python.quotient.dev
Message-ID <20060917194828.1717.1341762721.divmod.quotient.52119@ohm>
On Mon, 18 Sep 2006 03:57:40 +0900, Robert Gravina <[email protected]> wrote:
>Hi all and thanks again for your help with Axiom! I'm very happy with  it - 
>it's object oriented enough to make persisting objects easy  while still 
>having a relational DB underneath (no pickling and data  that's easy to get 
>at in case you get in a, wait for it, pickle!).  Thanks, I'll be here all 
>week. Try the veal.

Thanks :)

>Anyhow, I'd really like to build up AND query criteria  programatically.

I'll tell you how to do this, but I will caution you: building queries dynamically can easily result in queries which are not indexed, or have pathological query times.  While this is not great in any datbase, in Axiom it's particularly bad because the rest of your program is going to be totally stopped waiting for the rest of that query to execute.

We should be building more support into Axiom some time in the next year for easily determining if your query is indexable and whether it's going to be very expensive to run before executing it, but until then you should familiarize yourself with the indexing rules that SQLite uses.

>self.store.query(MyItem, AND(MyItem.name == "foo",
>                                                         MyItem.owner == 
>MyOwnerItem.storeID,
>                                                         MyOwnerItem.likes != 
>"icecream"))

This is equivalent to

  conditions = []
  conditions.append(MyItem.name == "foo")
  conditions.append(MyItem.owner == MyOwnerItem.storeID)
  conditions.append(MyOwnerItem.likes != "icecream")
  self.store.query(MyItem, AND(*conditions))

Hopefully that explains how to do what you want :).