U
    {h*                     @   s8  d dl mZmZmZ d dlmZ d dlmZmZm	Z	m
Z
 d dlmZmZmZ d dlmZ d dlmZmZmZmZmZmZmZ G dd de	ZG d	d
 d
eZG dd deZG dd dee
ZG dd deZG dd dZG dd deeZG dd deeZ G dd deZ!G dd deZ"eee dddZ#dS )    )AnyListTuple)BaseDatabaseWrapper)CombinedExpression
ExpressionFuncValue)BooleanFieldField
FloatField)SQLCompiler)AndMatchAllNotOrPhrase	PlainTextSearchQueryc                       s8   e Zd ZdZe Z fddZdeedddZ	  Z
S )	BM25Zbm25c                    s   d}t  j|  d S )N super__init__)selfZexpressions	__class__r   u/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/search/backends/database/sqlite/query.pyr      s    zBM25.__init__N)compiler
connectionc                 C   s   dg  }}||fS )Nz"bm25(wagtailsearch_indexentry_fts)r   )r   r   r   functiontemplatesqlparamsr   r   r   as_sql   s    
zBM25.as_sql)NN)__name__
__module____qualname__r    r   output_fieldr   r   r   r$   __classcell__r   r   r   r   r      s     r   c                   @   s>   e Zd ZdZdZdddZdd Zdd	 Zd
d Zdd Z	dS )LexemeCombinableANDORNc                 C   s:   t |tstdt| d|r.t||| S t| ||S )Nz4Lexeme can only be combined with other Lexemes, got .)
isinstancer*   	TypeErrortypeCombinedLexeme)r   other	connectorreversednoder   r   r   _combine"   s    
zLexemeCombinable._combinec                 C   s   |  || jdS NFr6   BITANDr   r2   r   r   r   bitand.   s    zLexemeCombinable.bitandc                 C   s   |  || jdS r7   r6   BITORr:   r   r   r   bitor1   s    zLexemeCombinable.bitorc                 C   s   |  || jdS r7   r<   r:   r   r   r   __or__4   s    zLexemeCombinable.__or__c                 C   s   |  || jdS r7   r8   r:   r   r   r   __and__7   s    zLexemeCombinable.__and__)N)
r%   r&   r'   r9   r=   r6   r;   r>   r?   r@   r   r   r   r   r*      s   
r*   c                   @   s   e Zd Zdd ZdS )SearchQueryFieldc                 C   s   d S Nr   )r   r   r   r   r   db_type<   s    zSearchQueryField.db_typeN)r%   r&   r'   rC   r   r   r   r   rA   ;   s   rA   c                       s4   e Zd Ze Zdddd fddZdd Z  ZS )	LexemeNF)prefixweightc                   s    || _ || _t j||d d S Nr(   )rE   rF   r   r   )r   valuer(   rE   rF   r   r   r   r   C   s    zLexeme.__init__c                 C   s0   | j dddd}| jr"d}nd}||gfS )N'z''\z\\z"%s"*"%s")rI   replacerE   )r   r   r   paramr!   r   r   r   r$   H   s
    zLexeme.as_sql)Nr%   r&   r'   rA   Z_output_fieldr   r$   r)   r   r   r   r   rD   @   s   rD   c                       s,   e Zd Ze Zd fdd	Zdd Z  ZS )r1   Nc                    s$   t  j|d || _|| _|| _d S rG   )r   r   r3   lhsrhsr   rP   r3   rQ   r(   r   r   r   r   V   s    zCombinedLexeme.__init__c           	      C   sd   g }| | j\}}|| | | j\}}|| | d| j d| }|t| }d|gfS )N z%s)compilerP   extendrQ   r3   tuple)	r   r   r   Zvalue_paramsZlsqlr#   ZrsqlZcombined_sqlZcombined_valuer   r   r   r$   \   s    

zCombinedLexeme.as_sql)NrO   r   r   r   r   r1   S   s   r1   c                   @   sF   e Zd ZdZdZdeedddZdd Zd	d
 Z	dd Z
