U
    cc+                  
   @   s   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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dS )    )OrderedDict)ResponseError   )Edge)VersionMismatchException)Node)PathzLabels addedzNodes createdzNodes deletedzRelationships deletedzProperties setz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__ZCOLUMN_UNKNOWNCOLUMN_SCALARCOLUMN_NODECOLUMN_RELATION r   r   [/var/www/html/project/venv/lib/python3.8/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   +   s   r   c                   @   s  e Z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d Zdd Zdd Zdd Zdd Zd d! Zed"d# Zd$d% Zed&d' Zed(d) Zed*d+ Zed,d- Zed.d/ Zed0d1 Zed2d3 Zed4d5 Zed6d7 Zed8d9 Z d:S )<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__;   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 )Nr   zversion mismatchr   r*   )
isinstancer   strr   )r3   r4   errorversionr   r   r   r.   X   s    zQueryResult._check_for_errorsc                 C   s.   |  || _t| jdkrd S | || _d S Nr   )parse_headerr,   r/   parse_recordsr-   )r3   raw_result_setr   r   r   r2   e   s    zQueryResult.parse_resultsc                 C   sZ   i | _ t|D ]\}}t|tr| ||< qtD ]"}| ||}|d k	r2|| j |< q2d S N)
statistics	enumerater6   bytesdecodeSTATS
_get_value)r3   Zraw_statisticsidxstatsvr   r   r   r0   n   s    
zQueryResult.parse_statisticsc                 C   s   |d }|S r:   r   )r3   r=   r,   r   r   r   r;   {   s    zQueryResult.parse_headerc                 C   s   g }|d }|D ]}g }t |D ]\}}| j| d tjkrN|| | q | j| d tjkrt|| | q | j| d tjkr|| 	| q t
d q || q|S )Nr   r   zUnknown column type.
)r@   r,   r	   r   appendparse_scalarr   
parse_noder   
parse_edgeprint)r3   r=   recordsr-   rowrecordrE   cellr   r   r   r<      s    
zQueryResult.parse_recordsc                 C   s<   i }|D ].}| j |d }| |dd  }|||< q|S Nr   r   )r+   Zget_propertyrJ   )r3   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 r>   )r6   rA   rB   r7   )r3   rQ   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 )Nr   r   r
   )node_idlabelrT   )intr/   rI   r+   Z	get_labelrV   r   )r3   rQ   rX   labelsZinner_labelrT   r   r   r   rK      s    zQueryResult.parse_nodec                 C   sT   t |d }| j|d }t |d }t |d }| |d }t|||||dS )Nr   r   r
   r   r   )edge_idrT   )rZ   r+   Zget_relationrV   r   )r3   rQ   r\   ZrelationZsrc_node_idZdest_node_idrT   r   r   r   rL      s        zQueryResult.parse_edgec                 C   s&   |  |d }|  |d }t||S rR   )rJ   r   )r3   rQ   Znodesedgesr   r   r   
parse_path   s    zQueryResult.parse_pathc                 C   sH   t  }t|}td|dD ](}| || }| ||d  ||< q|S )Nr   r
   r   )r   r/   rangerW   rJ   )r3   rQ   mZ	n_entriesikeyr   r   r   	parse_map   s    zQueryResult.parse_mapc                 C   s(   i }t |d |d< t |d |d< |S )Nr   Zlatituder   Z	longitude)float)r3   rQ   pr   r   r   parse_point   s    zQueryResult.parse_pointc                 C   sz  t |d }|d }d }|tjkr*d }nL|tjkrB| |}n4|tjkrXt |}n|tjkrt|trt|	 n|}|dkrd}n|dkrd}nt
d n|tjkrt|}n|tjkr| }}tt|D ]}| || ||< qn|tjkr| |}nt|tjkr| |}n\|tjkr2| |}nD|tjkrJ| |}n,|tjkrb| |}n|tjkrvt
d |S )	Nr   r   trueTfalseFzUnknown boolean type
zUnknown scalar type
)rZ   r   r   r   rW   r    r!   r6   rA   rB   rM   r"   rd   r#   r_   r/   rJ   r%   rK   r$   rL   r&   r^   r'   rc   r(   rf   r   )r3   rQ   Zscalar_typevalueZscalararrayra   r   r   r   rJ      sF    







zQueryResult.parse_scalarc                 C   s   dd |D | _ d S )Nc                 S   s"   g | ]}|d | d  qS )r   ,)indexstrip).0xr   r   r   
<listcomp>  s     z-QueryResult.parse_profile.<locals>.<listcomp>)r-   )r3   r4   r   r   r   r1     s    zQueryResult.parse_profilec                 C   s   t | jdkS r:   )r/   r-   r3   r   r   r   is_empty6  s    zQueryResult.is_emptyc                 C   s6   |D ],}| |krt |dd dd   S qd S )Nz: r    r   )rd   split)rU   r?   rF   r   r   r   rD   9  s    "zQueryResult._get_valuec                 C   s   || j kr| j | S dS r:   )r?   )r3   rF   r   r   r   	_get_statA  s    zQueryResult._get_statc                 C   s
   |  tS r>   )ru   LABELS_ADDEDrq   r   r   r   labels_addedD  s    zQueryResult.labels_addedc                 C   s
   |  tS r>   )ru   NODES_CREATEDrq   r   r   r   nodes_createdH  s    zQueryResult.nodes_createdc                 C   s
   |  tS r>   )ru   NODES_DELETEDrq   r   r   r   nodes_deletedL  s    zQueryResult.nodes_deletedc                 C   s
   |  tS r>   )ru   PROPERTIES_SETrq   r   r   r   properties_setP  s    zQueryResult.properties_setc                 C   s
   |  tS r>   )ru   RELATIONSHIPS_CREATEDrq   r   r   r   relationships_createdT  s    z!QueryResult.relationships_createdc                 C   s
   |  tS r>   )ru   RELATIONSHIPS_DELETEDrq   r   r   r   relationships_deletedX  s    z!QueryResult.relationships_deletedc                 C   s
   |  tS r>   )ru   INDICES_CREATEDrq   r   r   r   indices_created\  s    zQueryResult.indices_createdc                 C   s
   |  tS r>   )ru   INDICES_DELETEDrq   r   r   r   indices_deleted`  s    zQueryResult.indices_deletedc                 C   s   |  tdkS )Nr   )ru   CACHED_EXECUTIONrq   r   r   r   cached_executiond  s    zQueryResult.cached_executionc                 C   s
   |  tS r>   )ru   INTERNAL_EXECUTION_TIMErq   r   r   r   run_time_msh  s    zQueryResult.run_time_msN)F)!r   r   r   r5   r.   r2   r0   r;   r<   rV   rW   rK   rL   r^   rc   rf   rJ   r1   rr   staticmethodrD   ru   propertyrw   ry   r{   r}   r   r   r   r   r   r   r   r   r   r   r)   :   sN   
	
4$









r)   N)collectionsr   Zredisr   edger   
exceptionsr   noder   pathr   rv   rx   rz   r   r|   r~   r   r   r   r   rC   r	   r   r)   r   r   r   r   <module>   s:   