3
g                 @   sb   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gZG dd de
ZdS )	    )NotSupportedError)	StatementTable)
DeferrableFQ)BaseConstraint)QueryExclusionConstraintc                   s~   e Zd ZdZddddf 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               s6  |r|j  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std
t|td ttfstd|r|r|j  dkrtdt|ttfstd|rt	|t	|krtd|| _
| pd| _|| _|| _|rt|nf | _|| _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   a/var/www/tester-filtro-web/env/lib/python3.6/site-packages/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.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GIST)name>   r   r   )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#      sL    
zExclusionConstraint.__init__c                s   g }xt | jD ]\}\}}t|tr.t|}|j|d}|j|\}}	y| j| }
|
rdd||
f }W n tk
rz   Y nX |t	 fdd|	D  }|j
d||f  qW |S )N)queryz%s %sc             3   s   | ]} j |V  qd S )N)quote_value)r   p)schema_editorr   r   r   R   s    z:ExclusionConstraint._get_expression_sql.<locals>.<genexpr>z
%s WITH %s)	enumerater!   r   strr   Zresolve_expressioncompiler   
IndexErrorr   append)r$   compilerr)   r&   r!   idxZ
expressionoperatorsqlparamsZopclassr   )r)   r   _get_expression_sqlE   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)r'   )r   r(   )r)   r   r   r   [   s    z9ExclusionConstraint._get_condition_sql.<locals>.<genexpr>)r   Zbuild_whereZas_sql
connectionr   )r$   r/   r)   r&   wherer2   r3   r   )r)   r   _get_condition_sqlV   s
    
z&ExclusionConstraint._get_condition_sqlc                s   t  dd}|j|jd}| j|||}| j|||} fdd| jD }| j|j| j| j	dj
||j ||rzd| nd|j| jd	 S )
NF)Z
alias_cols)r5   c                s   g | ]} j j|jqS r   )_meta	get_fieldcolumn)r   
field_name)modelr   r   
<listcomp>b   s    z6ExclusionConstraint.constraint_sql.<locals>.<listcomp>z, z WHERE (%s) )r   r   r!   r   r6   r   )r	   Zget_compilerr5   r4   r7   r   template
quote_namer   r   joinZ_index_include_sqlZ_deferrable_constraint_sqlr   )r$   r<   r)   r&   r/   r!   r   r   r   )r<   r   constraint_sql]   s    

z"ExclusionConstraint.constraint_sqlc             C   s,   | j | tdt|jj|j| j||dS )Nz(ALTER TABLE %(table)s ADD %(constraint)s)table
constraint)check_supportedr   r   r8   Zdb_tabler@   rB   )r$   r<   r)   r   r   r   
create_sqll   s
    
zExclusionConstraint.create_sqlc             C   s   |j |j||j| jS )N)Z_delete_constraint_sqlZsql_delete_checkr@   r   )r$   r<   r)   r   r   r   
remove_sqlt   s    zExclusionConstraint.remove_sqlc             C   s   | j r|jjj rtdd S )Nz7Covering exclusion constraints requires PostgreSQL 12+.)r   r5   featuresZsupports_covering_gist_indexesr   )r$   r)   r   r   r   rE   {   s    z#ExclusionConstraint.check_supportedc                s   t  j \}}}| j|d< | jd k	r.| j|d< | j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kwargs)r%   r   r   rI      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	 j
|S )N)r   r%   r   r   r!   r   r   r   r   r"   __eq__)r$   other)r%   r   r   rM      s    zExclusionConstraint.__eq__c          
   C   sn   d| j j| j| j| jd krdnd| j | jd kr4dnd| j | jsFdndt| j | js\dndt| j f S )Nz+<%s: index_type=%s, expressions=%s%s%s%s%s>r>   z, condition=%sz, deferrable=%sz, include=%sz, opclasses=%s)	r%   __qualname__r   r!   r   r   r   reprr   )r$   r   r   r   __repr__   s    zExclusionConstraint.__repr__)__name__
__module__rO   r?   r#   r4   r7   rB   rF   rG   rE   rI   rM   rQ   __classcell__r   r   )r%   r   r
   
   s   6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.sqlr	   __all__r
   r   r   r   r   <module>   s   