Esempio n. 1
0
def contains_alias(alias):
    """Return a ``MapperOption`` that will indicate to the query that
    the main table has been aliased.

    `alias` is the string name or ``Alias`` object representing the
    alias.
    """

    class AliasedRow(MapperExtension):
        def __init__(self, alias):
            self.alias = alias
            if isinstance(self.alias, basestring):
                self.selectable = None
            else:
                self.selectable = alias
            self._row_translators = {}
        def get_selectable(self, mapper):
            if self.selectable is None:
                self.selectable = mapper.mapped_table.alias(self.alias)
            return self.selectable
        def translate_row(self, mapper, context, row):
            if mapper in self._row_translators:
                return self._row_translators[mapper](row)
            else:
                translator = create_row_adapter(self.get_selectable(mapper), mapper.mapped_table)
                self._row_translators[mapper] = translator
                return translator(row)

    return ExtensionOption(AliasedRow(alias))
Esempio n. 2
0
def contains_alias(alias):
    """Return a ``MapperOption`` that will indicate to the query that
    the main table has been aliased.

    `alias` is the string name or ``Alias`` object representing the
    alias.
    """
    class AliasedRow(MapperExtension):
        def __init__(self, alias):
            self.alias = alias
            if isinstance(self.alias, basestring):
                self.selectable = None
            else:
                self.selectable = alias

        def get_selectable(self, mapper):
            if self.selectable is None:
                self.selectable = mapper.mapped_table.alias(self.alias)
            return self.selectable

        def translate_row(self, mapper, context, row):
            newrow = sautil.DictDecorator(row)
            selectable = self.get_selectable(mapper)
            for c in mapper.mapped_table.c:
                c2 = selectable.corresponding_column(c,
                                                     keys_ok=True,
                                                     raiseerr=False)
                if c2 and c2 in row:
                    newrow[c] = row[c2]
            return newrow

    return ExtensionOption(AliasedRow(alias))
Esempio n. 3
0
def extension(ext):
    """Return a ``MapperOption`` that will insert the given
    ``MapperExtension`` to the beginning of the list of extensions
    that will be called in the context of the ``Query``.

    Used with ``query.options()``.
    """

    return ExtensionOption(ext)
Esempio n. 4
0
def contains_alias(alias):
    """Return a ``MapperOption`` that will indicate to the query that
    the main table has been aliased.

    `alias` is the string name or ``Alias`` object representing the
    alias.
    """
    class AliasedRow(MapperExtension):
        def __init__(self, alias):
            self.alias = alias
            if isinstance(self.alias, basestring):
                self.translator = None
            else:
                self.translator = create_row_adapter(alias)

        def translate_row(self, mapper, context, row):
            if not self.translator:
                self.translator = create_row_adapter(
                    mapper.mapped_table.alias(self.alias))
            return self.translator(row)

    return ExtensionOption(AliasedRow(alias))