o
    Š¨ÊhqD  ã                   @   sâ   d dl Z d dlmZ d dlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ d	Zd
ZdZdZdZdZdZdZdZdZdZdZeeeeeeeeeeeegZG dd„ dƒZG dd„ dƒZG dd„ dƒZG dd„ deƒZdd„ ZdS )é    N)ÚOrderedDict)ÚResponseErroré   )ÚEdge)ÚVersionMismatchException)ÚNode)ÚPathzLabels addedzLabels removedzNodes createdzNodes deletedzRelationships deletedzProperties setzProperties removedzRelationships createdzIndices createdzIndices deletedzCached executionzinternal execution timec                   @   s   e Zd ZdZdZdZdZdS )ÚResultSetColumnTypesr   r   é   é   N)Ú__name__Ú
__module__Ú__qualname__ÚCOLUMN_UNKNOWNÚCOLUMN_SCALARÚCOLUMN_NODEÚCOLUMN_RELATION© r   r   úS/var/www/html/env/lib/python3.10/site-packages/redis/commands/graph/query_result.pyr	   )   s
    r	   c                   @   s<   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdS )ÚResultSetScalarTypesr   r   r
   r   é   é   é   é   é   é	   é
   é   N)r   r   r   ÚVALUE_UNKNOWNÚ
VALUE_NULLÚVALUE_STRINGÚVALUE_INTEGERÚVALUE_BOOLEANÚVALUE_DOUBLEÚVALUE_ARRAYÚ
VALUE_EDGEÚ
VALUE_NODEÚ
VALUE_PATHÚ	VALUE_MAPÚVALUE_POINTr   r   r   r   r   0   s    r   c                   @   sz  e Zd ZdOdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zed.d/„ ƒZd0d1„ Zed2d3„ ƒZed4d5„ ƒZed6d7„ ƒZed8d9„ ƒZ ed:d;„ ƒZ!ed<d=„ ƒZ"ed>d?„ ƒZ#ed@dA„ ƒZ$edBdC„ ƒZ%edDdE„ ƒZ&edFdG„ ƒZ'edHdI„ ƒZ(edJdK„ ƒZ)edLdM„ ƒZ*dNS )PÚQueryResultFc                 C   sh   || _ g | _g | _|  |¡ t|ƒdkr|  |d ¡ dS |r&|  |¡ dS |  |d ¡ |  |¡ dS )a7  
        A class that represents a result of the query operation.

        Args:

        graph:
            The graph on which the query was executed.
        response:
            The response from the server.
        profile:
            A boolean indicating if the query command was "GRAPH.PROFILE"
        r   r   éÿÿÿÿN©ÚgraphÚheaderÚ
result_setÚ_check_for_errorsÚlenÚparse_statisticsÚparse_profileÚparse_results©Úselfr-   ÚresponseÚprofiler   r   r   Ú__init__@   s   
zQueryResult.__init__c                 C   sP   t |d tƒr|d }t|ƒdkr|d }t|ƒ}|‚t |d tƒr&|d ‚dS )z:
        Check if the response contains an error.
        r   zversion mismatchr   r+   N)Ú
isinstancer   Ústrr   )r6   r7   ÚerrorÚversionr   r   r   r0   ]   s   ÿzQueryResult._check_for_errorsc                 C   s.   |   |¡| _t| jƒdkrdS |  |¡| _dS ©zL
        Parse the query execution result returned from the server.
        r   N©Úparse_headerr.   r1   Úparse_recordsr/   ©r6   Úraw_result_setr   r   r   r4   m   s   zQueryResult.parse_resultsc                 C   sZ   i | _ t|ƒD ]\}}t|tƒr| ¡ ||< qtD ]}|  ||¡}|dur*|| j |< qdS )z@
        Parse the statistics returned in the response.
        N)Ú
statisticsÚ	enumerater:   ÚbytesÚdecodeÚSTATSÚ
_get_value)r6   Úraw_statisticsÚidxÚstatÚsÚvr   r   r   r2   y   s   
€
€ýzQueryResult.parse_statisticsc                 C   s   |d }|S )z1
        Parse the header of the result.
        r   r   )r6   rC   r.   r   r   r   r@   ‰   s   zQueryResult.parse_headerc                    s   ‡ fdd„|d D ƒ}|S )úF
        Parses the result set and returns a list of records.
        c                    s"   g | ]}‡ fd d„t |ƒD ƒ‘qS )c                    s(   g | ]\}}ˆ j ˆ j| d   |ƒ‘qS )r   ©Úparse_record_typesr.   ©Ú.0rK   Úcell©r6   r   r   Ú
