o
    Šåg¾?  ã                   @  st  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 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 eded�ZerhddlmZ G dd„ dejƒZG dd„ deƒZdd„ ZG dd„ deje ƒZG dd„ deƒZG dd „ d eƒZ G d!d"„ d"eƒZ!G d#d$„ d$eƒZ"G d%d&„ d&eƒZ#G d'd(„ d(eƒZ$d)S )*é    )Úannotations)ÚAny)ÚTYPE_CHECKING)ÚTypeVaré   )Útypes)ÚARRAYé   )Ú	coercions)Úelements)Ú
expression)Ú	functions)Úroles)Úschema)ÚColumnCollectionConstraint)ÚTEXT)ÚInternalTraversalÚ_T)Úbound)Ú_TraverseInternalsTypec                   @  sr   e Zd ZU dZd ZdZdejfdejfdejfgZ	de
d< 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:

    .. sourcecode:: sql

        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:

    .. sourcecode:: sql

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

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

    .. seealso::

        :class:`_functions.array_agg`

    Ú
postgresqlÚtargetÚtypeÚorder_byr   Ú_traverse_internalsc                 G  sj   t  tj|¡| _| jj| _t|ƒ}|dkrtdƒ‚|dkr)t  tj|d ¡| _d S t	j
|dtjiŽ| _d S )Nr   z)at least one ORDER BY element is requiredr   Ú_literal_as_text_role)r
   Úexpectr   ÚExpressionElementRoler   r   ÚlenÚ	TypeErrorr   r   Ú
ClauseList)Úselfr   r   Ú_lob© r$   ú^/var/www/html/flaskapp/venv/lib/python3.10/site-packages/sqlalchemy/dialects/postgresql/ext.pyÚ__init__N   s   


ÿÿÿzaggregate_order_by.__init__Nc                 C  s   | S ©Nr$   )r"   Úagainstr$   r$   r%   Ú
self_group^   s   zaggregate_order_by.self_groupc                 K  s   | j | jfS r'   ©r   r   )r"   Úkwargsr$   r$   r%   Úget_childrena   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_internalsd   s   z"aggregate_order_by._copy_internalsc                 C  s   | j j| jj S r'   )r   Ú_from_objectsr   ©r"   r$   r$   r%   r0   h   s   z aggregate_order_by._from_objectsr'   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__visit_name__Ústringify_dialectr   Údp_clauseelementÚdp_typer   Ú__annotations__r&   r)   r,   r   Ú_cloner/   Úpropertyr0   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

    Úexclude_constraintNFr   Úwherez:class:`.ExcludeConstraint`z$:paramref:`.ExcludeConstraint.where`c                 O  sü   g }g }i | _ t|Ž \}}tt tj|¡|ƒD ]+\\}}}	}
}|
dur(| |
¡ |dur/|jn|	}|dur:|| j |< | |||f¡ q|| _t	j
| g|¢R | d¡| d¡| d¡dœŽ | dd¡| _| d¡}|durut tj|¡| _| d	i ¡| _dS )
aß  
        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"},
                )
            )

        The exclude constraint defined in this example requires the
        ``btree_gist`` extension, that can be created using the
        command ``CREATE EXTENSION btree_gist;``.

        :param \*elements:

          A sequence of two tuples of the form ``(column, operator)`` where
          "column" is either a :class:`_schema.Column` object, or a SQL
          expression element (e.g. ``func.int8range(table.from, table.to)``)
          or the name of a column as string, and "operator" is a string
          containing the operator to use (e.g. `"&&"` or `"="`).

          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.
          The ``column`` may also be a string SQL expression when
          passed as :func:`_expression.literal_column` or
          :func:`_expression.text`

        :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@   rA   rB   ÚusingÚgistr?   Úops)Ú	operatorsÚzipr
   Ú expect_col_expression_collectionr   ÚDDLConstraintColumnRoleÚappendr@   Ú_render_exprsr   r&   ÚgetrC   r   ÚStatementOptionRoler?   rE   )r"   r   r.   ÚcolumnsÚrender_exprsÚexpressionsrF   ÚexprÚcolumnÚstrnameÚadd_elementÚoperatorr@   r?   r$   r$   r%   r&   ~   s>   _ÿü

