Ive integrated it with the mapper, which is quite a small change, just
added to select() (when used with a "where" clause, not if you send it a
full Select object) and select_by().  the major thing you lose with this
approach is that you cant call len() on a returned result set.  I tried
having __len()__ implemenet the count() method, but then the internal
calls for list(self) cause the __len()__ method to be called, adding an
extra query.

anyway the unit tests needed to call len() on the actual results, so i
also added a list() method on SelectResults that just executes SQL via the
underlying select_whereclause method and gives you the real results.  ive
gotten all the unit tests to pass.

i might look into having the SelectResults be optional in some way, as im
also looking to build up this "stock plugins" catalog idea, maybe this is
another candidate for that.  they do add a little bit of extra behavior
which might be better presented as "heres a plugin that will give you this
extra behavior".


Jonas Borgström wrote:
> Hi,
>
> As most of you people already know the SQLObject.select method does not
> return a list of the selected instances directly, instead a
> SelectResults instance is returned. This instance can then be used to
> further manipulate the search query.
> `SelectResults.filter` can be used to add another "where" clause.
> `SelectResults.order_by` can be used to specify the sort order.
> Python slicing of the instance will also be translated into the
> corresponding SQL equivalent (result[10:20] translates to OFFSET 10
> LIMIT 10)
>
> I've attached a simple work-alike version of this for sqlalchemy. It's
> currently not integrated with sqlalchemy at all since I'm not sure
> what's the best way to do that, or if it should be done at all.
>
> it currently works like this:
>
> res = SelectResults(mapper, table.c.column == "something")
> res = res.order_by([table.c.column]) #add an order clause
>
> for x in res[:10]:  # Fetch and print the top ten instances
>    print x.column2
>
> x = list(res) # execute the query
>
> # Count how many instances that have column2 > 42
> # and column == "something"
> print res.filter(table.c.column2 > 42).count()
>
> You can find more examples in the unit tests or the sqlobject
> documentation :)
>
> Cheers,
> Jonas
>



-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to