On Friday, October 3, 2003, at 11:36 AM, Simon Willison wrote:
> I just spent some time trying to figure out how to execute the
> equivalent of a "where col like '%text%'" query using SQLObject. I'm
> now using the following:
>
> results = Story.select(CONTAINSSTRING(Story.q.body, query))
>
> This is a bit of an eye-sore, especially compared to the neat
> Story.q.body.startswith('blah') and Story.q.body.endswith('blah')
> shortcuts.
You're in luck. select() takes a string as a where argument. So:
Story.select("body like '%something%'")
(replaced with proper variables and escaping the %'s as necessary, if
applicable.)
The startswith and endwith are just methods of Python string objects,
and because SQLObject is (mostly) transparent, string columns act like
Python strings.
> Ideally I'd like to be able to perform this type of query using
> Python's "in" operator. From reading through SQLBuilder.py this seems
> to be already overloaded to support SQL "something IN ('blah1',
> 'blah2')" operations. Is there any way this could be inteligently
> overloaded to perform like %% queries on strings and IN queries on
> tuples?
Python's "in" operator checks for the existence of a value in a
sequence. What you're trying to do is ask for all the objects (or all
the SQLObjects, as it were :) that meet a certain criteria. The
semantics are different.
> Alternatively, a contains() method similar to startswith() and
> endswith() would be useful.
Python strings already have a find() method, but I think you meant to
pass a string as the where clause.
Hope that helps,
--
Brad Bollenbach
BBnet.ca
|