o
    `fB                     @   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   Z/home/ubuntu/webapp/venv/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   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   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__A   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   )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ur*|| j |< qdS )z@
        Parse the statistics returned in the response.
        N)
statistics	enumerater;   bytesdecodeSTATS
_get_value)r7   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   rD   r/   r   r   r   rA      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/   .0rL   cellr7   r   r   
<listcomp>   s    z8QueryResult.parse_records.<locals>.<listcomp>.<listcomp>)rF   )rT   rowrV   r   r   rW      s    
z-QueryResult.parse_records.<locals>.<listcomp>r   r   )r7   rD   recordsr   rV   r   rB      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r7   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;   rG   rH   r<   r7   rU   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`   )intr2   appendr.   	get_labelrd   r   )r7   rU   rh   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`   rj   r.   get_relationrd   r   r7   rU   rr   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	   r7   rU   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   rangerf   r]   r7   rU   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)r7   rU   pr   r   r   parse_point   s   zQueryResult.parse_pointc                 C      dS )z%
        Parse a null value.
        Nr   re   r   r   r   
parse_null      zQueryResult.parse_nullc                 C      t |S )z8
        Parse the integer value from the cell.
        )rj   re   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;   rG   rH   r   
ValueErrorsysstderrwriter7   valuescalarr   r   r   parse_boolean  s   zQueryResult.parse_booleanc                 C   r   )z-
        Parse the cell as a double.
        r   re   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]   rT   r   r7   r   r   r   rW   !  s    z+QueryResult.parse_array.<locals>.<listcomp>r   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   re   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   )rj   parse_scalar_typesr7   rU   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)rT   xr   r   r   rW   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   rV   r   r   r   is_empty8  s   zQueryResult.is_emptyc                 C   s6   |D ]}| |v rt |dd dd   S qd S )Nz: r    r   )r   split)ra   rE   rM   r   r   r   rJ   ;  s
    zQueryResult._get_valuec                 C   s   || j v r
| j | S dS r   )rE   )r7   rM   r   r   r   	_get_statC  s   zQueryResult._get_statc                 C   
   |  tS )z/Returns the number of labels added in the query)r   LABELS_ADDEDrV   r   r   r   labels_addedF     
zQueryResult.labels_addedc                 C   r   )z1Returns the number of labels removed in the query)r   LABELS_REMOVEDrV   r   r   r   labels_removedK  r   zQueryResult.labels_removedc                 C   r   )z0Returns the number of nodes created in the query)r   NODES_CREATEDrV   r   r   r   nodes_createdP  r   zQueryResult.nodes_createdc                 C   r   )z0Returns the number of nodes deleted in the query)r   NODES_DELETEDrV   r   r   r   nodes_deletedU  r   zQueryResult.nodes_deletedc                 C   r   )z1Returns the number of properties set in the query)r   PROPERTIES_SETrV   r   r   r   properties_setZ  r   zQueryResult.properties_setc                 C   r   )z5Returns the number of properties removed in the query)r   PROPERTIES_REMOVEDrV   r   r   r   properties_removed_  r   zQueryResult.properties_removedc                 C   r   )z8Returns the number of relationships created in the query)r   RELATIONSHIPS_CREATEDrV   r   r   r   relationships_createdd  r   z!QueryResult.relationships_createdc                 C   r   )z8Returns the number of relationships deleted in the query)r   RELATIONSHIPS_DELETEDrV   r   r   r   relationships_deletedi  r   z!QueryResult.relationships_deletedc                 C   r   )z2Returns the number of indices created in the query)r   INDICES_CREATEDrV   r   r   r   indices_createdn  r   zQueryResult.indices_createdc                 C   r   )z2Returns the number of indices deleted in the query)r   INDICES_DELETEDrV   r   r   r   indices_deleteds  r   zQueryResult.indices_deletedc                 C   s   |  tdkS )z:Returns whether or not the query execution plan was cachedr   )r   CACHED_EXECUTIONrV   r   r   r   cached_executionx  s   zQueryResult.cached_executionc                 C   r   )z.Returns the server execution time of the query)r   INTERNAL_EXECUTION_TIMErV   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!   rf   r"   r   r#   r   r$   r   r%   r   r'   ro   r&   ry   r(   r   r)   r   r*   r   r   r   rV   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   ro   r   ry   r   r   rV   r   r   r   rR     s
   zQueryResult.parse_record_typesNF)+r   r   r   r:   r1   r5   r3   rA   rB   rd   rf   ro   ry   r   r   r   r   r   r   r   r   r   r]   r4   r   staticmethodrJ   r   propertyr   r   r   r   r   r   r   r   r   r   r   r   r   rR   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   rV   r   r   r   r:     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-   r6   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   rg   )r2   rk   r.   rl   rd   rj   r   )r7   rU   rm   rn   r`   rh   r   r   r   ro     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)rj   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 )rP   r   c                    s0   g | ]\}} j  j| d   |I dH qS )r   NrQ   rS   rV   r   r   rW     s
    z2AsyncQueryResult.parse_records.<locals>.<listcomp>N)rF   rk   )r7   rD   rY   rX   recordr   rV   r   rB     s   
zAsyncQueryResult.parse_recordsc                    s6   |  || _t| jdkrdS | |I dH | _dS r?   r@   rC   r   r   r   r5     s
   zAsyncQueryResult.parse_resultsc                    sJ   i }|D ]}| j |d I dH }| |dd I dH }|||< q|S )rZ   r   Nr   r[   r^   r   r   r   rd      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 )rp   r   r   Nr   r   r   rq   rs   ru   r   r   r   ry     s   

zAsyncQueryResult.parse_edgec                    s4   |  |d I dH }|  |d I dH }t||S )rz   r   Nr   r{   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   rW   <  s     z0AsyncQueryResult.parse_array.<locals>.<listcomp>Nr   r   r   r   r   r   8  s   "zAsyncQueryResult.parse_arrayNr   )r   r   r   __doc__r:   r   ro   r]   rB   r5   rd   ry   r   r   r   r   r   r   r   r     s    
r   ) r   collectionsr   distutils.utilr   redisr   edger   
exceptionsr   noder   pathr	   r   r   r   r   r   r   r   r   r   r   r   r   rI   r
   r   r+   r   r   r   r   r   <module>   sN      _