
     h'                         d dl 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 dlmZ dgZ G d de          Z G d de          ZdS )    N)OpClass)ValidationError)DEFAULT_DB_ALIASNotSupportedError)Expressions	StatementTable)BaseConstraint
DeferrableFQ)ExistsExpressionList)IndexExpression)PostgresOperatorLookup)Query)RemovedInDjango50WarningExclusionConstraintc                       e Zd ZdZdS )ExclusionConstraintExpressionz!%(expressions)s WITH %(operator)sN)__name__
__module____qualname__template     b/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/django/contrib/postgres/constraints.pyr   r      s        2HHHr   r   c                   |     e Zd ZdZddddddd fd
Zd Zd Zd Zd	 Zd
 Z	d Z
 fdZ fdZd ZdefdZ xZS )r   zdCONSTRAINT %(name)s EXCLUDE USING %(index_type)s (%(expressions)s)%(include)s%(where)s%(deferrable)sNr   )
index_type	condition
deferrableinclude	opclassesviolation_error_messagec                   |r%|                                 dvrt          d          |st          d          t          d |D                       st          d          t          |t	          d           t
          f          st          d          t          |t	          d           t          f          st          d          t          |t	          d           t          t          f          st          d          t          |t          t          f          st          d	          |r/t          |          t          |          k    rt          d
          || _
        |pd| _        || _        || _        |rt          |          nd| _        || _        | j        rt!          j        dt$          d           t'                                          ||           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              3   t   K   | ]3}t          |t          t          f          ot          |          d k    V  4dS )   N)
isinstancelisttuplelen).0exprs     r   	<genexpr>z/ExclusionConstraint.__init__.<locals>.<genexpr>0   sP       
 
CGJtdE]++>D		Q
 
 
 
 
 
r   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.z6ExclusionConstraint.opclasses must be a list or tuple.zhExclusionConstraint.expressions and ExclusionConstraint.opclasses must have the same number of elements.GISTr   zThe opclasses argument is deprecated in favor of using django.contrib.postgres.indexes.OpClass in ExclusionConstraint.expressions.r)   )category
stacklevel)namer$   )lower
ValueErrorallr*   typer   r   r+   r,   r-   expressionsr   r    r!   r"   r#   warningswarnr   super__init__)
selfr4   r9   r   r    r!   r"   r#   r$   	__class__s
            r   r=   zExclusionConstraint.__init__   s     	***,,4FFFM    	    
 
KV
 
 
 
 
 	L JKKK)d4jj!_55 	TRSSS*tDzz:&>?? 	O   'DJJe#<== 	USTTT)dE]33 	WUVVV 	[))S^^;;  
 '$."$)08uW~~~b"> 	M3 2    	d<STTTTTr   c                    g }t          | j                  D ]\  }\  }}t          |t                    rt	          |          }	 t          || j        |                   }n# t          $ r Y nw xY wt          ||          }|	                    |j
                   |                    |           t          |                     |          S )N)operator)	enumerater9   r*   strr   r   r#   
IndexErrorr   set_wrapper_classes
connectionappendr   resolve_expression)r>   schema_editorqueryr9   idx
expressionrA   s          r   _get_expressionsz$ExclusionConstraint._get_expressionsT   s    +4T5E+F+F 		+ 		+'C'*h*c** +z]]
$Z1DEE

   6zHUUUJ**=+CDDDz****{+>>uEEEs   A  
A-,A-c                     | j         d S |                    | j                   }|                    |j                  \  }}|t	          fd|D                       z  S )Nc              3   B   K   | ]}                     |          V  d S N)quote_value)r.   prI   s     r   r0   z9ExclusionConstraint._get_condition_sql.<locals>.<genexpr>g   s1      HHA=44Q77HHHHHHr   )r    build_whereas_sqlrF   r,   )r>   compilerrI   rJ   wheresqlparamss     `    r   _get_condition_sqlz&ExclusionConstraint._get_condition_sqlb   sg    >!4!!$.11ll8]-EFFVUHHHHHHHHHHHr   c                    t          d          }|                    |j                  }|                     ||          }j        j        }|                     |||          }fd| j        D             }t          | j	        t          ||j                  |                    | j                  | j        t          ||||j                  |rd|z  nd|                    |          |                    | j                            S )NF)
alias_cols)rF   c                 N    g | ]!}j                             |          j        "S r   )_meta	get_fieldcolumn)r.   
field_namemodels     r   
<listcomp>z6ExclusionConstraint.constraint_sql.<locals>.<listcomp>o   s9     
 
 
9CEK!!*--4
 
 
r   z WHERE (%s) )tabler4   r   r9   rV   r"   r!   )r   get_compilerrF   rM   r]   db_tablerY   r"   r   r   r	   
quote_namer4   r   r   rQ   _index_include_sql_deferrable_constraint_sqlr!   )	r>   ra   rI   rJ   rU   r9   rd   r    r"   s	    `       r   constraint_sqlz"ExclusionConstraint.constraint_sqli   s'   e...%%1I%JJ++M5AA$++HmUKK	
 
 
 
GK|
 
 
 M}788))$)44#{Hm.G  09@-)++b!44UGDD$??PP
 
 
 	
