o
    ^"<f,                     @   s   d dl mZmZ d dlmZ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gZG dd dZG d	d
 d
eZdS )    )names_digestsplit_identifier)ColExpressionListFFuncOrderByCollate)Q)Query)	partitionIndexc                   @   sz   e Zd ZdZdZdddddddddZedd	 Zd
d ZdddZ	dd Z
dd Zdd Zdd Zdd Zdd ZdS )r   idx    N)fieldsnamedb_tablespace	opclasses	conditionincludec                G   sn  |r|st dt|td tfst d|r|st dt|ttfs(t dt|ttfs3t d|s;|s;t d|rC|rCt d|rK|sKt d|rS|rSt d	|rat|t|krat d
|rptdd |D spt d|rx|sxt dt|td ttfst dt|| _dd | jD | _	|pd| _
|| _|| _|| _|rt|nd| _tdd |D | _d S )Nz(An index must be named to use opclasses.z%Index.condition must be a Q instance.z(An index must be named to use condition.z%Index.fields must be a list or tuple.z(Index.opclasses must be a list or tuple.z@At least one field or expression is required to define an index.z4Index.fields and expressions are mutually exclusive.z*An index must be named to use expressions.zgIndex.opclasses cannot be used with expressions. Use django.contrib.postgres.indexes.OpClass() instead.zGIndex.fields and Index.opclasses must have the same number of elements.c                 s   s    | ]}t |tV  qd S N)
isinstancestr).0fieldr   r   O/var/www/html/kck/venv/lib/python3.10/site-packages/django/db/models/indexes.py	<genexpr>7       z!Index.__init__.<locals>.<genexpr>z8Index.fields must contain only strings with field names.zA covering index must be named.z&Index.include must be a list or tuple.c                 S   s.   g | ]}| d r|dd dfn|dfqS )-   NDESC )
startswithr   
field_namer   r   r   
<listcomp>?   s     z"Index.__init__.<locals>.<listcomp>r#   r   c                 s   s&    | ]}t |trt|n|V  qd S r   )r   r   r   r   
expressionr   r   r   r   H   s
    
)
ValueErrorr   typer   listtuplelenallr   fields_ordersr   r   r   r   r   expressions)selfr   r   r   r   r   r   r1   r   r   r   __init__   sX   


zIndex.__init__c                 C   s
   t | jS r   )boolr1   r2   r   r   r   contains_expressionsM      
