o
    ѷ6i                     @  s   d Z ddlmZ ddlZddlZddlmZ ddlmZ dZ	e
dZe
dejZd"ddZeg dZeeeje Zddd e D Ze
de ejejB Zd#ddZdZdZd$d d!ZdS )%z
Implements a function for hoisting specially constructed comments in SQL
queries and using them to rewrite that query. This is done to support query
hints whilst obviating patching Django's ORM in complex ways.
    )annotationsN)OrderedDict)reducez/*QueryRewrite':z/\*QueryRewrite':(.*?)\*/a  
    index=
    (?P<table_name>`[^`]+`)
    \ # space
    (?P<rule>USE|IGNORE|FORCE)
    \ # space
    (
        FOR
        \ # space
        (?P<for_what>JOIN|ORDER\ BY|GROUP\ BY)
        \ # space
    )?
    (?P<index_names>(`[^`]+`(,`[^`]+`)*)|NONE)
    sqlstrreturnc              	   C  s   g }g }g }t | D ]<}|tv r|| q|dr&||dd   q|drGt|}|rG||d|d|d|df qt d| } |sT|sT|r[t	| |||} | S )	Nzlabel=   zindex=
table_nameruleindex_namesfor_what )
query_rewrite_refindallSELECT_HINT_TOKENSappend
startswithindex_rule_rematchgroupsub
modify_sql)r   commentshintsindex_hintsr   index_match r   _/var/www/hoanhtaovolam_webdjango/env/lib/python3.10/site-packages/django_mysql/rewrite_query.pyrewrite_query*   s.   


r   ))distinctness)ALLDISTINCTDISTINCTROW)priority)HIGH_PRIORITY)
join_order)STRAIGHT_JOIN)result_size)SQL_SMALL_RESULTSQL_BIG_RESULT)buffer_result)SQL_BUFFER_RESULT)query_cache)	SQL_CACHESQL_NO_CACHE)
found_rows)SQL_CALC_FOUND_ROWS
c                 c  s(    | ]\}}d j |d|dV  qdS )z (?P<{group_name}>({tokens})\s+)?|)
group_nametokensN)formatjoin).0r3   	token_setr   r   r   	<genexpr>]   s    

r9   z
        ^
        \s*
        (?P<keyword>SELECT|UPDATE|DELETE)
        # comments - N times /*a*/whitespace
        (?P<comments>(\s*/\*.*?\*/\s*)+|\s+)
    add_comments	list[str]	add_hintsadd_index_hintslist[tuple[str, str, str, str]]c              	     s  t | }|s	| S |dg}|d }|r|| |D ]}|d| d q|d dkret D ]/\} z fdd|D d	 }	||	 W q5 tyd   ||}
|
d
urb||
  Y q5w | |	 d
 }|d dkr|r|D ]
}t
|g|R  }qw|| d|S )a  
    Parse the start of the SQL, injecting each string in add_comments in
    individual SQL comments after the first keyword, and adding the named
    SELECT hints from add_hints, taking the latest in the list in cases of
    multiple mutually exclusive hints being given
    keywordr   z/*z*/r   SELECTc                   s   g | ]}| v r|qS r   r   )r7   hinthint_setr   r   
<listcomp>   s    zmodify_sql.<locals>.<listcomp>N )query_start_rer   r   stripr   SELECT_HINTSitems
IndexErrorrstripendmodify_sql_index_hintsr6   )r   r:   r<   r=   r   r4   r   commentr3   to_addexisting	remainder
index_hintr   rB   r   r   w   s4   




r   zm
    \b(?P<operator>FROM|JOIN)
    \s+
    (?P<table_name_with_alias>{table_name}(\s+[A-Z]+[0-9]+)?)
    \s+
zQ\g<operator> \g<table_name_with_alias> {rule} INDEX {for_section}({index_names}) r	   r
   r   r   c                 C  sT   t j|d}|rd| d}nd}tj|||dkrdn|d}tj||| dtjdS )	N)r	   zFOR rF   r   NONE)r
   for_sectionr      )countflags)table_spec_re_templater5   replacement_templaterer   VERBOSE)r   r	   r
   r   r   table_spec_rerU   replacementr   r   r   rN      s   rN   )r   r   r   r   )
r   r   r:   r;   r<   r;   r=   r>   r   r   )r   r   r	   r   r
   r   r   r   r   r   r   r   )__doc__
__future__r   operatorr[   collectionsr   	functoolsr   REWRITE_MARKERcompiler   r\   r   r   rI   	frozensetaddvaluesr   r6   rJ   hints_re_piece
IGNORECASErG   r   rY   rZ   rN   r   r   r   r   <module>   s>    

#


4