U
    dB                     @   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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S )    N)OrderedDict)	strtobool)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   E/tmp/pip-unpacked-wheel-f5h5_hbx/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   1   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   sd   || _ g | _g | _| | t|dkr8| |d  n(|rH| | n| |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.   responseZprofiler   r   r   __init__A   s    
zQueryResult.__init__c                 C   sP   t |d tr6|d }t|dkr2|d }t|}|t |d trL|d dS )z:
        Check if the response contains an error.
        r   zversion mismatchr   r,   N)
isinstancer   strr   )r7   r8   errorversionr   r   r   r1   ^   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/   r2   parse_recordsr0   r7   raw_result_setr   r   r   r5   n   s    zQueryResult.parse_resultsc                 C   sZ   i | _ t|D ]\}}t|tr| ||< qtD ]"}| ||}|dk	r2|| j |< q2dS )z@
        Parse the statistics returned in the response.
        N)
statistics	enumerater:   bytesdecodeSTATS
_get_value)r7   Zraw_statisticsidxstatsvr   r   r   r3   z   s    
zQueryResult.parse_statisticsc                 C   s   |d }|S )z1
        Parse the header of the result.
        r   r   )r7   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/   .0rJ   cellr7   r   r   
<listcomp>   s   z8QueryResult.parse_records.<locals>.<listcomp>.<listcomp>)rE   )rR   rowrT   r   r   rU      s   
z-QueryResult.parse_records.<locals>.<listcomp>r   r   )r7   rC   recordsr   rT   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.   Zget_propertyparse_scalarr7   props
propertiespropZ	prop_nameZ
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 dS )z-
        Parse the cell as a string.
        N)r:   rF   rG   r;   r7   rS   r   r   r   parse_string   s
    

zQueryResult.parse_stringc                 C   s`   t |d }d}t|d dkrDg }|d D ]}|| j| q,| |d }t|||dS )z+
        Parse the cell to a node.
        r   Nr   r   node_idlabelr]   )intr2   appendr.   	get_labelr_   r   )r7   rS   rc   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]   re   r.   Zget_relationr_   r   r7   rS   rm   ZrelationZsrc_node_idZ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   rZ   r	   r7   rS   Z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   r2   rangera   rZ   r7   rS   mZ	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   Zlatituder   Z	longitudefloat)r7   rS   pr   r   r   parse_point   s    zQueryResult.parse_pointc                 C   s   dS )z%
        Parse a null value.
        Nr   r`   r   r   r   