ÿþû
zExcludeConstraint.__init__c                   s&   t ƒ  ˆ ¡ ‡ fdd„| jD ƒ| _d S )Nc                   s0   g | ]\}}}t |tƒs|nˆ j| ||f‘qS r$   )Ú
isinstanceÚstrÚc)Ú.0rQ   r@   rU   ©Útabler$   r%   Ú
<listcomp>  s    üýÿz1ExcludeConstraint._set_parent.<locals>.<listcomp>)ÚsuperÚ_set_parentrK   )r"   r[   r.   ©Ú	__class__rZ   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ˆ¡|f‘qS r$   )r   Ú_copy_expressionÚparent)rY   rQ   Ú_rU   ©r"   Útarget_tabler$   r%   r\     s    ýþÿz+ExcludeConstraint._copy.<locals>.<listcomp>)r@   rA   rB   r?   rC   )	rK   r`   r@   rA   rB   r?   rC   ÚdispatchÚ_update)r"   re   r.   r   rX   r$   rd   r%   Ú_copy  s   ûúzExcludeConstraint._copyr'   )r2   r3   r4   r5   r6   r?   Úinherit_cacheÚcreate_drop_stringify_dialectr   Ú_document_text_coercionr&   r^   rh   Ú__classcell__r$   r$   r_   r%   r=   m   s    	ý
 r=   c                  O  s   t |d< tjj| i |¤Ž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.

    Ú_default_array_type)r   r   ÚfuncÚ	array_agg)Úargr.   r$   r$   r%   ro   $  s   ro   c                      s    e Zd ZdZ‡ fdd„Z‡  ZS )Ú_regconfig_fnTc                   sr   t |ƒ}t|ƒdkr!tjtj| d¡tˆ dd ƒˆ tj	d�}|g}ng }‡ fdd„|D ƒ}t
ƒ j|| i |¤Ž d S )Nr   r   r@   )r@   Úapply_propagate_attrsÚtype_c              	     ó(   g | ]}t jtj|tˆ d dƒˆ d�‘qS ©r@   N)r@   rr   ©r
   r   r   r   Úgetattr©rY   rX   r1   r$   r%   r\   @  ó    ú
üÿz*_regconfig_fn.__init__.<locals>.<listcomp>)Úlistr   r
   r   r   r   Úpoprw   r   Ú	REGCONFIGr]   r&   )r"   Úargsr+   Úinitial_argÚ
addtl_argsr_   r1   r%   r&   2  s   
û
ù	z_regconfig_fn.__init__)r2   r3   r4   ri   r&   rl   r$   r$   r_   r%   rq   /  s    rq   c                   @  ó   e Zd ZdZdZejZdS )Úto_tsvectoraÇ  The PostgreSQL ``to_tsvector`` SQL function.

    This function applies automatic casting of the REGCONFIG argument
    to use the :class:`_postgresql.REGCONFIG` datatype automatically,
    and applies a return type of :class:`_postgresql.TSVECTOR`.

    Assuming the PostgreSQL dialect has been imported, either by invoking
    ``from sqlalchemy.dialects import postgresql``, or by creating a PostgreSQL
    engine using ``create_engine("postgresql...")``,
    :class:`_postgresql.to_tsvector` will be used automatically when invoking
    ``sqlalchemy.func.to_tsvector()``, ensuring the correct argument and return
    type handlers are used at compile and execution time.

    .. versionadded:: 2.0.0rc1

    TN)r2   r3   r4   r5   ri   r   ÚTSVECTORr   r$   r$   r$   r%   r�   L  ó    
r�   c                   @  r€   )Ú
to_tsqueryaÃ  The PostgreSQL ``to_tsquery`` SQL function.

    This function applies automatic casting of the REGCONFIG argument
    to use the :class:`_postgresql.REGCONFIG` datatype automatically,
    and applies a return type of :class:`_postgresql.TSQUERY`.

    Assuming the PostgreSQL dialect has been imported, either by invoking
    ``from sqlalchemy.dialects import postgresql``, or by creating a PostgreSQL
    engine using ``create_engine("postgresql...")``,
    :class:`_postgresql.to_tsquery` will be used automatically when invoking
    ``sqlalchemy.func.to_tsquery()``, ensuring the correct argument and return
    type handlers are used at compile and execution time.

    .. versionadded:: 2.0.0rc1

    TN©r2   r3   r4   r5   ri   r   ÚTSQUERYr   r$   r$   r$   r%   r„   b  rƒ   r„   c                   @  r€   )Úplainto_tsqueryaÒ  The PostgreSQL ``plainto_tsquery`` SQL function.

    This function applies automatic casting of the REGCONFIG argument
    to use the :class:`_postgresql.REGCONFIG` datatype automatically,
    and applies a return type of :class:`_postgresql.TSQUERY`.

    Assuming the PostgreSQL dialect has been imported, either by invoking
    ``from sqlalchemy.dialects import postgresql``, or by creating a PostgreSQL
    engine using ``create_engine("postgresql...")``,
    :class:`_postgresql.plainto_tsquery` will be used automatically when
    invoking ``sqlalchemy.func.plainto_tsquery()``, ensuring the correct
    argument and return type handlers are used at compile and execution time.

    .. versionadded:: 2.0.0rc1

    TNr…   r$   r$   r$   r%   r‡   x  rƒ   r‡   c                   @  r€   )Úphraseto_tsqueryaÕ  The PostgreSQL ``phraseto_tsquery`` SQL function.

    This function applies automatic casting of the REGCONFIG argument
    to use the :class:`_postgresql.REGCONFIG` datatype automatically,
    and applies a return type of :class:`_postgresql.TSQUERY`.

    Assuming the PostgreSQL dialect has been imported, either by invoking
    ``from sqlalchemy.dialects import postgresql``, or by creating a PostgreSQL
    engine using ``create_engine("postgresql...")``,
    :class:`_postgresql.phraseto_tsquery` will be used automatically when
    invoking ``sqlalchemy.func.phraseto_tsquery()``, ensuring the correct
    argument and return type handlers are used at compile and execution time.

    .. versionadded:: 2.0.0rc1

    TNr…   r$   r$   r$   r%   rˆ   Ž  rƒ   rˆ   c                   @  r€   )Úwebsearch_to_tsqueryaá  The PostgreSQL ``websearch_to_tsquery`` SQL function.

    This function applies automatic casting of the REGCONFIG argument
    to use the :class:`_postgresql.REGCONFIG` datatype automatically,
    and applies a return type of :class:`_postgresql.TSQUERY`.

    Assuming the PostgreSQL dialect has been imported, either by invoking
    ``from sqlalchemy.dialects import postgresql``, or by creating a PostgreSQL
    engine using ``create_engine("postgresql...")``,
    :class:`_postgresql.websearch_to_tsquery` will be used automatically when
    invoking ``sqlalchemy.func.websearch_to_tsquery()``, ensuring the correct
    argument and return type handlers are used at compile and execution time.

    .. versionadded:: 2.0.0rc1

    TNr…   r$   r$   r$   r%   r‰   ¤  rƒ   r‰   c                      s(   e Zd ZdZdZeZ‡ fdd„Z‡  ZS )Úts_headlinea¾  The PostgreSQL ``ts_headline`` SQL function.

    This function applies automatic casting of the REGCONFIG argument
    to use the :class:`_postgresql.REGCONFIG` datatype automatically,
    and applies a return type of :class:`_types.TEXT`.

    Assuming the PostgreSQL dialect has been imported, either by invoking
    ``from sqlalchemy.dialects import postgresql``, or by creating a PostgreSQL
    engine using ``create_engine("postgresql...")``,
    :class:`_postgresql.ts_headline` will be used automatically when invoking
    ``sqlalchemy.func.ts_headline()``, ensuring the correct argument and return
    type handlers are used at compile and execution time.

    .. versionadded:: 2.0.0rc1

    Tc                   s¨   t |ƒ}t|ƒdk rd}nt|d tjƒr!|d jjtju r!d}nd}|r<t	j
tj| d¡ˆ tˆ dd ƒtjd�}|g}ng }‡ fdd	„|D ƒ}tƒ j|| i |¤Ž d S )
Né   Fr   Tr   r@   )rr   r@   rs   c              	     rt   ru   rv   rx   r1   r$   r%   r\   ì  ry   z(ts_headline.__init__.<locals>.<listcomp>)rz   r   rV   r   ÚColumnElementr   Ú_type_affinityr   r†   r
   r   r   r   r{   rw   r|   r]   r&   )r"   r}   r+   Úhas_regconfigr~   r   r_   r1   r%   r&   Ï  s,   ÿ
û
ù	zts_headline.__init__)	r2   r3   r4   r5   ri   r   r   r&   rl   r$   r$   r_   r%   rŠ   º  s
    rŠ   N)%Ú
__future__r   Útypingr   r   r   Ú r   Úarrayr   Úsqlr
   r   r   r   r   r   Ú
sql.schemar   Úsql.sqltypesr   Úsql.visitorsr   r   r   rŒ   r   r=   ro   ÚGenericFunctionrq   r�   r„   r‡   rˆ   r‰   rŠ   r$   r$   r$   r%   Ú<module>   s:   M 8