Example #1
0
def composite(class_, *cols, **kwargs):
    """Return a composite column-based property for use with a Mapper.

    This is very much like a column-based property except the given class
    is used to represent "composite" values composed of one or more columns.

    The class must implement a constructor with positional arguments matching
    the order of columns supplied here, as well as a __composite_values__()
    method which returns values in the same order.

    A simple example is representing separate two columns in a table as a
    single, first-class "Point" object::

      class Point(object):
          def __init__(self, x, y):
              self.x = x
              self.y = y
          def __composite_values__(self):
              return (self.x, self.y)

      # and then in the mapping:
      ... composite(Point, mytable.c.x, mytable.c.y) ...

    Arguments are:

    class\_
      The "composite type" class.

    \*cols
      List of Column objects to be mapped.

    group
      A group name for this property when marked as deferred.

    deferred
      When True, the column property is "deferred", meaning that
      it does not load immediately, and is instead loaded when the
      attribute is first accessed on an instance.  See also
      [sqlalchemy.orm#deferred()].

    comparator
      An optional instance of [sqlalchemy.orm#PropComparator] which
      provides SQL expression generation functions for this composite
      type.
    """

    return CompositeProperty(class_, *cols, **kwargs)
Example #2
0
def composite(class_, *cols, **kwargs):
    """Return a composite column-based property for use with a Mapper.

    This is very much like a column-based property except the given class is
    used to represent "composite" values composed of one or more columns.

    The class must implement a constructor with positional arguments matching
    the order of columns supplied here, as well as a __composite_values__()
    method which returns values in the same order.

    A simple example is representing separate two columns in a table as a
    single, first-class "Point" object::

      class Point(object):
          def __init__(self, x, y):
              self.x = x
              self.y = y
          def __composite_values__(self):
              return self.x, self.y
          def __eq__(self, other):
              return other is not None and self.x == other.x and self.y == other.y

      # and then in the mapping:
      ... composite(Point, mytable.c.x, mytable.c.y) ...

    The composite object may have its attributes populated based on the names
    of the mapped columns.  To override the way internal state is set,
    additionally implement ``__set_composite_values__``::

        class Point(object):
            def __init__(self, x, y):
                self.some_x = x
                self.some_y = y
            def __composite_values__(self):
                return self.some_x, self.some_y
            def __set_composite_values__(self, x, y):
                self.some_x = x
                self.some_y = y
            def __eq__(self, other):
                return other is not None and self.some_x == other.x and self.some_y == other.y

    Arguments are:

    class\_
      The "composite type" class.

    \*cols
      List of Column objects to be mapped.

    group
      A group name for this property when marked as deferred.

    deferred
      When True, the column property is "deferred", meaning that it does not
      load immediately, and is instead loaded when the attribute is first
      accessed on an instance.  See also :func:`~sqlalchemy.orm.deferred`.

    comparator_factory
      a class which extends ``sqlalchemy.orm.properties.CompositeProperty.Comparator``
      which provides custom SQL clause generation for comparison operations.

    extension
      an :class:`~sqlalchemy.orm.interfaces.AttributeExtension` instance,
      or list of extensions, which will be prepended to the list of
      attribute listeners for the resulting descriptor placed on the class.
      These listeners will receive append and set events before the
      operation proceeds, and may be used to halt (via exception throw)
      or change the value used in the operation.

    """
    return CompositeProperty(class_, *cols, **kwargs)