diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2006-05-25 14:20:23 +0000 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2006-05-25 14:20:23 +0000 |
| commit | bb79e2e871d0a4585164c1a6ed626d96d0231975 (patch) | |
| tree | 6d457ba6c36c408b45db24ec3c29e147fe7504ff /examples/vertical | |
| parent | 4fc3a0648699c2b441251ba4e1d37a9107bd1986 (diff) | |
| download | sqlalchemy-bb79e2e871d0a4585164c1a6ed626d96d0231975.tar.gz | |
merged 0.2 branch into trunk; 0.1 now in sqlalchemy/branches/rel_0_1
Diffstat (limited to 'examples/vertical')
| -rw-r--r-- | examples/vertical/vertical.py | 47 |
1 files changed, 30 insertions, 17 deletions
diff --git a/examples/vertical/vertical.py b/examples/vertical/vertical.py index 74ee35f60..fbd9021ff 100644 --- a/examples/vertical/vertical.py +++ b/examples/vertical/vertical.py @@ -5,7 +5,7 @@ import datetime represented in distinct database rows. This allows objects to be created with dynamically changing fields that are all persisted in a normalized fashion.""" -e = create_engine('sqlite://', echo=True) +e = BoundMetaData('sqlite://', echo=True) # this table represents Entity objects. each Entity gets a row in this table, # with a primary key and a title. @@ -48,13 +48,15 @@ class Entity(object): object's _entities dictionary for the appropriate value, and the __setattribute__ method is overridden to set all non "_" attributes as EntityValues within the _entities dictionary. """ - def __init__(self): - """the constructor sets the "_entities" member to an EntityDict. A mapper - will wrap this property with its own history-list object.""" - self._entities = EntityDict() + + # establish the type of '_entities' + _entities = EntityDict + def __getattr__(self, key): """getattr proxies requests for attributes which dont 'exist' on the object to the underying _entities dictionary.""" + if key[0] == '_': + return super(Entity, self).__getattr__(key) try: return self._entities[key].value except KeyError: @@ -88,7 +90,10 @@ class EntityValue(object): the value to the underlying datatype of its EntityField.""" def __init__(self, key=None, value=None): if key is not None: - self.field = class_mapper(EntityField).get_by(name=key) or EntityField(key) + sess = create_session() + self.field = sess.query(EntityField).get_by(name=key) or EntityField(key) + # close the session, which will make a loaded EntityField a detached instance + sess.close() if self.field.datatype is None: if isinstance(value, int): self.field.datatype = 'int' @@ -114,16 +119,17 @@ mapper(EntityField, entity_fields) mapper( EntityValue, entity_values, properties = { - 'field' : relation(EntityField, lazy=False) + 'field' : relation(EntityField, lazy=False, cascade='all') } ) -entitymapper = mapper(Entity, entities, properties = { - '_entities' : relation(EntityValue, lazy=False) +mapper(Entity, entities, properties = { + '_entities' : relation(EntityValue, lazy=False, cascade='save-update') }) # create two entities. the objects can be used about as regularly as # any object can. +session = create_session() entity = Entity() entity.title = 'this is the first entity' entity.name = 'this is the name' @@ -137,14 +143,15 @@ entity2.price = 50 entity2.data = ('hoo', 'ha') # commit -objectstore.commit() +[session.save(x) for x in (entity, entity2)] +session.flush() # we would like to illustate loading everything totally clean from -# the database, so we clear out the objectstore. -objectstore.clear() +# the database, so we clear out the session +session.clear() # select both objects and print -entities = entitymapper.select() +entities = session.query(Entity).select() for entity in entities: print entity.title, entity.name, entity.price, entity.data @@ -152,13 +159,19 @@ for entity in entities: entities[0].price=90 entities[0].title = 'another new title' entities[1].data = {'oof':5,'lala':8} +entity3 = Entity() +entity3.title = 'third entity' +entity3.name = 'new name' +entity3.price = '$1.95' +entity3.data = 'some data' +session.save(entity3) # commit changes. the correct rows are updated, nothing else. -objectstore.commit() +session.flush() -# lets see if that one came through. clear object store, re-select +# lets see if that one came through. clear the session, re-select # and print -objectstore.clear() -entities = entitymapper.select() +session.clear() +entities = session.query(Entity).select() for entity in entities: print entity.title, entity.name, entity.price, entity.data |
