
H`Tc        	   @   sJ  d  d l  m Z m Z m Z m Z m Z d  d l m Z m Z m	 Z	 d d l
 m Z m Z d d l  m Z d d l Z d d l m Z m Z m Z m Z m Z m Z m Z m Z d d	 l m Z m Z d d
 l m Z d d l m Z e d/  Z d e f d     YZ d   Z  d e! d  Z" d   Z# d e j$ f d     YZ% d e& f d     YZ' d e f d     YZ( e j) e'  d     e j) e(  d!    d d e+ e+ d"  Z, e+ e+ d e+ e+ e+ d#  Z- d d$  Z. d%   Z/ d&   Z0 d' e j1 f d(     YZ2 d e+ d d)  Z3 d d d*  Z4 d+   Z5 d,   Z6 d-   Z7 d.   Z8 d S(0   i   (   t   sqlt   utilt   eventt   exct
   inspection(   t
   expressionR   t	   operatorsi   (   t   PropComparatort   MapperProperty(   t
   attributesiN(   t   instance_strt	   state_strt   state_class_strt   attribute_strt   state_attribute_strt   object_mappert   object_statet	   _none_set(   t   class_mappert   _class_to_mapper(   t   _InspectionAttr(   t   PathRegistryt   deletes   delete-orphant   allt   merget   expunges   save-updates   refresh-expiret   nonet   CascadeOptionsc           B   s>   e  Z d  Z e j d d d g  Z e Z d   Z d   Z RS(   s9   Keeps track of the options sent to relationship().cascadeR   R   s   delete-orphanc         C   sk  t  g  t j d | p d  D] } | r | ^ q  } | j |  j  r t j d d j g  t | j |  j   D] } t	 |  ^ qq    n  d | k r | j
 |  j  n  d | k r | j   n  | j d  t j t |  } d | k | _ d | k | _ d	 | k | _ d
 | k | _ d | k | _ d | k | _ | j rg| j rgt j d  n  | S(   Ns   \s*,\s*t    s   Invalid cascade option(s): %ss   , R   R   s   save-updateR   s   refresh-expireR   R   s   delete-orphans5   The 'delete-orphan' cascade option requires 'delete'.(   t   sett   ret   splitt
   differencet   _allowed_cascadest   sa_exct   ArgumentErrort   joint   sortedt   reprt   updatet   _add_w_all_cascadest   cleart   discardt	   frozensett   __new__R   t   save_updateR   t   refresh_expireR   R   t   delete_orphanR   t   warn(   t   clst   argt   ct   valuest   xt   self(    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyR,   !   s2    	5c         C   s*   d d j  g  t |   D] } | ^ q  S(   Ns   CascadeOptions(%r)t   ,(   R$   R%   (   R6   R5   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt   __repr__D   s    (	   t   __name__t
   __module__t   __doc__t   all_cascadesR    R(   R!   R,   R8   (    (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyR      s   	#c            s   s  f d     n  | ri      f d   }      f d   }      f d   } n0      f d   }      f d   } t  j |  d | d t d	 t t  j |  d
 | d t d	 t | r t  j |  d | d t d	 t n  d S(   sN   Runs a validation method on an attribute value to be set or
    appended.
    c            s   |  j    j } | j | k	 S(   N(   t   managert   impl(   t   statet	   initiatorR>   (   t   key(    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt   detect_is_backrefQ   s    c            s7    s   |  |  r/  |  j     | t  S| Sd  S(   N(   t   objt   False(   R?   t   valueR@   (   RB   t	   validatorRA   t   include_backrefs(    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt   appendV   s    c            s7    s  |  |  r/  |  j      | t  S| Sd  S(   N(   RC   RD   (   R?   RE   t   oldvalueR@   (   RA   RB   RF   RG   (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt   set_\   s    c            s6    s   |  |  r2  |  j     | t  n  d  S(   N(   RC   t   True(   R?   RE   R@   (   RB   RF   RA   RG   (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt   removeb   s    c            s4    s   |  |  r,  |  j     |  S| Sd  S(   N(   RC   (   R?   RE   R@   (   RB   RF   RA   RG   (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyRH   g   s    c            s4    s  |  |  r,  |  j      |  S| Sd  S(   N(   RC   (   R?   RE   RI   R@   (   RA   RB   RF   RG   (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyRJ   m   s    RH   t   rawt   retvalR   RL   N(   R   t   listenRK   (   t   descRA   RF   t   include_removesRG   RH   RJ   RL   (    (   RB   RF   RA   RG   sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt   _validator_eventsJ   s    t   p_unionc            s  t  j   } i   i   x |  D] } |  | } t | t j  rZ | j   } | |  | <n  i  } x> | j D]3 } | j | j  | | | j <| j	  | j <qj W|  | <q W    f d   }	 g  }
 x |  j
   D] \ } } | d k	 rO|
 j t j g  | D] } |	 | |  ^ qt j t j |   j |  g d | g  q |
 j t j g  | D] } |	 | |  ^ qbd | g  q Wt j |
   j |  S(   s)  Create a ``UNION`` statement used by a polymorphic mapper.

    See  :ref:`concrete_inheritance` for an example of how
    this is used.

    :param table_map: mapping of polymorphic identities to
     :class:`.Table` objects.
    :param typecolname: string name of a "discriminator" column, which will be
     derived from the query, producing the polymorphic identity for
     each row.  If ``None``, no polymorphic discriminator is generated.
    :param aliasname: name of the :func:`~sqlalchemy.sql.expression.alias()`
     construct generated.
    :param cast_nulls: if True, non-existent columns, which are represented
     as labeled NULLs, will be passed into CAST.   This is a legacy behavior
     that is problematic on some backends such as Oracle - in which case it
     can be set to False.

    c            st   y  | |  SWn] t  k
 ro   rI t j t j    |   j |   St j t j    |   j |   Sn Xd  S(   N(   t   KeyErrorR    t   castt   nullt   labelt   type_coerce(   t   namet   table(   t
   cast_nullst   typest   colnamemaps(    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt   col   s    #t   from_objN(   R   t
   OrderedSett
   isinstanceR    t   Selectt   aliasR3   t   addRA   t   typet   itemst   NoneRH   t   selectt   literal_columnt   sql_utilt   _quote_ddl_exprRW   t	   union_all(   t	   table_mapt   typecolnamet	   aliasnameR[   t   colnamesRA   RZ   t   mR3   R^   t   resultRe   RY   (    (   R[   R]   R\   sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt   polymorphic_uniony   s6    
	%.c          O   si  |  rt  |   d k r[ |  d } y | j d  } Wq t k
 rW | j d  } q Xn[ t  |   d k r| |  \ } } n: t  |   d k r |  \ } } n t j d t  |     | r t j d d	 j |    n  t |  } d t   k r| j t	 j
 |   S| j |  S| j d
  } | rPt j d d	 j | j    n  t |  } | j |  S(   s  Generate "identity key" tuples, as are used as keys in the
    :attr:`.Session.identity_map` dictionary.

    This function has several call styles:

    * ``identity_key(class, ident)``

      This form receives a mapped class and a primary key scalar or
      tuple as an argument.

      E.g.::

        >>> identity_key(MyClass, (1, 2))
        (<class '__main__.MyClass'>, (1, 2))

      :param class: mapped class (must be a positional argument)
      :param ident: primary key, may be a scalar or tuple argument.


    * ``identity_key(instance=instance)``

      This form will produce the identity key for a given instance.  The
      instance need not be persistent, only that its primary key attributes
      are populated (else the key will contain ``None`` for those missing
      values).

      E.g.::

        >>> instance = MyClass(1, 2)
        >>> identity_key(instance=instance)
        (<class '__main__.MyClass'>, (1, 2))

      In this form, the given instance is ultimately run though
      :meth:`.Mapper.identity_key_from_instance`, which will have the
      effect of performing a database check for the corresponding row
      if the object is expired.

      :param instance: object instance (must be given as a keyword arg)

    * ``identity_key(class, row=row)``

      This form is similar to the class/tuple form, except is passed a
      database result row as a :class:`.RowProxy` object.

      E.g.::

        >>> row = engine.execute("select * from table where a=1 and b=2").first()
        >>> identity_key(MyClass, row=row)
        (<class '__main__.MyClass'>, (1, 2))

      :param class: mapped class (must be a positional argument)
      :param row: :class:`.RowProxy` row returned by a :class:`.ResultProxy`
       (must be given as a keyword arg)

    i   i    t   rowt   identi   i   s1   expected up to three positional arguments, got %ss   unknown keyword arguments: %ss   , t   instance(   t   lent   popRT   R"   R#   R$   R   t   localst   identity_key_from_primary_keyR   t   to_listt   identity_key_from_rowt   keysR   t   identity_key_from_instance(   t   argst   kwargst   class_Rt   Ru   t   mapperRv   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt   identity_key   s6    9
		t
   ORMAdapterc           B   s)   e  Z d  Z d e d d  Z d   Z RS(   s   Extends ColumnAdapter to accept ORM entities.

    The selectable is extracted from the given entity,
    and the AliasedClass if any is referenced.

    c         C   sk   t  j |  } | j |  _ | j } | j } | r? | |  _ n	 d  |  _ t j j	 |  | | | d | d  S(   Nt   adapt_required(
   R   t   inspectR   t
   selectablet   is_aliased_classt   aliased_classRg   Rj   t   ColumnAdaptert   __init__(   R6   t   entityt   equivalentsR   t   chain_tot   infoR   R   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyR     s    				c         C   sI   | j  j d d   } | s. | j |  j  rA t j j |  |  Sd  Sd  S(   Nt   parentmapper(   t   _annotationst   getRg   t   isaR   Rj   R   t   replace(   R6   t   elemR   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyR   '  s    N(   R9   R:   R;   Rg   RD   R   R   (    (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyR     s   t   AliasedClassc        	   B   sA   e  Z d  Z d d e e d d d e d  Z d   Z d   Z RS(   s  Represents an "aliased" form of a mapped class for usage with Query.

    The ORM equivalent of a :func:`sqlalchemy.sql.expression.alias`
    construct, this object mimics the mapped class using a
    __getattr__ scheme and maintains a reference to a
    real :class:`~sqlalchemy.sql.expression.Alias` object.

    Usage is via the :func:`.orm.aliased` function, or alternatively
    via the :func:`.orm.with_polymorphic` function.

    Usage example::

        # find all pairs of users with the same name
        user_alias = aliased(User)
        session.query(User, user_alias).\
                        join((user_alias, User.id > user_alias.id)).\
                        filter(User.name==user_alias.name)

    The resulting object is an instance of :class:`.AliasedClass`.
    This object implements an attribute scheme which produces the
    same attribute and method interface as the original mapped
    class, allowing :class:`.AliasedClass` to be compatible
    with any attribute technique which works on the original class,
    including hybrid attributes (see :ref:`hybrids_toplevel`).

    The :class:`.AliasedClass` can be inspected for its underlying
    :class:`.Mapper`, aliased selectable, and other information
    using :func:`.inspect`::

        from sqlalchemy import inspect
        my_alias = aliased(MyClass)
        insp = inspect(my_alias)

    The resulting inspection object is an instance of :class:`.AliasedInsp`.

    See :func:`.aliased` and :func:`.with_polymorphic` for construction
    argument descriptions.

    c
      
   C   s   t  |  }
 | d  k r6 |
 j j d | d |  } n  t |  |
 | | | rQ | n |
 j | d  k	 ri | n |
 j | |	 | 	 |  _ d |
 j j	 |  _	 d  S(   NRY   t   flats   AliasedClass_%s(
   R   Rg   t   _with_polymorphic_selectableRc   t   AliasedInspt   with_polymorphic_mapperst   polymorphic_ont   _aliased_inspR   R9   (   R6   R1   Rc   RY   R   t   adapt_on_namesR   t   with_polymorphic_discriminatort
   base_aliast   use_mapper_pathR   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyR   X  s"    	c         C   sV  y |  j  d } Wn t k
 r- t    nR XxN | j j D]4 } y t j | |  } Wn t k
 rm q; q; XPq; Wt |   t | t  r | j	 |  } t
 |  | |  | St | d  r
t | j | d   } | r| j d  k	 rt j j | j |  |   Sd  SnH t | d  rN| j d  |   } t | t  rG| j	 |  S| Sn | Sd  S(   NR   t	   func_codet   __get__(   t   __dict__RT   t   AttributeErrort   _targett   __mro__t   objectt   __getattribute__Ra   R   t   adapt_to_entityt   setattrt   hasattrt   getattrRg   t   __self__R   R\   t
   MethodTypet   __func__R   (   R6   RA   R   t   baset   attrt   rett	   is_method(    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt   __getattr__w  s4    c         C   s   d t  |   |  j j j f S(   Ns   <AliasedClass at 0x%x; %s>(   t   idR   R   R9   (   R6   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyR8     s    N(    (   R9   R:   R;   Rg   RD   R   R   R8   (    (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyR   /  s   '	#R   c           B   sk   e  Z d  Z d   Z e Z e d    Z e j	 d    Z
 d   Z d   Z d   Z d   Z d   Z RS(	   s  Provide an inspection interface for an
    :class:`.AliasedClass` object.

    The :class:`.AliasedInsp` object is returned
    given an :class:`.AliasedClass` using the
    :func:`.inspect` function::

        from sqlalchemy import inspect
        from sqlalchemy.orm import aliased

        my_alias = aliased(MyMappedClass)
        insp = inspect(my_alias)

    Attributes on :class:`.AliasedInsp`
    include:

    * ``entity`` - the :class:`.AliasedClass` represented.
    * ``mapper`` - the :class:`.Mapper` mapping the underlying class.
    * ``selectable`` - the :class:`.Alias` construct which ultimately
      represents an aliased :class:`.Table` or :class:`.Select`
      construct.
    * ``name`` - the name of the alias.  Also is used as the attribute
      name when returned in a result tuple from :class:`.Query`.
    * ``with_polymorphic_mappers`` - collection of :class:`.Mapper` objects
      indicating all those mappers expressed in the select construct
      for the :class:`.AliasedClass`.
    * ``polymorphic_on`` - an alternate column or SQL expression which
      will be used as the "discriminator" for a polymorphic load.

    .. seealso::

        :ref:`inspection_toplevel`

    c
         C   s   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ | p? |  |  _ | |  _ t j	 | d | j
 d |	 |  _ |	 |  _ | j |  _ xW |  j D]L }
 |
 | k	 r t |  j  |
 j j t |
 j | d |  d |	 d |  q q Wd  S(   NR   R   R   R   (   R   R   R   RY   R   R   t   _base_aliast   _use_mapper_pathRj   t   ClauseAdaptert   _equivalent_columnst   _adaptert   _adapt_on_namesR   R   R   R9   R   (   R6   R   R   R   RY   R   R   R   R   R   t   poly(    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyR     s&    								c         C   s
   |  j  j S(   sU   Return the mapped class ultimately represented by this
        :class:`.AliasedInsp`.(   R   R   (   R6   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyR     s    c         C   s$   |  j  r |  j j St j |   Sd  S(   N(   R   R   t   _path_registryR   t
   per_mapper(   R6   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyR     s    	
c         C   s^   i	 |  j  d 6|  j d 6|  j d 6|  j d 6|  j d 6|  j d 6|  j d 6|  j d 6|  j d	 6S(
   NR   R   Rc   RY   R   R   R   R   R   (	   R   R   R   RY   R   R   R   R   R   (   R6   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt   __getstate__  s    







c         C   sM   |  j  | d | d | d | d | d | d | d | d | d	 	 d  S(
   NR   R   Rc   RY   R   R   R   R   R   (   R   (   R6   R?   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt   __setstate__  s    c         C   s-   |  j  j |  j i |  j d 6|  j d 6 S(   Nt   parententityR   (   R   t   traverset	   _annotateR   R   (   R6   R   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt   _adapt_element	  s    
c         C   sd   |  j  } | | k r. t |  j | j j  j S| j |  j  rD |  St s` t	 d | |  f   d  S(   Ns"   mapper %s doesn't correspond to %s(
   R   R   R   R   R9   R   R   R   RD   t   AssertionError(   R6   R   t	   self_poly(    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt   _entity_for_mapper  s    	c         C   s   d t  |   |  j j f S(   Ns   <AliasedInsp at 0x%x; %s>(   R   R   R9   (   R6   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyR8     s    (   R9   R:   R;   R   RK   R   t   propertyR   R   t   memoized_propertyR   R   R   R   R   R8   (    (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyR     s   "					
c         C   s   |  j  S(   N(   R   (   t   target(    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt   <lambda>  s    c         C   s   |  S(   N(    (   R   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyR      s    c      
   C   sc   t  |  t j  r= | r* t j d   n  |  j | d | St |  d | d | d | d | Sd S(   s  Produce an alias of the given element, usually an :class:`.AliasedClass`
    instance.

    E.g.::

        my_alias = aliased(MyClass)

        session.query(MyClass, my_alias).filter(MyClass.id > my_alias.id)

    The :func:`.aliased` function is used to create an ad-hoc mapping
    of a mapped class to a new selectable.  By default, a selectable
    is generated from the normally mapped selectable (typically a
    :class:`.Table`) using the :meth:`.FromClause.alias` method.
    However, :func:`.aliased` can also be used to link the class to
    a new :func:`.select` statement.   Also, the :func:`.with_polymorphic`
    function is a variant of :func:`.aliased` that is intended to specify
    a so-called "polymorphic selectable", that corresponds to the union
    of several joined-inheritance subclasses at once.

    For convenience, the :func:`.aliased` function also accepts plain
    :class:`.FromClause` constructs, such as a :class:`.Table` or
    :func:`.select` construct.   In those cases, the :meth:`.FromClause.alias`
    method is called on the object and the new :class:`.Alias` object
    returned.  The returned :class:`.Alias` is not ORM-mapped in this case.

    :param element: element to be aliased.  Is normally a mapped class,
     but for convenience can also be a :class:`.FromClause` element.

    :param alias: Optional selectable unit to map the element to.  This should
     normally be a :class:`.Alias` object corresponding to the :class:`.Table`
     to which the class is mapped, or to a :func:`.select` construct that
     is compatible with the mapping.   By default, a simple anonymous
     alias of the mapped table is generated.

    :param name: optional string name to use for the alias, if not specified
     by the ``alias`` parameter.  The name, among other things, forms the
     attribute name that will be accessible via tuples returned by a
     :class:`.Query` object.

    :param flat: Boolean, will be passed through to the
     :meth:`.FromClause.alias` call so that aliases of :class:`.Join` objects
     don't include an enclosing SELECT.  This can lead to more efficient
     queries in many circumstances.  A JOIN against a nested JOIN will be
     rewritten as a JOIN against an aliased SELECT subquery on backends that
     don't support this syntax.

     .. versionadded:: 0.9.0

     .. seealso:: :meth:`.Join.alias`

    :param adapt_on_names: if True, more liberal "matching" will be used when
     mapping the mapped columns of the ORM entity to those of the
     given selectable - a name-based match will be performed if the
     given selectable doesn't otherwise have a column that corresponds
     to one on the entity.  The use case for this is when associating
     an entity with some derived selectable such as one that uses
     aggregate functions::

        class UnitPrice(Base):
            __tablename__ = 'unit_price'
            ...
            unit_id = Column(Integer)
            price = Column(Numeric)

        aggregated_unit_price = Session.query(
                                    func.sum(UnitPrice.price).label('price')
                                ).group_by(UnitPrice.unit_id).subquery()

        aggregated_unit_price = aliased(UnitPrice,
                    alias=aggregated_unit_price, adapt_on_names=True)

     Above, functions on ``aggregated_unit_price`` which refer to
     ``.price`` will return the
     ``fund.sum(UnitPrice.price).label('price')`` column, as it is
     matched on the name "price".  Ordinarily, the "price" function
     wouldn't have any "column correspondence" to the actual
     ``UnitPrice.price`` column as it is not a proxy of the original.

     .. versionadded:: 0.7.3


    s+   adapt_on_names only applies to ORM elementsR   Rc   RY   R   N(   Ra   R   t
   FromClauseR"   R#   Rc   R   (   t   elementRc   RY   R   R   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt   aliased#  s    Sc   
   
   C   sj   t  |   } | j | | d | \ }	 } | s6 | rK | j d |  } n  t |  | d |	 d | d | S(   s  Produce an :class:`.AliasedClass` construct which specifies
    columns for descendant mappers of the given base.

    .. versionadded:: 0.8
        :func:`.orm.with_polymorphic` is in addition to the existing
        :class:`.Query` method :meth:`.Query.with_polymorphic`,
        which has the same purpose but is not as flexible in its usage.

    Using this method will ensure that each descendant mapper's
    tables are included in the FROM clause, and will allow filter()
    criterion to be used against those tables.  The resulting
    instances will also have those columns already loaded so that
    no "post fetch" of those columns will be required.

    See the examples at :ref:`with_polymorphic`.

    :param base: Base class to be aliased.

    :param classes: a single class or mapper, or list of
        class/mappers, which inherit from the base class.
        Alternatively, it may also be the string ``'*'``, in which case
        all descending mapped classes will be added to the FROM clause.

    :param aliased: when True, the selectable will be wrapped in an
        alias, that is ``(SELECT * FROM <fromclauses>) AS anon_1``.
        This can be important when using the with_polymorphic()
        to create the target of a JOIN on a backend that does not
        support parenthesized joins, such as SQLite and older
        versions of MySQL.

    :param flat: Boolean, will be passed through to the
        :meth:`.FromClause.alias` call so that aliases of :class:`.Join`
        objects don't include an enclosing SELECT.  This can lead to more
        efficient queries in many circumstances.  A JOIN against a nested JOIN
        will be rewritten as a JOIN against an aliased SELECT subquery on
        backends that don't support this syntax.

     Setting ``flat`` to ``True`` implies the ``aliased`` flag is
     also ``True``.

     .. versionadded:: 0.9.0

     .. seealso:: :meth:`.Join.alias`

    :param selectable: a table or select() statement that will
        be used in place of the generated FROM clause. This argument is
        required if any of the desired classes use concrete table
        inheritance, since SQLAlchemy currently cannot generate UNIONs
        among tables automatically. If used, the ``selectable`` argument
        must represent the full set of tables and columns mapped by every
        mapped class. Otherwise, the unaccounted mapped columns will
        result in their table being appended directly to the FROM clause
        which will usually lead to incorrect results.

    :param polymorphic_on: a column to be used as the "discriminator"
        column for the given selectable. If not given, the polymorphic_on
        attribute of the base classes' mapper will be used, if any. This
        is useful for mappings that don't have polymorphic loading
        behavior by default.

    :param innerjoin: if True, an INNER JOIN will be used.  This should
       only be specified if querying for one specific subtype only
    t	   innerjoinR   R   R   R   (   R   t   _with_polymorphic_argsRc   R   (
   R   t   classesR   R   R   R   R   R   t   primary_mappert   mappers(    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt   with_polymorphic  s    C	c         C   s   t  j |  i t d 6|  S(   s   Deep copy the given ClauseElement, annotating each element with the
    "_orm_adapt" flag.

    Elements within the exclude collection will be cloned but not annotated.

    t
   _orm_adapt(   Rj   t   _deep_annotateRK   (   R   t   exclude(    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt   _orm_annotate  s    c         C   s   t  j |  d d S(   s   Remove annotations that link a column to a particular mapping.

    Note this doesn't affect "remote" and "foreign" annotations
    passed by the :func:`.orm.foreign` and :func:`.orm.remote`
    annotators.

    R4   R   R   (   s
   _orm_adapts   parententity(   Rj   t   _deep_deannotate(   R   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt   _orm_deannotate  s    	c         C   s   t  j |   S(   N(   Rj   R   (   R   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt   _orm_full_deannotate  s    t   _ORMJoinc           B   sJ   e  Z d  Z e j j Z d e d  Z d e d d  Z	 d d d  Z
 RS(   s/   Extend Join to support ORM constructs as input.c         C   s  t  j |  } t | d |  } t  j |  } | j } | |  _ t | t j  ri t | j |  } n  t | t	 j
  r | j j   }	 | j }
 n* t | t  r | }
 |
 j j }	 n d  }
 |
 rt j |	 | j  r |	 } n	 | j } |
 j d | d | d t d t d | j  \ } } } } } } | d  k	 r}| r\t j | | |  } | } qt j | | | |  } | } n | } | |  _ n  t j j |  | | | |  d  S(   Nt   _joined_from_infot   source_selectablet   dest_selectablet   source_polymorphict   dest_polymorphict   of_type(   R   R   R   R   R   Ra   R   t   string_typesR   R	   t   QueryableAttributet
   comparatort   _source_selectableR   R   t   parentRg   Rj   t   clause_is_presentt   _create_joinsRK   R   R    R$   t   _target_adapterR   t   JoinR   (   R6   t   leftt   rightt   onclauset   isoutert	   left_infot   left_orm_infot
   right_infot   adapt_tot   on_selectablet   propt
   adapt_fromt   pjt   sjt   sourcet   destt	   secondaryt   target_adapter(    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyR     sD    							c         C   s   t  |  | | |  S(   N(   R   (   R6   R   R   R   t   join_to_left(    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyR$   %  s    c         C   s   t  |  | | t  S(   N(   R   RK   (   R6   R   R   R   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt	   outerjoin(  s    N(   R9   R:   R;   R   R   t   __visit_name__Rg   RD   R   R$   R   (    (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyR     s
   3c         C   s   t  |  | | |  S(   s  Produce an inner join between left and right clauses.

    :func:`.orm.join` is an extension to the core join interface
    provided by :func:`.sql.expression.join()`, where the
    left and right selectables may be not only core selectable
    objects such as :class:`.Table`, but also mapped classes or
    :class:`.AliasedClass` instances.   The "on" clause can
    be a SQL expression, or an attribute or string name
    referencing a configured :func:`.relationship`.

    :func:`.orm.join` is not commonly needed in modern usage,
    as its functionality is encapsulated within that of the
    :meth:`.Query.join` method, which features a
    significant amount of automation beyond :func:`.orm.join`
    by itself.  Explicit usage of :func:`.orm.join`
    with :class:`.Query` involves usage of the
    :meth:`.Query.select_from` method, as in::

        from sqlalchemy.orm import join
        session.query(User).\
            select_from(join(User, Address, User.addresses)).\
            filter(Address.email_address=='foo@bar.com')

    In modern SQLAlchemy the above join can be written more
    succinctly as::

        session.query(User).\
                join(User.addresses).\
                filter(Address.email_address=='foo@bar.com')

    See :meth:`.Query.join` for information on modern usage
    of ORM level joins.

    .. versionchanged:: 0.8.1 - the ``join_to_left`` parameter
       is no longer used, and is deprecated.

    (   R   (   R   R   R   R   R   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyR$   ,  s    &c         C   s   t  |  | | t  S(   s  Produce a left outer join between left and right clauses.

    This is the "outer join" version of the :func:`.orm.join` function,
    featuring the same behavior except that an OUTER JOIN is generated.
    See that function's documentation for other usage details.

    (   R   RK   (   R   R   R   R   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyR   U  s    c         C   sm   t  | t j  r6 t |   } t | j |  j } n t  | t j  rT | j } n  | j	 t
 j |  d t S(   s$  Create filtering criterion that relates this query's primary entity
    to the given related instance, using established :func:`.relationship()`
    configuration.

    The SQL rendered is the same as that rendered when a lazy loader
    would fire off from the given parent on that attribute, meaning
    that the appropriate state is taken from the parent object in
    Python without the need to render joins to the parent table
    in the rendered statement.

    .. versionchanged:: 0.6.4
        This method accepts parent instances in all
        persistence states, including transient, persistent, and detached.
        Only the requisite primary key/foreign key attributes need to
        be populated.  Previous versions didn't work with transient
        instances.

    :param instance:
      An instance which has some :func:`.relationship`.

    :param property:
      String property name, or class-bound attribute, which indicates
      what relationship from the instance should be used to reconcile the
      parent/child relationship.

    t   value_is_parent(   Ra   R   R   R   R   R   R   R	   R   t   compareR   t   eqRK   (   Rv   R   R   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt   with_parent`  s    c         C   s   t  j |   } | j S(   s   Return True if the given object has a database
    identity.

    This typically corresponds to the object being
    in either the persistent or detached state.

    .. seealso::

        :func:`.was_deleted`

    (   R	   t   instance_statet   has_identity(   R   R?   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyR    s    c         C   s   t  j |   } | j S(   sj   Return True if the given object was deleted
    within a session flush.

    .. versionadded:: 0.8.0

    (   R	   R  t   deleted(   R   R?   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt   was_deleted  s    c          C   sk   d d l  m }  m } m } m } d d l m } d d l m } | | _	 |  _	 | _	 | _	 | _	 d S(   s  Use random-ordering sets within the unit of work in order
    to detect unit of work sorting issues.

    This is a utility function that can be used to help reproduce
    inconsistent unit of work sorting issues.   For example,
    if two kinds of objects A and B are being inserted, and
    B has a foreign key reference to A - the A must be inserted first.
    However, if there is no relationship between A and B, the unit of work
    won't know to perform this sorting, and an operation may or may not
    fail, depending on how the ordering works out.   Since Python sets
    and dictionaries have non-deterministic ordering, such an issue may
    occur on some runs and not on others, and in practice it tends to
    have a great dependence on the state of the interpreter.  This leads
    to so-called "heisenbugs" where changing entirely irrelevant aspects
    of the test program still cause the failure behavior to change.

    By calling ``randomize_unitofwork()`` when a script first runs, the
    ordering of a key series of sets within the unit of work implementation
    are randomized, so that the script can be minimized down to the
    fundamental mapping and operation that's failing, while still reproducing
    the issue on at least some runs.

    This utility is also available when running the test suite via the
    ``--reversetop`` flag.

    .. versionadded:: 0.8.1 created a standalone version of the
       ``--reversetop`` feature.

    i(   t
   unitofworkt   sessionR   t
   dependency(   t   topological(   t	   RandomSetN(
   t   sqlalchemy.ormR  R	  R   R
  t   sqlalchemy.utilR  t   sqlalchemy.testing.utilR  R   (   R  R	  R   R
  R  R  (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt   randomize_unitofwork  s    "(   s   deletes   delete-orphans   alls   merges   expunges   save-updates   refresh-expires   none(9   R   R    R   R   R   R"   R   R   Rj   R   t
   interfacesR   R   R	   R   R   R
   R   R   R   R   R   R   R   R   R   R   t   path_registryR   R+   R<   R   RR   RK   Rs   R   R   R   R   R   R   t	   _inspectsRg   RD   R   R   R   R   R   R   R   R$   R   R  R  R  R  (    (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/util.pyt   <module>	   sF   (: 	0	0?	Wp^M
		?)	&		