zIndex.contains_expressionsc                    s`   | j d u rd S t|dd}|| j }|j jd}|| j\}}|t fdd|D  S )NF)model
alias_cols)
connectionc                 3   s    | ]}  |V  qd S r   )quote_value)r   pschema_editorr   r   r   X   r   z+Index._get_condition_sql.<locals>.<genexpr>)r   r   build_whereget_compilerr:   as_sqlr-   )r2   r8   r>   querywherecompilersqlparamsr   r=   r   _get_condition_sqlQ   s   
zIndex._get_condition_sqlr#   c                    s    fdd| j D }|  |}| jr:g }| jD ]}t|}	|	|j ||	 qt| t	 dd}
d }d }n fdd| j
D }dd | j
D }d }
|j f|| j|| j|| j|||
d	|S )Nc                    s   g | ]	} j |jqS r   _meta	get_fieldcolumnr%   r8   r   r   r'   [   s    z$Index.create_sql.<locals>.<listcomp>F)r9   c                    s   g | ]
\}} j |qS r   )rI   rJ   )r   r&   _rL   r   r   r'   i   s    
c                 S   s   g | ]}|d  qS )r!   r   )r   orderr   r   r   r'   m       )	r   r   usingr   col_suffixesr   r   r   r1   )r   rG   r1   IndexExpressionset_wrapper_classesr:   appendr   resolve_expressionr   r0   _create_index_sqlr   r   r   )r2   r8   r>   rP   kwargsr   r   index_expressionsr)   index_expressionr1   r   rQ   r   rL   r   
create_sqlZ   s8   


zIndex.create_sqlc                 K   s   |j || jfi |S r   )_delete_index_sqlr   )r2   r8   r>   rW   r   r   r   
remove_sqlv   s   zIndex.remove_sqlc                 C   s   d| j j| j jf }|dd}d| ji}| jr| j|d< | jd ur'| j|d< | jr/| j|d< | jr7| j|d< | j	r?| j	|d	< || j
|fS )
Nz%s.%szdjango.db.models.indexeszdjango.db.modelsr   r   r   r   r   r   )	__class__
__module____name__replacer   r   r   r   r   r   r1   )r2   pathrW   r   r   r   deconstructy   s   






zIndex.deconstructc                 C   s   |   \}}}| j|i |S )zCreate a copy of this Index.)rb   r]   )r2   rM   argsrW   r   r   r   clone   s   zIndex.clonec                    s   t  jj\}} fdd| jD }dd t|| jD }|g| | jg }d|dd |d dd d	t|d
di| jf f | _t| j| j	ksNJ d| jd dks\| jd 
 rhd| jdd  | _dS dS )a  
        Generate a unique name for the index.

        The name is divided into 3 parts - table name (12 chars), field name
        (8 chars) and unique hash + suffix (10 chars). Each part is made to
        fit its size by truncating the excess length.
        c                    s   g | ]\}} j |jqS r   rH   )r   r&   rN   rL   r   r   r'      s    z-Index.set_name_with_model.<locals>.<listcomp>c                 S   s$   g | ]\}\}}|rd nd| qS )z-%sz%sr   )r   column_namer&   rN   r   r   r   r'      s    
z%s_%s_%sN   r      z%s_%slength   zVIndex too long for multiple database support. Is self.suffix longer than 3 characters?rM   zD%sr!   )r   rI   db_tabler0   zipsuffixr   r   r.   max_name_lengthisdigit)r2   r8   rM   
table_namecolumn_namescolumn_names_with_order	hash_datar   rL   r   set_name_with_model   s"   

zIndex.set_name_with_modelc              
   C   s   d| j j| js	dndd| j | jsdndddd | jD  | jd u r(dnd| j | js1dnd	d| j | js?df S d
d| j f S )Nz<%s:%s%s%s%s%s>r#   z fields='%s', z expressions='%s'c                 S      g | ]}t |qS r   )r   r(   r   r   r   r'      s    z"Index.__repr__.<locals>.<listcomp>z condition=%sz include='%s'z opclasses='%s')r]   r_   r   joinr1   r   r   r   r5   r   r   r   __repr__   s   zIndex.__repr__c                 C   s    | j |j kr|  | kS tS r   )r]   rb   NotImplemented)r2   otherr   r   r   __eq__   s   zIndex.__eq__)r#   )r_   r^   __qualname__rl   rm   r3   propertyr6   rG   rZ   r\   rb   rd   rs   rw   rz   r   r   r   r   r      s(    <

	c                       sV   e Zd ZdZdZeefZdddZe	dd Z
							d fd
d	Zdd Z  ZS )rR   z7Order and wrap expressions for CREATE INDEX statements.z%(expressions)sNc                 C   s.   |r|j jrtdd | jD | _d S d S d S )Nc                 S   s   g | ]}|t ur|qS r   r	   r   wrapper_clsr   r   r   r'      s
    z7IndexExpression.set_wrapper_classes.<locals>.<listcomp>)featurescollate_as_index_expressionr-   wrapper_classes)r2   r:   r   r   r   rS      s
   z#IndexExpression.set_wrapper_classesc                 G   s
   || _ d S r   )r   )clsr   r   r   r   register_wrappers   r7   z!IndexExpression.register_wrappersTFc                    s^  t   }t fdd|\}}dd |D }	t|	tt|	kr1tdddd  jD  |dt|d  |krLtd	dd
d  jD  |d }
|
|||||}t	|t
sdt|
dd}
|rt| fddd}dd |D }t|d d D ]\}}|||d  g q|d |
g  |d g n |
g t |||||S )Nc                    s   t |  jS r   )r   r   )er5   r   r   <lambda>   s    z4IndexExpression.resolve_expression.<locals>.<lambda>c                 S   ru   r   )r+   r   wrapperr   r   r   r'      rO   z6IndexExpression.resolve_expression.<locals>.<listcomp>zAMultiple references to %s can't be used in an indexed expression.rt   c                 S      g | ]}|j qS r   r{   r}   r   r   r   r'          r!   z8%s must be topmost expressions in an indexed expression.c                 S   r   r   r   r}   r   r   r   r'      r   z(%(expressions)s))templatec                    s    j t| S r   )r   indexr+   )wr5   r   r   r      s    )keyc                 S   s   g | ]}|  qS r   )copyr   r   r   r   r'      rO   r   )r,   flattenr   r.   setr*   rv   r   rU   r   r   r   sorted	enumerateset_source_expressionssuper)r2   rB   allow_joinsreuse	summarizefor_saver1   rX   wrapperswrapper_typesroot_expressionresolve_root_expressionir   r]   r5   r   rU      sX   




z"IndexExpression.resolve_expressionc                 K   s   | j ||fi |S r   )rA   )r2   rD   r:   extra_contextr   r   r   	as_sqlite	  s   zIndexExpression.as_sqliter   )NTNFF)r_   r^   r{   __doc__r   r   r
   r   rS   classmethodr   rU   r   __classcell__r   r   r   r   rR      s    
	
:rR   N)django.db.backends.utilsr   r   django.db.models.expressionsr   r   r   r   r   django.db.models.functionsr
   django.db.models.query_utilsr   django.db.models.sqlr   django.utils.functionalr   __all__r   rR   r   r   r   r   <module>   s     3