
    g=&                         d dl mZ d dlmZ d dlmZmZ d dlmZm	Z	m
Z
 d dlmZmZmZmZ d dlmZmZ d dlmZ d dlmZ d d	lmZ d
gZ G d de      Z G d d
e      Zy)    )NoneType)ValidationError)DEFAULT_DB_ALIASNotSupportedError)Expressions	StatementTable)BaseConstraint
DeferrableFQ)ExistsExpressionList)IndexExpression)PostgresOperatorLookup)QueryExclusionConstraintc                       e Zd ZdZy)ExclusionConstraintExpressionz!%(expressions)s WITH %(operator)sN)__name__
__module____qualname__template     {/var/www/python.vincentserveurtest.ovh/public_html/venv/lib/python3.12/site-packages/django/contrib/postgres/constraints.pyr   r      s    2Hr   r   c                        e Zd ZdZddddddd fd
Zd Zd Zd Zd Zd	 Z	d
 Z
d Z fdZ fdZd ZdefdZ xZS )r   zdCONSTRAINT %(name)s EXCLUDE USING %(index_type)s (%(expressions)s)%(include)s%(where)s%(deferrable)sN)
index_type	condition
deferrableincludeviolation_error_codeviolation_error_messagec                   |r|j                         dvrt        d      |st        d      t        d |D              st        d      t        |t        t
        f      st        d      t        |t        t        f      st        d      t        |t        t        t        f      st        d      || _	        |xs d	| _
        || _        || _        |rt        |      nd
| _        t        	| =  |||       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     r   	<genexpr>z/ExclusionConstraint.__init__.<locals>.<genexpr>.   s/      
KV4JtdE]+>D	Q>;s   02z+The expressions must be a list of 2-tuples.z3ExclusionConstraint.condition must be a Q instance.z=ExclusionConstraint.deferrable must be a Deferrable instance.z4ExclusionConstraint.include must be a list or tuple.GISTr   )namer"   r#   )lower
ValueErrorallr)   r   r   r   r*   r+   expressionsr   r   r    r!   super__init__)
selfr1   r5   r   r   r    r!   r"   r#   	__class__s
            r   r7   zExclusionConstraint.__init__   s	    ***,4FFM     
KV
 
 JKK)h]3RSS*x&<=O  'HdE#:;STT&$."$)0uW~b!5$; 	 	
