I'm trying to implement a Composite Pattern, 
http://en.wikipedia.org/wiki/Composite_pattern

In this case its to represent some holiday calendars. I need to
implement a union calendar that is a collection of other calendars.

So a straightforward hierarchy. Calendar, abstract as the top level
class. SimpleCalendar that inherits from Calendar. Then a union
calendar that inherits from Calendar, but I need to implement
children, a collection of calendars.

So its a self referential hierarchy. The point to note is that one
calendar can appear in more than one union calendar, so its a self
referential many to many. Its certain I need an association table as a
result.

I'm not quite sure how to configure this.

I'm trying something along these lines

calendars_table = Table \
    (
    'calendars',
    metadata,
    Column ('id', Integer, primary_key=True),
    Column ('code', String, nullable=False, unique=True),
    Column ('name', String),
    Column ('calendar_type', String (20), nullable=False),
    Column ('monday', Boolean),
    Column ('tuesday', Boolean),
    Column ('wednesday', Boolean),
    Column ('thurday', Boolean),
    Column ('friday', Boolean),
    Column ('saturday', Boolean),
    Column ('sunday', Boolean)
    )

calendar_children_table = Table \
    (
    'calendar_children',
    metadata,
    Column ('parent_id', Integer, ForeignKey ('calendars.id')),
    Column ('child_id',  Integer, ForeignKey ('calendars.id'))
    )

calendar_mapper = mapper (Calendar, calendars_table,
polymorphic_on=calendars_table.c.calendar_type,
polymorphic_identity="calendar")
simple_calendar_mapper = mapper (SimpleCalendar,
inherits=calendar_mapper,
polymorphic_identity='simple',properties={'holidays':relationship(Holiday,
backref='calendar')})
union_calendar_mapper  = mapper (UnionCalendar,
inherits=calendar_mapper, polymorphic_identity='union')

holidays_table = Table \
    (
    'holidays',
    metadata,
    Column ('id',          Integer, primary_key=True),
    Column ('holiday_type',        String(20),  nullable=False),
    Column ('calendar_id', Integer, ForeignKey ('calendars.id')),
    Column ('description', String ),
    Column ('year',        Integer),
    Column ('month',       Integer),
    Column ('day',         Integer)
    )

holiday_mapper           = mapper (Holiday,          holidays_table,
polymorphic_on=holidays_table.c.holiday_type,
polymorphic_identity='holiday')
repeating_holiday_mapper = mapper (RepeatingHoliday,
inherits=holiday_mapper, polymorphic_identity='repeating')
simple_holiday_mapper    = mapper (SimpleHoliday,
inherits=holiday_mapper, polymorphic_identity='simple')

However, I'm not sure how to map the relationships with the
association table.

Points that I think are relevant are

1. It's only the UnionCalendar that has children.
2. Deleting a UnionCalendar should delete the entries in the
association table, but not the children calendars.
3. Navigation is really only needed from the parent UnionCalendar to
the underlying children calendars. However, having a collection back
the other way might have some uses.
4. For SimpleCalender to its Holidays, I haven't yet implemented the
cascaded delete, but I know how to do this.

Thanks

Nick

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to