o
    gҀ                     @  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 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+ ed%e	d&Z,e'd' Z-e%rd(d)iZ.d*d)iZ/ni Z.i Z/ej0dZd+d)ie.G d,d- d-ee, Z1G d.d/ d/ee1e,  Z2G d0d1 d1e j#e, Z3G d2d3 d3e3e1e,  Z4G d4d5 d5e4e, Z5G d6d7 d7e3ee1e,   Z6G d8d9 d9e6e, Z7G d:d; d;e4e8 Z9G d<d= d=e4e8 Z:G d>d? d?e4e Z;G d@dA dAe4e Z<G dBdC dCe4e Z=G dDdE dEe4e Z>G dFdG dGe6e8 Z?G dHdI dIe6e8 Z@G dJdK dKe6e ZAG dLdM dMe6e ZBG dNdO dOe6e ZCG dPdQ dQe6e ZDdRZEdSZFd[dXdYZGdS )\    )annotationsN)date)datetime)	timedelta)Decimal)Any)cast)Generic)List)Optional)overload)Sequence)Tuple)Type)TYPE_CHECKING)TypeVar)Union   )ADJACENT_TO)CONTAINED_BY)CONTAINS)NOT_EXTEND_LEFT_OF)NOT_EXTEND_RIGHT_OF)OVERLAP)STRICTLY_LEFT_OF)STRICTLY_RIGHT_OF   )types)	operators)
TypeEngine)py310)Literal)ColumnElement)_TE)TypeEngineMixin_T)bound)z()[)z(]z[]slotsTkw_onlyfrozenc                   @  s  e Zd ZU dZdZded< 	 dZded< 	 er,ej	ddZ
ded	< ej	d
dZded< nej	dZddieZ
ded	< ej	dZdd
ieZded< esV		d[dd
dd\ddZd]ddZed]ddZed]ddZed]ddZed]ddZed]ddZed]ddZed^d!d"Zd_d%d&Zd`d(d)Z	
dadbd1d2Zdcd4d5Zddd7d8Zded:d;ZeZddd<d=Zddd>d?Z e Z!ddd@dAZ"e"Z#dddBdCZ$dddDdEZ%dfdFdGZ&dddHdIZ'dgdJdKZ(dgdLdMZ)dgdNdOZ*dgdPdQZ+dgdRdSZ,dgdTdUZ-dhdVdWZ.dhdXdYZ/dS )iRangea_  Represent a PostgreSQL range.

    E.g.::

        r = Range(10, 50, bounds="()")

    The calling style is similar to that of psycopg and psycopg2, in part
    to allow easier migration from previous SQLAlchemy versions that used
    these objects directly.

    :param lower: Lower bound value, or None
    :param upper: Upper bound value, or None
    :param bounds: keyword-only, optional string value that is one of
     ``"()"``, ``"[)"``, ``"(]"``, ``"[]"``.  Defaults to ``"[)"``.
    :param empty: keyword-only, optional bool indicating this is an "empty"
     range

    .. versionadded:: 2.0

    NOptional[_T]lowerupperr'   )default_BoundsTypeboundsFboolemptyr/   )r1   r3   c                C  s   | j ||||d d S )N)r-   r.   r1   r3   )__dict__update)selfr-   r.   r1   r3    r7   g/var/www/html/ecg_monitoring/venv/lib/python3.10/site-packages/sqlalchemy/dialects/postgresql/ranges.py__init__a   s   	zRange.__init__returnc                 C  s   | j  S Nr3   r6   r7   r7   r8   __bool__s      zRange.__bool__c                 C     | j S z$A synonym for the 'empty' attribute.r<   r=   r7   r7   r8   isemptyv      zRange.isemptyc                 C  r@   rA   r<   r=   r7   r7   r8   is_empty|   rC   zRange.is_emptyc                 C     | j d dkS )z,Return True if the lower bound is inclusive.r   [r1   r=   r7   r7   r8   	lower_inc      zRange.lower_incc                 C     | j  o| jdu S )zKReturn True if this range is non-empty and lower bound is
        infinite.N)r3   r-   r=   r7   r7   r8   	lower_inf      zRange.lower_infc                 C  rE   )z,Return True if the upper bound is inclusive.r   ]rG   r=   r7   r7   r8   	upper_inc   rI   zRange.upper_incc                 C  rJ   )zOReturn True if this range is non-empty and the upper bound is
        infinite.N)r3   r.   r=   r7   r7   r8   	upper_inf   rL   zRange.upper_infAbstractSingleRange[_T]c                 C     t  S r;   )AbstractSingleRanger=   r7   r7   r8   __sa_type_engine__      zRange.__sa_type_engine__valuer%   c                 C  s   | j rdS | jdu r | jdu p| jd dkr|| jk S || jkS | jdu r6| jd dkr1|| jkS || jkS | jd dkrB|| jkn|| jkoW| jd dkrS|| jk S || jkS )z3Return True if this range contains the given value.FNr   )r   ()r3   r-   r.   r1   r6   rU   r7   r7   r8   _contains_value   s0   



