[orm-devel] Re: Postgres & Views
Diedrich Vorberg
diedrich@tux4web.de
Thu, 21 Nov 2002 14:37:49 +0100
Hi Mike,
>Ran across something which I wonder if you could consider - the use of "views".
Personally I have never used views. My knowlage of advanced SQL
features is rather limited I have to admint. But here's a patch for
you, that you might want to try out.
This will give you a class dbclass.viewclass whoes attributes are read-
only (a set will raise an exception). To access your view you will
have to create a subclass of that just like you'd create a dbobj for
a table. The code that goes into adapters.psql.datasource will then
check if the dbclass passed as an argument is a subclass of dbview
and not add the relation.oid to the SELECT statement.
You might want to subclass a viewclass from an existing dbclass (the
relationship table/view looks kinda like class/subclass):
class person(dbclass):
columns = { this and that }
class view_person(viewclass, person):
"""
view_person will have the same columns as person.
It's important to have viewclass as the first class to inherit
from or else an SQL exception will be called on a set. (If two
methods from baseclasses are named the same the method of the
first baseclass will be called).
"""
You might find it a little overdone to introduce another baseclass
for all those dbclasses you want to use a view with. Perhaps a simple
"is_view" attribute in dbclass would do. This is just a design
suggestion.
This code is untested, sorry, but I didn't have the time.
Diedrich
PS: If the diff doesn't work download
http://diedri.ch/orm/orm-0.6-views.tar.gz
diff --recursive orm/orm/adapters/psql/datasource.py mike/orm/adapters/psql/datasource.py
73a74
> import dbclass
159,162c160,165
< if len(str) == 0:
< str = dbobj.relation() + ".oid"
< else:
< str = "%s.oid, %s" % (dbobj.relation(), str)
---
>
> if not issubclass(dbobj, dbclass.viewclass):
> if len(str) == 0:
> str = dbobj.relation() + ".oid"
> else:
> str = "%s.oid, %s" % (dbobj.relation(), str)
diff --recursive orm/orm/dbclass.py mike/orm/dbclass.py
317a318,324
>
> class viewclass(dbclass):
> def __init__(self):
> dbclass.__init__(self)
>
> def __setattr__(self, name, value):
> raise ViewsAreReadOnly("Views are read only!")
diff --recursive orm/orm/errors.py mike/orm/errors.py
53a54
> class ViewsAreReadOnly(Exception): pass
--
_..._ Diedrich Vorberg
.' '.
/ _ _ \ http://www.tux4web.de
| (o)_(o) | info@tux4web.de
\( ) / .---.
//'._.'\ \ / \ Internet Dienstleistungen
// . \ \ \.@-@./ und 'Consulting'.
|| . \ \ /`\_/`\
|\ : / | // _ \\ Linux Rules!
\ `) ' (` /_ | \ )|_
_)``".____,.'"` (_ /`\_`> <_/ \
) )'--'( ( \__/'---'\__/
'---` `---`