dd ZdS )SearchQueryCombinabler+   r,   F)r3   r4   c                 C   s8   t |tstdt|j |r,t||| S t| ||S )NzJSearchQuery can only be combined with other SearchQuery instances, got %s.)r.   rW   r/   r0   r%   CombinedSearchQueryExpression)r   r2   r3   r4   r   r   r   r6   m   s    
zSearchQueryCombinable._combinec                 C   s   |  || jdS r7   r<   r:   r   r   r   r?   z   s    zSearchQueryCombinable.__or__c                 C   s   |  || jdS NTr<   r:   r   r   r   __ror__}   s    zSearchQueryCombinable.__ror__c                 C   s   |  || jdS r7   r8   r:   r   r   r   r@      s    zSearchQueryCombinable.__and__c                 C   s   |  || jdS rY   r8   r:   r   r   r   __rand__   s    zSearchQueryCombinable.__rand__N)F)r%   r&   r'   r9   r=   strboolr6   r?   rZ   r@   r[   r   r   r   r   rW   i   s   rW   c                       sR   e Zd Zded fddZeeeee	e
e f dddZe	dd	d
Z  ZS )SearchQueryExpressionN)rI   c                    s@   t  jt d || _|| _t|tr6td| | _n|| _d S )NrH   rL   )	r   r   rA   usingextrar.   r\   r	   rI   )r   rI   r_   r`   r   r   r   r      s    
zSearchQueryExpression.__init__)r   r   extra_contextreturnc                 K   s   | | j\}}||fS rB   )rT   rI   )r   r   r   ra   r"   r#   r   r   r   r$      s    zSearchQueryExpression.as_sql)rb   c                 C   s
   | j  S rB   )rI   __repr__r   r   r   r   rc      s    zSearchQueryExpression.__repr__)N)r%   r&   r'   r*   r   r   r   r   r   r\   r   r$   rc   r)   r   r   r   r   r^      s   	r^   c                       s*   e Zd Zd fdd	Z fddZ  ZS )rX   Nc                    s   t  |||| d S rB   r   rR   r   r   r   r      s    z&CombinedSearchQueryExpression.__init__c                    s   dt    S )Nz(%s))r   __str__rd   r   r   r   re      s    z%CombinedSearchQueryExpression.__str__)N)r%   r&   r'   r   re   r)   r   r   r   r   rX      s   rX   c                       sH   e Zd ZdZdZe Zee e	dd fddZ
dd Zd	d
 Z  ZS )MatchExpressionTz%wagtailsearch_indexentry_fts MATCH %sN)columnsqueryrb   c                    s    t  j| jd || _|| _d S rG   )r   r   r(   rg   rh   )r   rg   rh   r   r   r   r      s    zMatchExpression.__init__c                 C   sF   d | j}|| j}|d t|d  }dj||dg}| j|fS )NrS   r      z{{{column}}} : ({query}))columnrh   )joinrg   rT   rh   rV   formatr!   )r   r   r   Zjoined_columnsZcompiled_queryZformatted_queryr#   r   r   r   r$      s     zMatchExpression.as_sqlc                 C   s   d| j d| jdS )Nz<MatchExpression: z = >)rg   rh   rd   r   r   r   rc      s    zMatchExpression.__repr__)r%   r&   r'   Z
filterabler!   r
   r(   r   r\   rW   r   r$   rc   r)   r   r   r   r   rf      s   rf   c                   @   s(   e Zd ZdZeedddZdd ZdS )AndNota  
    This is a binary search query, where there are two subqueries, and the search is done by searching the first, and excluding the second subquery.
    For example, AndNot(X, Y) would be equivalent to doing And(X, Not(Y)), where X is the first subquery, and Y is the second subquery (the negated one).
    This is done because the SQLite FTS5 module doesn't support the unary NOT operator.
    
subquery_a
subquery_bc                 C   s   || _ || _d S rB   ro   )r   rp   rq   r   r   r   r      s    zAndNot.__init__c                 C   s   dt | j dt | j dS )N<z AndNot rm   )reprrp   rq   rd   r   r   r   rc      s    zAndNot.__repr__N)r%   r&   r'   __doc__r   r   rc   r   r   r   r   rn      s   rn   )search_queryrb   c                 C   sZ  t | tr| S t | tr| S t | trdd | jD }dd |D }dd |D }dd |D }|g krpt|S |D ]}t |trttt   S qttt|t|S t | tr(dd | jD }dd |D }|g krt|S |D ]}t |trt   S qdd |D }d	d |D }tt t|S t | trFt	| j
}t|S t | trV| S d
S )aU  
    Turns this query into a normalized version.
    For example, And(Not(PlainText("Arepa")), PlainText("Crepe")) would be turned into AndNot(PlainText("Crepe"), PlainText("Arepa")): "Crepe AND NOT Arepa".
    This is done because we need to get the NOT operator to the front of the query, so it can be used in the search, because the SQLite FTS5 module doesn't support the unary NOT operator. This means that, in order to support the NOT operator, we need to match against the non-negated version of the query, and then return everything that is not in the results of the non-negated query.
    c                 S   s   g | ]}t |qS r   	normalize.0subqueryr   r   r   
<listcomp>   s    znormalize.<locals>.<listcomp>c                 S   s   g | ]}t |ts|qS r   r.   r   rx   r   r   r   r{      s   
c                 S   s   g | ]}t |ts|qS r   r.   r   rx   r   r   r   r{      s   
c                 S   s   g | ]}t |tr|jqS r   r.   r   rz   rx   r   r   r   r{      s   
c                 S   s   g | ]}t |qS r   rv   rx   r   r   r   r{     s    c                 S   s   g | ]}t |tr|jqS r   r~   rx   r   r   r   r{     s   
c                 S   s   g | ]}t |ts|qS r   r|   rx   r   r   r   r{     s   
c                 S   s   g | ]}t |ts|qS r   r}   rx   r   r   r   r{     s   
N)r.   r   r   r   Z
subqueriesr   r   rn   r   rw   rz   )ru   Znormalized_subqueriesZnot_negated_subqueriesZnegated_subqueriesrz   
normalizedr   r   r   rw      sf    





rw   N)$typingr   r   r   Zdjango.db.backends.base.baser   Zdjango.db.models.expressionsr   r   r   r	   Zdjango.db.models.fieldsr
   r   r   Zdjango.db.models.sql.compilerr   Zwagtail.search.queryr   r   r   r   r   r   r   r   r*   rA   rD   r1   rW   r^   rX   rf   rn   rw   r   r   r   r   <module>   s    $