diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-01-10 21:21:45 +0000 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-01-10 21:21:45 +0000 |
commit | b729503a3e1165dc37fb5ac4746226761e86fd51 (patch) | |
tree | 9161970621c976498bc8c52068227c4da9273e25 /examples/beaker_caching/model.py | |
parent | 517ed6b7ceb84ee19e43b371d946c7bba4d55ce0 (diff) | |
download | sqlalchemy-b729503a3e1165dc37fb5ac4746226761e86fd51.tar.gz |
- merge() will not issue a needless merge of attributes if the
given instance is the same instance which is already present.
- merge() now also merges the "options" associated with a given
state, i.e. those passed through query.options() which follow
along with an instance, such as options to eagerly- or
lazyily- load various attributes. This is essential for
the construction of highly integrated caching schemes. This
is a subtle behavioral change vs. 0.5.
- A bug was fixed regarding the serialization of the "loader
path" present on an instance's state, which is also necessary
when combining the usage of merge() with serialized state
and associated options that should be preserved.
- The "query_cache" examples have been removed, and are replaced
with a fully comprehensive approach that combines the usage of
Beaker with SQLAlchemy. New query options are used to indicate
the caching characteristics of a particular Query, which
can also be invoked deep within an object graph when lazily
loading related objects. See /examples/beaker_caching/README.
Diffstat (limited to 'examples/beaker_caching/model.py')
-rw-r--r-- | examples/beaker_caching/model.py | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/examples/beaker_caching/model.py b/examples/beaker_caching/model.py new file mode 100644 index 000000000..73f4a80d6 --- /dev/null +++ b/examples/beaker_caching/model.py @@ -0,0 +1,100 @@ +"""Model. We are modeling Person objects with a collection +of Address objects. Each Address has a PostalCode, which +in turn references a City and then a Country: + +Person --(1..n)--> Address +Address --(has a)--> PostalCode +PostalCode --(has a)--> City +City --(has a)--> Country + +""" +from sqlalchemy import Column, Integer, String, ForeignKey +from sqlalchemy.orm import relation +from meta import Base, FromCache, Session + +class Country(Base): + __tablename__ = 'country' + + id = Column(Integer, primary_key=True) + name = Column(String(100), nullable=False) + + def __init__(self, name): + self.name = name + +class City(Base): + __tablename__ = 'city' + + id = Column(Integer, primary_key=True) + name = Column(String(100), nullable=False) + country_id = Column(Integer, ForeignKey('country.id'), nullable=False) + country = relation(Country) + + def __init__(self, name, country): + self.name = name + self.country = country + +class PostalCode(Base): + __tablename__ = 'postal_code' + + id = Column(Integer, primary_key=True) + code = Column(String(10), nullable=False) + city_id = Column(Integer, ForeignKey('city.id'), nullable=False) + city = relation(City) + + @property + def country(self): + return self.city.country + + def __init__(self, code, city): + self.code = code + self.city = city + +class Address(Base): + __tablename__ = 'address' + + id = Column(Integer, primary_key=True) + person_id = Column(Integer, ForeignKey('person.id'), nullable=False) + street = Column(String(200), nullable=False) + postal_code_id = Column(Integer, ForeignKey('postal_code.id')) + postal_code = relation(PostalCode) + + @property + def city(self): + return self.postal_code.city + + @property + def country(self): + return self.postal_code.country + + def __str__(self): + return "%s\t"\ + "%s, %s\t"\ + "%s" % (self.street, self.city.name, + self.postal_code.code, self.country.name) + +class Person(Base): + __tablename__ = 'person' + + id = Column(Integer, primary_key=True) + name = Column(String(100), nullable=False) + addresses = relation(Address, collection_class=set) + + def __init__(self, name, *addresses): + self.name = name + self.addresses = set(addresses) + + def __str__(self): + return self.name + + def format_full(self): + return "\t".join([str(x) for x in [self] + list(self.addresses)]) + +# Caching options. A set of three FromCache options +# which can be applied to Query(), causing the "lazy load" +# of these attributes to be loaded from cache. +cache_address_bits = [ + FromCache("default", "byid", PostalCode.city), + FromCache("default", "byid", City.country), + FromCache("default", "byid", Address.postal_code), + ] + |