<listcomp>–   s    ÿÿz8QueryResult.parse_records.<locals>.<listcomp>.<listcomp>)rE   )rS   ÚrowrU   r   r   rV   •   s    
üþÿz-QueryResult.parse_records.<locals>.<listcomp>r   r   )r6   rC   Úrecordsr   rU   r   rA   ‘   s   
ûzQueryResult.parse_recordsc                 C   s<   i }|D ]}| j  |d ¡}|  |dd… ¡}|||< q|S )ú/
        Parse node / edge properties.
        r   r   N©r-   Úget_propertyÚparse_scalar©r6   ÚpropsÚ
propertiesÚpropÚ	prop_nameÚ
prop_valuer   r   r   Úparse_entity_propertiesŸ   s   
z#QueryResult.parse_entity_propertiesc                 C   s(   t |tƒr	| ¡ S t |tƒst|ƒS |S )z-
        Parse the cell as a string.
        )r:   rF   rG   r;   ©r6   rT   r   r   r   Úparse_string¬   s
   

zQueryResult.parse_stringc                 C   s`   t |d ƒ}d}t|d ƒdkr"g }|d D ]}| | j |¡¡ q|  |d ¡}t|||d�S )z+
        Parse the cell to a node.
        r   Nr   r
   ©Únode_idÚlabelr_   )Úintr1   Úappendr-   Ú	get_labelrc   r   )r6   rT   rg   ÚlabelsÚinner_labelr_   r   r   r   Ú
parse_node·   s   zQueryResult.parse_nodec                 C   sT   t |d ƒ}| j |d ¡}t |d ƒ}t |d ƒ}|  |d ¡}t|||||d�S )ú,
        Parse the cell to an edge.
        r   r   r
   r   r   ©Úedge_idr_   ©ri   r-   Úget_relationrc   r   ©r6   rT   rq   ÚrelationÚsrc_node_idÚdest_node_idr_   r   r   r   Ú
parse_edgeÈ   s   

ÿzQueryResult.parse_edgec                 C   s&   |   |d ¡}|   |d ¡}t||ƒS )ú+
        Parse the cell to a path.
        r   r   ©r\   r   ©r6   rT   ÚnodesÚedgesr   r   r   Ú
parse_pathÛ   s   
zQueryResult.parse_pathc                 C   sH   t ƒ }t|ƒ}td|dƒD ]}|  || ¡}|  ||d  ¡||< q|S )z*
        Parse the cell as a map.
        r   r
   r   ©r   r1   Úrangere   r\   ©r6   rT   ÚmÚ	n_entriesÚiÚkeyr   r   r   Ú	parse_mapã   s   zQueryResult.parse_mapc                 C   s(   i }t |d ƒ|d< t |d ƒ|d< |S )z*
        Parse the cell to point.
        r   Úlatituder   Ú	longitude©Úfloat)r6   rT   Úpr   r   r   Úparse_pointó   s   zQueryResult.parse_pointc                 C   ó   dS )z%
        Parse a null value.
        Nr   rd   r   r   r   Ú
parse_nullþ   ó   zQueryResult.parse_nullc                 C   ó   t |ƒS )z8
        Parse the integer value from the cell.
        )ri   rd   r   r   r   Úparse_integer  ó   zQueryResult.parse_integerc                 C   sX   t |tƒr	| ¡ n|}zt|ƒrd}W |S d}W |S  ty+   tj d¡ d}Y |S w )z4
        Parse the cell value as a boolean.
        TFzunknown boolean type
N)r:   rF   rG   Ú	strtoboolÚ
ValueErrorÚsysÚstderrÚwrite©r6   ÚvalueÚscalarr   r   r   Úparse_boolean
  s   üýýzQueryResult.parse_booleanc                 C   r�   )z-
        Parse the cell as a double.
        r‰   rd   r   r   r   Úparse_double  r’   zQueryResult.parse_doublec                    s    ‡ ‡fdd„t tˆƒƒD ƒ}|S )z+
        Parse an array of values.
        c                    s   g | ]	}ˆ   ˆ| ¡‘qS r   ©r\   ©rS   r„   ©r6   r™   r   r   rV      s    z+QueryResult.parse_array.<locals>.<listcomp>©r€   r1   r˜   r   rŸ   r   Úparse_array  s   zQueryResult.parse_arrayc                 C   s   t j d¡ dS )z/
        Parse a cell of unknown type.
        zUnknown type
N)r•   r–   r—   rd   r   r   r   Úparse_unknown#  s   zQueryResult.parse_unknownc                 C   s&   t |d ƒ}|d }| j| |ƒ}|S )zE
        Parse a scalar value from a cell in the result set.
        r   r   )ri   Úparse_scalar_types©r6   rT   Úscalar_typer™   rš   r   r   r   r\   *  s   zQueryResult.parse_scalarc                 C   s   dd„ |D ƒ| _ d S )Nc                 S   s"   g | ]}|d |  d¡…  ¡ ‘qS )r   ú,)ÚindexÚstrip)rS   Úxr   r   r   rV   5  s   " z-QueryResult.parse_profile.<locals>.<listcomp>)r/   )r6   r7   r   r   r   r3   4  s   zQueryResult.parse_profilec                 C   s   t | jƒdkS ©Nr   )r1   r/   rU   r   r   r   Úis_empty7  s   zQueryResult.is_emptyc                 C   s6   |D ]}| |v rt | d¡d  d¡d ƒ  S qd S )Nz: r   ú r   )rŠ   Úsplit)r`   rD   rL   r   r   r   rI   :  s
    ÿzQueryResult._get_valuec                 C   s   || j v r
