o
    ÕèFhs%  ã                   @   s¾   d dl mZ 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„ deƒZG dd„ deƒZdS )é    )ÚNoneType)ÚOpClass)ÚValidationError)ÚDEFAULT_DB_ALIASÚNotSupportedError)Ú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   ú\/var/www/html/loop/nvenv/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,   r0   r   r   r   r   r   r    ©Ú	__class__r   r   r2      s<   ÿÿÿÿ

ýzExclusionConstraint.__init__c                 C   s^   g }t | jƒD ] \}\}}t|tƒrt|ƒ}t||d�}| |j¡ | |¡ qt	|Ž  
|¡S )N)Úoperator)Ú	enumerater0   r$   Ústrr   r   Úset_wrapper_classesÚ
connectionÚappendr   Úresolve_expression)r3   Úschema_editorÚqueryr0   ÚidxÚ
expressionr6   r   r   r   Ú_get_expressionsF   s   
z$ExclusionConstraint._get_expressionsc                    sF   | j d u rd S | | j ¡}| |ˆ j¡\}}|t‡ fdd„|D ƒƒ S )Nc                 3   s   � | ]}ˆ   |¡V  qd S ©N)Úquote_value)r(   Úp©r=   r   r   r*   U   s   € z9ExclusionConstraint._get_condition_sql.<locals>.<genexpr>)r   Úbuild_whereÚas_sqlr:   r&   )r3   Úcompilerr=   r>   ÚwhereÚsqlÚparamsr   rE   r   Ú_get_condition_sqlP   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)r:   c                    s   g | ]	}ˆ j  |¡j‘qS r   )Ú_metaÚ	get_fieldÚcolumn)r(   Ú
field_name©Úmodelr   r   Ú
<listcomp>]   s    ÿz6ExclusionConstraint.constraint_sql.<locals>.<listcomp>z WHERE (%s)Ú )Útabler,   r   r0   rI   r   r   )r   Úget_compilerr:   rA   rN   Údb_tablerL   r   r   r   r	   Ú
quote_namer,   r   r   rC   Ú_index_include_sqlÚ_deferrable_constraint_sqlr   )	r3   rS   r=   r>   rH   r0   rV   r   r   r   rR   r   Úconstraint_sqlW   s(   
ÿ


ÿ

öz"ExclusionConstraint.constraint_sqlc                 C   s,   |   |¡ tdt|jj|jƒ|  ||¡d�S )Nz(ALTER TABLE %(table)s ADD %(constraint)s)rV   Ú
constraint)Úcheck_supportedr   r	   rN   rX   rY   r\   ©r3   rS   r=   r   r   r   Ú
create_sqlm   s   

ýzExclusionConstraint.create_sqlc                 C   s   |  |j|| | j¡¡S rB   )Ú_delete_constraint_sqlÚsql_delete_checkrY   r,   r_   r   r   r   Ú
remove_sqlu   s
   
ýzExclusionConstraint.remove_sqlc                 C   s2   | j r| j ¡ dkr|jjjstdƒ‚d S d S d S )Nr"   zMCovering exclusion constraints using an SP-GiST index require PostgreSQL 14+.)r   r   r-   r:   ÚfeaturesÚ supports_covering_spgist_indexesr   )r3   r=   r   r   r   r^   |   s   ÿÿÿûz#ExclusionConstraint.check_supportedc                    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 )Nr0   r   r!   r   r   r   )r1   Údeconstructr0   r   r   r-   r   r   )r3   ÚpathÚargsÚkwargsr4   r   r   rf   ‡   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 rB   )r$   r5   r,   r   r0   r   r   r   r   r    r1   Ú__eq__)r3   Úotherr4   r   r   rj   ”   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>rU   z condition=%sz deferrable=%rz include=%sz violation_error_code=%rz violation_error_message=%r)r5   r   Úreprr   r0   r,   r   r   r   r   r    Údefault_violation_error_message)r3   r   r   r   Ú__repr__¢   s"   
ÿ
þòïzExclusionConstraint.__repr__c                 C   s„  |j  |¡}|j|j|d�}dd„ | ¡ D ƒ}g }t| jƒD ]]\}	\}
}t|
tƒr-t	|
ƒ}
|rRt|
t	ƒr=|
j
|v r< d S n|
 ¡ D ]}t|t	ƒrQ|j
|v rQ  d S qA|
 |¡}t|
tƒrb|
 ¡ d }
t|tƒrm| ¡ d }t|
|d�}||_| |¡ q|j|Ž }| |j¡}|jjs•|d ur•|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>r   )ÚlhsÚrhs)Úpk)Úcode)Úusing)Ú_default_managerrx   Ú_get_field_value_maprN   Úitemsr7   r0   r$   r8   r   r,   ÚflattenÚreplace_expressionsr   Úget_source_expressionsr   Úpostgres_operatorr;   ÚfilterÚ_get_pk_valÚ_stateÚaddingrp   r   Úexistsr   Úget_violation_error_messager   r   Úcheck)r3   rS   Úinstancerp   rx   ÚquerysetÚreplacement_mapÚreplacementsÚlookupsr?   r@   r6   r)   Úrhs_expressionÚlookupÚmodel_class_pkr   r   r   Úvalidate¸   sV   ÿ


ÿ€




ÿÿÿ
ÿýzExclusionConstraint.validate)r   r   r   r   r2   rA   rL   r\   r`   rc   r^   rf   rj   rn   r   r�   Ú__classcell__r   r   r4   r   r      s(    ÿ
ö,
N)Útypesr   Údjango.contrib.postgres.indexesr   Ú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   r   Ú<module>   s    