
v^E                 @   sV   d  d l  m Z m Z d  d l m Z d  d l m Z d g Z Gd d   d  Z d S)    )names_digestsplit_identifier)Q)QueryIndexc               @   s   e  Z d  Z d Z d Z d f  d d d 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   fieldsnameNdb_tablespace	opclasses	conditionc            C   s-  | r | r t  d   t | t d   t f  s@ t  d   | rY | rY t  d   t | t t f  sz t  d   t | t t f  s t  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   sA   g  |  ]7 } | j  d   r1 | d d  d f n	 | d f  q S)-   NZDESC )
startswith).0
field_name r   :/tmp/pip-build-8lau8j11/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  St d |  } | j |  j   } | j d   j  } | 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                s     f d d   |  j  D } d d   |  j  D } |  j   |  } | j   | d |  j d | d |  j d | d |  j d	 | | S)
Nc                s(   g  |  ] \ } }   j  j |   q Sr   )_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 |  S)N)Z_delete_index_sqlr
   )r   r    r#   r0   r   r   r   
remove_sql;   s    zIndex.remove_sqlc             C   s   d |  j  j |  j  j f } | j d d  } d |  j d |  j i } |  j d  k	 rb |  j | d <|  j rx |  j | d <|  j r |  j | d <| f  | f S)	Nz%s.%szdjango.db.models.indexeszdjango.db.modelsr	   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 |   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  |  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 Sr   )r*   r+   column)r   r   r-   )r    r   r   r   X   s   	 z-Index.set_name_with_model.<locals>.<listcomp>c             S   s2   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 r0 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__)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   	