o
    lf'                     @   s   d dl Z d dlmZ 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mZmZmZ d dlmZmZ d dl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G dd deZdS )    N)OpClass)ValidationError)DEFAULT_DB_ALIASNotSupportedError)Expressions	StatementTable)BaseConstraint
DeferrableFQ)ExistsExpressionList)IndexExpression)PostgresOperatorLookup)Query)RemovedInDjango50WarningExclusionConstraintc                   @   s   e Zd ZdZdS )ExclusionConstraintExpressionz!%(expressions)s WITH %(operator)sN)__name__
__module____qualname__template r   r   g/var/www/ticemtrilhas/avaliacao_env/lib/python3.10/site-packages/django/contrib/postgres/constraints.pyr      s    r   c                       s   e Zd ZdZd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defddZ  ZS )r   zdCONSTRAINT %(name)s EXCLUDE USING %(index_type)s (%(expressions)s)%(include)s%(where)s%(deferrable)sNr   )
index_type	condition
deferrableinclude	opclassesviolation_error_messagec          	         s  |r|  dvrtd|stdtdd |D stdt|td tfs,tdt|td tfs9tdt|td ttfsGtd	t|ttfsRtd
|r`t	|t	|kr`td|| _
|pfd| _|| _|| _|rtt|nd| _|| _| jrtjdtd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   r   	<genexpr>0   s    
z/ExclusionConstraint.__init__.<locals>.<genexpr>z+The expressions must be a list of 2-tuples.z3ExclusionConstraint.condition must be a Q instance.z=ExclusionConstraint.deferrable must be a Deferrable instance.z4ExclusionConstraint.include must be a list or tuple.z6ExclusionConstraint.opclasses must be a list or tuple.zhExclusionConstraint.expressions and ExclusionConstraint.opclasses must have the same number of elements.GISTr   zThe opclasses argument is deprecated in favor of using django.contrib.postgres.indexes.OpClass in ExclusionConstraint.expressions.r#   )category
stacklevel)namer    )lower
ValueErrorallr$   typer   r
   r%   r&   r'   expressionsr   r   r   r   r   warningswarnr   super__init__)	selfr.   r3   r   r   r   r   r   r    	__class__r   r   r7      sN   
zExclusionConstraint.__init__c              	   C   s   g }t | jD ]4\}\}}t|trt|}z
t|| j| }W n	 ty)   Y nw t||d}|	|j
 || qt| |S )N)operator)	enumerater3   r$   strr   r   r   
IndexErrorr   set_wrapper_classes
connectionappendr   resolve_expression)r8   schema_editorqueryr3   idx
expressionr;   r   r   r   _get_expressionsT   s   
z$ExclusionConstraint._get_expressionsc                    sF   | j d u rd S || j }|| j\}}|t fdd|D  S )Nc                 3   s    | ]}  |V  qd S N)quote_value)r(   prC   r   r   r*   g   s    z9ExclusionConstraint._get_condition_sql.<locals>.<genexpr>)r   build_whereas_sqlr@   r&   )r8   compilerrC   rD   wheresqlparamsr   rK   r   _get_condition_sqlb   s
   
z&ExclusionConstraint._get_condition_sqlc           	         s   t  dd}|j|jd}| ||} jj}| |||} fdd| jD }t| j	t
||j|| j| jt||||j|rDd| nd| ||| jdS )	NF)
alias_cols)r@   c                    s   g | ]	} j |jqS r   )_meta	get_fieldcolumn)r(   
field_namemodelr   r   
<listcomp>o   s    z6ExclusionConstraint.constraint_sql.<locals>.<listcomp>z WHERE (%s) )tabler.   r   r3   rO   r   r   )r   get_compilerr@   rG   rT   db_tablerR   r   r   r   r   
quote_namer.   r   r   rI   _index_include_sql_deferrable_constraint_sqlr   )	r8   rY   rC   rD   rN   r3   r\   r   r   r   rX   r   constraint_sqli   s(   





z"ExclusionConstraint.constraint_sqlc                 C   s,   |  | tdt|jj|j| ||dS )Nz(ALTER TABLE %(table)s ADD %(constraint)s)r\   
constraint)check_supportedr   r   rT   r^   r_   rb   r8   rY   rC   r   r   r   
create_sql   s   

