B
    CVvg,  ã               @   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-install-o3oxmrkh/Django/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 )N)r   r   r   )r   Ú
expressionr   r   r   r   I   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__   sP    

zIndex.__init__c             C   s
   t | jƒS )N)Úboolr+   )r,   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 )N)Zquote_value)r   Úp)Úschema_editorr   r   r   X   s    z+Index._get_condition_sql.<locals>.<genexpr>)r   r   Zbuild_whereZget_compilerr2   Úas_sqlr'   )r,   r0   r4   ÚqueryÚwhereÚcompilerZsqlÚparamsr   )r4   r   Ú_get_condition_sqlQ   s    
zIndex._get_condition_sqlr   c                sÌ   ‡ fdd„| j D ƒ}|  ˆ |¡}| jrxg }x,| jD ]"}t|ƒ}	|	 |j¡ | |	¡ q2W 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Úcolumn)r   r!   )r0   r   r   r"   [   s    z$Index.create_sql.<locals>.<listcomp>F)r1   c                s   g | ]\}}ˆ j  |¡‘qS r   )r;   r<   )r   r!   Ú_)r0   r   r   r"   j   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_classesr2   Úappendr   Úresolve_expressionr   r*   Z_create_index_sqlr   r   r   )r,   r0   r4   r@   Úkwargsr   r   Úindex_expressionsr#   Zindex_expressionr+   r   rA   r   )r0   r   Ú
create_sqlZ   s*    

zIndex.create_sqlc             K   s   |j || jf|ŽS )N)Z_delete_index_sqlr   )r,   r0   r4   rF   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+   )r,   ÚpathrF   r   r   r   Údeconstructy   s    






zIndex.deconstructc             C   s   |   ¡ \}}}| j||ŽS )zCreate a copy of this Index.)rO   rJ   )r,   r>   ÚargsrF   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   )r;   r<   r=   )r   r!   r?   )r0   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!   r?   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?r>   zD%sr   )r   r;   Zdb_tabler*   ÚzipÚsuffixr   r   r(   Úmax_name_lengthÚAssertionErrorÚisdigit)r,   r0   r>   Z
table_nameZcolumn_namesZcolumn_names_with_orderZ	hash_datar   )r0   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, z expressions='%s'c             S   s   g | ]}t |ƒ‘qS r   )r   )r   r#   r   r   r   r"   °   s    z"Index.__repr__.<locals>.<listcomp>z condition=%sz include='%s'z opclasses='%s')rJ   rL   r   Újoinr+   r   r   r   )r,   r   r   r   Ú__repr__«   s    zIndex.__repr__c             C   s    | j |j kr|  ¡ | ¡ kS tS )N)rJ   rO   ÚNotImplemented)r,   Úotherr   r   r   Ú__eq__·   s    zIndex.__eq__)r   )rL   rK   Ú__qualname__rW   rX   r-   Úpropertyr/   r:   rH   rI   rO   rQ   r[   r]   r`   r   r   r   r   r      s"   4	
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 )rB   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   Úwrapper_clsr   r   r   r"   Æ   s   z7IndexExpression.set_wrapper_classes.<locals>.<listcomp>)ÚfeaturesZcollate_as_index_expressionr'   Úwrapper_classes)r,   r2   r   r   r   rC   Â   s    z#IndexExpression.set_wrapper_classesc             G   s
   || _ d S )N)re   )Úclsre   r   r   r   Úregister_wrappersË   s    z!IndexExpression.register_wrappersTFc                sf  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�}
|�rFt|‡ fdd„d�}dd„ |D ƒ}x2t|d d… ƒD ]\}}| ||d  g¡ �qW |d  |
g¡ ˆ  |d g¡ nˆ  |
g¡ tƒ  |||||¡S )Nc                s   t | ˆ jƒS )N)r   re   )Úe)r,   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.z, c             S   s   g | ]
}|j ‘qS r   )ra   )r   rc   r   r   r   r"   â   s    r   z8%s must be topmost expressions in an indexed expression.c             S   s   g | ]
}|j ‘qS r   )ra   )r   rc   r   r   r   r"   é   s    z(%(expressions)s))Útemplatec                s   ˆ j  t| ƒ¡S )N)re   Úindexr%   )Úw)r,   r   r   ri   ü   rj   )Úkeyc             S   s   g | ]}|  ¡ ‘qS r   )Úcopy)r   rk   r   r   r   r"   þ   s    éÿÿÿÿr   )r&   Úflattenr   r(   Úsetr$   r\   re   rE   r   r   r   ÚsortedÚ	enumerateZset_source_expressionsÚsuper)r,   r6   Zallow_joinsZreuseZ	summarizeZfor_saver+   rG   ÚwrappersZwrapper_typesZroot_expressionZresolve_root_expressionÚirk   )rJ   )r,   r   rE   Ï   sF    


z"IndexExpression.resolve_expressionc             K   s   | j ||f|ŽS )N)r5   )r,   r8   r2   Zextra_contextr   r   r   Ú	as_sqlite	  s    zIndexExpression.as_sqlite)N)NTNFF)rL   rK   ra   Ú__doc__rl   r   r	   re   rC   Úclassmethodrg   rE   ry   Ú__classcell__r   r   )rJ   r   rB   ½   s   
	    4rB   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   rB   r   r   r   r   Ú<module>   s    3