3
v^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|rt|t|krt d|st dt|| _dd	 | jD | _|pd
| _	|| _
|| _|| _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 )-   NDESC )
startswith).0
field_name r   </usr/lib/python3.6/site-packages/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)quote_value)r   p)schema_editorr   r   	<genexpr>/   s    z+Index._get_condition_sql.<locals>.<genexpr>)r   r   build_whereget_compilerr"   as_sqlr   )r   r!   r%   querywherecompiler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%   r4   kwargsr	   r5   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%   r6   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   pathr6   r   r   r   deconstruct>   s    



zIndex.deconstructc             C   s   | j  \}}}| jf |S )zCreate a copy of this Index.)r>   r9   )r   r2   r6   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st
d| jd dks| jd j rd| 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   )r0   r1   column)r   r   r3   )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   column_namer   r3   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?r2   zD%sr   )r   r0   db_tabler   zipsuffixr   r
   r   max_name_lengthAssertionErrorisdigit)r   r!   r2   
table_name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)r9   r;   joinr	   r   )r   r   r   r   __repr__l   s    zIndex.__repr__c             C   s   | j |j ko| j |j kS )N)r9   r>   )r   otherr   r   r   __eq__r   s    zIndex.__eq__)r   )r;   r:   __qualname__rH   rI   r    r/   r7   r8   r>   r?   rN   rP   rR   r   r   r   r   r      s   	

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