| j | S dS rª   )rD   )r6   rL   r   r   r   Ú	_get_statB  s   zQueryResult._get_statc                 C   ó
   |   t¡S )z/Returns the number of labels added in the query)r®   ÚLABELS_ADDEDrU   r   r   r   Úlabels_addedE  ó   
zQueryResult.labels_addedc                 C   r¯   )z1Returns the number of labels removed in the query)r®   ÚLABELS_REMOVEDrU   r   r   r   Úlabels_removedJ  r²   zQueryResult.labels_removedc                 C   r¯   )z0Returns the number of nodes created in the query)r®   ÚNODES_CREATEDrU   r   r   r   Únodes_createdO  r²   zQueryResult.nodes_createdc                 C   r¯   )z0Returns the number of nodes deleted in the query)r®   ÚNODES_DELETEDrU   r   r   r   Únodes_deletedT  r²   zQueryResult.nodes_deletedc                 C   r¯   )z1Returns the number of properties set in the query)r®   ÚPROPERTIES_SETrU   r   r   r   Úproperties_setY  r²   zQueryResult.properties_setc                 C   r¯   )z5Returns the number of properties removed in the query)r®   ÚPROPERTIES_REMOVEDrU   r   r   r   Úproperties_removed^  r²   zQueryResult.properties_removedc                 C   r¯   )z8Returns the number of relationships created in the query)r®   ÚRELATIONSHIPS_CREATEDrU   r   r   r   Úrelationships_createdc  r²   z!QueryResult.relationships_createdc                 C   r¯   )z8Returns the number of relationships deleted in the query)r®   ÚRELATIONSHIPS_DELETEDrU   r   r   r   Úrelationships_deletedh  r²   z!QueryResult.relationships_deletedc                 C   r¯   )z2Returns the number of indices created in the query)r®   ÚINDICES_CREATEDrU   r   r   r   Úindices_createdm  r²   zQueryResult.indices_createdc                 C   r¯   )z2Returns the number of indices deleted in the query)r®   ÚINDICES_DELETEDrU   r   r   r   Úindices_deletedr  r²   zQueryResult.indices_deletedc                 C   s   |   t¡dkS )z:Returns whether or not the query execution plan was cachedr   )r®   ÚCACHED_EXECUTIONrU   r   r   r   Úcached_executionw  s   zQueryResult.cached_executionc                 C   r¯   )z.Returns the server execution time of the query)r®   ÚINTERNAL_EXECUTION_TIMErU   r   r   r   Úrun_time_ms|  r²   zQueryResult.run_time_msc                 C   sd   t j| jt j| jt j| jt j| jt j	| j
t j| jt j| jt j| jt j| jt j| jt j| jt j| jiS ©N)r   r   rŽ   r    re   r!   r‘   r"   r›   r#   rœ   r$   r¡   r&   rn   r%   rx   r'   r~   r(   r†   r)   rŒ   r   r¢   rU   r   r   r   r£   �  s   ôzQueryResult.parse_scalar_typesc                 C   s$   t j| jt j| jt j| jt j| jiS rÉ   )	r	   r   r\   r   rn   r   rx   r   r¢   rU   r   r   r   rQ   ’  s
   üzQueryResult.parse_record_typesN©F)+r   r   r   r9   r0   r4   r2   r@   rA   rc   re   rn   rx   r~   r†   rŒ   rŽ   r‘   r›   rœ   r¡   r¢   r\   r3   r«   ÚstaticmethodrI   r®   Úpropertyr±   r´   r¶   r¸   rº   r¼   r¾   rÀ   rÂ   rÄ   rÆ   rÈ   r£   rQ   r   r   r   r   r*   ?   sl    