zRange._contains_valuer   c                 C  s^   t | jtst | jtrdS t | jtst | jtrdS t | jts(t | jtr-tddS dS )uA   Determine the “step” for this range, if it is a discrete one.r   N)days)
isinstancer-   intr.   r   r   r   r=   r7   r7   r8   _get_discrete_step   s   
zRange._get_discrete_stepvalue1bound1strvalue2bound2only_valuesr\   c                 C  sX  |dv }|dv }||  u rdu r n n||krdS |rdS dS |du r*|r(dS dS |du r4|r2dS dS ||kr>||kr>dS |dv }|dv }	|   }
|
durt|rY|sX||
7 }d}n|ra||
7 }d}|rl|	sk||
7 }d}	n|	rt||
7 }d}	||k rzdS ||krdS |rdS |r|	rdS |s|	s||krdS |rdS dS |s|rdS dS |	s|rdS dS dS )	a  Compare two range bounds.

        Return -1, 0 or 1 respectively when `value1` is less than,
        equal to or greater than `value2`.

        When `only_value` is ``True``, do not consider the *inclusivity*
        of the edges, just their values.
        >   rW   rF   Nr   r   >   rF   rM   TF)r]   )r6   r^   r_   ra   rb   rc   value1_is_lower_boundvalue2_is_lower_bound
value1_inc
value2_incstepr7   r7   r8   _compare_edges   sb   zRange._compare_edgesotherc           
      C  s   t |tstS | jr|jrdS | j|jkrdS | j}| jd }|j}|jd }| j}| jd }|j}|jd }	| ||||dkoJ| ||||	dkS )zCompare this range to the `other` taking into account
        bounds inclusivity, returning ``True`` if they are equal.
        TFr   r   )r[   r+   NotImplementedr3   r-   r1   r.   rj   
r6   rk   slowerslower_bolowerolower_bsuppersupper_boupperoupper_br7   r7   r8   __eq__+  s"   




zRange.__eq__	Range[_T]c           
      C  s   | j rdS |j r
dS | j}| jd }|j}|jd }| ||||dk r&dS | j}| jd }|j}|jd }	| ||||	dkrBdS dS )z7Determine whether this range is a contained by `other`.TFr   r   )r3   r-   r1   rj   r.   rm   r7   r7   r8   contained_byF  s"   



zRange.contained_byUnion[_T, Range[_T]]c                 C  s   t |tr
|| S | |S )z.Determine whether this range contains `value`.)r[   r+   rx   rY   rX   r7   r7   r8   containsc  s   


zRange.containsc           
      C  s   | j s|j rdS | j}| jd }| j}| jd }|j}|jd }|j}|jd }	| ||||dkr>| ||||	dkr>dS | ||||dkrT| ||||dkrTdS dS )z3Determine whether this range overlaps with `other`.Fr   r   Tr3   r-   r1   r.   rj   
r6   rk   rn   ro   rr   rs   rp   rq   rt   ru   r7   r7   r8   overlapsm  s"   



