U
    [h                     @  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j0f d+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 ZDdRZEdSZFdTdUdVdWdXZGdS )Y    )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rTej	ddZ
ded	< ej	d
dZded< n8ej	f ddieZ
ded	< ej	f dd
ieZded< esdXdd
ddddddddZddddZeddddZeddddZeddddZeddddZeddddZedddd Zed!dd"d#Zd$dd%d&d'Zd(dd)d*ZdYdd+dd+dd,d-d.d/Zd(dd0d1d2Zd3dd0d4d5Zd6dd%d7d8ZeZd3dd0d9d:Zd3dd0d;d<Z e Z!d3dd0d=d>Z"e"Z#d3dd0d?d@Z$d3dd0dAdBZ%dd+dd+ddCdDdEZ&d3dd0dFdGZ'd3d3d0dHdIZ(d3d3d0dJdKZ)d3d3d0dLdMZ*d3d3d0dNdOZ+d3d3d0dPdQZ,d3d3d0dRdSZ-d+ddTdUZ.d+ddVdWZ/dS )Z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

    NzOptional[_T]lowerupperr'   )default_BoundsTypeboundsFboolemptyr.   )r0   r2   r,   r-   r0   r2   c                C  s   | j ||||d d S )Nr3   )__dict__update)selfr,   r-   r0   r2    r7   i/var/www/html/timesheet/backend/venv/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/ranges.py__init__a   s    	zRange.__init__returnc                 C  s   | j  S Nr2   r6   r7   r7   r8   __bool__s   s    zRange.__bool__c                 C  s   | j S z$A synonym for the 'empty' attribute.r=   r>   r7   r7   r8   isemptyv   s    zRange.isemptyc                 C  s   | j S r@   r=   r>   r7   r7   r8   is_empty|   s    zRange.is_emptyc                 C  s   | j d dkS )z,Return True if the lower bound is inclusive.r   [r0   r>   r7   r7   r8   	lower_inc   s    zRange.lower_incc                 C  s   | j  o| jdkS )zKReturn True if this range is non-empty and lower bound is
        infinite.N)r2   r,   r>   r7   r7   r8   	lower_inf   s    zRange.lower_infc                 C  s   | j d dkS )z,Return True if the upper bound is inclusive.r   ]rD   r>   r7   r7   r8   	upper_inc   s    zRange.upper_incc                 C  s   | j  o| jdkS )zOReturn True if this range is non-empty and the upper bound is
        infinite.N)r2   r-   r>   r7   r7   r8   	upper_inf   s    zRange.upper_infzAbstractSingleRange[_T]c                 C  s   t  S r<   )AbstractSingleRanger>   r7   r7   r8   __sa_type_engine__   s    zRange.__sa_type_engine__r%   valuer;   c                 C  s   | j r
dS | jdkr@| jdkp>| jd dkr6|| jk S || jkS | jdkrl| jd dkrb|| jkS || jkS | jd dkr|| jkn|| jko| jd dkr|| jk S || jkS )z3Return True if this range contains the given value.FNr   )r   ()r2   r,   r-   r0   r6   rM   r7   r7   r8   _contains_value   s*    


zRange._contains_valuer   c                 C  sb   t | jtst | jtrdS t | jts4t | jtr8dS t | jtsPt | jtrZtddS 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strrT   )value1bound1value2bound2only_valuesr;   c                 C  sx  |dk}|dk}||  kr$dkrBn n||kr4dS |r<dS dS n(|dkrV|rRdS dS |dkrj|rfdS dS ||kr~||kr~dS |dk}|dk}	|   }
|
dk	r|r|s||
7 }d}n|r||
7 }d}|r|	s||
7 }d}	n|	r||
7 }d}	||k rdS ||krdS |rdS |r|	rdS |sH|	sH||kr8dS |rBdS dS n,|s\|rXdS dS |	sp|rldS d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.
        >   rC   rO   Nr   r   >   rC   rG   TF)rU   )r6   rW   rX   rY   rZ   r[   Zvalue1_is_lower_boundZvalue2_is_lower_boundZ
value1_incZ
value2_incstepr7   r7   r8   _compare_edges   s^    

zRange._compare_edgesotherr;   c           
      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o| ||||	dkS )zCompare this range to the `other` taking into account
        bounds inclusivity, returning ``True`` if they are equal.
        TFr   r   )rS   r+   NotImplementedr2   r,   r0   r-   r^   
