a
    ‘ÐOcï(  ã                   @   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   úZ/var/www/html/gsl.tools/lib/python3.9/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          	         s0  |r|  ¡ dvrtdƒ‚|s$tdƒ‚tdd„ |D ƒƒs>tdƒ‚t|td ƒtfƒsXtdƒ‚|rh|rhtdƒ‚t|td ƒtfƒs‚t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.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.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      sR    ÿÿÿÿÿ
ûzExclusionConstraint.__init__c              	   C   s†   g }t | jƒD ]h\}\}}t|tƒr,t|ƒ}zt|| j| ƒ}W n tyR   Y n0 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_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©r@   r   r   r*   i   ó    z9ExclusionConstraint._get_condition_sql.<locals>.<genexpr>)r   Zbuild_whereZas_sqlr>   r&   )r7   Úcompilerr@   rA   ÚwhereZsqlÚparamsr   rH   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ƒ|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>q   s   z6ExclusionConstraint.constraint_sql.<locals>.<listcomp>z WHERE (%s)Ú )Útabler-   r   r2   rK   r   r   )r   Zget_compilerr>   rD   rN   Údb_tablerM   r   r   r   r   Ú
quote_namer-   r   r   rF   Z_index_include_sqlZ_deferrable_constraint_sqlr   )	r7   rS   r@   rA   rJ   r2   rV   r   r   r   rR   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)rV   Ú
constraint)Úcheck_supportedr   r   rN   rW   rX   rY   ©r7   rS   r@   r   r   r   Ú
create_sql�   s    

ýzExclusionConstraint.create_sqlc                 C   s   |  |j|| | j¡¡S rE   )Z_delete_constraint_sqlZsql_delete_checkrX   r-   r\   r   r   r   Ú
remove_sql‰   s
    
ýzExclusionConstraint.remove_sqlc                 C   sP   | j r&| j ¡ dkr&|jjjs&tdƒ‚| j rL| j ¡ dkrL|jjjsLtdƒ‚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>   ÚfeaturesZsupports_covering_gist_indexesr   Z supports_covering_spgist_indexes)r7   r@   r   r   r   r[   �   s$    ÿþýÿÿþýÿz#ExclusionConstraint.check_supportedc                    s€   t ƒ  ¡ \}}}| j|d< | jd u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   rd   ³   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 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>rU   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�}g }t| jƒD ]Ì\}\}	}
t|	tƒrHt|	ƒ}	t|	tƒrv|rf|	j	|v rf d S | 
|	j	|	¡}n8|	 |¡}|r®| ¡ D ] }t|tƒrŒ|j	|v rŒ  d S qŒt|	tƒrÄ|	 ¡ d }	t|tƒrÚ| ¡ d }t|	|d�}|
|_| |¡ q*|j|Ž }| |j¡}|jj�s.|d u�r.|j|d�}| j�sN| ¡ �r|t|  ¡ ƒ‚n.| jt| | j¡ƒ@ j||d��r|t|  ¡ ƒ‚d S )N)ÚmetaÚexcluder   )ÚlhsÚrhs)Úpk)Úusing)Z_default_managerrm   Z_get_field_value_maprN   r;   r2   r$   r<   r   r-   ÚgetZreplace_referencesÚflattenr   Zget_source_expressionsr   Zpostgres_operatorr?   ÚfilterZ_get_pk_valÚ_stateZaddingri   r   Úexistsr   Zget_violation_error_messager   Úcheck)r7   rS   Úinstanceri   rm   ZquerysetZreplacement_mapÚlookupsrB   rC   r:   Zrhs_expressionr)   ÚlookupZmodel_class_pkr   r   r   ÚvalidateÍ   sF    ÿ







ÿzExclusionConstraint.validate)r   r   r   r   r6   rD   rM   rY   r]   r^   r[   r`   rd   rg   r   rw   Ú__classcell__r   r   r8   r   r      s&   ÿ
ö;)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   