U
    O+|j'  ã                   @   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   úkc:\users\snmko\desktop\web_content\django_cbt\venv\Lib\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krtdƒ‚|s$tdƒ‚tdd„ |D ƒƒs>tdƒ‚t|td ƒtfƒsXtdƒ‚t|td ƒtfƒsrtdƒ‚t|td ƒttfƒsŽtd	ƒ‚t|ttfƒs¤td
ƒ‚|rÀt	|ƒt	|ƒkrÀtdƒ‚|| _
|pÌd| _|| _|| _|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.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.Z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-   r2   r   r   r   r   r   r    ©Ú	__class__r   r   r6      sN    ÿÿÿÿÿ
ûzExclusionConstraint.__init__c              	   C   sˆ   g }t | jƒD ]j\}\}}t|tƒr,t|ƒ}zt|| j| ƒ}W n tk
rT   Y nX t||d�}| 	|j
¡ | |¡ qt|Ž  |¡S )N)Úoperator)Ú	enumerater2   r$   Ústrr   r   r   Ú
IndexErrorr   Zset_wrapper_classesÚ
connectionÚappendr   Zresolve_expression)r7   Úschema_editorÚqueryr2   ÚidxÚ
expressionr:   r   r   r   Ú_get_expressionsT   s    
z$ExclusionConstraint._get_expressionsc                    sF   | j d k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*   g   s     z9ExclusionConstraint._get_condition_sql.<locals>.<genexpr>)r   Zbuild_whereZas_sqlr>   r&   )r7   Úcompilerr@   rA   ÚwhereZsqlÚparamsr   rH   r   Ú_get_condition_sqlb   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ƒ|rˆd| nd| ˆ |¡| | j¡d�S )	NF)Z
alias_cols)r>   c                    s   g | ]}ˆ j  |¡j‘qS r   )Ú_metaÚ	get_fieldÚcolumn)r(   Ú
field_name©Úmodelr   r   Ú
<listcomp>o   s    z6ExclusionConstraint.constraint_sql.<locals>.<listcomp>z WHERE (%s)Ú )Útabler-   r   r2   rJ   r   r   )r   Zget_compilerr>   rD   rM   Údb_tablerL   r   r   r   r   Ú
quote_namer-   r   r   rF   Z_index_include_sqlZ_deferrable_constraint_sqlr   )	r7   rR   r@   rA   rI   r2   rU   r   r   r   rQ   r   Úconstraint_sqli   s.    
ÿ

   ÿ

öz"ExclusionConstraint.constraint_sqlc                 C   s,   |   |¡ tdt|jj|jƒ|  ||¡d�S )Nz(ALTER TABLE %(table)s ADD %(constraint)s)rU   Ú
constraint)Úcheck_supportedr   r   rM   rV   rW   rX   ©r7   rR   r@   r   r   r   Ú
create_sql   s    

ýzExclusionConstraint.create_sqlc                 C   s   |  |j|| | j¡¡S rE   )Z_delete_constraint_sqlZsql_delete_checkrW   r-   r[   r   r   r   Ú
remove_sql‡   s
    
ýzExclusionConstraint.remove_sqlc                 C   s*   | j r&| j ¡ dkr&|jjjs&tdƒ‚d S )Nr"   zMCovering exclusion constraints using an SP-GiST index require PostgreSQL 14+.)r   r   r.   r>   ÚfeaturesZ supports_covering_spgist_indexesr   )r7   r@   r   r   r   rZ   Ž   s    ÿþýÿz#ExclusionConstraint.check_supportedc                    s€   t ƒ  ¡ \}}}| j|d< | jd k	r.| j|d< | j ¡ dkrF| j|d< | jrV| j|d< | jrf| j|d< | jrv| j|d< |||fS )Nr2   r   r!   r   r   r   r   )	r5   Údeconstructr2   r   r   r.   r   r   r   )r7   ÚpathÚargsÚkwargsr8   r   r   r_   ™   s    






zExclusionConstraint.deconstructc                    sx   t || jƒrl| j|jkoj| j|jkoj| j|jkoj| j|jkoj| j|jkoj| j|jkoj| j|jkoj| j	|j	kS t
ƒ  |¡S rE   )r$   r9   r-   r   r2   r   r   r   r   r    r5   Ú__eq__)r7   Úotherr8   r   r   rc   ¨   s"    
ÿ
þ
ý
ü
û
ú
ø
zExclusionConstraint.__eq__c                 C   s~   d| j jt| jƒt| jƒt| jƒ| jd kr.dnd| j | jd krDdnd| j | jsVdndt| jƒ | j	sldndt| j	ƒ f S )Nz2<%s: index_type=%s expressions=%s name=%s%s%s%s%s>rT   z condition=%sz deferrable=%rz include=%sz opclasses=%s)
r9   r   Úreprr   r2   r-   r   r   r   r   )r7   r   r   r   Ú__repr__¶   s    øzExclusionConstraint.__repr__c                 C   s€  |j  |¡}|j|j|d�}dd„ | ¡ D ƒ}g }t| jƒD ]º\}	\}
}t|
tƒrZt	|
ƒ}
|r¤t|
t	ƒrz|
j
|kr¤ d S n*|
 ¡ D ] }t|t	ƒr‚|j
|kr‚  d S q‚|
 |¡}t|
tƒrÄ|
 ¡ d }
t|tƒrÚ| ¡ d }t|
|d�}||_| |¡ q<|j|Ž }| |j¡}|jj�s.|d k	�r.|j|d�}| j�sN| ¡ �r|t|  ¡ ƒ‚n.| jt| | j¡ƒ@ j||d��r|t|  ¡ ƒ‚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)Úusing)Z_default_managerro   Z_get_field_value_maprM   Úitemsr;   r2   r$   r<   r   r-   ÚflattenZreplace_expressionsr   Zget_source_expressionsr   Zpostgres_operatorr?   ÚfilterZ_get_pk_valÚ_stateZaddingrh   r   Úexistsr   Zget_violation_error_messager   Úcheck)r7   rR   Úinstancerh   ro   ZquerysetZreplacement_mapÚreplacementsÚlookupsrB   rC   r:   r)   Zrhs_expressionÚlookupZmodel_class_pkr   r   r   ÚvalidateÂ   sJ     ÿ








 ÿzExclusionConstraint.validate)r   r   r   r   r6   rD   rL   rX   r\   r]   rZ   r_   rc   rf   r   rz   Ú__classcell__r   r   r8   r   r      s&   ÿ
ö9)r3   Zdjango.contrib.postgres.indexesr   Zdjango.core.exceptionsr   Z	django.dbr   r   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   Zdjango.utils.deprecationr   Ú__all__r   r   r   r   r   r   Ú<module>   s   