r6   r`   slowerslower_bolowerolower_bsuppersupper_boupperoupper_br7   r7   r8   __eq__+  s"    




zRange.__eq__z	Range[_T]c           
      C  s   | j r
dS |j rdS | j}| jd }|j}|jd }| ||||dk rLdS | j}| jd }|j}|jd }	| ||||	dkrdS dS )z7Determine whether this range is a contained by `other`.TFr   r   )r2   r,   r0   r^   r-   rb   r7   r7   r8   contained_byF  s"    



zRange.contained_byzUnion[_T, Range[_T]]c                 C  s"   t |tr|| S | |S dS )z.Determine whether this range contains `value`.N)rS   r+   rl   rQ   rP   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r| ||||dkrdS dS )z3Determine whether this range overlaps with `other`.Fr   r   Tr2   r,   r0   r-   r^   
r6   r`   rc   rd   rg   rh   re   rf   ri   rj   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   )r2   r-   r0   r,   r^   )r6   r`   rg   rh   re   rf   r7   r7   r8   strictly_left_of  s    

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   rn   )r6   r`   rc   rd   ri   rj   r7   r7   r8   strictly_right_of  s    

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   )r2   r,   r0   r^   )r6   r`   rc   rd   re   rf   r7   r7   r8   not_extend_left_of  s    

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   )r2   r-   r0   r^   )r6   r`   rg   rh   ri   rj   r7   r7   r8   not_extend_right_of  s    

zRange.not_extend_right_of)rW   rX   rY   rZ   r;   c                 C  s   |  ||||d}|dkrr|  }|dkr.dS |dkrT|dkrJ||| kS ||kS q|dkrd||kS ||| kS n`|dkr|dkr|dks|dkr|d	kr|  }|dk	rdS |dkr|dkp|dko|d	kS dS dS )
zTDetermine whether an upper bound is immediately successive to a
        lower bound.Tr\   NFrG   rC   r   rN   rO   )r^   rU   )r6   rW   rX   rY   rZ   resr]   r7   r7   r8   _upper_edge_adjacent_to_lower  s>    
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 }	| ||||pn| ||	||S )z8Determine whether this range is adjacent to the `other`.Fr   r   )r2   r,   r0   r-   rv   ro   r7   r7   r8   adjacent_to  s.    



      zRange.adjacent_toc                 C  s   | j r
|S |j r| S | |s0| |s0td| j}| jd }| j}| jd }|j}|jd }|j}|jd }	| ||||dk r|}
|}n|}
|}| ||||	dkr|}|}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   rD   )r2   rp   rw   
ValueErrorr,   r0   r-   r^   r+   r   r/   r6   r`   rc   rd   rg   rh   re   rf   ri   rj   Zrlowerrlower_bZrupperrupper_br7   r7   r8   union  s>    



  zRange.unionc                 C  s
   |  |S r<   )r|   r6   r`   r7   r7   r8   __add__@  s    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 r|dkrtd| ||||	}| ||||}|dks|dk r| S |
dkr|dkrtddddS |
dkrZ|dkrZ|dkrZ|dkrdnd	}|dkrB|d	krB| ||||dkrBt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 dst	d|  d| dS )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=   rC   rN   rG   rD   rO   FzUnhandled case computing z - )
r2   r,   r0   r-   r^   rx   r+   r   r/   AssertionError)r6   r`   rc   rd   rg   rh   re   rf   ri   rj   Zsl_vs_olZsu_vs_ouZsl_vs_ouZsu_vs_olr{   rz   r7   r7   r8   
differenceC  sn    



zRange.differencec                 C  s
   |  |S r<   )r   r}   r7   r7   r8   __sub__  s    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 r|}
|}n|}
|}| ||||	dkr|}|	}n|}|}t|
|tt|| dS )zdCompute the intersection of this range with the `other`.

        .. versionadded:: 2.0.10

        NTr=   r   r   rD   )	r2   rp   r+   r,   r0   r-   r^   r   r/   ry   r7   r7   r8   intersection  s2    



