o
    y¬´\ñ  ã                   @   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ƒ‚|dkr t  |d ¡| _d S t j|dt jiŽ| _d S )Né    z)at least one ORDER BY element is requiredr   Ú_literal_as_text)r   Ú_literal_as_bindsÚtargetÚlenÚ	TypeErrorÚorder_byÚ
ClauseList)Úselfr   r   Ú_lob© r   úT/var/www/html/env/lib/python3.10/site-packages/sqlalchemy/dialects/postgresql/ext.pyÚ__init__4   s   ÿÿzaggregate_order_by.__init__Nc                 C   s   | S ©Nr   )r   Úagainstr   r   r   Ú
self_groupA   s   zaggregate_order_by.self_groupc                 K   s   | j | jfS r   ©r   r   )r   Úkwargsr   r   r   Úget_childrenD   s   zaggregate_order_by.get_childrenc                 K   s,   || j fi |¤Ž| _ || jfi |¤Ž| _d S r   r   )r   ÚcloneÚkwr   r   r   Ú_copy_internalsG   s   z"aggregate_order_by._copy_internalsc                 C   s   | j j| jj S r   )r   Ú_from_objectsr   ©r   r   r   r   r   K   s   z aggregate_order_by._from_objectsr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__visit_name__r   r   r   r   Ú_cloner   Úpropertyr   r   r   r   r   r      s    "
r   c                   @   s(   e Zd ZdZdZdZdd„ Zdd„ ZdS )ÚExcludeConstraintzÝ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

    Úexclude_constraintNc                 O   sô   g }g }i | _ t|Ž \}}t|  |¡|ƒD ]0\\}}}	}
}|
dur&| |
¡ |dur-|jn|	}|dur8|| j |< t |¡}| |||f¡ q|| _tj	| g|¢R | 
d¡| 
d¡| 
d¡dœŽ | 
dd¡| _| 
d¡}|durxt |¡| _dS 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.

          .. note::

                A plain string passed for the value of "column" is interpreted
                as an arbitrary SQL  expression; when passing a plain string,
                any necessary quoting and escaping syntaxes must be applied
                manually. 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 may
                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.

          .. note::

                A plain string passed here is interpreted as an arbitrary SQL
                expression; when passing a plain string, any necessary quoting
                and escaping syntaxes must be applied manually.

        NÚnameÚ
deferrableÚ	initially)r*   r+   r,   ÚusingÚgistÚwhere)Ú	operatorsÚzipÚ"_extract_col_expression_collectionÚappendr*   r   r
   Ú_render_exprsr   r   Úgetr-   r/   )r   r   r   ÚcolumnsÚrender_exprsÚexpressionsr0   ÚexprÚcolumnÚstrnameÚadd_elementÚoperatorr*   r/   r   r   r   r   ^   s:   K
ÿ


ÿþû
ÿ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   )r0   )Ú.0Úcolr    r   r   Ú
<listcomp>Í   s    z*ExcludeConstraint.copy.<locals>.<listcomp>)r*   r+   r,   r/   r-   )
r6   ÚkeysÚ	__class__r*   r+   r,   r/   r-   ÚdispatchÚ_update)r   r   r   Úcr   r    r   ÚcopyÌ   s   úzExcludeConstraint.copy)r!   r"   r#   r$   r%   r/   r   rF   r   r   r   r   r(   P   s    	nr(   c                  O   s   t |d< tjj| i |¤ŽS )zà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

    Ú_default_array_type)r   r   ÚfuncÚ	array_agg)Úargr   r   r   r   rI   Ú   s   	rI   N)Úarrayr   Úsqlr   r   r   Ú
sql.schemar   ÚColumnElementr   r(   rI   r   r   r   r   Ú<module>   s   A 