r*   c                   @   sj   e Zd Z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d„ Zdd„ Zdd„ ZdS )ÚAsyncQueryResultzp
    Async version for the QueryResult class - a class that
    represents a result of the query operation.
    c                 C   r�   )zC
        To init the class you must call self.initialize()
        Nr   rU   r   r   r   r9   ¢  r�   zAsyncQueryResult.__init__Fc                 Ã   sp   �|| _ g | _g | _|  |¡ t|ƒdkr|  |d ¡ | S |r'|  |¡ | S |  |d ¡ |  |¡I dH  | S )a  
        Initializes the class.
        Args:

        graph:
            The graph on which the query was executed.
        response:
            The response from the server.
        profile:
            A boolean indicating if the query command was "GRAPH.PROFILE"
        r   r   r+   Nr,   r5   r   r   r   Ú
initialize¨  s   €
ù
ýzAsyncQueryResult.initializec                 Ã   sn   �d}t |d ƒdkr g }|d D ]}| | j |¡I dH ¡ q|  |d ¡I dH }t|d ƒ}t|||d�S )z.
        Parses a node from the cell.
        Nr   r   r
   rf   )r1   rj   r-   rk   rc   ri   r   )r6   rT   rl   rm   r_   rg   r   r   r   rn   Æ  s   €zAsyncQueryResult.parse_nodec                 Ã   sT   �t |d ƒ}|d }z| j| |ƒI dH }W |S  ty)   | j| |ƒ}Y |S w )zA
        Parses a scalar value from the server response.
        r   r   N)ri   r£   Ú	TypeErrorr¤   r   r   r   r\   ×  s   €üüzAsyncQueryResult.parse_scalarc                 ƒ   s>   �g }|d D ]}‡ fdd„t |ƒD ƒI dH }| |¡ q|S )rO   r   c                 “   s0   �g | ]\}}ˆ j ˆ j| d   |ƒI dH ‘qS )r   NrP   rR   rU   r   r   rV   ë  s
   € ÿÿz2AsyncQueryResult.parse_records.<locals>.<listcomp>N)rE   rj   )r6   rC   rX   rW   Úrecordr   rU   r   rA   å  s   €
þzAsyncQueryResult.parse_recordsc                 Ã   s6   �|   |¡| _t| jƒdkrdS |  |¡I dH | _dS r>   r?   rB   r   r   r   r4   ó  s
   €zAsyncQueryResult.parse_resultsc                 Ã   sJ   �i }|D ]}| j  |d ¡I dH }|  |dd… ¡I dH }|||< q|S )rY   r   Nr   rZ   r]   r   r   r   rc   ÿ  s   €
z(AsyncQueryResult.parse_entity_propertiesc                 Ã   sb   �t |d ƒ}| j |d ¡I dH }t |d ƒ}t |d ƒ}|  |d ¡I dH }t|||||d�S )ro   r   r   Nr
   r   r   rp   rr   rt   r   r   r   rx     s   €

ÿzAsyncQueryResult.parse_edgec                 Ã   s4   �|   |d ¡I dH }|   |d ¡I dH }t||ƒS )ry   r   Nr   rz   r{   r   r   r   r~     s   €
zAsyncQueryResult.parse_pathc                 Ã   sP   �t ƒ }t|ƒ}td|dƒD ]}|  || ¡}|  ||d  ¡I dH ||< q|S )z*
        Parse the cell to a map.
        r   r
   r   Nr   r�   r   r   r   r†   '  s   €zAsyncQueryResult.parse_mapc                 ƒ   s(   �‡ ‡fdd„t tˆƒƒD ƒI dH }|S )z$
        Parse array value.
        c                 “   s"   �g | ]}ˆ   ˆ| ¡I d H ‘qS rÉ   r�   rž   rŸ   r   r   rV   ;  s   €  z0AsyncQueryResult.parse_array.<locals>.<listcomp>Nr    r˜   r   rŸ   r   r¡   7  s   €"zAsyncQueryResult.parse_arrayNrÊ   )r   r   r   Ú__doc__r9   rÎ   rn   r\   rA   r4   rc   rx   r~   r†   r¡   r   r   r   r   rÍ   œ  s    
rÍ   c                 C   s.   |   ¡ } | dv r
dS | dv rdS td| ›�ƒ‚)zø
    Convert a string representation of truth to true (1) or false (0).
    True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
    are 'n', 'no', 'f', 'false', 'off', and '0'.  Raises ValueError if
    'val' is anything else.
    )ÚyÚyesÚtÚtrueÚonÚ1T)ÚnÚnoÚfÚfalseÚoffÚ0Fzinvalid truth value )Úlowerr”   )Úvalr   r   r   r“   ?  s   r“   )r•   Úcollectionsr   Úredisr   Úedger   Ú
exceptionsr   Únoder   Úpathr   r°   r³   rµ   r·   r¿   r¹   r»   r½   rÁ   rÃ   rÅ   rÇ   rH   r	   r   r*   rÍ   r“   r   r   r   r   Ú<module>   sP    ô  _ $