U
    e<                     @   s   d dl mZ d dlmZmZ d dlmZ d dlmZm	Z	m
Z
mZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ ddddgZG dd dZG dd deZG dd deZG dd deZdS )    )Enum)
FieldErrorValidationError)connections)ExistsExpressionListFOrderBy)IndexExpression)Exact)Q)Query)DEFAULT_DB_ALIAS)gettext_lazyBaseConstraintCheckConstraint
DeferrableUniqueConstraintc                   @   sl   e Zd ZedZdZdddZedd Zdd Z	d	d
 Z
dd ZdefddZdd Zdd Zdd ZdS )r   u&   Constraint “%(name)s” is violated.Nc                 C   s"   || _ |d k	r|| _n| j| _d S N)nameviolation_error_messagedefault_violation_error_message)selfr   r    r   @/tmp/pip-unpacked-wheel-lctamlir/django/db/models/constraints.py__init__   s    zBaseConstraint.__init__c                 C   s   dS )NFr   r   r   r   r   contains_expressions   s    z#BaseConstraint.contains_expressionsc                 C   s   t dd S Nz.This method must be implemented by a subclass.NotImplementedErrorr   modelschema_editorr   r   r   constraint_sql   s    zBaseConstraint.constraint_sqlc                 C   s   t dd S r   r   r!   r   r   r   
create_sql"   s    zBaseConstraint.create_sqlc                 C   s   t dd S r   r   r!   r   r   r   
remove_sql%   s    zBaseConstraint.remove_sqlc                 C   s   t dd S r   r   )r   r"   instanceexcludeusingr   r   r   validate(   s    zBaseConstraint.validatec                 C   s   | j d| ji S )Nr   )r   r   r   r   r   r   get_violation_error_message+   s    z*BaseConstraint.get_violation_error_messagec                 C   sT   d| j j| j jf }|dd}d| ji}| jd k	rJ| j| jkrJ| j|d< |d|fS )Nz%s.%szdjango.db.models.constraintszdjango.db.modelsr   r   r   )	__class__
__module____name__replacer   r   r   )r   pathkwargsr   r   r   deconstruct.   s    


zBaseConstraint.deconstructc                 C   s   |   \}}}| j||S r   )r2   r,   )r   _argsr1   r   r   r   clone9   s    zBaseConstraint.clone)N)r.   r-   __qualname__r3   r   r   r   propertyr   r$   r%   r&   r   r*   r+   r2   r5   r   r   r   r   r      s   

c                       sp   e Zd Zdd fdd
Zdd Zdd Zd	d
 Zdd ZdefddZ	dd Z
 fddZ fddZ  ZS )r   Nr   c                   s.   || _ t|ddstdt j||d d S )NZconditionalFzACheckConstraint.check must be a Q instance or boolean expression.r8   )checkgetattr	TypeErrorsuperr   )r   r9   r   r   r,   r   r   r   ?   s    zCheckConstraint.__init__c                    sR   t |dd}|| j}|j jd}|| j\}}|t fdd|D  S )NFr"   
alias_cols
connectionc                 3   s   | ]}  |V  qd S r   Zquote_value.0pr#   r   r   	<genexpr>L   s     z1CheckConstraint._get_check_sql.<locals>.<genexpr>)r   build_wherer9   get_compilerrA   as_sqltupler   r"   r#   querywherecompilerZsqlparamsr   rF   r   _get_check_sqlG   s
    zCheckConstraint._get_check_sqlc                 C   s   |  ||}|| j|S r   )rQ   Z
_check_sqlr   r   r"   r#   r9   r   r   r   r$   N   s    zCheckConstraint.constraint_sqlc                 C   s   |  ||}||| j|S r   )rQ   Z_create_check_sqlr   rR   r   r   r   r%   R   s    zCheckConstraint.create_sqlc                 C   s   | || jS r   )Z_delete_check_sqlr   r!   r   r   r   r&   V   s    zCheckConstraint.remove_sqlc                 C   sN   |j |j|d}z$t| jj||ds2t|  W n tk
rH   Y nX d S )Nmetar(   r)   )_get_field_value_map_metar   r9   r   r+   r   )r   r"   r'   r(   r)   againstr   r   r   r*   Y   s    zCheckConstraint.validatec                 C   s   d| j j| jt| jf S )Nz<%s: check=%s name=%s>)r,   r6   r9   reprr   r   r   r   r   __repr__a   s
    zCheckConstraint.__repr__c                    s:   t |tr.| j|jko,| j|jko,| j|jkS t |S r   )
