o
    ˜¨Êhï(  ã                   @   sÆ   d dl 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 dlmZ dgZG dd„ deƒZG dd„ deƒZdS )é    N)ÚOpClass)ÚValidationError)ÚDEFAULT_DB_ALIASÚNotSupportedError)ÚExpressionsÚ	StatementÚTable)ÚBaseConstraintÚ
DeferrableÚFÚQ)ÚExistsÚExpressionList)ÚIndexExpression)ÚPostgresOperatorLookup)ÚQuery)ÚRemovedInDjango50WarningÚExclusionConstraintc                   @   s   e Zd ZdZdS )ÚExclusionConstraintExpressionz!%(expressions)s WITH %(operator)sN)Ú__name__Ú
__module__Ú__qualname__Útemplate© r   r   úU/var/www/html/env/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)sNr   )Ú
index_typeÚ	conditionÚ
deferrableÚincludeÚ	opclassesÚviolation_error_messagec          	         s.  |r|  ¡ dvrtdƒ‚|stdƒ‚tdd„ |D ƒƒstdƒ‚t|td ƒtfƒs,tdƒ‚|r4|r4tdƒ‚t|td ƒtfƒsAtd	ƒ‚t|td ƒttfƒsOtd
ƒ‚t|ttfƒsZtdƒ‚|rht	|ƒt	|ƒkrhtdƒ‚|| _
|pnd| _|| _|| _|r|t|ƒnd| _|| _| jr�tjdtd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>0   s   € 
ÿz/ExclusionConstraint.__init__.<locals>.<genexpr>z+The expressions must be a list of 2-tuples.z3ExclusionConstraint.condition must be a Q instance.z7ExclusionConstraint with conditions cannot be deferred.z=ExclusionConstraint.deferrable must be a Deferrable instance.z4ExclusionConstraint.include must be a list or tuple.z6ExclusionConstraint.opclasses must be a list or tuple.zhExclusionConstraint.expressions and ExclusionConstraint.opclasses must have the same number of elements.ÚGISTr   z‚The opclasses argument is deprecated in favor of using django.contrib.postgres.indexes.OpClass in ExclusionConstraint.expressions.r#   )ÚcategoryÚ
stacklevel)Únamer    )ÚlowerÚ
ValueErrorÚallr$   Útyper   r
   r%   r&   r'   Úexpressionsr   r   r   r   r   ÚwarningsÚwarnr   ÚsuperÚ__init__)	Úselfr.   r3   r   r   r   r   r   r    ©Ú	__class__r   r   r7      sR   ÿÿÿÿÿ
ûzExclusionConstraint.__init__c              	   C   s†   g }t | jƒD ]4\}\}}t|tƒrt|ƒ}z
t|| j| ƒ}W n	 ty)   Y nw t||d�}| 	|j
¡ | |¡ qt|Ž  |¡S )N)Úoperator)Ú	enumerater3   r$   Ústrr   r   r   Ú
IndexErrorr   Úset_wrapper_classesÚ
connectionÚappendr   Úresolve_expression)r8   Úschema_editorÚqueryr3   ÚidxÚ
expressionr;   r   r   r   Ú_get_expressionsV   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©rC   r   r   r*   i   s   € z9ExclusionConstraint._get_condition_sql.<locals>.<genexpr>)r   Úbuild_whereÚas_sqlr@   r&   )r8   ÚcompilerrC   rD   ÚwhereÚsqlÚparamsr   rK   r   Ú_get_condition_sqld   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>q   s    ÿz6ExclusionConstraint.constraint_sql.<locals>.<listcomp>z WHERE (%s)Ú )Útabler.   r   r3   rO   r   r   )r   Úget_compilerr@   rG   rT   Údb_tablerR   r   r   r   r   Ú
quote_namer.   r   r   rI   Ú_index_include_sqlÚ_deferrable_constraint_sqlr   )	r8   rY   rC   rD   rN   r3   r\   r   r   r   rX   r   Úconstraint_sqlk   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)Úcheck_supportedr   r   rT   r^   r_   rb   ©r8   rY   rC   r   r   r   Ú
create_sql�   s   

ýzExclusionConstraint.create_sqlc                 C   s   |  |j|| | j¡¡S rH   )Ú_delete_constraint_sqlÚsql_delete_checkr_   r.   re   r   r   r   Ú
remove_sql‰   s
   
ýzExclusionConstraint.remove_sqlc                 C   sX   | j r| j ¡ dkr|jjjstdƒ‚| j r&| j ¡ dkr(|jjjs*tdƒ‚d S d S d S )Nr!   zICovering exclusion constraints using a GiST index require PostgreSQL 12+.r"   zMCovering exclusion constraints using an SP-GiST index require PostgreSQL 14+.)r   r   r/   r@   ÚfeaturesÚsupports_covering_gist_indexesr   Ú supports_covering_spgist_indexes)r8   rC   r   r   r   rd   �   s$   ÿÿÿÿÿÿûz#ExclusionConstraint.check_supportedc                    s€   t ƒ  ¡ \}}}| j|d< | jd ur| j|d< | j ¡ dkr#| j|d< | jr+| j|d< | jr3| j|d< | jr;| j|d< |||fS )Nr3   r   r!   r   r   r   r   )	r6   Údeconstructr3   r   r   r/   r   r   r   )r8   ÚpathÚargsÚkwargsr9   r   r   rm   ¤   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 rH   )r$   r:   r.   r   r3   r   r   r   r   r    r6   Ú__eq__)r8   Úotherr9   r   r   rq   ³   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	s8df S dt| j	ƒ f S )Nz2<%s: index_type=%s expressions=%s name=%s%s%s%s%s>r[   z condition=%sz deferrable=%rz include=%sz opclasses=%s)
r:   r   Úreprr   r3   r.   r   r   r   r   )r8   r   r   r   Ú__repr__Á   s   øøzExclusionConstraint.__repr__c                 C   sx  |j  |¡}|j|j|d�}g }t| jƒD ]f\}\}	}
t|	tƒr$t|	ƒ}	t|	tƒr;|r3|	j	|v r3 d S | 
|	j	|	¡}n|	 |¡}|rW| ¡ D ]}t|tƒrV|j	|v rV  d S qFt|	tƒrb|	 ¡ d }	t|tƒrm| ¡ d }t|	|d�}|
|_| |¡ q|j|Ž }| |j¡}|jjs•|d ur•|j|d�}| js¤| ¡ r¢t|  ¡ ƒ‚d S | jt| | j¡ƒ@ j||d�rºt|  ¡ ƒ‚d S )N)ÚmetaÚexcluder   )ÚlhsÚrhs)Úpk)Úusing)Ú_default_managerrz   Ú_get_field_value_maprT   r<   r3   r$   r=   r   r.   ÚgetÚreplace_referencesÚflattenr   Úget_source_expressionsr   Úpostgres_operatorrA   ÚfilterÚ_get_pk_valÚ_stateÚaddingrv   r   Úexistsr   Úget_violation_error_messager   Úcheck)r8   rY   Úinstancerv   rz   ÚquerysetÚreplacement_mapÚlookupsrE   rF   r;   Úrhs_expressionr)   ÚlookupÚmodel_class_pkr   r   r   ÚvalidateÍ   sL   ÿ


€


ÿÿýzExclusionConstraint.validate)r   r   r   r   r7   rG   rR   rb   rf   ri   rd   rm   rq   rt   r   r�   Ú__classcell__r   r   r9   r   r      s(    ÿ
ö;)r4   Ú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   Údjango.utils.deprecationr   Ú__all__r   r   r   r   r   r   Ú<module>   s    