o
    [h~#                     @   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   /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/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>   gistZ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.Z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   r.      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   Zset_wrapper_classes
connectionappendr   Zresolve_expression)r/   schema_editorqueryr,   idx
expressionr2   r   r   r   _get_expressionsE   s   
z$ExclusionConstraint._get_expressionsc                 C   sD   t  }| jD ]\}}t|trt|}||| q| ||S N)setr,   r!   r4   r
   updateZ_get_expr_referencesZ_check_references)r/   modelr5   Z
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r7   r   r   r'   \   s    z9ExclusionConstraint._get_condition_sql.<locals>.<genexpr>)r   Zbuild_whereZas_sqlr5   r#   )r/   compilerr7   r8   wheresqlparamsr   rD   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)Z
alias_cols)r5   c                    s   g | ]	} j |jqS r   )_meta	get_fieldcolumn)r%   
field_namer?   r   r   
<listcomp>d   s    z6ExclusionConstraint.constraint_sql.<locals>.<listcomp>z WHERE (%s) )tabler(   r   r,   rF   r   r   )r   Zget_compilerr5   r;   rJ   db_tablerI   r   r   r   r   
quote_namer(   r   r   rB   Z_index_include_sqlZ_deferrable_constraint_sqlr   )	r/   r?   r7   r8   rE   r,   rQ   r   r   r   rN   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)rQ   
constraint)r   r   rJ   rR   rS   rT   r/   r?   r7   r   r   r   
create_sqlt   s
   
zExclusionConstraint.create_sqlc                 C   s   | |j||| jS r<   )Z_delete_constraint_sqlZsql_delete_checkrS   r(   rV   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   )r/   pathargskwargsr0   r   r   rY      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!   r1   r(   r   r,   r   r   r   r   r   r-   __eq__)r/   otherr0   r   r   r]      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>rP   z condition=%sz deferrable=%rz include=%sz violation_error_code=%rz violation_error_message=%r)r1   r   reprr   r,   r(   r   r   r   r   r   Zdefault_violation_error_message)r/   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)Z_default_managerrk   Z_get_field_expression_maprJ   itemsr,   r!   r4   r
   Z_expression_refs_excludeZreplace_expressionshasattrrf   r   Zpostgres_operatorr6   filterZ_get_pk_val_stateZaddingZ
_is_pk_setrb   r   existsr   Zget_violation_error_messager   r   check)r/   r?   instancerb   rk   ZquerysetZreplacement_mapZreplacementsZlookupsr:   r2   Zrhs_expressionlookupZmodel_class_pkr   r   r   validate   sH   






zExclusionConstraint.validate)r   r   r   r   r.   r;   rA   rI   rT   rW   rX   rY   r]   r`   r   rt   __classcell__r   r   r0   r   r      s(    
,
N)typesr   Zdjango.core.exceptionsr   Z	django.dbr   Z!django.db.backends.ddl_referencesr   r   r   Zdjango.db.modelsr   r	   r
   r   Zdjango.db.models.expressionsr   r   Zdjango.db.models.indexesr   Zdjango.db.models.lookupsr   Zdjango.db.models.sqlr   __all__r   r   r   r   r   r   <module>   s    