On Thursday 21 August 2008 17:57:31 Michael Bayer wrote:
> On Aug 21, 2008, at 9:03 AM, [EMAIL PROTECTED] wrote:
> > hi
> > i plan to implement "embedded structures" in dbcook (as opposed
> > to referenced structures living in separate tables), and
> > composite props seems to fit nicely.
> >
> > the idea is to achieve something like:
> >
> > class Point( embeddableBase):
> > x = Int()
> > y = Int()
> >
> > class Vertex( base):
> > p1 = Point()
> > p2 = Point()
> >
> > which should create a table/mapping
> > with columns ( p1_x, p1_y, p2_x, p2_y ) and a mapper with the p1,
> > p2 as composite_props.
> >
> > the plain columns are still accessible via the mapper, right?
> > e.g. query(Vertex).filter( Vertex.p1_x >4 )
> > can composite's props be used in query expressions? e.g.
> > query(Vertex).filter( Vertex.p1.x > 4 )
>
> Not by default, the composite attribute p1 and p2 would prevent
> p1_x, p1_y, etc. from being mapped. you could try explicitly
> mapping them, i havent experimented much with that. it becomes a
> gray area since which attribute would it favor for the ultimate
> value to be persisted? plain descriptors look like a better
> solution here (composites are probably an unnecessary feature in
> the first place).
>
> > can composite's props be assigned separately ? e.g. would this
> > work? v = Vertex( ...)
> > v.p1.x = 3
> > v.p2 = Point( 1,2)
> > v.p1_y = 5
>
> yeah that wont work at all. With a descriptor based approach (and
> a Point object that knows how to proxy to the parent object), it
> could.
no worries, here a simulation of what can be done under the dbcook'
hood:
class composer( object):
class proxy( object):
__slots__ = 'obj name'.split()
@staticmethod
def make_name( name, subname): return name + '_' + subname
def __init__( me, obj, name):
me.obj = obj
me.name = name
def __getattr__( me, key):
return getattr( me.obj, me.make_name( me.name, key))
def __setattr__( me, key, val):
if key in me.__slots__:
return object.__setattr__( me, key, val)
return setattr( me.obj, me.make_name( me.name, key), val)
def __init__( me, name):
me.name = name
def __get__( me, obj, klas ):
return me.proxy( obj or klas, me.name)
class Vertex( Base):
a_x = Int()
a_y = Int()
a = composer( 'a')
b_x = Int()
b_y = Int()
b = composer( 'b')
#set/save
...
e2 = Vertex()
e2.a.x = ax = 4
e2.a_y = ay = 12
e2.b.x = 3
e2.b.y = by = 123
assert e2.a.x == e2.a_x == ax
assert e2.b.y == e2.b_y == by
...
#query
print session.query( Vertex).filter( Vertex.a.x < 6 ).all()
print session.query( Vertex).filter( Vertex.a_x < 6 ).all()
so far so good.
now this one obviously wont work as such - i dont know how to make a
arbitrary descriptor behave like a column_expression.. should i
inherit interfaces.PropComparator? (hi Eric)
print session.query( Vertex).filter( Vertex.a == whatever(
6,12) ).all()
svil
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---