
    Ng5                    >   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  ede          Zej        fdZej        fdZ G d dej        e                   Z G d dej                  Zd ZdS )    )annotationsN)Any)Optional)TypeVar   )CONTAINED_BY)CONTAINS)OVERLAP   )types)util)
expression)	operators)_TypeEngineArgument_T)boundc                .    |                     | |          S )zjA synonym for the ARRAY-level :meth:`.ARRAY.Comparator.any` method.
    See that method for details.

    )anyotherarrexproperators      `/var/www/html/ai-engine/env/lib/python3.11/site-packages/sqlalchemy/dialects/postgresql/array.pyr   r           ;;uh'''    c                .    |                     | |          S )zjA synonym for the ARRAY-level :meth:`.ARRAY.Comparator.all` method.
    See that method for details.

    )allr   s      r   Allr   '   r   r   c                  T     e Zd ZdZd ZdZdZ fdZe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`

    
postgresqlTc                   |                     dd           } t                      j        t          j        g|R i | d | j        D             | _        ||n| j        r| j        d         nt          j        }t          |t                    r.t          |j        |j        
|j        dz   nd          | _        d S t          |          | _        d S )Ntype_c                    g | ]	}|j         
S  )type).0args     r   
<listcomp>z"array.__init__.<locals>.<listcomp>k   s    ===CH===r   r   r      )
dimensions)popsuper__init__r   comma_opclauses_type_tuplesqltypesNULLTYPE
isinstanceARRAY	item_typer+   r&   )selfr0   kwtype_arg	main_type	__class__s        r   r.   zarray.__init__g   s    66'4((+<g<<<<<<===== # H(,(8O!!$$h>O 	 i'' 
	)# !+7 (1,,  DIII i((DIIIr   c                    | fS Nr%   r7   s    r   _select_iterablezarray._select_iterable   s	    wr   FNc                     |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)_assume_scalarr#   )_bind_param)r'   or   r7   r#   s     r   r)   z%array._bind_param.<locals>.<listcomp>   sI         $$ !D %    r   )r   getitemr   BindParameterr&   r    )r7   r   objrE   r#   s   ``  `r   rF   zarray._bind_param   s     	X):::+&."&)          !	    r   c                x    |t           j        t           j        t           j        fv rt	          j        |           S | S r=   )r   any_opall_oprH   r   Grouping)r7   againsts     r   
self_groupzarray.self_group   s3    y')99;LMMM&t,,,Kr   )FNr=   )__name__
__module____qualname____doc____visit_name__stringify_dialectinherit_cacher.   propertyr?   rF   rP   __classcell__)r;   s   @r   r    r    0   s        / /b N$M) ) ) ) )0   X   *       r   r    c                      e Zd ZdZ	 	 	 dddZ G d dej        j                  ZeZe	d             Z
e	d             Zd Zej        d             Zd Zd Zd ZdS )r5   a[
  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.

    FNr6   _TypeEngineArgument[Any]as_tupleboolr+   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)r4   r5   
ValueErrorr&   r6   r\   r+   r_   )r7   r6   r\   r+   r_   s        r   r.   zARRAY.__init__   sp    N i'' 	?   i&& 	$!	I" $(r   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 )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	   r2   Boolean)r7   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.
            re   )rg   r   r2   rh   r7   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.
            re   )rg   r
   r2   rh   rl   s     r   overlapzARRAY.Comparator.overlap2  s     <<H<L<MMMr   N)rQ   rR   rS   rT   rj   rm   ro   r%   r   r   
Comparatorrc     sS        	 		O 	O 	O	 	 		N 	N 	N 	N 	Nr   rp   c                    | j         S r=   )r\   r>   s    r   hashablezARRAY.hashable:  s
    }r   c                    t           S r=   )listr>   s    r   python_typezARRAY.python_type>  s    r   c                    ||k    S r=   r%   )r7   xys      r   compare_valueszARRAY.compare_valuesB  s    Avr   c                X    t          | j        t          j                  o| j        j        S r=   )r4   r6   r2   Enumnative_enumr>   s    r   _against_native_enumzARRAY._against_native_enumE  s'     t~x}55 +*	
r   c                      j                             |                              |          d S d  fd}|S )Nc                4    dd                     |            dS )NzARRAY[z, ])join)elementss    r   to_strz'ARRAY.literal_processor.<locals>.to_strS  s    2DIIh//2222r   c                B                         | j                  }|S r=   )_apply_item_processorr+   )valueinner	item_procr7   r   s     r   processz(ARRAY.literal_processor.<locals>.processV  s*    ..y$/6 E Lr   )r6   dialect_implliteral_processor)r7   dialectr   r   r   s   `  @@r   r   zARRAY.literal_processorL  ss    N//88JJ
 
	 4	3 	3 	3	 	 	 	 	 	 	 r   c                p      j                             |                              |           fd}|S )Nc                P    | | S                      | j        t                    S r=   )r   r+   rt   r   r   r7   s    r   r   z%ARRAY.bind_processor.<locals>.processc  s0    }119dot  r   )r6   r   bind_processor)r7   r   r   r   s   `  @r   r   zARRAY.bind_processor^  sQ    N//88GG
 
		 	 	 	 	 	 r   c                      j                             |                              ||           fd} j        r!|t	          j        d          fdfd}|S )Nc                l    | | S                      | j        j        rt          nt                    S r=   )r   r+   r\   tuplert   r   s    r   r   z'ARRAY.result_processor.<locals>.processr  s>    }11O!]4EE	  r   z^{(.*)}$c                r                         |                               d          }t          |          S )Nr   )matchgroup_split_enum_values)r   r   patterns     r   handle_raw_stringz1ARRAY.result_processor.<locals>.handle_raw_string  s0    e,,22155)%000r   c                b    | | S  t          | t                    r |           n|           S r=   )r4   str)r   r   super_rps    r   r   z'ARRAY.result_processor.<locals>.process  sI    = L  x!%--%%e,,,  r   )r6   r   result_processorr}   recompile)r7   r   coltyper   r   r   r   r   s   `   @@@@r   r   zARRAY.result_processorm  s    N//88IIW
 
			 		 		 		 		 		 $ 	Hj--G1 1 1 1 1
 
 
 
 
 
 r   )FNF)r6   r[   r\   r]   r+   r^   r_   r]   )rQ   rR   rS   rT   r.   r2   r5   rp   comparator_factoryrX   rr   ru   ry   r   memoized_propertyr}   r   r   r   r%   r   r   r5   r5      s       B BN $("1) 1) 1) 1) 1)fN N N N NX^. N N NB $  X   X   

 
 
  $  $ $ $ $ $r   r5   c                   d| vr| r|                      d          ng S |                     dd          }|                    dd          }g }t          j         d|          }d}|D ]`}|dk    r| }|r*|                    |                    dd                     8|                    t          j        d	|                     a|S )
N",z\"z_$ESC_QUOTE$_z\\\z(")Fz([^\s,]+),?)splitreplacer   appendextendfindall)array_stringtextresult	on_quotes	in_quotestoks         r   r   r     s    
,*6>|!!#&&&B> 77D<<t$$DF&&II ; ;#::%II 	;MM#++os;;<<<<MM"*^S99::::Mr   )
__future__r   r   typingr   r   r   r   r   r	   r
    r   r2   r   sqlr   sql._typingr   r   eqr   ExpressionClauseListr    r5   r   r%   r   r   <module>r      s   # " " " " " 				                   # # # # # #             ! ! ! ! ! !                   . . . . . . WT "+ ( ( ( ( "+ ( ( ( (l l l l lJ+B/ l l l^r r r r rHN r r rj    r   