zRange.intersectionc                 C  s
   |  |S r<   )r   r}   r7   r7   r8   __mul__  s    zRange.__mul__c                 C  s   |   S r<   )
_stringifyr>   r7   r7   r8   __str__  s    zRange.__str__c                 C  s^   | j r
dS | j| j }}|d kr$dn|}|d kr4dn|}td| j\}}| | d| | S )Nr2    zTuple[str, str],)r2   r,   r-   r   r0   )r6   lrZb0Zb1r7   r7   r8   r     s    zRange._stringify)NN)F)0__name__
__module____qualname____doc__r,   __annotations__r-   r   dataclassesfieldr0   r2   	dc_kwonlyr    r9   r?   propertyrA   rB   rE   rF   rH   rI   rK   rQ   rU   r^   rk   rl   rm   __contains__rp   rq   
__lshift__rr   
__rshift__rs   rt   rv   rw   r|   r~   r   r   r   r   r   r   r7   r7   r7   r8   r+   ;   sn   
   Y /.L&r+   c                   @  s"   e Zd ZdZed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`.
    zAbstractMultiRange[_T]r:   c                 C  s   t  S r<   )AbstractMultiRanger>   r7   r7   r8   rK     s    zMultiRange.__sa_type_engine__N)r   r   r   r   r   rK   r7   r7   r7   r8   r     s   r   c                      sx   e Zd ZdZdZdZeddddddZed	dd
dddZddd
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.Tz	Type[_TE]r   r#   )clskwr;   c                 K  s   d S r<   r7   r6   r   r   r7   r7   r8   adapt  s    zAbstractRange.adaptzType[TypeEngineMixin]zTypeEngine[Any]c                 K  s   d S r<   r7   r   r7   r7   r8   r     s    z-Type[Union[TypeEngine[Any], TypeEngineMixin]]c                   sL   t |ttfr<|| jk	r<| j}t| d|| jfd|i S t |S d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.

        Z	RangeImpl__visit_name__N)
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dddZddddd	Zdddd
dZdddddZeZdddddZ	e	Z
dddddZdddddZdddddZdddddZdddddZdddddZdS )z AbstractRange.comparator_factoryz-Define comparison operations for range types.r   zColumnElement[bool])r`   r   r;   c                 K  s   | 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   r`   r   r7   r7   r8   rm     s    z)AbstractRange.comparator_factory.containsr_   c                 C  s   | j t|S )zsBoolean expression. Returns true if the column is contained
            within the right hand operand.
            )r   r   r   r}   r7   r7   r8   rl   #  s    z-AbstractRange.comparator_factory.contained_byc                 C  s   | j t|S )zBoolean expression. Returns true if the column overlaps
            (has points in common with) the right hand operand.
            )r   r   r   r}   r7   r7   r8   rp   )  s    z)AbstractRange.comparator_factory.overlapsc                 C  s   | j t|S )zsBoolean expression. Returns true if the column is strictly
            left of the right hand operand.
            )r   r   r   r}   r7   r7   r8   rq   /  s    z1AbstractRange.comparator_factory.strictly_left_ofc                 C  s   | j t|S )ztBoolean expression. Returns true if the column is strictly
            right of the right hand operand.
            )r   r   r   r}   r7   r7   r8   rr   7  s    z2AbstractRange.comparator_factory.strictly_right_ofc                 C  s   | j t|S )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   rt   ?  s    z4AbstractRange.comparator_factory.not_extend_right_ofc                 C  s   | j t|S )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   rs   E  s    z3AbstractRange.comparator_factory.not_extend_left_ofc                 C  s   | j t|S )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   rw   K  s    z,AbstractRange.comparator_factory.adjacent_toc                 C  s   | 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  s    z&AbstractRange.comparator_factory.unionc                 C  s   | j tj|S r   )r   r   r   subr}   r7   r7   r8   r   X  s    z+AbstractRange.comparator_factory.differencezColumnElement[Range[_T]]c                 C  s   | j tj|S )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   _  s    z-AbstractRange.comparator_factory.intersectionN)r   r   r   r   rm   rl   rp   rq   r   rr   r   rt   rs   rw   r|   r   r   r7   r7   r7   r8   comparator_factory  s   
