o
    þÞhŸ.  ã                   @   s�   d dl mZ 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 )é    )ÚNoneType)Ú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   sf  |r|st dƒ‚t|ttfƒst dƒ‚|r|st dƒ‚t|ttfƒs&t dƒ‚t|ttfƒs1t dƒ‚|s9|s9t dƒ‚|rA|rAt dƒ‚|rI|sIt dƒ‚|rQ|rQt d	ƒ‚|r_t|ƒt|ƒkr_t d
ƒ‚|rntdd„ |D ƒƒsnt dƒ‚|rv|svt dƒ‚t|tttfƒs‚t dƒ‚t|ƒ| _dd„ | jD ƒ| _	|p“d| _
|| _|| _|| _|r¤t|ƒ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   úX/var/www/html/premium_crap/venv/lib/python3.10/site-packages/django/db/models/indexes.pyÚ	<genexpr>;   ó   € 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 ¡| d ¡rdndf‘qS )ú-ÚDESCÚ )ÚremoveprefixÚ
startswith©r   Ú
field_namer   r   r   Ú
<listcomp>C   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   L   s
   € ÿ
ÿ)Ú
ValueErrorr   r   r   ÚlistÚtupleÚlenÚallr   Úfields_ordersr   r   r   r   r   Úexpressions)Úselfr   r   r   r   r   r   r0   r   r   r   Ú__init__   s\   
ÿÿÿÿ
þ
þzIndex.__init__c                 C   s
   t | jƒS r   )Úboolr0   ©r1   r   r   r   Úcontains_expressionsQ   ó   
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   \   r    z+Index._get_condition_sql.<locals>.<genexpr>)r   r   Úbuild_whereÚget_compilerr9   Úas_sqlr,   )r1   r7   r=   ÚqueryÚwhereÚcompilerÚsqlÚparamsr   r<   r   Ú_get_condition_sqlU   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 ƒ}|jjjrRdd„ | j
D ƒ}ndgt| j
ƒ }d }
|jˆ f|| j|| j|| j|||
dœ	|¤ŽS )	Nc                    s   g | ]	}ˆ j  |¡j‘qS r   ©Ú_metaÚ	get_fieldÚcolumnr&   ©r7   r   r   r(   _   s    ÿz$Index.create_sql.<locals>.<listcomp>F)r8   c                    s   g | ]
\}}ˆ j  |¡‘qS r   )rH   rI   )r   r'   Ú_rK   r   r   r(   o   s    
ÿÿc                 S   s   g | ]}|d  ‘qS )é   r   )r   Úorderr   r   r   r(   t   ó    r#   )	r   r   Úusingr   Úcol_suffixesr   r   r   r0   )r   rF   r0   ÚIndexExpressionÚset_wrapper_classesr9   Úappendr   Úresolve_expressionr   r/   ÚfeaturesÚsupports_index_column_orderingr-   Ú_create_index_sqlr   r   r   )r1   r7   r=   rP   Úkwargsr   r   Úindex_expressionsr)   Úindex_expressionr0   r   rQ   r   rK   r   Ú
create_sql^   sJ   
ÿ

ÿ
þ
ÿöõzIndex.create_sqlc                 K   s   |j || jfi |¤ŽS r   )Ú_delete_index_sqlr   )r1   r7   r=   rY   r   r   r   Ú
remove_sql†   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   r0   )r1   ÚpathrY   r   r   r   Údeconstruct‰   s   






zIndex.deconstructc                 C   s   |   ¡ \}}}| j|i |¤ŽS )zCreate a copy of this Index.)rd   r_   )r1   rL   ÚargsrY   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rNt
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   rG   )r   r'   rN   rK   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?rL   zD%srM   )r   rH   Údb_tabler/   ÚzipÚsuffixr   r   r-   Úmax_name_lengthr*   Úisdigit)r1   r7   rL   Ú
table_nameÚcolumn_namesÚcolumn_names_with_orderÚ	hash_datar   rK   r   Úset_name_with_modelž   s,   
þÿþ
ýÿÿzIndex.set_name_with_modelc                 C   s°   d| j j| js	dndt| jƒ | jsdndt| jƒ | jsdndt| jƒ | jd u r,dndt| jƒ | jd u r9dnd| j | jsBdndt| jƒ | j	sOdf S d	t| j	ƒ f S )
Nz<%s:%s%s%s%s%s%s%s>r#   z
 fields=%sz expressions=%sz name=%sz db_tablespace=%sz condition=%sz include=%sz opclasses=%s)
r_   Ú__qualname__r   Úreprr0   r   r   r   r   r   r4   r   r   r   Ú__repr__Á   s   
ÿôôzIndex.__repr__c                 C   s    | j |j kr|  ¡ | ¡ kS tS r   )r_   rd   ÚNotImplemented)r1   Úotherr   r   r   Ú__eq__Ñ   s   zIndex.__eq__)r#   )ra   r`   rv   rn   ro   r2   Úpropertyr5   rF   r\   r^   rd   rf   ru   rx   r{   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>)rV   Úcollate_as_index_expressionr,   Úwrapper_classes)r1   r9   r   r   r   rS   Ý   s   þ
ÿÿz#IndexExpression.set_wrapper_classesc                 G   s
   || _ d S r   )r€   )Úclsr€   r   r   r   Úregister_wrappersè   r6   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�}
|rŸt|‡ 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€   )Úer4   r   r   Ú<lambda>÷   s    z4IndexExpression.resolve_expression.<locals>.<lambda>c                 S   s   g | ]}t |ƒ‘qS r   )Útype©r   Úwrapperr   r   r   r(   ú   rO   z6IndexExpression.resolve_expression.<locals>.<listcomp>zAMultiple references to %s can't be used in an indexed expression.z, c                 S   ó   g | ]}|j ‘qS r   ©rv   r}   r   r   r   r(      ó    rM   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…   )Úwr4   r   r   r„     s    )Úkeyc                 S   s   g | ]}|  ¡ ‘qS r   )Úcopyr†   r   r   r   r(     rO   éÿÿÿÿr   )r+   Úflattenr   r-   Úsetr*   Újoinr€   rU   r   r   r   ÚsortedÚ	enumerateÚset_source_expressionsÚsuper)r1   rA   Úallow_joinsÚreuseÚ	summarizeÚfor_saver0   rZ   ÚwrappersÚwrapper_typesÚroot_expressionÚresolve_root_expressionÚir‡   ©r_   r4   r   rU   ì   s\   
þÿþÿÿÿÿû

þ
ÿz"IndexExpression.resolve_expressionc                 K   s   | j ||fi |¤ŽS r   )r@   )r1   rC   r9   Úextra_contextr   r   r   Ú	as_sqlite)  s   zIndexExpression.as_sqliter   )NTNFF)ra   r`   rv   Ú__doc__r‹   r	   r   r€   rS   Úclassmethodr‚   rU   r£   Ú__classcell__r   r   r¡   r   rR   ×   s    

ú=rR   N)Útypesr   Ú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     K