zRange.overlapsc                 C  sD   | j s|j rdS | j}| jd }|j}|jd }| ||||dk S )zBDetermine whether this range is completely to the left of `other`.Fr   r   )r3   r.   r1   r-   rj   )r6   rk   rr   rs   rp   rq   r7   r7   r8   strictly_left_of     

zRange.strictly_left_ofc                 C  sD   | j s|j rdS | j}| jd }|j}|jd }| ||||dkS )zCDetermine whether this range is completely to the right of `other`.Fr   r   r{   )r6   rk   rn   ro   rt   ru   r7   r7   r8   strictly_right_of  r   zRange.strictly_right_ofc                 C  sD   | j s|j rdS | j}| jd }|j}|jd }| ||||dkS )z>Determine whether this does not extend to the left of `other`.Fr   )r3   r-   r1   rj   )r6   rk   rn   ro   rp   rq   r7   r7   r8   not_extend_left_of  r   zRange.not_extend_left_ofc                 C  sD   | j s|j rdS | j}| jd }|j}|jd }| ||||dkS )z?Determine whether this does not extend to the right of `other`.Fr   r   )r3   r.   r1   rj   )r6   rk   rr   rs   rt   ru   r7   r7   r8   not_extend_right_of  r   zRange.not_extend_right_ofc                 C  s   |  ||||d}|dkr7|  }|du rdS |dkr)|dkr%||| kS ||kS |dkr1||kS ||| kS |dkre|dkrC|dksK|dkrU|d	krU|  }|durUdS |dkr]|dkpd|dkod|d	kS dS )
zTDetermine whether an upper bound is immediately successive to a
        lower bound.Trd   NFrM   rF   r   rV   rW   )rj   r]   )r6   r^   r_   ra   rb   resri   r7   r7   r8   _upper_edge_adjacent_to_lower  s6   z#Range._upper_edge_adjacent_to_lowerc           
      C  sp   | j s|j rdS | j}| jd }| j}| jd }|j}|jd }|j}|jd }	| ||||p7| ||	||S )z8Determine whether this range is adjacent to the `other`.Fr   r   )r3   r-   r1   r.   r   r|   r7   r7   r8   adjacent_to  s"   



zRange.adjacent_toc                 C  s   | j r|S |j r
| S | |s| |std| j}| jd }| j}| jd }|j}|jd }|j}|jd }	| ||||dk rG|}
|}n|}
|}| ||||	dkrZ|}|}n|}|	}t|
|t	t
|| dS )zCompute the union of this range with the `other`.

        This raises a ``ValueError`` exception if the two ranges are
        "disjunct", that is neither adjacent nor overlapping.
        zAAdding non-overlapping and non-adjacent ranges is not implementedr   r   rG   )r3   r}   r   
ValueErrorr-   r1   r.   rj   r+   r   r0   r6   rk   rn   ro   rr   rs   rp   rq   rt   ru   rlowerrlower_brupperrupper_br7   r7   r8   union  s:   



zRange.unionc                 C  
   |  |S r;   )r   r6   rk   r7   r7   r8   __add__@     
zRange.__add__c                 C  s  | j s|j r| S | j}| jd }| j}| jd }|j}|jd }|j}|jd }	| ||||}
| ||||	}|
dk rD|dkrDtd| ||||	}| ||||}|dks\|dk r^| S |
dkrm|dkrmtddddS |
dkr|dkr|dkr|dkrdnd	}|dkr|d	kr| ||||dkrtddddS t||tt|| d
S |
dkr|dkr|dkr|	d	krdnd}|dkr|d	kr| ||||dkrtddddS t||tt|| d
S J d|  d| )zCompute the difference between this range and the `other`.

        This raises a ``ValueError`` exception if the two ranges are
        "disjunct", that is neither adjacent nor overlapping.
        r   r   z5Subtracting a strictly inner range is not implementedNTr<   rF   rV   rM   rG   rW   FzUnhandled case computing z - )	r3   r-   r1   r.   rj   r   r+   r   r0   )r6   rk   rn   ro   rr   rs   rp   rq   rt   ru   sl_vs_olsu_vs_ousl_vs_ousu_vs_olr   r   r7   r7   r8   
