3
W­E^E  ã               @   s@   d dl mZmZ d dlmZ d dlmZ dgZG dd„ dƒZdS )é    )Únames_digestÚsplit_identifier)ÚQ)ÚQueryÚIndexc               @   sl   e Zd ZdZdZf ddf ddœ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Ú	conditionc            C   sÎ   |r| rt dƒ‚t|td ƒtfƒs,t dƒ‚|r>| r>t dƒ‚t|ttfƒsTt dƒ‚t|ttfƒsjt dƒ‚|r†t|ƒt|ƒkr†t dƒ‚|s’t dƒ‚t|ƒ| _dd	„ | jD ƒ| _|p´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.zGIndex.fields and Index.opclasses must have the same number of elements.z2At least one field is required to define an index.c             S   s.   g | ]&}|j d ƒr"|dd… dfn|df‘qS )ú-é   NZDESCÚ )Ú
startswith)Ú.0Ú
field_name© r   ú:/tmp/pip-build-78s3knkw/Django/django/db/models/indexes.pyú
<listcomp>    s   z"Index.__init__.<locals>.<listcomp>r   )Ú
ValueErrorÚ
isinstanceÚtyper   ÚlistÚtupleÚlenr	   Úfields_ordersr
   r   r   r   )Úselfr	   r
   r   r   r   r   r   r   Ú__init__   s*    



zIndex.__init__c                s^   | j d krd S t|d�}|j| j ƒ}|jˆ jd�}|j|ˆ jƒ\}}|t‡ fdd„|D ƒƒ S )N)Úmodel)Ú
connectionc             3   s   | ]}ˆ j |ƒV  qd S )N)Zquote_value)r   Úp)Úschema_editorr   r   ú	<genexpr>/   s    z+Index._get_condition_sql.<locals>.<genexpr>)r   r   Zbuild_whereZget_compilerr!   Zas_sqlr   )r   r    r#   ÚqueryÚwhereÚcompilerZsqlÚparamsr   )r#   r   Ú_get_condition_sql(   s    

zIndex._get_condition_sqlr   c          	      sX   ‡ fdd„| j D ƒ}dd„ | j D ƒ}| jˆ |ƒ}|jˆ |f| j|| j|| j|dœ|—ŽS )Nc                s   g | ]\}}ˆ j j|ƒ‘qS r   )Ú_metaÚ	get_field)r   r   Ú_)r    r   r   r   2   s    z$Index.create_sql.<locals>.<listcomp>c             S   s   g | ]}|d  ‘qS )r   r   )r   Úorderr   r   r   r   3   s    )r
   Úusingr   Úcol_suffixesr   r   )r   r)   Z_create_index_sqlr
   r   r   )r   r    r#   r.   Úkwargsr	   r/   r   r   )r    r   Ú
create_sql1   s    zIndex.create_sqlc             K   s   |j || jf|ŽS )N)Z_delete_index_sqlr
   )r   r    r#   r0   r   r   r   Ú
remove_sql;   s    zIndex.remove_sqlc             C   sl   d| j j| j jf }|jddƒ}| j| jdœ}| jd k	rB| j|d< | jrR| j|d< | jrb| j|d< |f |fS )Nz%s.%szdjango.db.models.indexeszdjango.db.models)r	   r
   r   r   r   )	Ú	__class__Ú
__module__Ú__name__Úreplacer	   r
   r   r   r   )r   Úpathr0   r   r   r   Údeconstruct>   s    



zIndex.deconstructc             C   s   | j ƒ \}}}| jf |ŽS )zCreate a copy of this Index.)r8   r3   )r   r,   r0   r   r   r   ÚcloneJ   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 jƒ 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|ƒj‘qS r   )r*   r+   Úcolumn)r   r   r-   )r    r   r   r   X   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   Z   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   r    r,   Z
table_nameZcolumn_namesZcolumn_names_with_orderZ	hash_datar   )r    r   Úset_name_with_modelO   s    
zIndex.set_name_with_modelc             C   s.   d| j jdj| jƒ| jd kr dnd| j f S )Nz<%s: fields='%s'%s>z, r   z, condition=%s)r3   r5   Újoinr	   r   )r   r   r   r   Ú__repr__l   s    zIndex.__repr__c             C   s   | j |j ko| jƒ |jƒ kS )N)r3   r8   )r   Úotherr   r   r   Ú__eq__r   s    zIndex.__eq__)r   )r5   r4   Ú__qualname__r@   rA   r   r)   r1   r2   r8   r9   rD   rF   rH   r   r   r   r   r      s   	

N)	Zdjango.db.backends.utilsr   r   Zdjango.db.models.query_utilsr   Zdjango.db.models.sqlr   Ú__all__r   r   r   r   r   Ú<module>   s   