U
    }®c,  ã                   @   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   st  |r|st dƒ‚t|td ƒtfƒs*t dƒ‚|r:|s:t dƒ‚t|ttfƒsPt dƒ‚t|ttfƒsft dƒ‚|sv|svt dƒ‚|r†|r†t dƒ‚|r–|s–t dƒ‚|r¦|r¦t d	ƒ‚|rÂt|ƒt|ƒkrÂt d
ƒ‚|ràtdd„ |D ƒƒsàt dƒ‚|rð|sðt dƒ‚t|td ƒttfƒ�st dƒ‚t|ƒ| _dd„ | jD ƒ| _	|�p2d| _
|| _|| _|| _|�rVt|ƒ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   ú</tmp/pip-unpacked-wheel-7m9rsep5/django/db/models/indexes.pyÚ	<genexpr>7   s     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 )ú-é   NZ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   r/   r   r   r   Ú__init__   sX    
ÿÿÿ
þþzIndex.__init__c                 C   s
   t | jƒS r   )Úboolr/   ©r0   r   r   r   Úcontains_expressionsM   s    zIndex.contains_expressionsc                    s`   | j d k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   )Zquote_value)r   Úp©Úschema_editorr   r   r   X   s     z+Index._get_condition_sql.<locals>.<genexpr>)r   r   Zbuild_whereZget_compilerr7   Úas_sqlr+   )r0   r5   r:   ÚqueryÚwhereÚcompilerZsqlÚparamsr   r9   r   Ú_get_condition_sqlQ   s    
zIndex._get_condition_sqlr!   c                    sÈ   ‡ fdd„| j D ƒ}|  ˆ |¡}| jrtg }| jD ]"}t|ƒ}	|	 |j¡ | |	¡ q0t|Ž  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#   ©r5   r   r   r%   [   s     z$Index.create_sql.<locals>.<listcomp>F)r6   c                    s   g | ]\}}ˆ j  |¡‘qS r   )rB   rC   )r   r$   Ú_rE   r   r   r%   i   s   ÿc                 S   s   g | ]}|d  ‘qS )r    r   )r   Úorderr   r   r   r%   m   s     )	r   r   Úusingr   Úcol_suffixesr   r   r   r/   )r   r@   r/   ÚIndexExpressionÚset_wrapper_classesr7   Úappendr   Úresolve_expressionr   r.   Z_create_index_sqlr   r   r   )r0   r5   r:   rH   Úkwargsr   r   Úindex_expressionsr'   Zindex_expressionr/   r   rI   r   rE   r   Ú
create_sqlZ   sB    

ÿ
þÿ     üüzIndex.create_sqlc                 K   s   |j || jf|ŽS r   )Z_delete_index_sqlr   )r0   r5   r:   rN   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 k	rN| j|d< | jr^| j|d< | jrn| 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   r/   )r0   ÚpathrN   r   r   r   Údeconstructy   s    






zIndex.deconstructc                 C   s   |   ¡ \}}}| j||ŽS )zCreate a copy of this Index.)rW   rR   )r0   rF   ÚargsrN   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sœt
dƒ‚| jd dks¸| jd  ¡ rÌd| jdd…  | _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   rA   )r   r$   rG   rE   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   Zcolumn_namer$   rG   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?rF   zD%sr    )r   rB   Zdb_tabler.   ÚzipÚsuffixr   r   r,   Úmax_name_lengthÚAssertionErrorÚisdigit)r0   r5   rF   Z
table_nameZcolumn_namesZcolumn_names_with_orderZ	hash_datar   rE   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 krPdnd| j | jsbdnd	d | j¡ | jszdnd
d | j¡ f S )Nz<%s:%s%s%s%s%s>r!   z fields='%s'ú, z expressions='%s'c                 S   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')rR   rT   r   Újoinr/   r   r   r   r3   r   r   r   Ú__repr__«   s    ÿøzIndex.__repr__c                 C   s    | j |j kr|  ¡ | ¡ kS tS r   )rR   rW   ÚNotImplemented)r0   Úotherr   r   r   Ú__eq__·   s    zIndex.__eq__)r!   )rT   rS   Ú__qualname__r_   r`   r1   Úpropertyr4   r@   rP   rQ   rW   rY   rc   rf   ri   r   r   r   r   r      s&   ø<
	