differenceC  sb   



zRange.differencec                 C  r   r;   )r   r   r7   r7   r8   __sub__  r   zRange.__sub__c                 C  s   | j s|j s| |stddddS | j}| jd }| j}| jd }|j}|jd }|j}|jd }	| ||||dk rA|}
|}n|}
|}| ||||	dkrT|}|	}n|}|}t|
|tt|| dS )zdCompute the intersection of this range with the `other`.

        .. versionadded:: 2.0.10

        NTr<   r   r   rG   )	r3   r}   r+   r-   r1   r.   rj   r   r0   r   r7   r7   r8   intersection  s2   



zRange.intersectionc                 C  r   r;   )r   r   r7   r7   r8   __mul__  r   zRange.__mul__c                 C  s   |   S r;   )
_stringifyr=   r7   r7   r8   __str__  r?   zRange.__str__c                 C  s^   | j rdS | j| j}}|d u rdn|}|d u rdn|}td| j\}}| | d| | S )Nr3    zTuple[str, str],)r3   r-   r.   r   r1   )r6   lrb0b1r7   r7   r8   r     s   zRange._stringifyr7   )NN)r-   r,   r.   r,   r1   r0   r3   r2   )r:   r2   )r:   rP   )rU   r%   r:   r2   )r:   r   )F)r^   r,   r_   r`   ra   r,   rb   r`   rc   r2   r:   r\   )rk   r   r:   r2   )rk   rw   r:   r2   )rU   ry   r:   r2   )
r^   r,   r_   r`   ra   r,   rb   r`   r:   r2   )rk   rw   r:   rw   )r:   r`   )0__name__
__module____qualname____doc__r-   __annotations__r.   r   dataclassesfieldr1   r3   	dc_kwonlyr    r9   r>   propertyrB   rD   rH   rK   rN   rO   rS   rY   r]   rj   rv   rx   rz   __contains__r}   r~   
__lshift__r   
__rshift__r   r   r   r   r   r   r   r   r   r   r   r   r7   r7   r7   r8   r+   ;   st   
 



Y



 




/

.

L

&
r+   c                   @  s   e Zd ZdZedddZdS )
MultiRangea?  Represents a multirange sequence.

    This list subclass is an utility to allow automatic type inference of
    the proper multi-range SQL type depending on the single range values.
    This is useful when operating on literal multi-ranges::

        import sqlalchemy as sa
        from sqlalchemy.dialects.postgresql import MultiRange, Range

        value = literal(MultiRange([Range(2, 4)]))

        select(tbl).where(tbl.c.value.op("@")(MultiRange([Range(-3, 7)])))

    .. versionadded:: 2.0.26

    .. seealso::

        - :ref:`postgresql_multirange_list_use`.
    r:   AbstractMultiRange[_T]c                 C  rQ   r;   )AbstractMultiRanger=   r7   r7   r8   rS     rT   zMultiRange.__sa_type_engine__N)r:   r   )r   r   r   r   r   rS   r7   r7   r7   r8   r     s    r   c                      s`   e Zd ZdZdZdZedd	d
Zeddd
Zd fdd
ZG dd dej	e
e  Z  ZS )AbstractRangez0Base class for single and multi Range SQL types.Tcls	Type[_TE]kwr   r:   r#   c                 K     d S r;   r7   r6   r   r   r7   r7   r8   adapt  s   zAbstractRange.adaptType[TypeEngineMixin]TypeEngine[Any]c                 K  r   r;   r7   r   r7   r7   r8   r     s   -Type[Union[TypeEngine[Any], TypeEngineMixin]]c                   sH   t |ttfr|| jur| j}t| d|| jfd|i S t |S )a	  Dynamically adapt a range type to an abstract impl.

        For example ``INT4RANGE().adapt(_Psycopg2NumericRange)`` should
        produce a type that will have ``_Psycopg2NumericRange`` behaviors
        and also render as ``INT4RANGE`` in SQL and DDL.

        	RangeImpl__visit_name__)
