o
    &gqD                     @   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   `/root/parts/websockify/install/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      _ $