isinstancer   r   r9   r   r<   __eq__r   otherr=   r   r   r\   h   s    


zCheckConstraint.__eq__c                    s$   t   \}}}| j|d< |||fS )Nr9   )r<   r2   r9   r   r0   r4   r1   r=   r   r   r2   q   s    
zCheckConstraint.deconstruct)r.   r-   r6   r   rQ   r$   r%   r&   r   r*   rZ   r\   r2   __classcell__r   r   r=   r   r   >   s   	c                   @   s   e Zd ZdZdZdd ZdS )r   deferredZ	immediatec                 C   s   | j j d| j S )N.)r,   r6   _name_r   r   r   r   rZ   |   s    zDeferrable.__repr__N)r.   r-   r6   ZDEFERREDZ	IMMEDIATErZ   r   r   r   r   r   w   s   c                       s   e Zd Zdddddddd fdd
Ze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efddZ  ZS )r   r   N)fieldsr   	condition
deferrableinclude	opclassesr   c          	         sV  |st d|s|st d|r,|r,t dt|td tfsFt d|rV|rVt d|rf|rft d|rv|rvt d|r|rt d|r|rt d	t|td tfst d
t|td ttfst dt|ttfst d|rt|t|krt dt|| _|| _	|| _
|r"t|nd| _|| _tdd |D | _t j||d d S )Nz"A unique constraint must be named.zKAt least one field or expression is required to define a unique constraint.z?UniqueConstraint.fields and expressions are mutually exclusive.z0UniqueConstraint.condition must be a Q instance.z4UniqueConstraint with conditions cannot be deferred.z8UniqueConstraint with include fields cannot be deferred.z3UniqueConstraint with opclasses cannot be deferred.z5UniqueConstraint with expressions cannot be deferred.zrUniqueConstraint.opclasses cannot be used with expressions. Use django.contrib.postgres.indexes.OpClass() instead.z:UniqueConstraint.deferrable must be a Deferrable instance.z1UniqueConstraint.include must be a list or tuple.z3UniqueConstraint.opclasses must be a list or tuple.z]UniqueConstraint.fields and UniqueConstraint.opclasses must have the same number of elements.r   c                 s   s$   | ]}t |trt|n|V  qd S r   )r[   strr   )rD   
expressionr   r   r   rG      s   z,UniqueConstraint.__init__.<locals>.<genexpr>r8   )
ValueErrorr[   typer   r   listrK   lenrd   re   rf   rg   rh   expressionsr<   r   )	r   rd   r   re   rf   rg   rh   r   ro   r=   r   r   r      sZ    

zUniqueConstraint.__init__c                 C   s
   t | jS r   )boolro   r   r   r   r   r      s    z%UniqueConstraint.contains_expressionsc                    s`   | j d krd S t|dd}|| j }|j jd}|| j\}}|t fdd|D  S )NFr>   r@   c                 3   s   | ]}  |V  qd S r   rB   rC   rF   r   r   rG      s     z6UniqueConstraint._get_condition_sql.<locals>.<genexpr>)re   r   rH   rI   rA   rJ   rK   rL   r   rF   r   _get_condition_sql   s    
z#UniqueConstraint._get_condition_sqlc                 C   sN   | j s
d S g }| j D ]"}t|}||j || qt| t|ddS )NF)r?   )ro   r
   Zset_wrapper_classesrA   appendr   Zresolve_expressionr   )r   r"   r#   Zindex_expressionsrj   Zindex_expressionr   r   r   _get_index_expressions   s    

z'UniqueConstraint._get_index_expressionsc              
      s`    fdd| j D } fdd| jD }|  |}|  |}|j || j|| j|| j|dS )Nc                    s   g | ]} j |qS r   rW   	get_fieldrD   
