
    h                     \    d dl mZmZ d dlmZmZmZ d dlmZ d dl	m
Z
 dgZ G d de      Zy)    )	StatementTable)
DeferrableFQ)BaseConstraint)QueryExclusionConstraintc                   d     e Zd ZdZdddd fd
Zd Zd Zd Zd Zd	 Z	 fd
Z
 fdZd Z xZS )r
   zYCONSTRAINT %(name)s EXCLUDE USING %(index_type)s (%(expressions)s)%(where)s%(deferrable)sN)
index_type	condition
deferrablec                   |r|j                         dvrt        d      |st        d      t        d |D              st        d      t        |t	        d       t
        f      st        d      |r|rt        d      t        |t	        d       t        f      st        d      || _        |xs d	| _        || _	        || _
        t        | 1  |
       y )N>   gistspgistz;Exclusion constraints only support GiST or SP-GiST indexes.zFAt least one expression is required to define an exclusion constraint.c              3   h   K   | ]*  }t        |t        t        f      xr t        |      d k(   , yw)   N)
isinstancelisttuplelen).0exprs     f/var/www/html/ranktracker/api/venv/lib/python3.12/site-packages/django/contrib/postgres/constraints.py	<genexpr>z/ExclusionConstraint.__init__.<locals>.<genexpr>   s3      
 tdE]+>D	Q>
s   02z+The expressions must be a list of 2-tuples.z3ExclusionConstraint.condition must be a Q instance.z7ExclusionConstraint with conditions cannot be deferred.z=ExclusionConstraint.deferrable must be a Deferrable instance.GIST)name)lower
ValueErrorallr   typer   r   expressionsr   r   r   super__init__)selfr   r"   r   r   r   	__class__s         r   r$   zExclusionConstraint.__init__   s     ***,4FFM     
#
 
 JKK)d4j!_5E  I  *tDz:&>?O  '$."$d#    c                    g }| j                   D ]s  \  }}t        |t              rt        |      }|j	                  |      }|j                  |      \  }}|t        fd|D              z  }|j                  |d|       u |S )N)queryc              3   @   K   | ]  }j                  |        y wNquote_valuer   pschema_editors     r   r   z:ExclusionConstraint._get_expression_sql.<locals>.<genexpr>7   s     Kqm77:K   z WITH )r"   r   strr   resolve_expressioncompiler   append)	r%   compilerr0   r)   r"   
expressionoperatorsqlparamss	     `      r   _get_expression_sqlz'ExclusionConstraint._get_expression_sql0   s    $($4$4 	? J*c*z]
#66U6CJ"**:6KCKFKKKCsH=>	? r'   c                     | j                   y |j                  | j                         }|j                  |j                        \  }}|t	        fd|D              z  S )Nc              3   @   K   | ]  }j                  |        y wr+   r,   r.   s     r   r   z9ExclusionConstraint._get_condition_sql.<locals>.<genexpr>@   s     HA=44Q7Hr1   )r   build_whereas_sql
connectionr   )r%   r6   r0   r)   wherer9   r:   s     `    r   _get_condition_sqlz&ExclusionConstraint._get_condition_sql;   sT    >>!!!$..1ll8]-E-EFVUHHHHHr'   c                 n   t        |d      }|j                  |j                        }| j                  |||      }| j	                  |||      }| j
                  |j                  | j                        | j                  dj                  |      |rd|z  nd|j                  | j                        dz  S )NF)
alias_cols)r@   z, z WHERE (%s) )r   r   r"   rA   r   )r	   get_compilerr@   r;   rB   template
quote_namer   r   join_deferrable_constraint_sqlr   )r%   modelr0   r)   r6   r"   r   s          r   constraint_sqlz"ExclusionConstraint.constraint_sqlB   s    e.%%1I1I%J..xN++HmUK	}}!,,TYY7//99[12;]Y.'BB4??S 
 
 	
r'   c                     t        dt        |j                  j                  |j                        | j                  ||            S )Nz(ALTER TABLE %(table)s ADD %(constraint)s)table
constraint)r   r   _metadb_tablerH   rL   r%   rK   r0   s      r   
create_sqlzExclusionConstraint.create_sqlO   s<    6,,m.F.FG**5-@
 	
r'   c                 n    |j                  |j                  ||j                  | j                              S r+   )_delete_constraint_sqlsql_delete_checkrH   r   rR   s      r   
remove_sqlzExclusionConstraint.remove_sqlV   s3    33**$$TYY/
 	
r'   c                    t         |          \  }}}| j                  |d<   | j                  | j                  |d<   | j                  j                         dk7  r| j                  |d<   | j                  r| j                  |d<   |||fS )Nr"   r   r   r   r   )r#   deconstructr"   r   r   r   r   )r%   pathargskwargsr&   s       r   rY   zExclusionConstraint.deconstruct]   s    "W02dF $ 0 0}>>%"&..F;??  "f,#'??F< ??#'??F< T6!!r'   c                 X   t        || j                        r| j                  |j                  k(  xrj | j                  |j                  k(  xrO | j                  |j                  k(  xr4 | j
                  |j
                  k(  xr | j                  |j                  k(  S t        | !  |      S r+   )	r   r&   r   r   r"   r   r   r#   __eq__)r%   otherr&   s     r   r^   zExclusionConstraint.__eq__h   s    eT^^,		UZZ' 45#3#334  E$5$554 %//14 5#3#33 w~e$$r'   c           	          d| j                   j                  d| j                  d| j                  | j                  dnd| j                  z  | j
                  dd	S d| j
                  z  d	S )N<z: index_type=z, expressions=rE   z, condition=%sz, deferrable=%s>)r&   __qualname__r   r"   r   r   )r%   s    r   __repr__zExclusionConstraint.__repr__s   so    NN''OO..(B.>.OO//)BR
 	

 0A4??/RR
 	
r'   )__name__
__module__rc   rG   r$   r;   rB   rL   rS   rW   rY   r^   rd   __classcell__)r&   s   @r   r
   r
   	   sA    jH 04t"$H	I


	"	%
r'   N)!django.db.backends.ddl_referencesr   r   django.db.modelsr   r   r   django.db.models.constraintsr   django.db.models.sqlr	   __all__r
    r'   r   <module>rn      s*    > - - 7 & 
!q
. q
r'   