From 485fb57fc7fedc423bb4dc6f998e80b14382a42a Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 13 Mar 2007 03:45:28 +0000 Subject: [PATCH] - added a handy multi-use "identity_key()" method to Session, allowing the generation of identity keys for primary key values, instances, and rows, courtesy Daniel Miller --- CHANGES | 4 +++ lib/sqlalchemy/orm/session.py | 48 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/CHANGES b/CHANGES index 18b24cf540..bf8a19f8a0 100644 --- a/CHANGES +++ b/CHANGES @@ -96,6 +96,10 @@ worked anyway), but also query.selectfirst(), query.selectone() which will be used as is (i.e. no query is compiled). works similarly to sending the results to instances(). + + - added a handy multi-use "identity_key()" method to Session, allowing + the generation of identity keys for primary key values, instances, + and rows, courtesy Daniel Miller - added "refresh-expire" cascade [ticket:492]. allows refresh() and expire() calls to propigate along relationships. diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py index a6e1e9ee25..c85b356b3e 100644 --- a/lib/sqlalchemy/orm/session.py +++ b/lib/sqlalchemy/orm/session.py @@ -446,6 +446,54 @@ class Session(object): self.save(merged) return merged + def identity_key(self, *args, **kwargs): + """Get an identity key + + Valid call signatures: + + identity_key(class_, ident, entity_name=None) + class_ - mapped class + ident - primary key, if the key is composite this is a tuple + entity_name - optional entity name. May be given as a + positional arg or as a keyword arg. + + identity_key(instance=instance) + instance - object instance (must be given as a keyword arg) + + identity_key(row=row, class=class_, entity_name=None) + row - result proxy row (must be given as a keyword arg) + """ + if args: + kw = {} + if len(args) == 2: + class_, ident = args + entity_name = kwargs.pop("entity_name", None) + assert not kwargs, ("unknown keyword arguments: %s" + % (kwargs.keys(),)) + else: + assert len(args) == 3, ("two or three positional args are " + "accepted, got %s" % len(args)) + class_, ident, entity_name = args + mapper = _class_mapper(class_, entity_name=entity_name) + return mapper.instance_key_from_primary_key(ident, + entity_name=entity_name) + else: + try: + instance = kwargs.pop("instance") + except KeyError: + row = kwargs.pop("row") + class_ = kwargs.pop("class") + entity_name = kwargs.pop("entity_name", None) + assert not kwargs, ("unknown keyword arguments: %s" + % (kwargs.keys(),)) + mapper = _class_mapper(class_, entity_name=entity_name) + return mapper.identity_key_from_row(row) + else: + assert not kwargs, ("unknown keyword arguments: %s" + % (kwargs.keys(),)) + mapper = _object_mapper(instance) + return mapper.identity_key_from_instance(instance) + def _save_impl(self, object, **kwargs): if hasattr(object, '_instance_key'): if not self.identity_map.has_key(object._instance_key): -- 2.47.2