r   )r   r   r   r   render_bind_cast__abstract__r   r   r   
Comparatorr+   r   r   __classcell__r7   r7   r   r8   r     s   "r   c                   @  s$   e Zd ZdZdZdddddZdS )	rJ   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>`_

    Tz
Range[Any]r   rL   c                 C  s   |j d k	r|j n|j}t|tr6t|r.t S t S nFt|ttfrJt	 S t|t
rf|js`t S t S t|trvt S tjS d S r<   )r,   r-   rS   rT   	_is_int32	INT4RANGE	INT8RANGEr   floatNUMRANGEr   tzinfoTSRANGE	TSTZRANGEr   	DATERANGEsqltypesNULLTYPE)r6   rM   specr7   r7   r8   _resolve_for_literalt  s    


z(AbstractSingleRange._resolve_for_literalNr   r   r   r   r   r   r7   r7   r7   r8   rJ   g  s   
rJ   c                   @  s   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     s   r   c                   @  s$   e Zd ZdZdZdddddZdS )	r   zBase for PostgreSQL MULTIRANGE types.

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

    TzSequence[Range[Any]]r   rL   c                 C  s   |s
t jS |d }|jd k	r"|jn|j}t|trRtdd |D rJt S t S nFt|t	t
frft S t|tr|js|t S t S t|trt S t jS d 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-   rS   rT   allINT4MULTIRANGEINT8MULTIRANGEr   r   NUMMULTIRANGEr   r   TSMULTIRANGETSTZMULTIRANGEr   DATEMULTIRANGE)r6   rM   firstr   r7   r7   r8   r     s    


z'AbstractMultiRange._resolve_for_literalNr   r7   r7   r7   r8   r     s   r   c                   @  s   e Zd ZdZdS )r   zPMarker for AbstractMultiRange that will apply a subclass-specific
    adaptationNr   r7   r7   r7   r8   r     s   r   c                   @  s   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     s   r   c                   @  s   e Zd ZdZd ZdS )r   z(Represent the PostgreSQL INT8RANGE type.Nr   r7   r7   r7   r8   r     s   r   c                   @  s   e Zd ZdZd ZdS )r   z'Represent the PostgreSQL NUMRANGE type.Nr   r7   r7   r7   r8   r     s   r   c                   @  s   e Zd ZdZd ZdS )r   z(Represent the PostgreSQL DATERANGE type.Nr   r7   r7   r7   r8   r     s   r   c                   @  s   e Zd ZdZd ZdS )r   &Represent the PostgreSQL TSRANGE type.Nr   r7   r7   r7   r8   r     s   r   c                   @  s   e Zd ZdZd ZdS )r   (Represent the PostgreSQL TSTZRANGE type.Nr   r7   r7   r7   r8   r     s   r   c                   @  s   e Zd ZdZd ZdS )r   z-Represent the PostgreSQL INT4MULTIRANGE type.Nr   r7   r7   r7   r8   r     s   r   c                   @  s   e Zd ZdZd ZdS )r   z-Represent the PostgreSQL INT8MULTIRANGE type.Nr   r7   r7   r7   r8   r     s   r   c                   @  s   e Zd ZdZd ZdS )r   z,Represent the PostgreSQL NUMMULTIRANGE type.Nr   r7   r7   r7   r8   r     s   r   c                   @  s   e Zd ZdZd ZdS )r   z-Represent the PostgreSQL DATEMULTIRANGE type.Nr   r7   r7   r7   r8   r     s   r   c                   @  s   e Zd ZdZd ZdS )r   r   Nr   r7   r7   r7   r8   r     s   r   c                   @  s   e Zd ZdZd ZdS )r   r   Nr   r7   r7   r7   r8   r     s   r   ii   z
Range[int]r1   )r   r;   c                 C  sH   | j d ks$t| j   kotkn  oF| jd kpFt| j  koBtkS   S r<   )r,   _min_int_32_max_int_32r-   )r   r7   r7   r8   r     s    $"r   )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   sqlZsql.type_apir   utilr    Zutil.typingr!   Zsql.elementsr"   r#   r$   r%   r/   Zdc_slotsr   	dataclassr+   r   r   rJ   r   r   r   rT   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r7   r7   r7   r8   <module>   s   
      #$