o
    uvXjÑ  ã                   @   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                   @   st   e Zd ZdZdZddddddœdd„Z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|st dƒ‚t|td ƒtfƒst dƒ‚|r|st dƒ‚t|ttfƒs(t dƒ‚t|ttfƒs3t dƒ‚|rAt|ƒt|ƒkrAt dƒ‚|sGt dƒ‚t|ƒ| _dd	„ | jD ƒ| _|pXd
| _	| j	rw|  
¡ }t| j	ƒ| jkrq| d| j ¡ |rwt |ƒ‚|| _|| _|| _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 | ]}|  d ¡r|dd… dfn|df‘qS )ú-é   NÚDESCÚ )Ú
startswith)Ú.0Ú
field_namer	   r	   úU/var/www/html/myproject/venv/lib/python3.10/site-packages/django/db/models/indexes.pyÚ
<listcomp>   s     ÿÿz"Index.__init__.<locals>.<listcomp>r   z0Index names cannot be longer than %s characters.)Ú
ValueErrorÚ
isinstanceÚtyper   ÚlistÚtupleÚlenr
   Úfields_ordersr   Ú
check_nameÚmax_name_lengthÚappendr   r   r   )Úselfr
   r   r   r   r   Úerrorsr	   r	   r   Ú__init__   s8   
þ

zIndex.__init__c                 C   sd   g }| j d dkr| d¡ d| j dd …  | _ |S | j d  ¡ r0| d¡ d| j dd …  | _ |S )Nr   Ú_z0Index names cannot start with an underscore (_).zD%sr   z-Index names cannot start with a number (0-9).)r   r!   Úisdigit)r"   r#   r	   r	   r   r   .   s   
ý
zIndex.check_namec                 C   s\   | j d u rd S t|d�}| | j ¡ |j|jd�}|jj||jd�\}}|tt|j	|ƒƒ S )N©Úmodel)Ú
connection)Úcompilerr)   )
r   r   Úadd_qÚget_compilerr)   ÚwhereÚas_sqlr   ÚmapÚquote_value)r"   r(   Úschema_editorÚqueryr*   ÚsqlÚparamsr	   r	   r   Ú_get_condition_sql:   s   

zIndex._get_condition_sqlr   c              
      sP   ‡ fdd„| j D ƒ}dd„ | j D ƒ}|  ˆ |¡}|jˆ || j|| j|| j|d�S )Nc                    s   g | ]
\}}ˆ j  |¡‘qS r	   )Ú_metaÚ	get_field)r   r   r%   r'   r	   r   r   G   s    z$Index.create_sql.<locals>.<listcomp>c                 S   s   g | ]}|d  ‘qS )r   r	   )r   Úorderr	   r	   r   r   H   s    )r   Úusingr   Úcol_suffixesr   r   )r   r5   Ú_create_index_sqlr   r   r   )r"   r(   r1   r9   r
   r:   r   r	   r'   r   Ú
create_sqlF   s   þzIndex.create_sqlc                 C   s   |  || j¡S ©N)Ú_delete_index_sqlr   )r"   r(   r1   r	   r	   r   Ú
remove_sqlO   s   zIndex.remove_sqlc                 C   sl   d| j j| j jf }| dd¡}| j| jdœ}| jd ur!| j|d< | jr)| j|d< | jr1| j|d< |d|fS )	Nz%s.%szdjango.db.models.indexeszdjango.db.models)r
   r   r   r   r   r	   )	Ú	__class__Ú
__module__Ú__name__Úreplacer
   r   r   r   r   )r"   ÚpathÚkwargsr	   r	   r   ÚdeconstructR   s   




zIndex.deconstructc                 C   s   |   ¡ \}}}| jdi |¤ŽS )zCreate a copy of this Index.Nr	   )rF   r@   )r"   r%   rE   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sNJ 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	   )r6   r7   Úcolumn)r   r   r8   r'   r	   r   r   l   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   r8   r	   r	   r   r   m   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   r6   Údb_tabler   ÚzipÚsuffixr   r   r   r    r   )r"   r(   r%   Ú
table_nameÚcolumn_namesÚcolumn_names_with_orderÚ	hash_datar	   r'   r   Úset_name_with_modelc   s   
þ
ýÿzIndex.set_name_with_modelc                 C   s2   d| j jd | j¡| jd u rdf S d| j f S )Nz<%s: fields='%s'%s>z, r   z, condition=%s)r@   rB   Újoinr
   r   )r"   r	   r	   r   Ú__repr__   s   þþzIndex.__repr__c                 C   s   | j |j ko|  ¡ | ¡ kS r=   )r@   rF   )r"   Úotherr	   r	   r   Ú__eq__…   s   zIndex.__eq__)r   )rB   rA   Ú__qualname__rP   r    r$   r   r5   r<   r?   rF   rG   rU   rW   rY   r	   r	   r	   r   r      s     
	N)	Údjango.db.backends.utilsr   r   Údjango.db.models.query_utilsr   Údjango.db.models.sqlr   Ú__all__r   r	   r	   r	   r   Ú<module>   s
    