r   c                    g }t        | j                        D ]\  \  }\  }}t        |t              rt	        |      }t        ||      }|j                  |j                         |j                  |       ^ t        | j                  |      S )N)operator)	enumerater5   r)   strr   r   set_wrapper_classes
connectionappendr   resolve_expression)r8   schema_editorqueryr5   idx
expressionr;   s          r   _get_expressionsz$ExclusionConstraint._get_expressionsE   s    +4T5E5E+F'C'*h*c*z]
6zHUJ**=+C+CDz* ,G {+>>uEEr   c                     t               }| j                  D ]@  \  }}t        |t              rt	        |      }|j                  |j                  |             B | j                  ||      S N)setr5   r)   r=   r   update_get_expr_references_check_references)r8   modelr?   
referencesr.   _s         r   _checkzExclusionConstraint._checkO   s^    U
''GD!$$we88>? ( %%eZ88r   c                     | j                   y |j                  | j                         }|j                  |j                        \  }}|t	        fd|D              z  S )Nc              3   @   K   | ]  }j                  |        y wrH   )quote_value)r-   prB   s     r   r/   z9ExclusionConstraint._get_condition_sql.<locals>.<genexpr>\   s     HA=44Q7s   )r   build_whereas_sqlr?   r+   )r8   compilerrB   rC   wheresqlparamss     `    r   _get_condition_sqlz&ExclusionConstraint._get_condition_sqlW   sT    >>!!!$..1ll8]-E-EFVUHHHHHr   c                    t        |d      }|j                  |j                        }| j                  ||      }|j                  j
                  }| j                  |||      }| j                  D cg c]'  }|j                  j                  |      j                  ) }	}t        | j                  t        ||j                        |j                  | j                        | j                  t!        ||||j"                        |rd|z  nd|j%                  ||	      |j'                  | j(                              S c c}w )NF)
alias_cols)r?   z WHERE (%s) )tabler1   r   r5   rX   r!   r    )r   get_compilerr?   rF   _metadb_tabler[   r!   	get_fieldcolumnr   r   r	   
quote_namer1   r   r   rS   _index_include_sql_deferrable_constraint_sqlr    )
r8   rM   rB   rC   rW   r5   r_   r   
field_namer!   s
             r   constraint_sqlz"ExclusionConstraint.constraint_sql^   s   e.%%1I1I%J++M5A$$++HmUK	GK||
GSEKK!!*-44| 	 
 MM}778))$))4#{Hm.G.G 09-)+b!44UGD$??P
 	

s   3,D;c                     | j                  |       t        dt        |j                  j                  |j
                        | j                  ||            S )Nz(ALTER TABLE %(table)s ADD %(constraint)s)r_   
constraint)check_supportedr   r	   ra   rb   re   ri   r8   rM   rB   s      r   
create_sqlzExclusionConstraint.create_sqlt   sJ    ]+6,,m.F.FG**5-@
 	
r   c                 n    |j                  |j                  ||j                  | j                              S rH   )_delete_constraint_sqlsql_delete_checkre   r1   rm   s      r   
remove_sqlzExclusionConstraint.remove_sql|   s3    33**$$TYY/
 	
r   c                     | j                   rJ| j                  j                         dk(  r,|j                  j                  j
                  st        d      y y y )Nr&   zMCovering exclusion constraints using an SP-GiST index require PostgreSQL 14+.)r!   r   r2   r?   features supports_covering_spgist_indexesr   )r8   rB   s     r   rl   z#ExclusionConstraint.check_supported   sU    LL%%'83!,,55VV#*  W 4 r   c                 J   t         |          \  }}}| j                  |d<   | j                  | j                  |d<   | j                  j                         dk7  r| j                  |d<   | j                  r| j                  |d<   | j                  r| j                  |d<   |||fS )Nr5   r   r%   r   r    r!   )r6   deconstructr5   r   r   r2   r    r!   )r8   pathargskwargsr9   s       r   rw   zExclusionConstraint.deconstruct   s    "W02dF $ 0 0}>>%"&..F;??  "f,#'??F< ??#'??F< << $F9T6!!r   c                    t        || j                        r| j                  |j                  k(  xr | j                  |j                  k(  xr | j                  |j                  k(  x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 rH   )r)   r9   r1   r   r5   r   r    r!   r"   r#   r6   __eq__)r8   otherr9   s     r   r|   zExclusionConstraint.__eq__   s    eT^^,		UZZ' ROOu'7'77R$$(9(99R NNeoo5R OOu'7'77	R
 LLEMM1R --1K1KKR 00E4Q4QQ	 w~e$$r   c                 &   d| j                   j                  dt        | j                        dt        | j                        dt        | j
                        | j                  dnd| j                  z  | j                  dnd| j                  z  | j                  sdndt        | j                        z  | j                  dnd	| j                  z  | j                  | j                  | j                  k(  rddS d
| j                  z  dS )N<z: index_type=z expressions=z name=r^   z condition=%sz deferrable=%rz include=%sz violation_error_code=%rz violation_error_message=%r>)r9   r   reprr   r5   r1   r   r    r!   r"   r#   default_violation_error_message)r8   s    r   __repr__zExclusionConstraint.__repr__   s   NN''!!!"O..(Bo.NN//)B/?$///QQllBT\\8J(JJ ,,4 /$2K2KKL //7//43W3WW  R
 	
" 3T5Q5QQR
 	
r   c                    |j                   j                  |      }|j                  |j                  |      }|j	                         D ci c]  \  }}t        |      | }	}}g }
t        | j                        D ]  \  }\  }}t        |t              rt        |      }|rWt        |t
              r|j                  |v r9 y |j                         D ]$  }t        |t
              s|j                  |v s#  y  |j                  |	      }t        |d      r|j                         }t        |d      r|j                         }t        ||      }||_        |
j#                  |         |j$                  |
 }|j'                  |j                        }|j(                  j*                  s||j-                  |      }| j.                  s6|j1                         r%t3        | j5                         | j6                        y | j.                  t9        |j%                  | j.                              z  j;                  ||      r%t3        | j5                         | j6                        y c c}}w )N)metaexcludeget_expression_for_validation)lhsrhs)pk)code)using)_default_managerr   _get_field_expression_mapra   itemsr   r<   r5   r)   r=   r1   flattenreplace_expressionshasattrr   r   postgres_operatorr@   filter_get_pk_val_stateaddingr   r   existsr   get_violation_error_messager"   r   check)r8   rM   instancer   r   querysetreplacement_mapfieldvaluereplacementslookupsrD   rE   r;   r.   rhs_expressionlookupmodel_class_pks                     r   validatezExclusionConstraint.validate   s-   ))//6"<<g = 
 =L<Q<Q<ST<SLE5%%<ST+4T5E5E+F'C'*h*c*z]
j!,!'1 * 2 2 4%dA.4993G" !5 (;;LINz#BC'EEG
~'FG!/!M!M!O+
OF'/F$NN6"% ,G& #8??G,!--ekk:%%.*D''>':H~~ %446T=V=V  !
 (G!HHOOu P  &446T=V=V ? Us   I')r   r   r   r   r7   rF   rP   r[   ri   rn   rr   rl   rw   r|   r   r   r   __classcell__)r9   s   @r   r   r      sh    	>  ! $*
XF9I
,

	"%
, 15<L )r   N)typesr   django.core.exceptionsr   	django.dbr   r   !django.db.backends.ddl_referencesr   r   r	   django.db.modelsr
   r   r   r   django.db.models.expressionsr   r   django.db.models.indexesr   django.db.models.lookupsr   django.db.models.sqlr   __all__r   r   r   r   r   <module>r      sH     2 9 K K = = ? 4 ; & 
!3O 3U. Ur   