> SomeTable.select(SomeTable.q.Foo > 30)
>
> Why doesn't the inner parameter, SomeTable.q.Foo > 30, get evaluated to some boolean value?

.q is an object that returns special attributes of type sqlbuilder.SQLExpression. SQLExpression is a special class that overrides almost all Python magic methods and upon any operation instead of evaluating it constructs another instance of SQLExpression that remembers what operation it has to do. A kind of symbolic algebra. Example:

SQLExpression("foo") > 30

produces SQLExpression("foo", ">", 30) (well, it really produces SQLExpression(SQLExpression("foo")...))

> How does the select(...) method know what to do?

In short, .select() recursively evaluates the top-most SQLExpression to a string:

SQLExpression("foo", ">", 30) => "foo > 30"

and passes the result as a string to the SQL backend.

The longer but more detailed and correct explanation is that .select() produces an instance of SelectResults class that upon being iterated over produces an instance of Iteration class that upon calling its .next() method (it is iterator!) construct the SQL query string, passes it to the backend, fetches the results, wraps every row as SQLObject instance and passes them back to the user.

For the details of the implementation see sqlobject/main.py for SQLObject, sqlobject/sqlbuilder.py for SQLExpression, sqlobject/dbconnection.py for DBConnection class (that constructs the query strings) and Iteration class, and different subdirectories of sqlobject for concrete implementations of connection classes - different backends require different query strings.

Get SQLObject at SourceForge.net. Fast, secure and Free Open Source software downloads