B
    \                 @   sj   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 G dd dejZ	G d	d
 d
eZ
dd ZdS )   )ARRAY   )elements)
expression)	functions)ColumnCollectionConstraintc               @   sH   e Zd ZdZd Zdd ZdddZdd Zej	fd	d
Z
edd ZdS )aggregate_order_bya  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`

    c             G   sX   t || _t|}|dkr&tdn.|dkr@t |d | _nt j|dt ji| _d S )N    z)at least one ORDER BY element is requiredr   _literal_as_text)r   Z_literal_as_bindstargetlen	TypeErrororder_byZ
ClauseList)selfr   r   Z_lob r   Alib/python3.7/site-packages/sqlalchemy/dialects/postgresql/ext.py__init__4   s    
zaggregate_order_by.__init__Nc             C   s   | S )Nr   )r   Zagainstr   r   r   
self_groupA   s    zaggregate_order_by.self_groupc             K   s   | j | jfS )N)r   r   )r   kwargsr   r   r   get_childrenD   s    zaggregate_order_by.get_childrenc             K   s$   || j f|| _ || jf|| _d S )N)r   r   )r   Zclonekwr   r   r   _copy_internalsG   s    z"aggregate_order_by._copy_internalsc             C   s   | j j| jj S )N)r   _from_objectsr   )r   r   r   r   r   K   s    z aggregate_order_by._from_objects)N)__name__
__module____qualname____doc____visit_name__r   r   r   r   Z_cloner   propertyr   r   r   r   r   r      s   !
r   c               @   s6   e Zd ZdZdZdZeddddd Zd	d
 Z	dS )ExcludeConstraintzA 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

    Zexclude_constraintNwherez:class:`.ExcludeConstraint`z$:paramref:`.ExcludeConstraint.where`c             O   s   g }g }i | _ t| \}}xtt| ||D ]`\\}}}	}
}|
dk	rN||
 |dk	r\|jn|	}|dk	rr|| j |< t|}||||f q,W || _tj	| f||
d|
d|
dd |
dd| _|
d}|dk	rtj|d	d
| _dS )a  
        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.

        Nname
deferrable	initially)r!   r"   r#   usingZgistr    T)Zallow_coercion_to_text)	operatorszipZ"_extract_col_expression_collectionappendr!   r   Z_literal_as_columnZ_render_exprsr   r   getr$   r
   r    )r   r   r   columnsZrender_exprsZexpressionsr%   exprcolumnZstrnameZadd_elementoperatorr!   r    r   r   r   r   ^   s2    D



zExcludeConstraint.__init__c                sL    fdd j  D } j| j j j j jd}|j	 j |S )Nc                s   g | ]}| j | fqS r   )r%   ).0col)r   r   r   
<listcomp>   s    z*ExcludeConstraint.copy.<locals>.<listcomp>)r!   r"   r#   r    r$   )
r)   keys	__class__r!   r"   r#   r    r$   dispatchZ_update)r   r   r   cr   )r   r   copy   s    zExcludeConstraint.copy)
r   r   r   r   r   r    r   Z_document_text_coercionr   r4   r   r   r   r   r   P   s   gr   c              O   s   t |d< tjj| |S )zPostgreSQL-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

    Z_default_array_type)r   r   func	array_agg)argr   r   r   r   r6      s    	r6   N)Zarrayr   Zsqlr   r   r   Z
sql.schemar   ZColumnElementr   r   r6   r   r   r   r   <module>   s   A 