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   úc/var/www/html/premium_crap/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ˆ| ¡ r†t|  ¡ | jd�‚d S | jt| | j¡ƒ@ j||d�r¡t|  ¡ | 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    