o
    es%                     @   s   d dl m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gZG dd deZG dd deZdS )    )NoneType)OpClass)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
 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,   r0   r   r   r   r   r   r    	__class__r   r   r2      s<   

zExclusionConstraint.__init__c                 C   s^   g }t | jD ] \}\}}t|trt|}t||d}||j || qt	| 
|S )N)operator)	enumerater0   r$   strr   r   set_wrapper_classes
connectionappendr   resolve_expression)r3   schema_editorqueryr0   idx
expressionr6   r   r   r   _get_expressionsF   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r=   r   r   r*   U   s    z9ExclusionConstraint._get_condition_sql.<locals>.<genexpr>)r   build_whereas_sqlr:   r&   )r3   compilerr=   r>   wheresqlparamsr   rE   r   _get_condition_sqlP   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>]   s    z6ExclusionConstraint.constraint_sql.<locals>.<listcomp>z WHERE (%s) )tabler,   r   r0   rI   r   r   )r   get_compilerr:   rA   rN   db_tablerL   r   r   r   r	   
quote_namer,   r   r   rC   _index_include_sql_deferrable_constraint_sqlr   )	r3   rS   r=   r>   rH   r0   rV   r   r   r   rR   r   constraint_sqlW   s(   





z"ExclusionConstraint.constraint_sqlc                 C   s,   |  | tdt|jj|j| ||dS )Nz(ALTER TABLE %(table)s ADD %(constraint)s)rV   
constraint)check_supportedr   r	   rN   rX   rY   r\   r3   rS   r=   r   r   r   
create_sqlm   s   

zExclusionConstraint.create_sqlc                 C   s   | |j||| jS rB   )_delete_constraint_sqlsql_delete_checkrY   r,   r_   r   r   r   
remove_sqlu   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   )r3   r=   r   r   r   r^   |   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 )Nr0   r   r!   r   r   r   )r1   deconstructr0   r   r   r-   r   r   )r3   pathargskwargsr4   r   r   rf      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 rB   )r$   r5   r,   r   r0   r   r   r   r   r    r1   __eq__)r3   otherr4   r   r   rj      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>rU   z condition=%sz deferrable=%rz include=%sz violation_error_code=%rz violation_error_message=%r)r5   r   reprr   r0   r,   r   r   r   r   r    default_violation_error_message)r3   r   r   r   __repr__   s"   

zExclusionConstraint.__repr__c                 C   s  |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|  | 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>r   )lhsrhs)pk)code)using)_default_managerrx   _get_field_value_maprN   itemsr7   r0   r$   r8   r   r,   flattenreplace_expressionsr   get_source_expressionsr   postgres_operatorr;   filter_get_pk_val_stateaddingrp   r   existsr   get_violation_error_messager   r   check)r3   rS   instancerp   rx   querysetreplacement_mapreplacementslookupsr?   r@   r6   r)   rhs_expressionlookupmodel_class_pkr   r   r   validate   sV   








zExclusionConstraint.validate)r   r   r   r   r2   rA   rL   r\   r`   rc   r^   rf   rj   rn   r   r   __classcell__r   r   r4   r   r      s(    
,
N)typesr   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   __all__r   r   r   r   r   r   <module>   s    