o
    <g=&                     @   s   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
gZG dd deZG dd
 d
eZdS )    )NoneType)ValidationError)DEFAULT_DB_ALIASNotSupportedError)Expressions	StatementTable)BaseConstraint
DeferrableFQ)ExistsExpressionList)IndexExpression)PostgresOperatorLookup)QueryExclusionConstraintc                   @   s   e Zd ZdZdS )ExclusionConstraintExpressionz!%(expressions)s WITH %(operator)sN)__name__
__module____qualname__template r   r   V/var/www/html/venv/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
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)sN)
index_type	condition
deferrableincludeviolation_error_codeviolation_error_messagec          	         s   |r|  dvrtd|stdtdd |D stdt|ttfs*tdt|ttfs5tdt|tttfsAtd	|| _	|pGd
| _
|| _|| _|rUt|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   r   	<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=ExclusionConstraint.deferrable must be a Deferrable instance.z4ExclusionConstraint.include must be a list or tuple.GISTr   )namer   r   )lower
ValueErrorallr#   r   r   r
   r$   r%   expressionsr   r   r   r   super__init__)	selfr+   r/   r   r   r   r   r   r   	__class__r   r   r1      s<   

zExclusionConstraint.__init__c                 C   s^   g }t | jD ] \}\}}t|trt|}t||d}||j || qt	| 
|S )N)operator)	enumerater/   r#   strr   r   set_wrapper_classes
connectionappendr   resolve_expression)r2   schema_editorqueryr/   idx
expressionr5   r   r   r   _get_expressionsE   s   
z$ExclusionConstraint._get_expressionsc                 C   sD   t  }| jD ]\}}t|trt|}||| q| ||S N)setr/   r#   r7   r   update_get_expr_references_check_references)r2   modelr9   
referencesr(   _r   r   r   _checkO   s   
zExclusionConstraint._checkc                    sF   | j d u rd S || j }|| j\}}|t fdd|D  S )Nc                 3   s    | ]}  |V  qd S rA   )quote_value)r'   pr<   r   r   r)   \   s    z9ExclusionConstraint._get_condition_sql.<locals>.<genexpr>)r   build_whereas_sqlr9   r%   )r2   compilerr<   r=   wheresqlparamsr   rL   r   _get_condition_sqlW   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)r9   c                    s   g | ]	} j |jqS r   )_meta	get_fieldcolumn)r'   
field_namerF   r   r   
<listcomp>d   s    z6ExclusionConstraint.constraint_sql.<locals>.<listcomp>z WHERE (%s) )tabler+   r   r/   rP   r   r   )r   get_compilerr9   r@   rU   db_tablerS   r   r   r   r   
quote_namer+   r   r   rJ   _index_include_sql_deferrable_constraint_sqlr   )	r2   rF   r<   r=   rO   r/   r\   r   r   r   rY   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)r\   
constraint)check_supportedr   r   rU   r^   r_   rb   r2   rF   r<   r   r   r   
create_sqlt   s   

zExclusionConstraint.create_sqlc                 C   s   | |j||| jS rA   )_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,   r9   features supports_covering_spgist_indexesr   )r2   r<   r   r   r   rd      s   z#ExclusionConstraint.check_supportedc                    sp   t   \}}}| j|d< | jd ur| j|d< | j dkr#| j|d< | jr+| j|d< | jr3| j|d< |||fS )Nr/   r   r    r   r   r   )r0   deconstructr/   r   r   r,   r   r   )r2   pathargskwargsr3   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 rA   )r#   r4   r+   r   r/   r   r   r   r   r   r0   __eq__)r2   otherr3   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	d u r8dnd| j	 | j
d u sG| j
| jkrKdf	 S d| j
 f	 S )Nz4<%s: index_type=%s expressions=%s name=%s%s%s%s%s%s>r[   z condition=%sz deferrable=%rz include=%sz violation_error_code=%rz violation_error_message=%r)r4   r   reprr   r/   r+   r   r   r   r   r   default_violation_error_message)r2   r   r   r   __repr__   s"   

zExclusionConstraint.__repr__c                 C   s|  |j |}|j|j|d}dd | D }g }t| jD ]Y\}	\}
}t|
tr-t	|
}
|rRt|
t	r=|
j
|v r< d S n|
 D ]}t|t	rQ|j
|v rQ  d S qA|
|}t|
dr`|
 }
t|dri| }t|
|d}||_|| q|j| }||j}|jjs|d ur|j|d}| js| rt|  | jdd S | jt|| j@ j||drt|  | jdd S )	N)metaexcludec                 S   s   i | ]	\}}t ||qS r   )r   )r'   fieldvaluer   r   r   
<dictcomp>   s    z0ExclusionConstraint.validate.<locals>.<dictcomp>get_expression_for_validation)lhsrhs)pk)code)using)_default_managerr   _get_field_expression_maprU   itemsr6   r/   r#   r7   r   r+   flattenreplace_expressionshasattrrz   r   postgres_operatorr:   filter_get_pk_val_stateaddingrv   r   existsr   get_violation_error_messager   r   check)r2   rF   instancerv   r   querysetreplacement_mapreplacementslookupsr>   r?   r5   r(   rhs_expressionlookupmodel_class_pkr   r   r   validate   sV   








zExclusionConstraint.validate)r   r   r   r   r1   r@   rI   rS   rb   rf   ri   rd   rl   rp   rt   r   r   __classcell__r   r   r3   r   r      s*    
,
N)typesr   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   __all__r   r   r   r   r   r   <module>   s    