o
    ^"<f                     @   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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  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                   s2  |r|  dvrtd|stdtdd |D stdt|td tfs,td|r4|r4tdt|td tfsAtd	t|td ttfsOtd
|r]|r]|  dkr]tdt|ttfshtd|rvt	|t	|krvtd|| _
|p|d| _|| _|| _|rt|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   Z/var/www/html/kck/venv/lib/python3.10/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.r   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.GISTr   )name)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$      s^   
zExclusionConstraint.__init__c              	      s   g }t | jD ]K\}\}}t|trt|}|j|d}||\}}	z| j| }
|
r1d||
f }W n	 ty;   Y nw |t	 fdd|	D  }|
d||f  q|S )N)queryz%s %sc                 3       | ]}  |V  qd S Nquote_valuer   pschema_editorr   r   r   R       z:ExclusionConstraint._get_expression_sql.<locals>.<genexpr>z
%s WITH %s)	enumerater"   r   strr   resolve_expressioncompiler   
IndexErrorr   append)r%   compilerr0   r(   r"   idx
expressionoperatorsqlparamsopclassr   r/   r   _get_expression_sqlE   s"   

z'ExclusionConstraint._get_expression_sqlc                    sF   | j d u rd S || j }|| j\}}|t fdd|D  S )Nc                 3   r)   r*   r+   r-   r/   r   r   r   [   r1   z9ExclusionConstraint._get_condition_sql.<locals>.<genexpr>)r   build_whereas_sql
connectionr   )r%   r8   r0   r(   wherer<   r=   r   r/   r   _get_condition_sqlV   s
   
z&ExclusionConstraint._get_condition_sqlc              	      s   t  dd}|j|jd}| |||}| |||} fdd| jD }| j|| j| j	d
|| ||r=d| nd|| jd	 S )
NF)
alias_cols)rB   c                    s   g | ]	} 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   rC   r   )r	   get_compilerrB   r?   rD   r   template
quote_namer   r   join_index_include_sql_deferrable_constraint_sqlr   )r%   rK   r0   r(   r8   r"   r   r   r   rJ   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)table
constraint)check_supportedr   r   rF   db_tablerP   rT   r%   rK   r0   r   r   r   
create_sqll   s   

zExclusionConstraint.create_sqlc                 C   s   | |j||| jS r*   )_delete_constraint_sqlsql_delete_checkrP   r   rY   r   r   r   
remove_sqlt   s
   
zExclusionConstraint.remove_sqlc                 C   s    | j r|jjjstdd S d S )Nz7Covering exclusion constraints requires PostgreSQL 12+.)r   rB   featuressupports_covering_gist_indexesr   )r%   r0   r   r   r   rW   {   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 )Nr"   r   r   r   r   r   r   )	r#   deconstructr"   r   r   r   r   r   r   )r%   pathargskwargsr&   r   r   r`      s   







zExclusionConstraint.deconstructc                    sl   t || jr0| 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	 
|S r*   )r   r'   r   r   r"   r   r   r   r   r#   __eq__)r%   otherr&   r   r   rd      s   





	zExclusionConstraint.__eq__c              
   C   sr   d| j j| j| 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s0df S dt| j f S )Nz+<%s: index_type=%s, expressions=%s%s%s%s%s>rM   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__rf   rO   r$   r?   rD   rT   rZ   r]   rW   r`   rd   rh   __classcell__r   r   r&   r   r
   
   s    8N)	django.dbr   !django.db.backends.ddl_referencesr   r   django.db.modelsr   r   r   django.db.models.constraintsr   django.db.models.sqlr	   __all__r
   r   r   r   r   <module>   s    