
    ؄_/                        d dl Z ddlmZ ddlmZ ddlmZ ddlmZ ej        fdZ	ej        fdZ
 G d	 d
ej                  Z ej        dd          Z ej        dd          Z ej        dd          Z G d dej                  ZdS )    N   )types)util)
expression)	operatorsc                 .    |                     | |          S )zA synonym for the :meth:`.ARRAY.Comparator.any` method.

    This method is legacy and is here for backwards-compatibility.

    .. seealso::

        :func:`_expression.any_`

    )anyotherarrexproperators      i/var/www/book.euthymeo.com/html/venv/lib/python3.11/site-packages/sqlalchemy/dialects/postgresql/array.pyAnyr           ;;uh'''    c                 .    |                     | |          S )zA synonym for the :meth:`.ARRAY.Comparator.all` method.

    This method is legacy and is here for backwards-compatibility.

    .. seealso::

        :func:`_expression.all_`

    )allr
   s      r   Allr      r   r   c                   6     e Zd ZdZd Z fdZddZ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::

        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 ``type_`` keyword argument is passed::

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

    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::

        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`

    c                 (    t          t          |           j        |i | t          | j        t
                    r=t          | j        j        | j        j        | j        j        dz   nd          | _        d S t          | j                  | _        d S )N      )
dimensions)superr   __init__
isinstancetypeARRAY	item_typer   )selfclauseskw	__class__s      r   r   zarray.__init__c   s    #eT#W3333di'' 	)	#9'3  9/!33	  DIII di((DIIIr   FNc                      |st           j        u rt          j        d | j        d          S t           fd|D                       S )NT)_compared_to_operatortype__compared_to_typeuniquec                 B    g | ]}                     |d           S )T)_assume_scalarr'   )_bind_param).0or   r!   r'   s     r   
<listcomp>z%array._bind_param.<locals>.<listcomp>|   sI         $$ !D %    r   )r   getitemr   BindParameterr   r   )r!   r   objr+   r'   s   ``  `r   r,   zarray._bind_paramo   s     	X):::+&."&)          !	    r   c                 x    |t           j        t           j        t           j        fv rt	          j        |           S | S N)r   any_opall_opr0   r   Grouping)r!   againsts     r   
self_groupzarray.self_group   s3    y')99;LMMM&t,,,Kr   )FNr4   )	__name__
__module____qualname____doc____visit_name__r   r,   r9   __classcell__)r$   s   @r   r   r   ,   sr        1 1f N
) 
) 
) 
) 
)   *       r   r   z@>   )
precedencez<@z&&c                       e Zd ZdZ G d dej        j                  ZeZ	 ddZe	d             Z
e	d             Zd	 Zd
 Zej        d             Zej        d             Zd Zd Zd ZdS )r   a  PostgreSQL ARRAY type.

    .. versionchanged:: 1.1 The :class:`_postgresql.ARRAY` type is now
       a subclass of the core :class:`_types.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])

    The :class:`_postgresql.ARRAY` type may not be supported on all
    PostgreSQL DBAPIs; it is currently known to work on psycopg2 only.

    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`.

    .. seealso::

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

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

    c                   $    e Zd ZdZd Zd Z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`.

        c                 P    |                      t          |t          j                  S )zBoolean expression.  Test if elements are a superset of the
            elements of the argument array expression.
            result_type)operateCONTAINSsqltypesBoolean)r!   r   kwargss      r   containszARRAY.Comparator.contains   s     <<%X=M<NNNr   c                 P    |                      t          |t          j                  S )zBoolean expression.  Test if elements are a proper subset of the
            elements of the argument array expression.
            rF   )rH   CONTAINED_BYrJ   rK   r!   r   s     r   contained_byzARRAY.Comparator.contained_by   s)     <<e1A     r   c                 P    |                      t          |t          j                  S )zuBoolean expression.  Test if array has elements in common with
            an argument array expression.
            rF   )rH   OVERLAPrJ   rK   rP   s     r   overlapzARRAY.Comparator.overlap   s     <<H<L<MMMr   N)r:   r;   r<   r=   rM   rQ   rT    r   r   
ComparatorrD      sS        	 		O 	O 	O	 	 		N 	N 	N 	N 	Nr   rV   FNc                     t          |t                    rt          d          t          |t                    r
 |            }|| _        || _        || _        || _        dS )aP  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.

         .. versionadded:: 0.9.5


        zUDo not nest ARRAY types; ARRAY(basetype) handles multi-dimensional arrays of basetypeN)r   r   
ValueErrorr   r    as_tupler   zero_indexes)r!   r    rY   r   rZ   s        r   r   zARRAY.__init__   sp    L i'' 	?   i&& 	$!	I" $(r   c                     | j         S r4   )rY   r!   s    r   hashablezARRAY.hashable  s
    }r   c                     t           S r4   )listr\   s    r   python_typezARRAY.python_type  s    r   c                     ||k    S r4   rU   )r!   xys      r   compare_valueszARRAY.compare_values  s    Avr   c                      t          |          }dk    s&H|r"t          |d         t           t          f          s$r fd|D                       S  |          S   fd|D                       S )Nr   r   c              3   .   K   | ]} |          V  d S r4   rU   )r-   rb   itemprocs     r   	<genexpr>z$ARRAY._proc_array.<locals>.<genexpr>.  s+      !;!;!((1++!;!;!;!;!;!;r   c              3   V   K   | ]#}                     |dz
  nd           V  $d S )Nr   )_proc_array)r-   rb   
collectiondimrg   r!   s     r   rh   z$ARRAY._proc_array.<locals>.<genexpr>2  sa            "C!GGD	      r   )r_   r   tuple)r!   arrrg   rl   rk   s   ` ```r   rj   zARRAY._proc_array  s    ;s))C1HH{
   "#a&4-88   '!z!;!;!;!;s!;!;!;;;;!z#&:             r   c                 N    | j         pt          | j        t          j                  S r4   )_against_native_enumr   r    rJ   JSONr\   s    r   _require_castzARRAY._require_cast<  s'    ( 
JNHM-
 -
 	
r   c                 X    t          | j        t          j                  o| j        j        S r4   )r   r    rJ   Enumnative_enumr\   s    r   rp   zARRAY._against_native_enumB  s'     t~x}55 +*	
r   c                 >    | j         rt          j        ||           S |S r4   )rr   r   cast)r!   	bindvalues     r   bind_expressionzARRAY.bind_expressionI  s%     	?9d333r   c                 p      j                             |                              |           fd}|S )Nc                 P    | | S                      | j        t                    S r4   )rj   r   r_   value	item_procr!   s    r   processz%ARRAY.bind_processor.<locals>.processT  s0    }''9dot  r   )r    dialect_implbind_processor)r!   dialectr   r~   s   `  @r   r   zARRAY.bind_processorO  sQ    N//88GG
 
		 	 	 	 	 	 r   c                       j                             |                              ||           fd} j        r|d fd}|S )Nc                 l    | | S                      | j        j        rt          nt                    S r4   )rj   r   rY   rm   r_   r|   s    r   r   z'ARRAY.result_processor.<locals>.processc  s>    }''O!]4EE	  r   c                     t          j        d|                               d          }|r|                    d          ng S )Nz^{(.*)}$r   ,)rematchgroupsplit)r}   inners     r   handle_raw_stringz1ARRAY.result_processor.<locals>.handle_raw_stringq  s=    e44::1==+08u{{3'''b8r   c                 l    | | S  t          | t          j                  r |           n|           S r4   )r   r   string_types)r}   r   super_rps    r   r   z'ARRAY.result_processor.<locals>.processu  sL    = L  x!%):;;%%e,,,  r   )r    r   result_processorrp   )r!   r   coltyper   r   r~   r   s   `   @@@r   r   zARRAY.result_processor^  s    N//88IIW
 
			 		 		 		 		 		 $ 	H9 9 9
 
 
 
 
 
 r   )FNF)r:   r;   r<   r=   rJ   r   rV   comparator_factoryr   propertyr]   r`   rd   rj   r   memoized_propertyrr   rp   ry   r   r   rU   r   r   r   r      s9       + +ZN N N N NX^. N N N> $ HM0) 0) 0) 0)d   X   X    : 

 
 

 

 
 
    # # # # #r   r   )r    r   rJ   r   sqlr   r   eqr   r   Tupler   	custom_oprI   rO   rS   r   rU   r   r   <module>r      sK   
			 ! ! ! ! ! !                   "+ ( ( ( ( "+ ( ( ( (\ \ \ \ \J \ \ \~ 9t222"y"4A666
)
dq
1
1
1o o o o oHN o o o o or   