parse_null   s    zQueryResult.parse_nullc                 C   s   t |S )z8
        Parse the integer value from the cell.
        )re   r`   r   r   r   parse_integer  s    zQueryResult.parse_integerc                 C   sT   t |tr| n|}zt|r$dnd}W n$ tk
rN   tjd d}Y nX |S )z4
        Parse the cell value as a boolean.
        TFzunknown boolean type
N)r:   rF   rG   r   
ValueErrorsysstderrwriter7   valuescalarr   r   r   parse_boolean  s    
zQueryResult.parse_booleanc                 C   s   t |S )z-
        Parse the cell as a double.
        r}   r`   r   r   r   parse_double  s    zQueryResult.parse_doublec                    s     fddt tD }|S )z+
        Parse an array of values.
        c                    s   g | ]}  | qS r   rZ   rR   rz   r7   r   r   r   rU   !  s     z+QueryResult.parse_array.<locals>.<listcomp>rw   r2   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   r`   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   )re   parse_scalar_typesr7   rS   Zscalar_typer   r   r   r   r   rZ   +  s    zQueryResult.parse_scalarc                 C   s   dd |D | _ d S )Nc                 S   s"   g | ]}|d | d  qS )r   ,)indexstrip)rR   xr   r   r   rU   6  s     z-QueryResult.parse_profile.<locals>.<listcomp>)r0   )r7   r8   r   r   r   r4   5  s    zQueryResult.parse_profilec                 C   s   t | jdkS Nr   )r2   r0   rT   r   r   r   is_empty8  s    zQueryResult.is_emptyc                 C   s6   |D ],}| |krt |dd dd   S qd S )Nz: r    r   )r~   split)r^   rD   rK   r   r   r   rI   ;  s    "zQueryResult._get_valuec                 C   s   || j kr| j | S dS r   )rD   )r7   rK   r   r   r   	_get_statC  s    zQueryResult._get_statc                 C   s
   |  tS )z/Returns the number of labels added in the query)r   LABELS_ADDEDrT   r   r   r   labels_addedF  s    zQueryResult.labels_addedc                 C   s
   |  tS )z1Returns the number of labels removed in the query)r   LABELS_REMOVEDrT   r   r   r   labels_removedK  s    zQueryResult.labels_removedc                 C   s
   |  tS )z0Returns the number of nodes created in the query)r   NODES_CREATEDrT   r   r   r   nodes_createdP  s    zQueryResult.nodes_createdc                 C   s
   |  tS )z0Returns the number of nodes deleted in the query)r   NODES_DELETEDrT   r   r   r   nodes_deletedU  s    zQueryResult.nodes_deletedc                 C   s
   |  tS )z1Returns the number of properties set in the query)r   PROPERTIES_SETrT   r   r   r   properties_setZ  s    zQueryResult.properties_setc                 C   s
   |  tS )z5Returns the number of properties removed in the query)r   PROPERTIES_REMOVEDrT   r   r   r   properties_removed_  s    zQueryResult.properties_removedc                 C   s
   |  tS )z8Returns the number of relationships created in the query)r   RELATIONSHIPS_CREATEDrT   r   r   r   relationships_createdd  s    z!QueryResult.relationships_createdc                 C   s
   |  tS )z8Returns the number of relationships deleted in the query)r   RELATIONSHIPS_DELETEDrT   r   r   r   relationships_deletedi  s    z!QueryResult.relationships_deletedc                 C   s
   |  tS )z2Returns the number of indices created in the query)r   INDICES_CREATEDrT   r   r   r   indices_createdn  s    zQueryResult.indices_createdc                 C   s
   |  tS )z2Returns the number of indices deleted in the query)r   INDICES_DELETEDrT   r   r   r   indices_deleteds  s    zQueryResult.indices_deletedc                 C   s   |  tdkS )z:Returns whether or not the query execution plan was cachedr   )r   CACHED_EXECUTIONrT   r   r   r   cached_executionx  s    zQueryResult.cached_executionc                 C   s
   |  tS )z.Returns the server execution time of the query)r   INTERNAL_EXECUTION_TIMErT   r   r   r   run_time_ms}  s    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!   ra   r"   r   r#   r   r$   r   r%   r   r'   rj   r&   rp   r(   ru   r)   r|   r*   r   r   r   rT   r   r   r   r     s2                zQueryResult.parse_scalar_typesc                 C   s$   t j| jt j| jt j| jt j| jiS r   )	r
   r   rZ   r   rj   r   rp   r   r   rT   r   r   r   rP     s        zQueryResult.parse_record_typesN)F)+r   r   r   r9   r1   r5   r3   r@   rA   r_   ra   rj   rp   ru   r|   r   r   r   r   r   r   r   rZ   r4   r   staticmethodrI   r   propertyr   r   r   r   r   r   r   r   r   r   r   r   r   rP   r   r   r   r   r+   @   sj   















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   s   dS )zC
        To init the class you must call self.initialize()
        Nr   rT   r   r   r   r9     s    zAsyncQueryResult.__init__Fc                    sj   || _ g | _g | _| | t|dkr8| |d  n.|rH| | n| |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-   r6   r   r   r   
initialize  s    
zAsyncQueryResult.initializec                    sl   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   rb   )r2   rf   r.   rg   r_   re   r   )r7   rS   rh   ri   r]   rc   r   r   r   rj     s    zAsyncQueryResult.parse_nodec                    sT   t |d }|d }z| j| |I dH }W n" tk
rN   | j| |}Y nX |S )zA
        Parses a scalar value from the server response.
        r   r   N)re   r   	TypeErrorr   r   r   r   rZ     s    zAsyncQueryResult.parse_scalarc                    s<   g }|d D ]*} fddt |D I dH }|| q|S )rN   r   c                    s.   g | ]&\}} j  j| d   |I dH qS )r   NrO   rQ   rT   r   r   rU     s   z2AsyncQueryResult.parse_records.<locals>.<listcomp>N)rE   rf   )r7   rC   rW   rV   recordr   rT   r   rA     s    
zAsyncQueryResult.parse_recordsc                    s4   |  || _t| jdkrdS | |I dH | _dS r>   r?   rB   r   r   r   r5     s    zAsyncQueryResult.parse_resultsc                    sH   i }|D ]:}| j |d I dH }| |dd I dH }|||< q|S )rX   r   Nr   rY   r[   r   r   r   r_      s    
z(AsyncQueryResult.parse_entity_propertiesc                    s`   t |d }| j|d I dH }t |d }t |d }| |d I dH }t|||||dS )rk   r   r   Nr   r   r   rl   rn   ro   r   r   r   rp     s    
    zAsyncQueryResult.parse_edgec                    s2   |  |d I dH }|  |d I dH }t||S )rq   r   Nr   rr   rs   r   r   r   ru      s    zAsyncQueryResult.parse_pathc                    sN   t  }t|}td|dD ].}| || }| ||d  I dH ||< q|S )z*
        Parse the cell to a map.
        r   r   r   Nrv   rx   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   rU   <  s     z0AsyncQueryResult.parse_array.<locals>.<listcomp>Nr   r   r   r   r   r   8  s    "zAsyncQueryResult.parse_arrayN)F)r   r   r   __doc__r9   r   rj   rZ   rA   r5   r_   rp   ru   r|   r   r   r   r   r   r     s   
r   ) r   collectionsr   distutils.utilr   Zredisr   Z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   <module>   sL     _