
vÅÏ^Ë  ã               @   so   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
 Gd d „  d e ƒ Z d S)é    )Ú	StatementÚTable)ÚFÚQ)ÚBaseConstraint)ÚQueryÚExclusionConstraintc                   sš   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
 d d „  Z d d „  Z ‡  S)r   zKCONSTRAINT %(name)s EXCLUDE USING %(index_type)s (%(expressions)s)%(where)sÚ
index_typeNÚ	conditionc               sº   | r$ | j  ƒ  d k r$ t d ƒ ‚ | s6 t d ƒ ‚ t d d „  | Dƒ ƒ s[ t 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   s6   |  ], } t  | t t f ƒ o- t | ƒ d  k Vq d S)é   N)Ú
isinstanceÚlistÚtupleÚlen)Ú.0Úexpr© r   úE/tmp/pip-build-8lau8j11/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   s®   g  } x¡ |  j  D]– \ } } t | t ƒ r7 t | ƒ } t | t ƒ ra | j d | d d ƒ } n | j d | ƒ } | j | | ƒ \ } } | j d | | | f ƒ q W| S)NÚqueryZ
simple_colTz
%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                sa   |  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   s™   t  | ƒ } | j d | j ƒ } |  j | | j | ƒ } |  j | | | ƒ } |  j d | j |  j ƒ d |  j d d j	 | ƒ d | r‘ d | n d i S)	Nr&   r   r	   r   z, r-   z WHERE (%s)Ú )
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   s4   t  d d t | j j | j ƒ d |  j | | ƒ ƒ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	 rA |  j | d <|  j j ƒ  d k rc |  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   sX   t  | |  j ƒ oW |  j | j k oW |  j | j k oW |  j | j k oW |  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@   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
   	