3
W­E^Ë  ã               @   sR   d dl mZmZ d dl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)ÚFÚQ)ÚBaseConstraint)ÚQueryÚExclusionConstraintc                   sl   e Zd ZdZ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
dd„ Zdd„ Z‡  ZS )r   zKCONSTRAINT %(name)s EXCLUDE USING %(index_type)s (%(expressions)s)%(where)sN)Ú
index_typeÚ	conditionc               s€   |r|j ƒ dkrtdƒ‚|s$tdƒ‚tdd„ |D ƒƒs>tdƒ‚t|td ƒtfƒsXtdƒ‚|| _|pdd	| _|| _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   úE/tmp/pip-build-78s3knkw/Django/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.ZGIST)Úname>   r   r   )ÚlowerÚ
ValueErrorÚallr   Útyper   Úexpressionsr	   r
   ÚsuperÚ__init__)Úselfr   r   r	   r
   )Ú	__class__r   r   r      s"    

zExclusionConstraint.__init__c       	      C   sz   g }xp| j D ]f\}}t|tƒr&t|ƒ}t|tƒr@|j|dd�}n|j|d�}|j||ƒ\}}|jd|| |f ƒ qW |S )NT)ÚqueryZ
simple_col)r!   z
%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_sql$   s    

z'ExclusionConstraint._get_expression_sqlc                sF   | j d krd S |j| j ƒ}|j|ˆ jƒ\}}|t‡ fdd„|D ƒƒ S )Nc             3   s   | ]}ˆ j |ƒV  qd S )N)Zquote_value)r   Úp)Úschema_editorr   r   r   6   s    z9ExclusionConstraint._get_condition_sql.<locals>.<genexpr>)r
   Zbuild_wherer#   r&   r   )r   r%   r,   r!   Úwherer(   r)   r   )r,   r   Ú_get_condition_sql1   s
    
z&ExclusionConstraint._get_condition_sqlc             C   sd   t |ƒ}|j|jd�}| j||j|ƒ}| j|||ƒ}| j|j| jƒ| jdj	|ƒ|rZd| nddœ S )N)r&   z, z WHERE (%s)Ú )r   r	   r   r-   )
r   Zget_compilerr&   r*   r.   ÚtemplateÚ
quote_namer   r	   Újoin)r   Úmodelr,   r!   r%   r   r
   r   r   r   Úconstraint_sql8   s    
z"ExclusionConstraint.constraint_sqlc             C   s"   t dt|jj|jƒ| j||ƒd�S )Nz(ALTER TABLE %(table)s ADD %(constraint)s)ÚtableÚ
constraint)r   r   Z_metaZdb_tabler1   r4   )r   r3   r,   r   r   r   Ú
create_sqlD   s    zExclusionConstraint.create_sqlc             C   s   |j |j||j| jƒƒS )N)Z_delete_constraint_sqlZsql_delete_checkr1   r   )r   r3   r,   r   r   r   Ú
remove_sqlK   s    zExclusionConstraint.remove_sqlc                sP   t ƒ jƒ \}}}| j|d< | jd k	r.| j|d< | jjƒ dkrF| j|d< |||fS )Nr   r
   r   r	   )r   Údeconstructr   r
   r	   r   )r   ÚpathÚargsÚkwargs)r    r   r   r9   R   s    



zExclusionConstraint.deconstructc             C   s<   t || jƒo:| j|jko:| j|jko:| j|jko:| j|jkS )N)r   r    r   r	   r   r
   )r   Úotherr   r   r   Ú__eq__[   s
    zExclusionConstraint.__eq__c             C   s,   d| j j| j| j| jd krdnd| j f S )Nz%<%s: index_type=%s, expressions=%s%s>r/   z, condition=%s)r    Ú__qualname__r	   r   r
   )r   r   r   r   Ú__repr__d   s
    zExclusionConstraint.__repr__)Ú__name__Ú
__module__r?   r0   r   r*   r.   r4   r7   r8   r9   r>   r@   Ú__classcell__r   r   )r    r   r   	   s   		N)Z!django.db.backends.ddl_referencesr   r   Zdjango.db.modelsr   r   Zdjango.db.models.constraintsr   Zdjango.db.models.sqlr   Ú__all__r   r   r   r   r   Ú<module>   s
   