
    l iC                    F   d dl mZ d dl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 ddlmZ e	rTd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#          Z. e
d$e#          Z/ej0        fd7d-Zej0        fd7d.Z1 G d/ d0ej2        e.                   Z3 G d1 d2ej4        e.                   Z4d8d6Z5dS )9    )annotationsN)Any)Iterable)Optional)Sequence)TYPE_CHECKING)TypeVar)Union   )CONTAINED_BY)CONTAINS)OVERLAP   )types)util)
expression)	operators)InternalTraversal)Dialect)_ColumnExpressionArgument)_TypeEngineArgument)ColumnElement)Grouping)BindParameter)OperatorType)_SelectIterable)_BindProcessorType)_LiteralProcessorType)_ResultProcessorType)
TypeEngine)_TraverseInternalsType)Self_T)bound_CTother
typing_Anyarrexpr_ColumnExpressionArgument[_T]operatorr   returnColumnElement[bool]c                .    |                     | |          S )zjA synonym for the ARRAY-level :meth:`.ARRAY.Comparator.any` method.
    See that method for details.

    )anyr&   r(   r*   s      [/var/www/findabus/venv/lib/python3.11/site-packages/sqlalchemy/dialects/postgresql/array.pyr   r   2        ;;uh'''    c                .    |                     | |          S )zjA synonym for the ARRAY-level :meth:`.ARRAY.Comparator.all` method.
    See that method for details.

    )allr/   s      r0   Allr5   ?   r1   r2   c                       e Zd ZU dZd ZdZdej        fdej        fgZ	de
d<   ddd fdZed d            Z	 	 d!d"dZ	 d#d$dZ xZS )%arraya  A PostgreSQL ARRAY literal.

    This is used to produce ARRAY literals in SQL expressions, e.g.::

        from sqlalchemy.dialects.postgresql import array
        from sqlalchemy.dialects import postgresql
        from sqlalchemy import select, func

        stmt = select(array([1, 2]) + array([3, 4, 5]))

        print(stmt.compile(dialect=postgresql.dialect()))

    Produces the SQL:

    .. sourcecode:: sql

        SELECT ARRAY[%(param_1)s, %(param_2)s] ||
            ARRAY[%(param_3)s, %(param_4)s, %(param_5)s]) AS anon_1

    An instance of :class:`.array` will always have the datatype
    :class:`_types.ARRAY`.  The "inner" type of the array is inferred from the
    values present, unless the :paramref:`_postgresql.array.type_` keyword
    argument is passed::

        array(["foo", "bar"], type_=CHAR)

    When constructing an empty array, the :paramref:`_postgresql.array.type_`
    argument is particularly important as PostgreSQL server typically requires
    a cast to be rendered for the inner type in order to render an empty array.
    SQLAlchemy's compilation for the empty array will produce this cast so
    that::

        stmt = array([], type_=Integer)
        print(stmt.compile(dialect=postgresql.dialect()))

    Produces:

    .. sourcecode:: sql

        ARRAY[]::INTEGER[]

    As required by PostgreSQL for empty arrays.

    .. versionadded:: 2.0.40 added support to render empty PostgreSQL array
       literals with a required cast.

    Multidimensional arrays are produced by nesting :class:`.array` constructs.
    The dimensionality of the final :class:`_types.ARRAY`
    type is calculated by
    recursively adding the dimensions of the inner :class:`_types.ARRAY`
    type::

        stmt = select(
            array(
                [array([1, 2]), array([3, 4]), array([column("q"), column("x")])]
            )
        )
        print(stmt.compile(dialect=postgresql.dialect()))

    Produces:

    .. sourcecode:: sql

        SELECT ARRAY[
            ARRAY[%(param_1)s, %(param_2)s],
            ARRAY[%(param_3)s, %(param_4)s],
            ARRAY[q, x]
        ] AS anon_1

    .. versionadded:: 1.3.6 added support for multidimensional array literals

    .. seealso::

        :class:`_postgresql.ARRAY`

    
postgresqlclausestyper!   _traverse_internalsN)type_Iterable[_T]r<   !Optional[_TypeEngineArgument[_T]]kwr'   c               V    t                      j        t          j        g|R i | ||n$| j        r| j        d         j        nt          j        }t          |t                    r.t          |j
        |j        
|j        dz   nd          | _        dS t          |          | _        dS )a  Construct an ARRAY literal.

        :param clauses: iterable, such as a list, containing elements to be
         rendered in the array
        :param type\_: optional type.  If omitted, the type is inferred
         from the contents of the array.

        Nr   r      )
dimensions)super__init__r   comma_opr9   r:   sqltypesNULLTYPE
isinstanceARRAY	item_typerB   )selfr9   r<   r?   	main_type	__class__s        r0   rD   zarray.__init__   s     	+<g<<<<<<   E)-La%%8;L 	 i'' 
	)# !+7 (1,,  DIII i((DIIIr2   r+   r   c                    | fS N rK   s    r0   _select_iterablezarray._select_iterable   s	    wr2   Fr*   r   objOptional[TypeEngine[_T]]_assume_scalarboolBindParameter[_T]c                     |st           j        u rt          j        d | j        d          S t           fd|D                       S )NT)_compared_to_operatorr<   _compared_to_typeuniquec                B    g | ]}                     |d           S )T)rU   r<   )_bind_param).0or*   rK   r<   s     r0   
<listcomp>z%array._bind_param.<locals>.<listcomp>   sI         $$ !D %    r2   )r   getitemr   r   r:   r7   )rK   r*   rS   r<   rU   s   `` ` r0   r]   zarray._bind_param   s      	X):::+&."&)          !	    r2   againstOptional[OperatorType]Union[Self, Grouping[_T]]c                x    |t           j        t           j        t           j        fv rt	          j        |           S | S rO   )r   any_opall_opra   r   r   )rK   rb   s     r0   
self_groupzarray.self_group   s5     y')99;LMMM&t,,,Kr2   )r9   r=   r<   r>   r?   r'   )r+   r   )NF)
r*   r   rS   r'   r<   rT   rU   rV   r+   rW   rO   )rb   rc   r+   rd   )__name__
__module____qualname____doc____visit_name__stringify_dialectr   dp_clauseelement_tupledp_typer;   __annotations__rD   propertyrR   r]   rh   __classcell__)rM   s   @r0   r7   r7   L   s         K KZ N$ 
%<=	"*+3     48	!) !) !) !) !) !) !) !)F    X +/$    8 15        r2   r7   c                      e Zd ZdZ	 	 	 dddZ G d dej        j        e                   ZeZ	e
j        dd            ZddZddZddZdS ) rI   an
  PostgreSQL ARRAY type.

    The :class:`_postgresql.ARRAY` type is constructed in the same way
    as the core :class:`_types.ARRAY` type; a member type is required, and a
    number of dimensions is recommended if the type is to be used for more
    than one dimension::

        from sqlalchemy.dialects import postgresql

        mytable = Table(
            "mytable",
            metadata,
            Column("data", postgresql.ARRAY(Integer, dimensions=2)),
        )

    The :class:`_postgresql.ARRAY` type provides all operations defined on the
    core :class:`_types.ARRAY` type, including support for "dimensions",
    indexed access, and simple matching such as
    :meth:`.types.ARRAY.Comparator.any` and
    :meth:`.types.ARRAY.Comparator.all`.  :class:`_postgresql.ARRAY`
    class also
    provides PostgreSQL-specific methods for containment operations, including
    :meth:`.postgresql.ARRAY.Comparator.contains`
    :meth:`.postgresql.ARRAY.Comparator.contained_by`, and
    :meth:`.postgresql.ARRAY.Comparator.overlap`, e.g.::

        mytable.c.data.contains([1, 2])

    Indexed access is one-based by default, to match that of PostgreSQL;
    for zero-based indexed access, set
    :paramref:`_postgresql.ARRAY.zero_indexes`.

    Additionally, the :class:`_postgresql.ARRAY`
    type does not work directly in
    conjunction with the :class:`.ENUM` type.  For a workaround, see the
    special type at :ref:`postgresql_array_of_enum`.

    .. container:: topic

        **Detecting Changes in ARRAY columns when using the ORM**

        The :class:`_postgresql.ARRAY` type, when used with the SQLAlchemy ORM,
        does not detect in-place mutations to the array. In order to detect
        these, the :mod:`sqlalchemy.ext.mutable` extension must be used, using
        the :class:`.MutableList` class::

            from sqlalchemy.dialects.postgresql import ARRAY
            from sqlalchemy.ext.mutable import MutableList


            class SomeOrmClass(Base):
                # ...

                data = Column(MutableList.as_mutable(ARRAY(Integer)))

        This extension will allow "in-place" changes such to the array
        such as ``.append()`` to produce events which will be detected by the
        unit of work.  Note that changes to elements **inside** the array,
        including subarrays that are mutated in place, are **not** detected.

        Alternatively, assigning a new array value to an ORM element that
        replaces the old one will always trigger a change event.

    .. seealso::

        :class:`_types.ARRAY` - base array type

        :class:`_postgresql.array` - produces a literal array value.

    FNrJ   _TypeEngineArgument[_T]as_tuplerV   rB   Optional[int]zero_indexesc                    t          |t                    rt          d          t          |t                    r
 |            }|| _        || _        || _        || _        dS )a-  Construct an ARRAY.

        E.g.::

          Column("myarray", ARRAY(Integer))

        Arguments are:

        :param item_type: The data type of items of this array. Note that
          dimensionality is irrelevant here, so multi-dimensional arrays like
          ``INTEGER[][]``, are constructed as ``ARRAY(Integer)``, not as
          ``ARRAY(ARRAY(Integer))`` or such.

        :param as_tuple=False: Specify whether return results
          should be converted to tuples from lists. DBAPIs such
          as psycopg2 return lists by default. When tuples are
          returned, the results are hashable.

        :param dimensions: if non-None, the ARRAY will assume a fixed
         number of dimensions.  This will cause the DDL emitted for this
         ARRAY to include the exact number of bracket clauses ``[]``,
         and will also optimize the performance of the type overall.
         Note that PG arrays are always implicitly "non-dimensioned",
         meaning they can store any number of dimensions no matter how
         they were declared.

        :param zero_indexes=False: when True, index values will be converted
         between Python zero-based and PostgreSQL one-based indexes, e.g.
         a value of one will be added to all index values before passing
         to the database.

        zUDo not nest ARRAY types; ARRAY(basetype) handles multi-dimensional arrays of basetypeN)rH   rI   
ValueErrorr:   rJ   rv   rB   rx   )rK   rJ   rv   rB   rx   s        r0   rD   zARRAY.__init__6  sp    N i'' 	?   i&& 	$!	I" $(r2   c                  *    e Zd ZdZddZddZdd	Zd
S )ARRAY.Comparatora*  Define comparison operations for :class:`_types.ARRAY`.

        Note that these operations are in addition to those provided
        by the base :class:`.types.ARRAY.Comparator` class, including
        :meth:`.types.ARRAY.Comparator.any` and
        :meth:`.types.ARRAY.Comparator.all`.

        r&   r'   kwargsr+   r,   c                P    |                      t          |t          j                  S )zBoolean expression.  Test if elements are a superset of the
            elements of the argument array expression.

            kwargs may be ignored by this operator but are required for API
            conformance.
            result_type)operater   rF   Boolean)rK   r&   r}   s      r0   containszARRAY.Comparator.containss  s     <<%X=M<NNNr2   c                P    |                      t          |t          j                  S )zBoolean expression.  Test if elements are a proper subset of the
            elements of the argument array expression.
            r   )r   r   rF   r   rK   r&   s     r0   contained_byzARRAY.Comparator.contained_by~  s)     <<e1A     r2   c                P    |                      t          |t          j                  S )zuBoolean expression.  Test if array has elements in common with
            an argument array expression.
            r   )r   r   rF   r   r   s     r0   overlapzARRAY.Comparator.overlap  s     <<H<L<MMMr2   N)r&   r'   r}   r'   r+   r,   )r&   r'   r+   r,   )ri   rj   rk   rl   r   r   r   rP   r2   r0   
Comparatorr|   i  sd        	 			O 		O 		O 		O	 	 	 		N 	N 	N 	N 	N 	Nr2   r   r+   c                X    t          | j        t          j                  o| j        j        S rO   )rH   rJ   rF   Enumnative_enumrQ   s    r0   _against_native_enumzARRAY._against_native_enum  s'     t~x}55 +*	
r2   dialectr   #Optional[_LiteralProcessorType[_T]]c                      j                             |                              |          d S d	dd
 fd}|S )NelementsIterable[typing_Any]r+   strc                4    dd                     |            dS )NzARRAY[z, ])join)r   s    r0   to_strz'ARRAY.literal_processor.<locals>.to_str  s    2DIIh//2222r2   valueSequence[typing_Any]c                B                         | j                  }|S rO   )_apply_item_processorrB   )r   inner	item_procrK   r   s     r0   processz(ARRAY.literal_processor.<locals>.process  s*    ..y$/6 E Lr2   )r   r   r+   r   )r   r   r+   r   )rJ   dialect_implliteral_processor)rK   r   r   r   r   s   `  @@r0   r   zARRAY.literal_processor  s     N//88JJ
 
	 4	3 	3 	3 	3	 	 	 	 	 	 	 	 r2   2Optional[_BindProcessorType[Sequence[typing_Any]]]c                r      j                             |                              |          d fd}|S )Nr   Optional[Sequence[typing_Any]]r+   Optional[list[typing_Any]]c                P    | | S                      | j        t                    S rO   )r   rB   listr   r   rK   s    r0   r   z%ARRAY.bind_processor.<locals>.process  s2     }119dot  r2   )r   r   r+   r   )rJ   r   bind_processor)rK   r   r   r   s   `  @r0   r   zARRAY.bind_processor  sX     N//88GG
 
		 	 	 	 	 	 	 r2   coltypeobject*_ResultProcessorType[Sequence[typing_Any]]c                      j                             |                              ||          d fd} j        r#|t	          j        d          dfd	dfd
}|S )Nr   r   r+   r   c                l    | | S                      | j        j        rt          nt                    S rO   )r   rB   rv   tupler   r   s    r0   r   z'ARRAY.result_processor.<locals>.process  s@     }11O!]4EE	  r2   z^{(.*)}$r   Sequence[Optional[str]]c                r                         |                               d          }t          |          S )Nr   )matchgroup_split_enum_values)r   r   patterns     r0   handle_raw_stringz1ARRAY.result_processor.<locals>.handle_raw_string  s0    e,,22155)%000r2   c                b    | | S  t          | t                    r |           n|           S rO   )rH   r   )r   r   super_rps    r0   r   z'ARRAY.result_processor.<locals>.process  sK     = L  x!%--%%e,,,  r2   )r   r   r+   r   )r   r   r+   r   )rJ   r   result_processorr   recompile)rK   r   r   r   r   r   r   r   s   `   @@@@r0   r   zARRAY.result_processor  s     N//88IIW
 
		 	 	 	 	 	 	 $ 	Hj--G1 1 1 1 1 1       r2   )FNF)rJ   ru   rv   rV   rB   rw   rx   rV   )r+   rV   )r   r   r+   r   )r   r   r+   r   )r   r   r   r   r+   r   )ri   rj   rk   rl   rD   rF   rI   r   r%   comparator_factoryr   memoized_propertyr   r   r   r   rP   r2   r0   rI   rI      s        E ET $("1) 1) 1) 1) 1)f!N !N !N !N !NX^.s3 !N !N !NF $	
 
 
 
   (   &* * * * * *r2   rI   array_stringr   r   c                   d| vr#d | r|                      d          ng D             S |                     dd          }|                    dd          }g }t          j         d|          }d	}|D ]j}|dk    r| }|r*|                    |                    dd                     8|                    d
 t          j        d|          D                        k|S )N"c                "    g | ]}|d k    r|ndS NULLNrP   r^   rs     r0   r`   z&_split_enum_values.<locals>.<listcomp>  s4     
 
 
 fAA$
 
 
r2   ,z\"z_$ESC_QUOTE$_z\\\z(")Fc                "    g | ]}|d k    r|ndS r   rP   r   s     r0   r`   z&_split_enum_values.<locals>.<listcomp>  s4        fAA$  r2   z([^\s,]+),?)splitreplacer   appendextendfindall)r   textresult	on_quotes	in_quotestoks         r0   r   r     s#   
,
 
1=El((---2
 
 
 	
 77D<<t$$DF&&II  #::%II 		MM#++os;;<<<< MM Z<<      Mr2   )r&   r'   r(   r)   r*   r   r+   r,   )r   r   r+   r   )6
__future__r   r   typingr   r'   r   r   r   r   r	   r
   r   r   r   r    r   rF   r   sqlr   sql.visitorsr   engine.interfacesr   sql._typingr   r   sql.elementsr   r   sql.expressionr   sql.operatorsr   sql.selectabler   sql.type_apir   r   r   r    r!   util.typingr"   r#   r%   eqr5   ExpressionClauseListr7   rI   r   rP   r2   r0   <module>r      s1   # " " " " " 				 $ $ $ $ $ $                                           # # # # # #             ! ! ! ! ! !                   - - - - - - $,,,,,,888888222222------((((((//////------111111222222555555444444******666666###### WT$$$ge:&&& '\
( 
( 
( 
( 
(  '\
( 
( 
( 
( 
(_ _ _ _ _J+B/ _ _ _Dx x x x xHN2 x x xv     r2   