issubclassAbstractSingleRangeImplAbstractMultiRangeImpl	__class__r   typesuperr   )r6   r   r   
visit_namer   r7   r8   r     s   
c                   @  s   e Zd ZdZdddZd d	d
Zd ddZd ddZeZd ddZ	e	Z
d ddZd ddZd ddZd ddZd ddZd!ddZdS )"z AbstractRange.comparator_factoryz-Define comparison operations for range types.rk   r   r   r:   ColumnElement[bool]c                 K     | j t|S )a
  Boolean expression. Returns true if the right hand operand,
            which can be an element or a range, is contained within the
            column.

            kwargs may be ignored by this operator but are required for API
            conformance.
            )exproperater   )r6   rk   r   r7   r7   r8   rz     s   z)AbstractRange.comparator_factory.containsc                 C  r   )zsBoolean expression. Returns true if the column is contained
            within the right hand operand.
            )r   r   r   r   r7   r7   r8   rx   #  rI   z-AbstractRange.comparator_factory.contained_byc                 C  r   )zBoolean expression. Returns true if the column overlaps
            (has points in common with) the right hand operand.
            )r   r   r   r   r7   r7   r8   r}   )  rI   z)AbstractRange.comparator_factory.overlapsc                 C  r   )zsBoolean expression. Returns true if the column is strictly
            left of the right hand operand.
            )r   r   r   r   r7   r7   r8   r~   /  rI   z1AbstractRange.comparator_factory.strictly_left_ofc                 C  r   )ztBoolean expression. Returns true if the column is strictly
            right of the right hand operand.
            )r   r   r   r   r7   r7   r8   r   7  rI   z2AbstractRange.comparator_factory.strictly_right_ofc                 C  r   )zBoolean expression. Returns true if the range in the column
            does not extend right of the range in the operand.
            )r   r   r   r   r7   r7   r8   r   ?  rI   z4AbstractRange.comparator_factory.not_extend_right_ofc                 C  r   )zBoolean expression. Returns true if the range in the column
            does not extend left of the range in the operand.
            )r   r   r   r   r7   r7   r8   r   E  rI   z3AbstractRange.comparator_factory.not_extend_left_ofc                 C  r   )z}Boolean expression. Returns true if the range in the column
            is adjacent to the range in the operand.
            )r   r   r   r   r7   r7   r8   r   K  rI   z,AbstractRange.comparator_factory.adjacent_toc                 C     | j tj|S zRange expression. Returns the union of the two ranges.
            Will raise an exception if the resulting range is not
            contiguous.
            )r   r   r   addr   r7   r7   r8   r   Q     z&AbstractRange.comparator_factory.unionc                 C  r   r   )r   r   r   subr   r7   r7   r8   r   X  r   z+AbstractRange.comparator_factory.differenceColumnElement[Range[_T]]c                 C  r   )zRange expression. Returns the intersection of the two ranges.
            Will raise an exception if the resulting range is not
            contiguous.
            )r   r   r   mulr   r7   r7   r8   r   _  r   z-AbstractRange.comparator_factory.intersectionN)rk   r   r   r   r:   r   )rk   r   r:   r   )rk   r   r:   r   )r   r   r   r   rz   rx   r}   r~   r   r   r   r   r   r   r   r   r   r7   r7   r7   r8   comparator_factory  s    










r   )r   r   r   r   r:   r#   )r   r   r   r   r:   r   )r   r   r   r   r:   r   )r   r   r   r   render_bind_cast__abstract__r   r   r   
Comparatorr+   r   r   __classcell__r7   r7   r   r8   r     s    ""r   c                   @     e Zd ZdZdZd
ddZd	S )rR   zBase for PostgreSQL RANGE types.

    These are types that return a single :class:`_postgresql.Range` object.

    .. seealso::

        `PostgreSQL range functions <https://www.postgresql.org/docs/current/static/functions-range.html>`_

    TrU   