zExclusionConstraint.create_sqlc                 C   s   | |j||| jS rH   )_delete_constraint_sqlsql_delete_checkr_   r.   re   r   r   r   
remove_sql   s
   
zExclusionConstraint.remove_sqlc                 C   s2   | j r| j dkr|jjjstdd S d S d S )Nr"   zMCovering exclusion constraints using an SP-GiST index require PostgreSQL 14+.)r   r   r/   r@   features supports_covering_spgist_indexesr   )r8   rC   r   r   r   rd      s   z#ExclusionConstraint.check_supportedc                    s   t   \}}}| j|d< | jd ur| j|d< | j dkr#| j|d< | jr+| j|d< | jr3| j|d< | jr;| j|d< |||fS )Nr3   r   r!   r   r   r   r   )	r6   deconstructr3   r   r   r/   r   r   r   )r8   pathargskwargsr9   r   r   rl      s   







zExclusionConstraint.deconstructc                    sx   t || jr6| j|jko5| j|jko5| j|jko5| j|jko5| j|jko5| j|jko5| j|jko5| j	|j	kS t
 |S rH   )r$   r:   r.   r   r3   r   r   r   r   r    r6   __eq__)r8   otherr9   r   r   rp      s"   







zExclusionConstraint.__eq__c                 C   s   d| j jt| jt| jt| j| jd u rdnd| j | jd u r"dnd| j | js+dndt| j | j	s8df S dt| j	 f S )Nz2<%s: index_type=%s expressions=%s name=%s%s%s%s%s>r[   z condition=%sz deferrable=%rz include=%sz opclasses=%s)
r:   r   reprr   r3   r.   r   r   r   r   )r8   r   r   r   __repr__   s   zExclusionConstraint.__repr__c                 C   sx  |j |}|j|j|d}dd | D }g }t| jD ]]\}	\}
}t|
tr-t	|
}
|rRt|
t	r=|
j
|v r< d S n|
 D ]}t|t	rQ|j
|v rQ  d S qA|
|}t|
trb|
 d }
t|trm| d }t|
|d}||_|| q|j| }||j}|jjs|d ur|j|d}| js| rt|  d S | jt|| j@ j||drt|  d S )N)metaexcludec                 S   s   i | ]	\}}t ||qS r   )r   )r(   fieldvaluer   r   r   
<dictcomp>   s    z0ExclusionConstraint.validate.<locals>.<dictcomp>r   )lhsrhs)pk)using)_default_managerr|   _get_field_value_maprT   itemsr<   r3   r$   r=   r   r.   flattenreplace_expressionsr   get_source_expressionsr   postgres_operatorrA   filter_get_pk_val_stateaddingru   r   existsr   get_violation_error_messager   check)r8   rY   instanceru   r|   querysetreplacement_mapreplacementslookupsrE   rF   r;   r)   rhs_expressionlookupmodel_class_pkr   r   r   validate   sN   






zExclusionConstraint.validate)r   r   r   r   r7   rG   rR   rb   rf   ri   rd   rl   rp   rs   r   r   __classcell__r   r   r9   r   r      s(    
9)r4   django.contrib.postgres.indexesr   django.core.exceptionsr   	django.dbr   r   !django.db.backends.ddl_referencesr   r   r   django.db.modelsr	   r
   r   r   django.db.models.expressionsr   r   django.db.models.indexesr   django.db.models.lookupsr   django.db.models.sqlr   django.utils.deprecationr   __all__r   r   r   r   r   r   <module>   s    