U
    ø×ccÌ!  ã                   @   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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é   )Úutil)Ú	coercions)Úelements)Ú
expression)Ú	functions)Úroles)Úschema)ÚColumnCollectionConstraint)ÚInternalTraversalc                   @   sh   e Zd ZdZd ZdZdejfdejfdejfg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:`_functions.array_agg`

    Ú
postgresqlÚtargetÚtypeÚorder_byc                 G   sj   t  tj|¡| _| jj| _t|ƒ}|dkr4tdƒ‚n2|dkrRt  tj|d ¡| _nt	j
|dtjiŽ| _d S )Né    z)at least one ORDER BY element is requiredr   Z_literal_as_text_role)r   Úexpectr	   ZExpressionElementRoler   r   ÚlenÚ	TypeErrorr   r   Z
ClauseList)Úselfr   r   Z_lob© r   úF/tmp/pip-unpacked-wheel-8u86ls_i/sqlalchemy/dialects/postgresql/ext.pyÚ__init__@   s    

 ÿÿÿzaggregate_order_by.__init__Nc                 C   s   | S ©Nr   )r   Zagainstr   r   r   Ú
self_groupP   s    zaggregate_order_by.self_groupc                 K   s   | j | jfS r   ©r   r   )r   Úkwargsr   r   r   Úget_childrenS   s    zaggregate_order_by.get_childrenc                 K   s$   || j f|Ž| _ || jf|Ž| _d S r   r   )r   ÚcloneÚkwr   r   r   Ú_copy_internalsV   s    z"aggregate_order_by._copy_internalsc                 C   s   | j j| jj S r   )r   Ú_from_objectsr   )r   r   r   r   r"   Z   s    z aggregate_order_by._from_objects)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__visit_name__Zstringify_dialectr   Zdp_clauseelementZdp_typeZ_traverse_internalsr   r   r   r   Z_cloner!   Úpropertyr"   r   r   r   r   r      s   "ý
r   c                       sP   e Zd ZdZdZdZdZdZe 	ddd¡d	d
„ ƒZ
‡ fdd„Zddd„Z‡  ZS )ÚExcludeConstraintzäA table-level EXCLUDE constraint.

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

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

    Zexclude_constraintNFr   Úwherez:class:`.ExcludeConstraint`z$:paramref:`.ExcludeConstraint.where`c                 O   sú   g }g }i | _ t|Ž \}}tt tj|¡|ƒD ]V\\}}}	}
}|
dk	rP| |
¡ |dk	r^|jn|	}|dk	rt|| j |< | |||f¡ q.|| _t	j
| f|ž| d¡| d¡| d¡dœŽ | dd¡| _| d¡}|dk	rèt tj|¡| _| d	i ¡| _dS )
a2  
        Create an :class:`.ExcludeConstraint` object.

        E.g.::

            const = ExcludeConstraint(
                (Column('period'), '&&'),
                (Column('group'), '='),
                where=(Column('group') != 'some group'),
                ops={'group': 'my_operator_class'}
            )

        The constraint is normally embedded into the :class:`_schema.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',
                    ops={'group': 'my_operator_class'}
                )
            )

        :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:`_schema.Column` object,
          and "operator" is a string
          containing the operator to use.   In order to specify a column name
          when a  :class:`_schema.Column` object is not available,
          while ensuring
          that any necessary quoting rules take effect, an ad-hoc
          :class:`_schema.Column` or :func:`_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.

        :param ops:
          Optional dictionary.  Used to define operator classes for the
          elements; works the same way as that of the
          :ref:`postgresql_ops <postgresql_operator_classes>`
          parameter specified to the :class:`_schema.Index` construct.

          .. versionadded:: 1.3.21

          .. seealso::

            :ref:`postgresql_operator_classes` - general description of how
            PostgreSQL operator classes are specified.

        NÚnameÚ
deferrableÚ	initially)r+   r,   r-   ÚusingZgistr*   Úops)Ú	operatorsÚzipr   Z expect_col_expression_collectionr	   ZDDLConstraintColumnRoleÚappendr+   Ú_render_exprsr   r   Úgetr.   r   ZStatementOptionRoler*   r/   )r   r   r    ÚcolumnsZrender_exprsZexpressionsr0   ÚexprÚcolumnZstrnameZadd_elementÚoperatorr+   r*   r   r   r   r   p   s@    W ÿü

ÿþû
zExcludeConstraint.__init__c                    s0   t t| ƒ |¡ dd„ t | j| j¡D ƒ| _d S )Nc                 S   s0   g | ](\\}}}}t |tjƒr"|n|||f‘qS r   )Ú
isinstancer   ZClauseElement)Ú.0r6   r+   r8   Zcolexprr   r   r   Ú
<listcomp>ñ   s
   üýz1ExcludeConstraint._set_parent.<locals>.<listcomp>)Úsuperr)   Ú_set_parentr   Úzip_longestr3   r5   )r   Útabler    ©Ú	__class__r   r   r=   î   s     ÿúzExcludeConstraint._set_parentc                    sJ   ‡ ‡fdd„ˆ j D ƒ}ˆ j|ˆ jˆ jˆ jˆ jˆ jdœŽ}|j ˆ j¡ |S )Nc                    s(   g | ] }t  |ˆ jˆ¡ˆ j|j f‘qS r   )r
   Z_copy_expressionÚparentr0   r+   )r:   r6   ©r   Útarget_tabler   r   r;   ý   s   ý
þz+ExcludeConstraint._copy.<locals>.<listcomp>)r+   r,   r-   r*   r.   )	r5   rA   r+   r,   r-   r*   r.   ÚdispatchÚ_update)r   rD   r    r   Úcr   rC   r   Ú_copyü   s    ûúzExcludeConstraint._copy)N)r#   r$   r%   r&   r'   r*   Zinherit_cacheZcreate_drop_stringify_dialectr   Z_document_text_coercionr   r=   rH   Ú__classcell__r   r   r@   r   r)   _   s   	ý
yr)   c                  O   s   t |d< tjj| |ŽS )zêPostgreSQL-specific form of :class:`_functions.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   rK     s    	rK   N)Úarrayr   Ú r   Zsqlr   r   r   r   r	   r
   Z
sql.schemar   Zsql.visitorsr   ZColumnElementr   r)   rK   r   r   r   r   Ú<module>   s   K 2