o
    `Zhs%                     @   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   Z/var/www/html/lang_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)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>   spgistgistz;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.Z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   Zset_wrapper_classes
connectionappendr   Zresolve_expression)r2   schema_editorqueryr/   idx
expressionr5   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   Zbuild_whereZas_sqlr8   r&   )r2   compilerr:   r;   wheresqlparamsr   rB   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)Z
alias_cols)r8   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   r/   rD   r   r   )r   Zget_compilerr8   r>   rH   db_tablerG   r   r   r   r	   
quote_namer+   r   r   r@   Z_index_include_sqlZ_deferrable_constraint_sqlr   )	r2   rM   r:   r;   rC   r/   rP   r   r   r   rL   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)rP   
constraint)check_supportedr   r	   rH   rQ   rR   rS   r2   rM   r:   r   r   r   
create_sqlm   s   

zExclusionConstraint.create_sqlc                 C   s   | |j||| jS r?   )Z_delete_constraint_sqlZsql_delete_checkrR   r+   rV   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,   r8   featuresZ supports_covering_spgist_indexesr   )r2   r:   r   r   r   rU   |   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   rZ      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 r?   )r$   r4   r+   r   r/   r   r   r   r   r    r0   __eq__)r2   otherr3   r   r   r^      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>rO   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    Z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 ]]\}	\}
}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)Z_default_managerrk   Z_get_field_value_maprH   itemsr6   r/   r$   r7   r   r+   flattenZreplace_expressionsr   Zget_source_expressionsr   Zpostgres_operatorr9   filterZ_get_pk_val_stateZaddingrc   r   existsr   Zget_violation_error_messager   r   check)r2   rM   instancerc   rk   ZquerysetZreplacement_mapZreplacementslookupsr<   r=   r5   r)   Zrhs_expressionlookupZmodel_class_pkr   r   r   validate   sV   








zExclusionConstraint.validate)r   r   r   r   r1   r>   rG   rS   rW   rX   rU   rZ   r^   ra   r   ru   __classcell__r   r   r3   r   r      s(    
,
N)typesr   Zdjango.contrib.postgres.indexesr   Zdjango.core.exceptionsr   Z	django.dbr   r   Z!django.db.backends.ddl_referencesr   r   r	   Zdjango.db.modelsr
   r   r   r   Zdjango.db.models.expressionsr   r   Zdjango.db.models.indexesr   Zdjango.db.models.lookupsr   Zdjango.db.models.sqlr   __all__r   r   r   r   r   r   <module>   s    