o
    (S-g=&  ã                   @   s²   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)Ú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   úb/var/www/html/map_library/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
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   r1      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)r2   Úschema_editorÚqueryr/   ÚidxÚ
expressionr5   r   r   r   Ú_get_expressionsE   s   
z$ExclusionConstraint._get_expressionsc                 C   sD   t ƒ }| jD ]\}}t|tƒrt|ƒ}| | |¡¡ q|  ||¡S ©N)Úsetr/   r#   r7   r   ÚupdateÚ_get_expr_referencesÚ_check_references)r2   Úmodelr9   Ú
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 rA   )Úquote_value)r'   Úp©r<   r   r   r)   \   s   € z9ExclusionConstraint._get_condition_sql.<locals>.<genexpr>)r   Úbuild_whereÚas_sqlr9   r%   )r2   Úcompilerr<   r=   ÚwhereÚsqlÚparamsr   rL   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)r9   c                    s   g | ]	}ˆ j  |¡j‘qS r   )Ú_metaÚ	get_fieldÚcolumn)r'   Ú
field_name©rF   r   r   Ú
<listcomp>d   s    ÿz6ExclusionConstraint.constraint_sql.<locals>.<listcomp>z WHERE (%s)Ú )Útabler+   r   r/   rP   r   r   )r   Úget_compilerr9   r@   rU   Údb_tablerS   r   r   r   r   Ú
quote_namer+   r   r   rJ   Ú_index_include_sqlÚ_deferrable_constraint_sqlr   )	r2   rF   r<   r=   rO   r/   r\   r   r   r   rY   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)Úcheck_supportedr   r   rU   r^   r_   rb   ©r2   rF   r<   r   r   r   Ú
create_sqlt   s   

ýzExclusionConstraint.create_sqlc                 C   s   |  |j|| | j¡¡S rA   )Ú_delete_constraint_sqlÚsql_delete_checkr_   r+   re   r   r   r   Ú
remove_sql|   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,   r9   ÚfeaturesÚ supports_covering_spgist_indexesr   )r2   r<   r   r   r   rd   ƒ   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 )Nr/   r   r    r   r   r   )r0   Údeconstructr/   r   r   r,   r   r   )r2   ÚpathÚargsÚkwargsr3   r   r   rl   Ž   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 rA   )r#   r4   r+   r   r/   r   r   r   r   r   r0   Ú__eq__)r2   Úotherr3   r   r   rp   ›   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>r[   z condition=%sz deferrable=%rz include=%sz violation_error_code=%rz violation_error_message=%r)r4   r   Úreprr   r/   r+   r   r   r   r   r   Údefault_violation_error_message)r2   r   r   r   Ú__repr__©   s"   
ÿ
þòïzExclusionConstraint.__repr__c                 C   s|  |j  |¡}|j|j|d�}dd„ | ¡ D ƒ}g }t| jƒD ]Y\}	\}
}t|
tƒr-t	|
ƒ}
|rRt|
t	ƒr=|
j
|v r< d S n|
 ¡ D ]}t|t	ƒrQ|j
|v rQ  d S qA|
 |¡}t|
dƒr`|
 ¡ }
t|dƒri| ¡ }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>Úget_expression_for_validation)ÚlhsÚrhs)Úpk)Úcode)Úusing)Ú_default_managerr   Ú_get_field_expression_maprU   Úitemsr6   r/   r#   r7   r   r+   ÚflattenÚreplace_expressionsÚhasattrrz   r   Úpostgres_operatorr:   ÚfilterÚ_get_pk_valÚ_stateÚaddingrv   r   Úexistsr   Úget_violation_error_messager   r   Úcheck)r2   rF   Úinstancerv   r   ÚquerysetÚreplacement_mapÚreplacementsÚlookupsr>   r?   r5   r(   Úrhs_expressionÚlookupÚmodel_class_pkr   r   r   Úvalidate¿   sV   ÿ


ÿ€




ÿÿÿ
ÿýzExclusionConstraint.validate)r   r   r   r   r1   r@   rI   rS   rb   rf   ri   rd   rl   rp   rt   r   r–   Ú__classcell__r   r   r3   r   r      s*    ÿ
ö,
N)Útypesr   Ú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    