U
    8ÄT_n  ã                   @   sV   d dl mZmZ d dlmZmZmZ d dlmZ d dl	m
Z
 dgZG dd„ deƒZdS )é    )Ú	StatementÚTable)Ú
DeferrableÚFÚQ)ÚBaseConstraint)ÚQueryÚExclusionConstraintc                       sr   e Zd ZdZddddœ‡ f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‡  ZS )r	   zYCONSTRAINT %(name)s EXCLUDE USING %(index_type)s (%(expressions)s)%(where)s%(deferrable)sN)Ú
index_typeÚ	conditionÚ
deferrablec                   s°   |r|  ¡ dk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	ƒ‚|| _|pŽ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Úexpr© r   úG/tmp/pip-unpacked-wheel-dv63caxw/django/contrib/postgres/constraints.pyÚ	<genexpr>   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.ZGIST)Úname)ÚlowerÚ
ValueErrorÚallr   Útyper   r   Úexpressionsr
   r   r   ÚsuperÚ__init__)Úselfr   r   r
   r   r   ©Ú	__class__r   r   r       s:    ÿÿþÿÿÿ
zExclusionConstraint.__init__c           	      C   s\   g }| j D ]L\}}t|tƒr$t|ƒ}|j|d�}| ||¡\}}| d|| |f ¡ q
|S )N)Úqueryz
%s WITH %s)r   r   Ústrr   Zresolve_expressionÚas_sqlÚappend)	r!   ÚcompilerÚ
connectionr$   r   Z
expressionÚoperatorÚsqlÚparamsr   r   r   Ú_get_expression_sql0   s    
z'ExclusionConstraint._get_expression_sqlc                    sF   | j d krd S | | j ¡}| |ˆ j¡\}}|t‡ fdd„|D ƒƒ S )Nc                 3   s   | ]}ˆ   |¡V  qd S ©N)Zquote_value)r   Úp©Úschema_editorr   r   r   ?   s     z9ExclusionConstraint._get_condition_sql.<locals>.<genexpr>)r   Zbuild_wherer&   r)   r   )r!   r(   r1   r$   Úwherer+   r,   r   r0   r   Ú_get_condition_sql:   s
    
z&ExclusionConstraint._get_condition_sqlc                 C   sr   t |dd�}|j|jd�}|  ||j|¡}|  |||¡}| j| | j¡| jd 	|¡|r^d| nd| 
| j¡dœ S )NF)Z
alias_cols)r)   z, z WHERE (%s)Ú )r   r
   r   r2   r   )r   Zget_compilerr)   r-   r3   ÚtemplateÚ
quote_namer   r
   ÚjoinZ_deferrable_constraint_sqlr   )r!   Úmodelr1   r$   r(   r   r   r   r   r   Úconstraint_sqlA   s    

ûz"ExclusionConstraint.constraint_sqlc                 C   s"   t dt|jj|jƒ|  ||¡d�S )Nz(ALTER TABLE %(table)s ADD %(constraint)s)ÚtableÚ
constraint)r   r   Z_metaZdb_tabler6   r9   ©r!   r8   r1   r   r   r   Ú
create_sqlN   s
    
ýzExclusionConstraint.create_sqlc                 C   s   |  |j|| | j¡¡S r.   )Z_delete_constraint_sqlZsql_delete_checkr6   r   r<   r   r   r   Ú
remove_sqlU   s
    
ýzExclusionConstraint.remove_sqlc                    s`   t ƒ  ¡ \}}}| j|d< | jd k	r.| j|d< | j ¡ dkrF| j|d< | jrV| j|d< |||fS )Nr   r   r   r
   r   )r   Údeconstructr   r   r
   r   r   )r!   ÚpathÚargsÚkwargsr"   r   r   r?   \   s    




zExclusionConstraint.deconstructc                    sT   t || jƒrH| j|jkoF| j|jkoF| j|jkoF| j|jkoF| j|jkS tƒ  |¡S r.   )	r   r#   r   r
   r   r   r   r   Ú__eq__)r!   Úotherr"   r   r   rC   g   s    
ÿ
þ
ý
ûzExclusionConstraint.__eq__c                 C   sB   d| j j| j| j| jd krdnd| j | jd kr4dnd| j f S )Nz'<%s: index_type=%s, expressions=%s%s%s>r4   z, condition=%sz, deferrable=%s)r#   Ú__qualname__r
   r   r   r   )r!   r   r   r   Ú__repr__r   s    ûzExclusionConstraint.__repr__)Ú__name__Ú
__module__rE   r5   r    r-   r3   r9   r=   r>   r?   rC   rF   Ú__classcell__r   r   r"   r   r	   	   s    þ$
N)Z!django.db.backends.ddl_referencesr   r   Zdjango.db.modelsr   r   r   Zdjango.db.models.constraintsr   Zdjango.db.models.sqlr   Ú__all__r	   r   r   r   r   Ú<module>   s
   