o
    "7h~#                     @   s   d dl mZ d dlmZ d dl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 deZG dd
 d
eZdS )    )NoneType)ValidationError)DEFAULT_DB_ALIAS)Expressions	StatementTable)BaseConstraint
DeferrableFQ)ExistsExpressionList)IndexExpression)PostgresOperatorLookup)QueryExclusionConstraintc                   @   s   e Zd ZdZdS )ExclusionConstraintExpressionz!%(expressions)s WITH %(operator)sN)__name__
__module____qualname__template r   r   x/var/www/epreuve.sigeris.cm/public_html/epreuve/venv/lib/python3.10/site-packages/django/contrib/postgres/constraints.pyr      s    r   c                       s   e Zd ZdZddddddd fdd
Zdd Zdd	 Zd
d Zdd Zdd Z	dd Z
 fddZ fddZdd ZdefddZ  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          	         s   |r|  dvrtd|stdtdd |D stdt|ttfs*tdt|ttfs5tdt|tttfsAtd	|| _	|pGd
| _
|| _|| _|rUt|nd| _t j|||d d S )N>   gistspgistz;Exclusion constraints only support GiST or SP-GiST indexes.zFAt least one expression is required to define an exclusion constraint.c                 s   s*    | ]}t |ttfot|d kV  qdS )   N)
isinstancelisttuplelen).0exprr   r   r   	<genexpr>.   s    
z/ExclusionConstraint.__init__.<locals>.<genexpr>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__)	selfr*   r.   r   r   r   r   r   r   	__class__r   r   r0      s<   

zExclusionConstraint.__init__c                 C   s^   g }t | jD ] \}\}}t|trt|}t||d}||j || qt	| 
|S )N)operator)	enumerater.   r"   strr
   r   set_wrapper_classes
connectionappendr   resolve_expression)r1   schema_editorqueryr.   idx
expressionr4   r   r   r   _get_expressionsE   s   
z$ExclusionConstraint._get_expressionsc                 C   sD   t  }| jD ]\}}t|trt|}||| q| ||S N)setr.   r"   r6   r
   update_get_expr_references_check_references)r1   modelr8   
referencesr'   _r   r   r   _checkO   s   
zExclusionConstraint._checkc                    sF   | j d u rd S || j }|| j\}}|t fdd|D  S )Nc                 3   s    | ]}  |V  qd S r@   )quote_value)r&   pr;   r   r   r(   \   s    z9ExclusionConstraint._get_condition_sql.<locals>.<genexpr>)r   build_whereas_sqlr8   r$   )r1   compilerr;   r<   wheresqlparamsr   rK   r   _get_condition_sqlW   s
   
z&ExclusionConstraint._get_condition_sqlc           	         s   t  dd}|j|jd}| ||} jj}| |||} fdd| jD }t| j	t
||j|| j| jt||||j|rDd| nd| ||| jdS )	NF)
alias_cols)r8   c                    s   g | ]	} j |jqS r   )_meta	get_fieldcolumn)r&   
field_namerE   r   r   
<listcomp>d   s    z6ExclusionConstraint.constraint_sql.<locals>.<listcomp>z WHERE (%s) )tabler*   r   r.   rO   r   r   )r   get_compilerr8   r?   rT   db_tablerR   r   r   r   r   
quote_namer*   r   r   rI   _index_include_sql_deferrable_constraint_sqlr   )	r1   rE   r;   r<   rN   r.   r[   r   r   r   rX   r   constraint_sql^   s(   





z"ExclusionConstraint.constraint_sqlc                 C   s"   t dt|jj|j| ||dS )Nz(ALTER TABLE %(table)s ADD %(constraint)s)r[   
constraint)r   r   rT   r]   r^   ra   r1   rE   r;   r   r   r   
create_sqlt   s
   
zExclusionConstraint.create_sqlc                 C   s   | |j||| jS r@   )_delete_constraint_sqlsql_delete_checkr^   r*   rc   r   r   r   
remove_sql{   s
   
zExclusionConstraint.remove_sqlc                    sp   t   \}}}| j|d< | jd ur| j|d< | j dkr#| j|d< | jr+| j|d< | jr3| j|d< |||fS )Nr.   r   r   r   r   r   )r/   deconstructr.   r   r   r+   r   r   )r1   pathargskwargsr2   r   r   rh      s   






zExclusionConstraint.deconstructc                    sx   t || jr6| j|jko5| j|jko5| j|jko5| j|jko5| j|jko5| j|jko5| j|jko5| j	|j	kS t
 |S r@   )r"   r3   r*   r   r.   r   r   r   r   r   r/   __eq__)r1   otherr2   r   r   rl      s"   







zExclusionConstraint.__eq__c                 C   s   d| j jt| jt| jt| j| jd u rdnd| j | jd u r"dnd| j | js+dndt| j | j	d u r8dnd| j	 | j
d u sG| j
| jkrKdf	 S d| j
 f	 S )Nz4<%s: index_type=%s expressions=%s name=%s%s%s%s%s%s>rZ   z condition=%sz deferrable=%rz include=%sz violation_error_code=%rz violation_error_message=%r)r3   r   reprr   r.   r*   r   r   r   r   r   default_violation_error_message)r1   r   r   r   __repr__   s"   

zExclusionConstraint.__repr__c                 C   sF  |j |}|j|j|d}dd | D }g }| jD ]>\}	}
t|	tr)t|	}	|r5| 	||	|r5 d S |	
|}t|	drC|	 }	t|drL| }t|	|d}|
|_|| q|j| }||j}|jjsv||jrv|j|d}| js| rt|  | jdd S | jt|| j@ j||drt|  | jdd S )	N)metaexcludec                 S   s   i | ]	\}}t ||qS r   )r
   )r&   fieldvaluer   r   r   
<dictcomp>   s    z0ExclusionConstraint.validate.<locals>.<dictcomp>get_expression_for_validation)lhsrhs)pk)code)using)_default_managerr{   _get_field_expression_maprT   itemsr.   r"   r6   r
   _expression_refs_excludereplace_expressionshasattrrv   r   postgres_operatorr9   filter_get_pk_val_stateadding
_is_pk_setrr   r   existsr   get_violation_error_messager   r   check)r1   rE   instancerr   r{   querysetreplacement_mapreplacementslookupsr>   r4   rhs_expressionlookupmodel_class_pkr   r   r   validate   sH   






zExclusionConstraint.validate)r   r   r   r   r0   r?   rH   rR   ra   rd   rg   rh   rl   rp   r   r   __classcell__r   r   r2   r   r      s(    
,
N)typesr   django.core.exceptionsr   	django.dbr   !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   r   <module>   s    