a
    xd                     @   sV   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 )    )	StatementTable)
DeferrableFQ)BaseConstraint)QueryExclusionConstraintc                       sr   e Zd ZdZdddd f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	   zYCONSTRAINT %(name)s EXCLUDE USING %(index_type)s (%(expressions)s)%(where)s%(deferrable)sN)
index_type	condition
deferrablec                   s   |r|  dvrtd|s$tdtdd |D s>tdt|td tfsXtd|rh|rhtdt|td tfstd	|| _|pd
| _|| _	|| _
t j|d d S )N>   gistZ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expr r   [/var/www/html/Ranjet/env/lib/python3.9/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.ZGIST)name)lower
ValueErrorallr   typer   r   expressionsr
   r   r   super__init__)selfr   r   r
   r   r   	__class__r   r   r      s:    
zExclusionConstraint.__init__c           	         sp   g }| j D ]`\}}t|tr$t|}|j|d}||\}}|t fdd|D  }|d||f  q
|S )N)queryc                 3   s   | ]}  |V  qd S NZquote_valuer   pschema_editorr   r   r   7       z:ExclusionConstraint._get_expression_sql.<locals>.<genexpr>z
%s WITH %s)r   r   strr   Zresolve_expressioncompiler   append)	r    compilerr)   r#   r   Z
expressionoperatorsqlparamsr   r(   r   _get_expression_sql0   s    
z'ExclusionConstraint._get_expression_sqlc                    sF   | j d u rd S || j }|| j\}}|t fdd|D  S )Nc                 3   s   | ]}  |V  qd S r$   r%   r&   r(   r   r   r   @   r*   z9ExclusionConstraint._get_condition_sql.<locals>.<genexpr>)r   Zbuild_whereZas_sql
connectionr   )r    r.   r)   r#   wherer0   r1   r   r(   r   _get_condition_sql;   s
    
z&ExclusionConstraint._get_condition_sqlc                 C   sp   t |dd}|j|jd}| |||}| |||}| j|| j| jd	||r\d| nd|
| jd S )NF)Z
alias_cols)r3   z, z WHERE (%s) )r   r
   r   r4   r   )r   Zget_compilerr3   r2   r5   template
quote_namer   r
   joinZ_deferrable_constraint_sqlr   )r    modelr)   r#   r.   r   r   r   r   r   constraint_sqlB   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)r   r   Z_metaZdb_tabler8   r;   r    r:   r)   r   r   r   
create_sqlO   s
    
zExclusionConstraint.create_sqlc                 C   s   | |j||| jS r$   )Z_delete_constraint_sqlZsql_delete_checkr8   r   r>   r   r   r   
remove_sqlV   s
    
zExclusionConstraint.remove_sqlc                    s`   t   \}}}| j|d< | jd ur.| j|d< | j dkrF| j|d< | jrV| j|d< |||fS )Nr   r   r   r
   r   )r   deconstructr   r   r
   r   r   )r    pathargskwargsr!   r   r   rA   ]   s    




zExclusionConstraint.deconstructc                    sT   t || jrH| j|jkoF| j|jkoF| j|jkoF| j|jkoF| j|jkS t |S r$   )	r   r"   r   r
   r   r   r   r   __eq__)r    otherr!   r   r   rE   h   s    



zExclusionConstraint.__eq__c                 C   sB   d| j j| j| j| jd u rdnd| j | jd u r4dnd| j f S )Nz'<%s: index_type=%s, expressions=%s%s%s>r6   z, condition=%sz, deferrable=%s)r"   __qualname__r
   r   r   r   )r    r   r   r   __repr__s   s    zExclusionConstraint.__repr__)__name__
__module__rG   r7   r   r2   r5   r;   r?   r@   rA   rE   rH   __classcell__r   r   r!   r   r	   	   s   $N)Z!django.db.backends.ddl_referencesr   r   Zdjango.db.modelsr   r   r   Zdjango.db.models.constraintsr   Zdjango.db.models.sqlr   __all__r	   r   r   r   r   <module>   s
   