r   c                     |                      |           t          dt          |j        j        |j                  |                     ||                    S )Nz(ALTER TABLE %(table)s ADD %(constraint)s)rd   
constraint)check_supportedr   r	   r]   rf   rg   rj   r>   ra   rI   s      r   
create_sqlzExclusionConstraint.create_sql   sX    ]+++6,m.FGG**5-@@
 
 
 	
r   c                 j    |                     |j        ||                    | j                            S rP   )_delete_constraint_sqlsql_delete_checkrg   r4   rn   s      r   
remove_sqlzExclusionConstraint.remove_sql   s6    33*$$TY//
 
 	
r   c                     | 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   r5   rF   features supports_covering_spgist_indexesr   )r>   rI   s     r   rm   z#ExclusionConstraint.check_supported   se    L	%%''833!,5V 4 $*  	 	3333r   c                 @   t                                                      \  }}}| j        |d<   | j        
| j        |d<   | j                                        dk    r
| j        |d<   | j        r
| j        |d<   | j        r
| j        |d<   | j        r
| j        |d<   |||fS )Nr9   r    r&   r   r!   r"   r#   )	r<   deconstructr9   r    r   r5   r!   r"   r#   )r>   pathargskwargsr?   s       r   rx   zExclusionConstraint.deconstruct   s    "WW0022dF $ 0}>%"&.F;?  ""f,,#'?F< ? 	3#'?F< < 	- $F9> 	1"&.F;T6!!r   c                 p   t          || j                  r| j        |j        k    oo| j        |j        k    o_| j        |j        k    oO| j        |j        k    o?| j        |j        k    o/| j        |j        k    o| j        |j        k    o| j	        |j	        k    S t                                          |          S rP   )r*   r?   r4   r   r9   r    r!   r"   r#   r$   r<   __eq__)r>   otherr?   s     r   r}   zExclusionConstraint.__eq__   s    eT^,, 
		UZ' ROu'77R$(99R Neo5R Ou'77	R
 LEM1R Neo5R 0E4QQ	 ww~~e$$$r   c                 l   d| j         j        dt          | j                  dt          | j                  dt          | j                  | j        dn	d| j        z  | j        dn	d| j        z  | j        sdndt          | j                  z  | j	        sdnd	t          | j	                  z  d
S )N<z: index_type=z expressions=z name=rc   z condition=%sz deferrable=%rz include=%sz opclasses=%s>)
r?   r   reprr   r9   r4   r    r!   r"   r#   )r>   s    r   __repr__zExclusionConstraint.__repr__   s     N'''!!!!!""""OOO.(BBo.NN/)BB/?$//QQlJBBT\8J8J(JJnPBB/D<P<P*PPP	
 		
r   c                    |j                             |          }|                    |j        |          }d |                                D             }g }t          | j                  D ]*\  }	\  }
}t          |
t                    rt          |
          }
|r[t          |
t                    r|
j
        |v r d S n9|
                                D ]$}t          |t                    r|j
        |v r  d S %|
                    |          }t          |
t                    r|
                                d         }
t          |t                    r|                                d         }t          |
|          }||_        |                    |           , |j        | }|                    |j                  }|j        j        s||                    |          }| j        s7|                                r!t3          |                                           d S | j        t7          |                    | j                            z                      ||          r!t3          |                                           d S )N)metaexcludec                 4    i | ]\  }}t          |          |S r   )r   )r.   fieldvalues      r   
<dictcomp>z0ExclusionConstraint.validate.<locals>.<dictcomp>   s$    TTTLE5%%TTTr   r   )lhsrhs)pk)using)_default_managerr   _get_field_value_mapr]   itemsrB   r9   r*   rC   r   r4   flattenreplace_expressionsr   get_source_expressionsr   postgres_operatorrG   filter_get_pk_val_stateaddingr   r    existsr   get_violation_error_messager   check)r>   ra   instancer   r   querysetreplacement_mapreplacementslookupsrK   rL   rA   r/   rhs_expressionlookupmodel_class_pks                   r   validatezExclusionConstraint.validate   s   )//66"77g 8 
 
 UTO<Q<Q<S<STTT+4T5E+F+F 	# 	#'C'*h*c** +z]]
 #j!,, #!'11 2 !+ 2 2 4 4 # #%dA.. #493G3G"FFF';;LIIN *g.. D'>>@@C
.'22 L!/!F!F!H!H!K+
OOOF'/F$NN6"""""8?G,!--ek::% 	;.*D''>'::H~ 	J   J%d&F&F&H&HIIIJ J (G(G!H!HHOOu P   J &d&F&F&H&HIIIJ Jr   )r   r   r   r   r=   rM   rY   rj   ro   rs   rm   rx   r}   r   r   r   __classcell__)r?   s   @r   r   r      s-       	>   $7U 7U 7U 7U 7U 7U 7UrF F FI I I
 
 
,
 
 

 
 
	 	 	" " " " "% % % % %

 

 

 15<L 'J 'J 'J 'J 'J 'J 'J 'Jr   )r:   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   django.utils.deprecationr   __all__r   r   r   r   r   <module>r      sf    3 3 3 3 3 3 2 2 2 2 2 2 9 9 9 9 9 9 9 9 K K K K K K K K K K = = = = = = = = = = = = ? ? ? ? ? ? ? ? 4 4 4 4 4 4 ; ; ; ; ; ; & & & & & & = = = = = = 
!3 3 3 3 3O 3 3 3TJ TJ TJ TJ TJ. TJ TJ TJ TJ TJr   