Christopher,

thanks for the quick reply.

>>I'm trying to declare an SQLObject derived class, where a column
>>contains a computed value; the SQL query should look like:
>>
>>SELECT x, my_function(x) FROM mytable
> 
> Is 'my_function' an SQL function?

yes.

> 
> Remember, with SQLObject you don't select rows, you retrieve objects.
> 
> Easiest thing would be to add a getter to your SQLObject class:
> 
> class MyClass(SQLObject):
>     col = StringCol()
> 
>     def _get_computed_value(self):
>         return my_function(self.col)
> 
> where 'my_funtion' is a *Python* function, defined somewhere else, and then 
> do:

Good idea. Actually, your hint gave me a clue how to get the
computed column values the complicated way ;) I added a view to the
database, which returns the computed value, created a ForeignKey
column for this view in the SQLObject for the main table, and added
a _get_ method for the attribute that retreives the value from the
ForeignKey column.

Not very efficient, but I prefer to leave the function in the
database, instead of replacing it by a Python function. If it is
left in the database, I can also access the value without the
SQLObject machinery, and in the case of changes in the function, I
need to maintain only one version, that is independent of the way
how I access the database.

> for obj in MyClass.select():
>     print obj.col, obj.computed_value
> 
>>SELECT x, y, y != my_function(x) FROM mytable
>>
>>Reading the docs, I could not figure out, how a colunm definition
>>should look for the last column.
> 
> Use 'func' from the sqlbuilder module:
> 
> from sqlobject.sqlbuilder import *
> 
> MyClass.select(MyClass.q.y != func.my_function(MyClass.q.y))

As I understand it, this adds the where expression "y !=
my_function(y)" -- but i want again the expression as a column of
the results ;) But that's of course only an "extension" of my first
problem.

Thanks
Abel

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
sqlobject-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to