Range[Any]r:   r   c                 C  sz   |j d ur|j n|j}t|trt|rt S t S t|ttfr$t	 S t|t
r2|js/t S t S t|tr:t S tjS r;   )r-   r.   r[   r\   	_is_int32	INT4RANGE	INT8RANGEr   floatNUMRANGEr   tzinfoTSRANGE	TSTZRANGEr   	DATERANGEsqltypesNULLTYPE)r6   rU   specr7   r7   r8   _resolve_for_literalt  s   


z(AbstractSingleRange._resolve_for_literalN)rU   r   r:   r   r   r   r   r   r   r   r7   r7   r7   r8   rR   g  s    
rR   c                   @     e Zd ZdZdS )r   zQMarker for AbstractSingleRange that will apply a subclass-specific
    adaptationNr   r   r   r   r7   r7   r7   r8   r         r   c                   @  r   )r   zBase for PostgreSQL MULTIRANGE types.

    these are types that return a sequence of :class:`_postgresql.Range`
    objects.

    TrU   Sequence[Range[Any]]r:   r   c                 C  s   |st jS |d }|jd ur|jn|j}t|tr(tdd |D r%t S t S t|t	t
fr2t S t|tr@|js=t S t S t|trHt S t jS )Nr   c                 s  s    | ]}t |V  qd S r;   )r   ).0r   r7   r7   r8   	<genexpr>  s    z:AbstractMultiRange._resolve_for_literal.<locals>.<genexpr>)r   r   r-   r.   r[   r\   allINT4MULTIRANGEINT8MULTIRANGEr   r   NUMMULTIRANGEr   r   TSMULTIRANGETSTZMULTIRANGEr   DATEMULTIRANGE)r6   rU   firstr   r7   r7   r8   r     s   


z'AbstractMultiRange._resolve_for_literalN)rU   r   r:   r   r   r7   r7   r7   r8   r     s    r   c                   @  r   )r   zPMarker for AbstractMultiRange that will apply a subclass-specific
    adaptationNr   r7   r7   r7   r8   r     r   r   c                   @     e Zd ZdZd ZdS )r   z(Represent the PostgreSQL INT4RANGE type.Nr   r   r   r   r   r7   r7   r7   r8   r         r   c                   @  r   )r   z(Represent the PostgreSQL INT8RANGE type.Nr   r7   r7   r7   r8   r     r   r   c                   @  r   )r   z'Represent the PostgreSQL NUMRANGE type.Nr   r7   r7   r7   r8   r     r   r   c                   @  r   )r   z(Represent the PostgreSQL DATERANGE type.Nr   r7   r7   r7   r8   r     r   r   c                   @  r   )r   &Represent the PostgreSQL TSRANGE type.Nr   r7   r7   r7   r8   r     r   r   c                   @  r   )r   (Represent the PostgreSQL TSTZRANGE type.Nr   r7   r7   r7   r8   r     r   r   c                   @  r   )r   z-Represent the PostgreSQL INT4MULTIRANGE type.Nr   r7   r7   r7   r8   r     r   r   c                   @  r   )r   z-Represent the PostgreSQL INT8MULTIRANGE type.Nr   r7   r7   r7   r8   r     r   r   c                   @  r   )r   z,Represent the PostgreSQL NUMMULTIRANGE type.Nr   r7   r7   r7   r8   r     r   r   c                   @  r   )r   z-Represent the PostgreSQL DATEMULTIRANGE type.Nr   r7   r7   r7   r8   r     r   r   c                   @  r   )r   r   Nr   r7   r7   r7   r8   r     r   r   c                   @  r   )r   r   Nr   r7   r7   r7   r8   r     r   r   ii   r   
Range[int]r:   r2   c                 C  sH   | j d u st| j   kotkn  o#| jd u p#t| j  ko!tkS   S r;   )r-   _min_int_32_max_int_32r.   )r   r7   r7   r8   r     s
   $r   r7   )r   r   r:   r2   )H
__future__r   r   r   r   r   decimalr   typingr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   sqlsql.type_apir   utilr    util.typingr!   sql.elementsr"   r#   r$   r%   r0   dc_slotsr   	dataclassr+   r   r   rR   r   r   r   r\   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r7   r7   r7   r8   <module>   s   
      #$