
    g.                         d dl mZ 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      Z G d
 de	      Zy)    )NoneType)names_digestsplit_identifier)ColExpressionListFFuncOrderBy)Collate)Q)Query)	partitionIndexc                   l    e Zd ZdZdZddddddddZed        Zd Zdd	Z	d
 Z
d Zd Zd Zd Zd Zy)r   idx    N)fieldsnamedb_tablespace	opclasses	conditionincludec                   |r|st        d      t        |t        t        f      st        d      |r|st        d      t        |t        t
        f      st        d      t        |t        t
        f      st        d      |s|st        d      |r|rt        d      |r|st        d      |r|rt        d	      |r"t        |      t        |      k7  rt        d
      |rt        d |D              st        d      |r|st        d      t        |t        t        t
        f      st        d      t	        |      | _        | j                  D cg c](  }|j                  d      |j                  d      rdndf* c}| _        |xs d| _        || _        || _        || _        |rt        |      nd| _        t        d |D              | _        y c c}w )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              3   <   K   | ]  }t        |t                y wN)
isinstancestr).0fields     p/var/www/python.vincentserveurtest.ovh/public_html/venv/lib/python3.12/site-packages/django/db/models/indexes.py	<genexpr>z!Index.__init__.<locals>.<genexpr>;   s     I&j4&s   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.-DESC r   c              3   V   K   | ]!  }t        |t              rt        |      n| # y wr   )r   r   r   )r   
expressions     r!   r"   z!Index.__init__.<locals>.<genexpr>L   s*      !
)
 (
C8AjMjH)s   '))
ValueErrorr   r   r   listtuplelenallr   removeprefix
startswithfields_ordersr   r   r   r   r   expressions)	selfr   r   r   r   r   r   r0   
field_names	            r!   __init__zIndex.__init__   s    TGHH)h]3DEETGHH&4-0DEE)dE]3GHH6R  6F  tIJJ9E  VI6  #I&IIWXX4>??'HdE#:;EFF6l #kk
)
 $$S)Z5J5J35O6UWX)
 JB	*"")0uW~b  !
)!
 

s   -Gc                 ,    t        | j                        S r   )boolr0   r1   s    r!   contains_expressionszIndex.contains_expressionsQ   s    D$$%%    c                    | j                   y t        |d      }|j                  | j                         }|j                  j                        }|j                  |j                        \  }}|t        fd|D              z  S )NF)model
alias_cols)
connectionc              3   @   K   | ]  }j                  |        y wr   )quote_value)r   pschema_editors     r!   r"   z+Index._get_condition_sql.<locals>.<genexpr>\   s     HA=44Q7s   )r   r   build_whereget_compilerr<   as_sqlr*   )r1   r:   r@   querywherecompilersqlparamss     `     r!   _get_condition_sqlzIndex._get_condition_sqlU   sy    >>!Ee4!!$..1%%1I1I%Jll8]-E-EFVUHHHHHr8   c                 L   | j                   D cg c]'  }|j                  j                  |      j                  ) }}| j	                  ||      }| j
                  rqg }| j
                  D ]9  }	t        |	      }
|
j                  |j                         |j                  |
       ; t        | j                  t        |d            }d }d }n| j                  D cg c]   \  }}|j                  j                  |      " }}}|j                  j                  j                  r| j                  D cg c]  }|d   	 }}ndgt!        | j                        z  }d } |j"                  |f|| j$                  || j&                  || j(                  |||d	|S c c}w c c}}w c c}w )NF)r;      r%   )	r   r   usingr   col_suffixesr   r   r   r0   )r   _meta	get_fieldcolumnrI   r0   IndexExpressionset_wrapper_classesr<   appendr   resolve_expressionr   r/   featuressupports_index_column_orderingr+   _create_index_sqlr   r   r   )r1   r:   r@   rL   kwargsr2   r   r   index_expressionsr'   index_expressionr0   r   rM   _orders                   r!   
create_sqlzIndex.create_sql^   s   GK||
GSEKK!!*-44| 	 
 ++E=A	 ""..
#2:#>  44]5M5MN!(()9: / )*;<OOe.K FL &*%7%7%7MJ %%j1%7   ''00OO6:6H6HI6HUa6HI "tc$*<*<&==K.}..
,,%nn#
 
 	
3
 
  Js   ,F%F1F!c                 >     |j                   || j                  fi |S r   )_delete_index_sqlr   )r1   r:   r@   rX   s       r!   
remove_sqlzIndex.remove_sql   s     .}..udiiJ6JJr8   c                    | j                   j                  d| j                   j                  }|j                  dd      }d| j                  i}| j
                  r| j
                  |d<   | j                  | j                  |d<   | j                  r| j                  |d<   | j                  r| j                  |d<   | j                  r| j                  |d	<   || j                  |fS )
N.zdjango.db.models.indexeszdjango.db.modelsr   r   r   r   r   r   )	__class__
__module____name__replacer   r   r   r   r   r   r0   )r1   pathrX   s      r!   deconstructzIndex.deconstruct   s    ..33T^^5L5LM||68JK$))$;;#{{F8)&*&8&8F?#>>"&..F;>>"&..F;<< $F9d&&//r8   c                 N    | j                         \  }}} | j                  |i |S )zCreate a copy of this Index.)rh   rc   )r1   r[   argsrX   s       r!   clonezIndex.clone   s,    **,4t~~t.v..r8   c           	         t        |j                  j                        \  }}| j                  D cg c]*  \  }}|j                  j	                  |      j
                  , }}}t        || j                        D cg c]  \  }\  }}|rdnd|z   }}}}|g|z   | j                  gz   }	|dd d|d   dd dt        |	dd	id| j                  | _	        t        | j                        | j                  kD  rt        d
      | j                  d   dk(  s| j                  d   j                         rd| j                  dd z  | _	        yyc c}}w c c}}}w )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.
        z-%sz%sN   r[   r      length   zVIndex too long for multiple database support. Is self.suffix longer than 3 characters?zD%srK   )r   rN   db_tabler/   rO   rP   zipsuffixr   r   r+   max_name_lengthr(   isdigit)
