From: Mike Bayer Date: Mon, 5 Dec 2011 22:25:53 +0000 (-0500) Subject: refine this a bit X-Git-Tag: rel_0_7_4~22 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a582c987d7fb714cc24abc7bba59c536a145c8d8;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git refine this a bit --- diff --git a/lib/sqlalchemy/ext/hybrid.py b/lib/sqlalchemy/ext/hybrid.py index 31732b93b3..02a3d7d530 100644 --- a/lib/sqlalchemy/ext/hybrid.py +++ b/lib/sqlalchemy/ext/hybrid.py @@ -489,16 +489,30 @@ using :attr:`.Operators.eq` against the left and right sides, passing into We can modify the pattern to be more verbose but flexible by separating the "join" step from the "filter" step. The tricky part here is ensuring that successive instances of ``GrandparentTransformer`` use the same -:class:`.AliasedClass` object against ``Node`` - we put it at the -class level here but other memoizing approaches can be used:: +:class:`.AliasedClass` object against ``Node``. Below we use a simple +memoizing approach that associates a ``GrandparentTransformer`` +with each class:: + + class Node(Base): + + # ... + + @grandparent.comparator + def grandparent(cls): + # memoize a GrandparentTransformer + # per class + if '_gp' not in cls.__dict__: + cls._gp = GrandparentTransformer(cls) + return cls._gp class GrandparentTransformer(Comparator): - parent_alias = aliased(Node) + + def __init__(self, cls): + self.parent_alias = aliased(cls) @property def join(self): def go(q): - expression = self.__clause_element__() return q.join(self.parent_alias, Node.parent) return go