[orm-devel] Re: Postgres & Views

Julio Rommi Oņa Miranda juliorom@ceibo.entelnet.bo
Fri, 22 Nov 2002 00:03:12 -0400


Hi Diedrich,

This solution works with view over a single table. But what about a join
query?

There should be something like (I think):

class person(dbclass):
  ...

class address(dbclass):
  ...

class person_adress(viewclass, person, address):
  """information in how to join both classes using
  some commond link (one2one, one2many,many2many)
  and what columns to show"""

This view class should create the sql query that join both tables and handle
the one to many realitons as a list.

Regards

El Jue 21 Nov 2002 09:37, escribiķ:
> 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

--
Julio