ó
¿b›]c           @   sŒ   d  d l  m Z d d l m Z d d l m Z d d l m Z d d l m Z d e j f d „  ƒ  YZ	 d	 e f d
 „  ƒ  YZ
 d „  Z d S(   i   (   t   ARRAYi   (   t   elements(   t
   expression(   t	   functions(   t   ColumnCollectionConstraintt   aggregate_order_byc           B   sP   e  Z d  Z d Z d „  Z d d „ Z d „  Z e j	 d „ Z
 e d „  ƒ Z RS(   sõ  Represent a PostgreSQL aggregate order by expression.

    E.g.::

        from sqlalchemy.dialects.postgresql import aggregate_order_by
        expr = func.array_agg(aggregate_order_by(table.c.a, table.c.b.desc()))
        stmt = select([expr])

    would represent the expression::

        SELECT array_agg(a ORDER BY b DESC) FROM table;

    Similarly::

        expr = func.string_agg(
            table.c.a,
            aggregate_order_by(literal_column("','"), table.c.a)
        )
        stmt = select([expr])

    Would represent::

        SELECT string_agg(a, ',' ORDER BY a) FROM table;

    .. versionadded:: 1.1

    .. versionchanged:: 1.2.13 - the ORDER BY argument may be multiple terms

    .. seealso::

        :class:`.array_agg`

    R   c         G   s}   t  j | ƒ |  _ t | ƒ } | d k r9 t d ƒ ‚ n@ | d k r^ t  j | d ƒ |  _ n t  j d t  j | Œ |  _ d  S(   Ni    s)   at least one ORDER BY element is requiredi   t   _literal_as_text(   R   t   _literal_as_bindst   targett   lent	   TypeErrort   order_byt
   ClauseList(   t   selfR   R   t   _lob(    (    sA   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/ext.pyt   __init__4   s    	c         C   s   |  S(   N(    (   R   t   against(    (    sA   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/ext.pyt
   self_groupA   s    c         K   s   |  j  |  j f S(   N(   R   R   (   R   t   kwargs(    (    sA   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/ext.pyt   get_childrenD   s    c         K   s.   | |  j  |  |  _  | |  j |  |  _ d  S(   N(   R   R   (   R   t   clonet   kw(    (    sA   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/ext.pyt   _copy_internalsG   s    c         C   s   |  j  j |  j j S(   N(   R   t   _from_objectsR   (   R   (    (    sA   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/ext.pyR   K   s    N(   t   __name__t
   __module__t   __doc__t   __visit_name__R   t   NoneR   R   R   t   _cloneR   t   propertyR   (    (    (    sA   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/ext.pyR      s   !		t   ExcludeConstraintc           B   sA   e  Z d  Z d Z d Z e j d d d ƒ d „  ƒ Z d „  Z	 RS(   sÝ   A table-level EXCLUDE constraint.

    Defines an EXCLUDE constraint as described in the `postgres
    documentation`__.

    __ http://www.postgresql.org/docs/9.0/static/sql-createtable.html#SQL-CREATETABLE-EXCLUDE

    t   exclude_constraintt   wheres   :class:`.ExcludeConstraint`s$   :paramref:`.ExcludeConstraint.where`c      	   O   si  g  } g  } i  |  _  t | Œ  \ } } xª t |  j | ƒ | ƒ D] \ \ } } }	 }
 } |
 d k	 rt | j |
 ƒ n  | d k	 r‰ | j n |	 } | d k	 r« | |  j  | <n  t j | ƒ } | j | | | f ƒ q@ W| |  _ t	 j
 |  d | j d ƒ d | j d ƒ d | j d ƒ | Œ| j d d ƒ |  _ | j d ƒ } | d k	 ret j | d t ƒ|  _ n  d S(	   s°  
        Create an :class:`.ExcludeConstraint` object.

        E.g.::

            const = ExcludeConstraint(
                (Column('period'), '&&'),
                (Column('group'), '='),
                where=(Column('group') != 'some group')
            )

        The constraint is normally embedded into the :class:`.Table` construct
        directly, or added later using :meth:`.append_constraint`::

            some_table = Table(
                'some_table', metadata,
                Column('id', Integer, primary_key=True),
                Column('period', TSRANGE()),
                Column('group', String)
            )

            some_table.append_constraint(
                ExcludeConstraint(
                    (some_table.c.period, '&&'),
                    (some_table.c.group, '='),
                    where=some_table.c.group != 'some group',
                    name='some_table_excl_const'
                )
            )

        :param \*elements:

          A sequence of two tuples of the form ``(column, operator)`` where
          "column" is a SQL expression element or a raw SQL string, most
          typically a :class:`.Column` object, and "operator" is a string
          containing the operator to use.   In order to specify a column name
          when a  :class:`.Column` object is not available, while ensuring
          that any necessary quoting rules take effect, an ad-hoc
          :class:`.Column` or :func:`.sql.expression.column` object should be
          used.

        :param name:
          Optional, the in-database name of this constraint.

        :param deferrable:
          Optional bool.  If set, emit DEFERRABLE or NOT DEFERRABLE when
          issuing DDL for this constraint.

        :param initially:
          Optional string.  If set, emit INITIALLY <value> when issuing DDL
          for this constraint.

        :param using:
          Optional string.  If set, emit USING <index_method> when issuing DDL
          for this constraint. Defaults to 'gist'.

        :param where:
          Optional SQL expression construct or literal SQL string.
          If set, emit WHERE <predicate> when issuing DDL
          for this constraint.

        t   namet
   deferrablet	   initiallyt   usingt   gistR!   t   allow_coercion_to_textN(   t	   operatorst   zipt"   _extract_col_expression_collectionR   t   appendR"   R   t   _literal_as_columnt   _render_exprsR   R   t   getR%   R   t   TrueR!   (   R   R   R   t   columnst   render_exprst   expressionsR(   t   exprt   columnt   strnamet   add_elementt   operatorR"   R!   (    (    sA   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/ext.pyR   ^   s0    D	+	c         K   s‚   g  |  j  j ƒ  D] } | |  j | f ^ q } |  j d |  j d |  j d |  j d |  j d |  j | Œ } | j	 j
 |  j	 ƒ | S(   NR"   R#   R$   R!   R%   (   R0   t   keysR(   t	   __class__R"   R#   R$   R!   R%   t   dispatcht   _update(   R   R   t   colR   t   c(    (    sA   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/ext.pyt   copyÈ   s    /					N(
   R   R   R   R   R   R!   R   t   _document_text_coercionR   R>   (    (    (    sA   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/ext.pyR   P   s   gc          O   s   t  | d <t j j |  | Ž  S(   sà   PostgreSQL-specific form of :class:`.array_agg`, ensures
    return type is :class:`.postgresql.ARRAY` and not
    the plain :class:`.types.ARRAY`, unless an explicit ``type_``
    is passed.

    .. versionadded:: 1.1

    t   _default_array_type(   R    R   t   funct	   array_agg(   t   argR   (    (    sA   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/ext.pyRB   Ö   s    	
N(   t   arrayR    t   sqlR   R   R   t
   sql.schemaR   t   ColumnElementR   R   RB   (    (    (    sA   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/ext.pyt   <module>   s   A†