r1   r:   r[   
table_namer2   r\   column_namescolumn_namecolumn_names_with_order	hash_datas
             r!   set_name_with_modelzIndex.set_name_with_model   sk    ))=)=>: &*%7%7
%7!
E KK!!*-44%7 	 
 58d005#
500j% e$+55 	  #
  L#::dkk]J	sOOBQ#Y9q94;;G
	
 tyy>D000,  99Q<3$))A,"6"6"8		!"-DI #9/
#
s   /E>Ec                 @   d| j                   j                  d| j                  sdndt        | j                        z  | j                  sdndt        | j                        z  | j
                  sdndt        | j
                        z  | j                  dndt        | j                        z  | j                  dnd| j                  z  | j                  sdnd	t        | j                        z  | j                  sddS d
t        | j                        z  dS )N<:r%   z
 fields=%sz expressions=%sz name=%sz db_tablespace=%sz condition=%sz include=%sz opclasses=%s>)
rc   __qualname__r   reprr0   r   r   r   r   r   r6   s    r!   __repr__zIndex.__repr__   s   NN''kkB|d4;;6G'GG&&B,=TEUEU@V,VViiBZ$tyy/%AA %%- (40B0B+CCD ..(Bo.NNllBT\\8J(JJnnBP
 	
 +:D<P*PP
 	
r8   c                     | j                   |j                   k(  r!| j                         |j                         k(  S t        S r   )rc   rh   NotImplemented)r1   others     r!   __eq__zIndex.__eq__   s5    >>U__,##%):):)<<<r8   )r%   )re   rd   r   rs   rt   r3   propertyr7   rI   r]   r`   rh   rk   r{   r   r   r   r8   r!   r   r      sh    F O
 <
| & &I&
PK0 /
!.F
 r8   c                   X     e Zd ZdZdZeefZddZe	d        Z
	 	 	 	 	 d fd	Zd Z xZS )	rQ   z7Order and wrap expressions for CREATE INDEX statements.z%(expressions)sc                     |rH|j                   j                  r1t        | j                  D cg c]  }|t        ur| c}      | _        y y y c c}w r   )rU   collate_as_index_expressionr*   wrapper_classesr   )r1   r<   wrapper_clss      r!   rR   z#IndexExpression.set_wrapper_classes   sX    *--II#( (,';';';"'1  ';$D  J:s   Ac                     || _         y r   )r   )clsr   s     r!   register_wrappersz!IndexExpression.register_wrappers   s
    -r8   c           	          t         j                               }t         fd|      \  }}|D 	cg c]  }	t        |	       }
}	t	        |
      t	        t        |
            k7  r?t        ddj                   j                  D cg c]  }|j                   c}      z        |dt	        |      dz    |k7  r?t        ddj                   j                  D cg c]  }|j                   c}      z        |d   }|j                  |||||      }t        |t              st        |d      }|rt        | fd	      }|D 	cg c]  }	|	j                          }}	t!        |d d
       D ]  \  }}	|	j#                  ||dz      g        |d
   j#                  |g        j#                  |d   g       n j#                  |g       t$         )  |||||      S c c}	w c c}w c c}w c c}	w )Nc                 0    t        | j                        S r   )r   r   )er1   s    r!   <lambda>z4IndexExpression.resolve_expression.<locals>.<lambda>   s    jD$8$89r8   zAMultiple references to %s can't be used in an indexed expression.z, rK   z8%s must be topmost expressions in an indexed expression.z(%(expressions)s))templatec                 L    j                   j                  t        |             S r   )r   indextype)wr1   s    r!   r   z4IndexExpression.resolve_expression.<locals>.<lambda>  s    d2288aAr8   )keyr   )r)   flattenr   r   r+   setr(   joinr   r   rT   r   r   r	   sortedcopy	enumerateset_source_expressionssuper)r1   rD   allow_joinsreuse	summarizefor_saver0   rY   wrapperswrapperwrapper_typesr   root_expressionresolve_root_expressionirc   s   `              r!   rT   z"IndexExpression.resolve_expression   s    4<<>*&/9'
#8 7??h7gh?}S%7!88))AEAUAUVAU+[--AUV  q3x=1,-9J))AEAUAUVAU+[--AUV  ,A."1"D"D#
 137"?=PQOAH 7??h7hH?'"6
7..Q/@A 7 RL//0AB''!6 ''(9:w);y(
 	
W @ W W* @s   GG!G&G+c                 *     | j                   ||fi |S r   )rC   )r1   rF   r<   extra_contexts       r!   	as_sqlitezIndexExpression.as_sqlite)  s    t{{8ZA=AAr8   r   )NTNFF)re   rd   r   __doc__r   r
   r   r   rR   classmethodr   rT   r   __classcell__)rc   s   @r!   rQ   rQ      sK    A H(O	 . .
 ;
zBr8   rQ   N)typesr   django.db.backends.utilsr   r   django.db.models.expressionsr   r   r   r	   r
   django.db.models.functionsr   django.db.models.query_utilsr   django.db.models.sqlr   django.utils.functionalr   __all__r   rQ   r   r8   r!   <module>r      sA     C N N . * & -)G GTTBd TBr8   