o
    hZB                     @  s  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	rd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j/fd9d,d-Zej/fd9d.d/Z0G d0d1 d1ej1e. Z2G d2d3 d3ej3e. Z3d:d7d8Z4dS );    )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other
typing_Anyarrexpr_ColumnExpressionArgument[_T]operatorr   returnColumnElement[bool]c                 C     | | |S )zjA synonym for the ARRAY-level :meth:`.ARRAY.Comparator.any` method.
    See that method for details.

    )anyr$   r&   r(    r.   x/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sqlalchemy/dialects/postgresql/array.pyr   1      
r   c                 C  r+   )zjA synonym for the ARRAY-level :meth:`.ARRAY.Comparator.all` method.
    See that method for details.

    )allr-   r.   r.   r/   All>   r0   r2   c                      sz   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dZed$ddZ		d%d&ddZ	d'd(d!d"Z  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]r8   !Optional[_TypeEngineArgument[_T]]kwr%   c                  s   t  jtjg|R i | |dur|n| jr| jd jntj}t|t	r9t	|j
|jdur2|jd nd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_opr5   r6   sqltypesNULLTYPE
isinstanceARRAY	item_typer=   )selfr5   r8   r;   	main_type	__class__r.   r/   r?      s   


	zarray.__init__r)   r   c                 C  s   | fS Nr.   rF   r.   r.   r/   _select_iterable   s   zarray._select_iterableFr(   r   objOptional[TypeEngine[_T]]_assume_scalarboolBindParameter[_T]c                   s@   |s t ju rtjd | jddS t fdd|D S )NT)_compared_to_operatorr8   _compared_to_typeuniquec                   s   g | ]}j  |d dqS )T)rO   r8   )_bind_param).0or(   rF   r8   r.   r/   
<listcomp>   s    z%array._bind_param.<locals>.<listcomp>)r   getitemr   r   r6   r3   )rF   r(   rM   r8   rO   r.   rX   r/   rU      s   
zarray._bind_paramagainstOptional[OperatorType]Union[Self, Grouping[_T]]c                 C  s"   |t jt jt jfv rt| S | S rJ   )r   any_opall_oprZ   r   r   )rF   r[   r.   r.   r/   
self_group   s   
zarray.self_group)r5   r9   r8   r:   r;   r%   )r)   r   )NF)
r(   r   rM   r%   r8   rN   rO   rP   r)   rQ   rJ   )r[   r\   r)   r]   )__name__
__module____qualname____doc____visit_name__stringify_dialectr   dp_clauseelement_tupledp_typer7   __annotations__r?   propertyrL   rU   r`   __classcell__r.   r.   rH   r/   r3   K   s    
 M	#r3   c                   @  sl   e Zd ZdZ			dd ddZG dd dejje ZeZ	e
jd!ddZd"ddZd#ddZd$ddZdS )%rD   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.

    FNrE   _TypeEngineArgument[_T]as_tuplerP   r=   Optional[int]zero_indexesc                 C  s>   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)rC   rD   
ValueErrorr6   rE   rm   r=   ro   )rF   rE   rm   r=   ro   r.   r.   r/   r?   5  s   
'

zARRAY.__init__c                   @  s.   e Zd ZdZdddZdd	d
ZdddZdS )z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                 K     | j t|tjd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   rA   Boolean)rF   r$   rq   r.   r.   r/   containsr  s   	zARRAY.Comparator.containsc                 C  rr   )zBoolean expression.  Test if elements are a proper subset of the
            elements of the argument array expression.
            rs   )ru   r   rA   rv   rF   r$   r.   r.   r/   contained_by}  s   zARRAY.Comparator.contained_byc                 C  rr   )zuBoolean expression.  Test if array has elements in common with
            an argument array expression.
            rs   )ru   r   rA   rv   rx   r.   r.   r/   overlap  s   zARRAY.Comparator.overlapN)r$   r%   rq   r%   r)   r*   )r$   r%   r)   r*   )ra   rb   rc   rd   rw   ry   rz   r.   r.   r.   r/   
Comparatorh  s
    
	
r{   r)   c                 C  s   t | jtjo
| jjS rJ   )rC   rE   rA   Enumnative_enumrK   r.   r.   r/   _against_native_enum  s   zARRAY._against_native_enumdialectr   #Optional[_LiteralProcessorType[_T]]c                   s>   j ||  d u rd S dddd fd	d
}|S )NelementsIterable[typing_Any]r)   strc                 S  s   dd |  dS )NzARRAY[z, ])join)r   r.   r.   r/   to_str  s   z'ARRAY.literal_processor.<locals>.to_strvalueSequence[typing_Any]c                   s    |  j}|S rJ   )_apply_item_processorr=   r   inner	item_procrF   r   r.   r/   process  s   
z(ARRAY.literal_processor.<locals>.process)r   r   r)   r   )r   r   r)   r   )rE   dialect_implliteral_processorrF   r   r   r.   r   r/   r     s   
zARRAY.literal_processor2Optional[_BindProcessorType[Sequence[typing_Any]]]c                   s&   j || d fdd}|S )Nr   Optional[Sequence[typing_Any]]r)   Optional[list[typing_Any]]c                   s   | d u r| S  |  jtS rJ   )r   r=   listr   r   rF   r.   r/   r     s
   
z%ARRAY.bind_processor.<locals>.process)r   r   r)   r   )rE   r   bind_processorr   r.   r   r/   r     s
   
zARRAY.bind_processorcoltypeobject*_ResultProcessorType[Sequence[typing_Any]]c                   sZ   j |||dfdd}jr+|tddfd
d d fdd}|S )Nr   r   r)   r   c                   s*   | d u r| S  |  jjrtS tS rJ   )r   r=   rm   tupler   r   r   r.   r/   r     s   z'ARRAY.result_processor.<locals>.processz^{(.*)}$r   	list[str]c                   s     | d}t|S )Nr
   )matchgroup_split_enum_valuesr   )patternr.   r/   handle_raw_string  s   z1ARRAY.result_processor.<locals>.handle_raw_stringc                   s(   | d u r| S t | tr | S | S rJ   )rC   r   r   )r   super_rpr.   r/   r     s   )r   r   r)   r   )r   r   r)   r   )rE   r   result_processorr~   recompile)rF   r   r   r   r.   )r   r   r   rF   r   r/   r     s   
zARRAY.result_processor)FNF)rE   rl   rm   rP   r=   rn   ro   rP   )r)   rP   )r   r   r)   r   )r   r   r)   r   )r   r   r   r   r)   r   )ra   rb   rc   rd   r?   rA   rD   r{   r"   comparator_factoryr   memoized_propertyr~   r   r   r   r.   r.   r.   r/   rD      s    J3#

rD   array_stringr   r   c                 C  s   d| vr| r|  dS g S | dd}|dd}g }t d|}d}|D ]}|dkr/| }q%|r;||dd q%|td	| q%|S )
N",z\"z_$ESC_QUOTE$_z\\\z(")Fz([^\s,]+),?)splitreplacer   appendextendfindall)r   textresult	on_quotes	in_quotestokr.   r.   r/   r     s   r   )r$   r%   r&   r'   r(   r   r)   r*   )r   r   r)   r   )5
__future__r   r   typingr   r%   r   r   r   r   r   r	   r   r   r   r    r   rA   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"   eqr2   ExpressionClauseListr3   rD   r   r.   r.   r.   r/   <module>   sT    # |