U
    5øif  ã                   @   sn   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
 d dlmZ d dlmZ dgZG dd„ de
ƒZd	S )
é    )ÚNotSupportedError)Ú	StatementÚTable)Ú
DeferrableÚFÚQ)ÚBaseConstraint)ÚCol)ÚQueryÚExclusionConstraintc                       s~   e Zd ZdZ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‡  ZS )r   zdCONSTRAINT %(name)s EXCLUDE USING %(index_type)s (%(expressions)s)%(include)s%(where)s%(deferrable)sN© )Ú
index_typeÚ	conditionÚ
deferrableÚincludeÚ	opclassesc                   s4  |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	ƒ‚t|td ƒttfƒsžtd
ƒ‚|rº|rº|  ¡ dkrºtdƒ‚t|ttfƒsÐtdƒ‚|rìt	|ƒt	|ƒkrìtdƒ‚|| _
|pød| _|| _|| _|�rt|ƒn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   úG/tmp/pip-unpacked-wheel-gcyiijx6/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.z4ExclusionConstraint.include must be a list or tuple.r   z9Covering exclusion constraints only support GiST indexes.z6ExclusionConstraint.opclasses must be a list or tuple.zhExclusionConstraint.expressions and ExclusionConstraint.opclasses must have the same number of elements.ZGISTr   )Úname)ÚlowerÚ
ValueErrorÚallr   Útyper   r   r   r   r   Úexpressionsr   r   r   r   r   ÚsuperÚ__init__)Úselfr   r"   r   r   r   r   r   ©Ú	__class__r   r   r$      s^    ÿÿþÿÿÿÿÿÿÿ
zExclusionConstraint.__init__c              	      sÂ   g }t | jƒD ]®\}\}}t|tƒr,t|ƒ}|j|d�}| |¡\}}	t|tƒs\d|› d�}z| j| }
|
rxd||
f }W n t	k
rŽ   Y nX |t
‡ fdd„|	D ƒƒ }| d||f ¡ q|S )N)Úqueryú(ú)z%s %sc                 3   s   | ]}ˆ   |¡V  qd S ©N©Zquote_value©r   Úp©Úschema_editorr   r   r   U   s     z:ExclusionConstraint._get_expression_sql.<locals>.<genexpr>z
%s WITH %s)Ú	enumerater"   r   Ústrr   Zresolve_expressionÚcompiler	   r   Ú
IndexErrorr   Úappend)r%   Úcompilerr0   r(   r"   ÚidxZ
expressionÚoperatorÚsqlÚparamsZopclassr   r/   r   Ú_get_expression_sqlF   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 r+   r,   r-   r/   r   r   r   ^   s     z9ExclusionConstraint._get_condition_sql.<locals>.<genexpr>)r   Zbuild_whereZas_sqlÚ
connectionr   )r%   r6   r0   r(   Úwherer9   r:   r   r/   r   Ú_get_condition_sqlY   s
    
z&ExclusionConstraint._get_condition_sqlc              	      sŽ   t ˆ dd�}|j|jd�}|  |||¡}|  |||¡}‡ fdd„| jD ƒ}| j| | j¡| j	d 
|¡| ˆ |¡|rzd| 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>e   s     z6ExclusionConstraint.constraint_sql.<locals>.<listcomp>z, z WHERE (%s)Ú )r   r   r"   r   r=   r   )r
   Zget_compilerr<   r;   r>   r   ÚtemplateÚ
quote_namer   r   ÚjoinZ_index_include_sqlZ_deferrable_constraint_sqlr   )r%   rD   r0   r(   r6   r"   r   r   r   rC   r   Úconstraint_sql`   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)Úcheck_supportedr   r   r?   Zdb_tablerH   rJ   ©r%   rD   r0   r   r   r   Ú
create_sqlo   s    

ýzExclusionConstraint.create_sqlc                 C   s   |  |j|| | j¡¡S r+   )Z_delete_constraint_sqlZsql_delete_checkrH   r   rN   r   r   r   Ú
remove_sqlw   s
    
ýzExclusionConstraint.remove_sqlc                 C   s   | j r|jjjstdƒ‚d S )Nz7Covering exclusion constraints requires PostgreSQL 12+.)r   r<   ÚfeaturesZsupports_covering_gist_indexesr   )r%   r0   r   r   r   rM   ~   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 )Nr"   r   r   r   r   r   r   )	r#   Údeconstructr"   r   r   r   r   r   r   )r%   ÚpathÚargsÚkwargsr&   r   r   rR   „   s    






zExclusionConstraint.deconstructc                    sl   t || jƒr`| j|jko^| j|jko^| j|jko^| j|jko^| j|jko^| j|jko^| j|jkS t	ƒ  
|¡S r+   )r   r'   r   r   r"   r   r   r   r   r#   Ú__eq__)r%   Úotherr&   r   r   rV   “   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>rF   z condition=%sz deferrable=%rz include=%sz opclasses=%s)
r'   Ú__qualname__Úreprr   r"   r   r   r   r   r   )r%   r   r   r   Ú__repr__    s    øzExclusionConstraint.__repr__)Ú__name__Ú
__module__rX   rG   r$   r;   r>   rJ   rO   rP   rM   rR   rV   rZ   Ú__classcell__r   r   r&   r   r      s       þ8N)Z	django.dbr   Z!django.db.backends.ddl_referencesr   r   Zdjango.db.modelsr   r   r   Zdjango.db.models.constraintsr   Zdjango.db.models.expressionsr	   Zdjango.db.models.sqlr
   Ú__all__r   r   r   r   r   Ú<module>   s   