On Friday, October 3, 2003, at 10: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.
>
> 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?
Hmm... well, there's two string situations, (str in col) and (col in
str). (str in col) means "col LIKE '%str%'", and (col in str) means
"str LIKE '%'||col||'%'". And then (col in tuple) means "col IN
(tuple...)".
But I don't think (col in str) can work, because we can't override
str's __contains__. Which is why we can't really do that with tuples
either (and hence the IN() function -- though that function could be
extended to take strings in addition to tuples.
> Alternatively, a contains() method similar to startswith() and
> endswith() would be useful.
That seems better. Since we can't make the "in" operator work for all
cases, I think it will just be more confusing if it works some places
and not others. Easier to just add another method.
Ian
|