c                       sL   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 )rJ   z7Order and wrap expressions for CREATE INDEX statements.z%(expressions)sNc                 C   s&   |r"|j jr"tdd„ | jD ƒƒ| _d S )Nc                 S   s   g | ]}|t k	r|‘qS r   r	   ©r   Zwrapper_clsr   r   r   r%   Å   s   þz7IndexExpression.set_wrapper_classes.<locals>.<listcomp>)ÚfeaturesZcollate_as_index_expressionr+   Úwrapper_classes)r0   r7   r   r   r   rK   Â   s    þz#IndexExpression.set_wrapper_classesc                 G   s
   || _ d S r   )rn   )Úclsrn   r   r   r   Úregister_wrappersË   s    z!IndexExpression.register_wrappersTFc                    sb  t ˆ  ¡ ƒ}t‡ fdd„|ƒ\}}dd„ |D ƒ}	t|	ƒtt|	ƒƒkrbtdd dd„ ˆ jD ƒ¡ ƒ‚|dt|ƒd … |kr˜td	d d
d„ ˆ jD ƒ¡ ƒ‚|d }
|
 |||||¡}t	|t
ƒsÈt|
dd�}
|�rBt|‡ 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   rn   )Úer3   r   r   Ú<lambda>Ú   ó    z4IndexExpression.resolve_expression.<locals>.<lambda>c                 S   s   g | ]}t |ƒ‘qS r   )r)   ©r   Úwrapperr   r   r   r%   Ý   s     z6IndexExpression.resolve_expression.<locals>.<listcomp>zAMultiple references to %s can't be used in an indexed expression.rd   c                 S   s   g | ]
}|j ‘qS r   ©rj   rl   r   r   r   r%   á   s    r    z8%s must be topmost expressions in an indexed expression.c                 S   s   g | ]
}|j ‘qS r   rv   rl   r   r   r   r%   è   s    z(%(expressions)s))Útemplatec                    s   ˆ j  t| ƒ¡S r   )rn   Úindexr)   )Úwr3   r   r   rr   ü   rs   )Úkeyc                 S   s   g | ]}|  ¡ ‘qS r   )Úcopyrt   r   r   r   r%   þ   s     éÿÿÿÿr   )r*   Úflattenr   r,   Úsetr(   re   rn   rM   r   r   r   ÚsortedÚ	enumerateZset_source_expressionsÚsuper)r0   r<   Zallow_joinsZreuseZ	summarizeZfor_saver/   rO   ÚwrappersZwrapper_typesZroot_expressionZresolve_root_expressionÚiru   ©rR   r3   r   rM   Ï   sX    
þ
ÿÿÿ
ÿÿÿû

þz"IndexExpression.resolve_expressionc                 K   s   | j ||f|ŽS r   )r;   )r0   r>   r7   Zextra_contextr   r   r   Ú	as_sqlite	  s    zIndexExpression.as_sqlite)N)NTNFF)rT   rS   rj   Ú__doc__rw   r   r
   rn   rK   Úclassmethodrp   rM   r…   Ú__classcell__r   r   r„   r   rJ   ½   s   
	
     ú:rJ   N)Zdjango.db.backends.utilsr   r   Zdjango.db.models.expressionsr   r   r   r   r   Zdjango.db.models.functionsr
   Zdjango.db.models.query_utilsr   Zdjango.db.models.sqlr   Zdjango.utils.functionalr   Ú__all__r   rJ   r   r   r   r   Ú<module>   s    3