field_namer"   r   r   
<listcomp>   s     z3UniqueConstraint.constraint_sql.<locals>.<listcomp>c                    s   g | ]} j |jqS r   rW   ru   columnrv   rx   r   r   ry      s    re   rf   rg   rh   ro   )rd   rg   rq   rs   Z_unique_sqlr   rf   rh   r   r"   r#   rd   rg   re   ro   r   rx   r   r$      s     
zUniqueConstraint.constraint_sqlc              
      s`    fdd| j D } fdd| jD }|  |}|  |}|j || j|| j|| j|dS )Nc                    s   g | ]} j |qS r   rt   rv   rx   r   r   ry      s     z/UniqueConstraint.create_sql.<locals>.<listcomp>c                    s   g | ]} j |jqS r   rz   rv   rx   r   r   ry      s    r|   )rd   rg   rq   rs   Z_create_unique_sqlr   rf   rh   r}   r   rx   r   r%      s     
zUniqueConstraint.create_sqlc              	      sJ   |   |} fdd| jD }|  |}|j | j|| j|| j|dS )Nc                    s   g | ]} j |jqS r   rz   rv   rx   r   r   ry      s    z/UniqueConstraint.remove_sql.<locals>.<listcomp>r|   )rq   rg   rs   Z_delete_unique_sqlr   rf   rh   )r   r"   r#   re   rg   ro   r   rx   r   r&      s    
zUniqueConstraint.remove_sqlc                 C   s   d| j j| jsdndt| j | js(dndt| j dt| j | jd krNdnd| j | jd krddnd| j | jsvdndt| j | j	sdnd	t| j	 f S )
Nz<%s:%s%s%s%s%s%s%s> z
 fields=%sz expressions=%sz name=%sz condition=%sz deferrable=%rz include=%sz opclasses=%s)
r,   r6   rd   rY   ro   r   re   rf   rg   rh   r   r   r   r   rZ     s    zUniqueConstraint.__repr__c                    sv   t |trj| j|jkoh| j|jkoh| j|jkoh| j|jkoh| j|jkoh| j|jkoh| j|jkoh| j	|j	kS t
 |S r   )r[   r   r   rd   re   rf   rg   rh   ro   r   r<   r\   r]   r=   r   r   r\     s"    








zUniqueConstraint.__eq__c                    sl   t   \}}}| jr | j|d< | jr0| j|d< | jr@| j|d< | jrP| j|d< | jr`| j|d< || j|fS )Nrd   re   rf   rg   rh   )r<   r2   rd   re   rf   rg   rh   ro   r_   r=   r   r   r2   %  s    




zUniqueConstraint.deconstructc                 C   s6  |j |}| jri }| jD ]Z}|r2||kr2 d S |j|}t||j}	|	d ksf|	dkrlt| jj	rl d S |	||j
< q|jf |}n|r| jD ]T}
t|
dr|
 D ] }t|tr|j
|kr  d S qqt|
tr|
j
|kr d S qdd |j|j|d D }g }| jD ].}t|tr$|j}|t||| q|j| }||j}|jjst|d k	rt|j|d}| js| r2| jrt|  |  D ]2\}}|D ]"}|| krt|!|| jqqnZ|j|j|d}z2| jt"|| j@ j#||drt|  W n t$k
r0   Y nX d S )Nr~   flattenc                 S   s   i | ]\}}t ||qS r   )r   )rD   fieldvaluer   r   r   
<dictcomp>O  s    z-UniqueConstraint.validate.<locals>.<dictcomp>rS   )pkrU   )%Z_default_managerr)   rd   rW   ru   r:   Zattnamer   featuresZ!interprets_empty_strings_as_nullsr   filterro   hasattrr   r[   r   rV   itemsr	   rj   rr   r   Zreplace_expressionsZ_get_pk_val_stateZaddingr(   re   existsr   r+   Zget_constraintsZunique_error_messager   r9   r   )r   r"   r'   r(   r)   ZquerysetZlookup_kwargsrw   r   Zlookup_valuerj   exprreplacementsro   Zmodel_class_pkconstraints
constraintrX   r   r   r   r*   3  st    



 



 zUniqueConstraint.validate)r.   r-   r6   r   r7   r   rq   rs   r$   r%   r&   rZ   r\   r2   r   r*   r`   r   r   r=   r   r      s&   =
	N)enumr   Zdjango.core.exceptionsr   r   Z	django.dbr   Zdjango.db.models.expressionsr   r   r   r	   Zdjango.db.models.indexesr
   Zdjango.db.models.lookupsr   Zdjango.db.models.query_utilsr   Zdjango.db.models.sql.queryr   Zdjango.db.utilsr   Zdjango.utils.translationr   r3   __all__r   r   r   r   r   r   r   r   <module>   s   .9	