
    PfB                        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          Z G d d          Z G d d          Z G 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                       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     S/var/www/html/env/lib/python3.11/site-packages/redis/commands/graph/query_result.pyr   r   *   s"        NMKOOOr   r   c                   >    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   sJ        MJLMMLKJJJIKKKr   r   c                      e Zd Zd)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 Zd Zd Zd Zd Zd Zd Zd Zd Zed             Zd Zed             Zed             Zed             Zed             Z ed             Z!ed             Z"ed              Z#ed!             Z$ed"             Z%ed#             Z&ed$             Z'ed%             Z(ed&             Z)ed'             Z*d(S )*QueryResultFc                 L   || _         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selfr4   responseprofiles       r   __init__zQueryResult.__init__A   s     
 	x(((x==A!!(1+..... 	)x((((( !!(2,///x(((((r   c                     t          |d         t                    r4|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   r2   N)
isinstancer   strr   )r=   r>   errorversions       r   r7   zQueryResult._check_for_errors^   s|     hqk=11 	QKE5zz///"1+099K hrlM22 	2,	 	r   c                     |                      |          | _        t          | j                  dk    rdS |                     |          | _        dS zL
        Parse the query execution result returned from the server.
        r   Nparse_headerr5   r8   parse_recordsr6   r=   raw_result_sets     r   r;   zQueryResult.parse_resultsn   sN     ''77 t{q  F,,^<<r   c                     i | _         t          |          D ]1\  }}t          |t                    r|                                ||<   2t
          D ]$}|                     ||          }|
|| j         |<   %dS )z@
        Parse the statistics returned in the response.
        N)
statistics	enumeraterB   bytesdecodeSTATS
_get_value)r=   raw_statisticsidxstatsvs         r   r9   zQueryResult.parse_statisticsz   s      #>22 	4 	4IC$&& 4&*kkmms# 	' 	'A>22A}%&"	' 	'r   c                     |d         }|S )z1
        Parse the header of the result.
        r   r   )r=   rL   r5   s      r   rI   zQueryResult.parse_header   s    
  "r   c                 0      fd|d         D             }|S )F
        Parses the result set and returns a list of records.
        c                 F    g | ]}fd t          |          D             S )c                 d    g | ],\  }} j         j        |         d                   |          -S )r   parse_record_typesr5   .0rU   cellr=   s      r   
<listcomp>z8QueryResult.parse_records.<locals>.<listcomp>.<listcomp>   sK       C ='C(8(;<TBB  r   )rO   )ra   rowr=   s     r   rc   z-QueryResult.parse_records.<locals>.<listcomp>   sR     
 
 

 	   !*3  
 
 
r   r   r   )r=   rL   recordss   `  r   rJ   zQueryResult.parse_records   s9    
 
 
 

 &a(
 
 
 r   c                     i }|D ]D}| j                             |d                   }|                     |dd                   }|||<   E|S )/
        Parse node / edge properties.
        r   r   Nr4   get_propertyparse_scalarr=   props
propertiesprop	prop_name
prop_values         r   parse_entity_propertiesz#QueryResult.parse_entity_properties   s_    
 
 	/ 	/D
//Q88I**4844J$.Jy!!r   c                     t          |t                    r|                                S t          |t                    st          |          S |S )z-
        Parse the cell as a string.
        )rB   rP   rQ   rC   r=   rb   s     r   parse_stringzQueryResult.parse_string   sG     dE"" 	;;== D#&& 	t99Kr   c                 0   t          |d                   }d}t          |d                   dk    r:g }|d         D ]/}|                    | j                            |                     0|                     |d                   }t          |||          S )z+
        Parse the cell to a node.
        r   Nr   r   node_idlabelrm   )intr8   appendr4   	get_labelrq   r	   )r=   rb   rw   labelsinner_labelrm   s         r   
parse_nodezQueryResult.parse_node   s     d1g,,tAw<<!F#Aw A Adj22;??@@@@11$q'::
G6jIIIIr   c                    t          |d                   }| j                            |d                   }t          |d                   }t          |d                   }|                     |d                   }t	          |||||          S ),
        Parse the cell to an edge.
        r   r   r   r   r   edge_idrm   ry   r4   get_relationrq   r   r=   rb   r   relationsrc_node_iddest_node_idrm   s          r   
parse_edgezQueryResult.parse_edge   s     d1g,,:**4733$q'll47||11$q'::
<Z
 
 
 	
r   c                     |                      |d                   }|                      |d                   }t          ||          S )+
        Parse the cell to a path.
        r   r   rj   r
   r=   rb   nodesedgess       r   
parse_pathzQueryResult.parse_path   sB     !!$q'**!!$q'**E5!!!r   c                     t                      }t          |          }t          d|d          D ]>}|                     ||                   }|                     ||dz                      ||<   ?|S )z*
        Parse the cell as a map.
        r   r   r   r   r8   rangert   rj   r=   rb   m	n_entriesikeys         r   	parse_mapzQueryResult.parse_map   sr     MMII	
 q)Q'' 	4 	4A##DG,,C&&tAE{33AcFFr   c                 j    i }t          |d                   |d<   t          |d                   |d<   |S )z*
        Parse the cell to point.
        r   latituder   	longitudefloat)r=   rb   ps      r   parse_pointzQueryResult.parse_point   s6      d1g*tAw+r   c                     dS )z%
        Parse a null value.
        Nr   rs   s     r   
parse_nullzQueryResult.parse_null   s	     tr   c                      t          |          S )z8
        Parse the integer value from the cell.
        )ry   rs   s     r   parse_integerzQueryResult.parse_integer  s     4yyr   c                     t          |t                    r|                                n|}	 t          |          rdnd}n1# t          $ r$ t
          j                            d           d}Y nw xY w|S )z4
        Parse the cell value as a boolean.
        TFzunknown boolean type
N)rB   rP   rQ   r   
ValueErrorsysstderrwriter=   valuescalars      r   parse_booleanzQueryResult.parse_boolean  s     #-UE":":E	&u--8TT5FF 	 	 	J5666FFF	 s   A +A/.A/c                      t          |          S )z-
        Parse the cell as a double.
        r   rs   s     r   parse_doublezQueryResult.parse_double  s     T{{r   c                 \      fdt          t                              D             }|S )z+
        Parse an array of values.
        c                 F    g | ]}                     |                   S r   rj   ra   r   r=   r   s     r   rc   z+QueryResult.parse_array.<locals>.<listcomp>!  s+    III!$##E!H--IIIr   r   r8   r   s   `` r   parse_arrayzQueryResult.parse_array  s6     JIIIIuSZZ7H7HIIIr   c                 D    t           j                            d           dS )z/
        Parse a cell of unknown type.
        zUnknown type
N)r   r   r   rs   s     r   parse_unknownzQueryResult.parse_unknown$  s      	
)***tr   c                 l    t          |d                   }|d         } | j        |         |          }|S )zE
        Parse a scalar value from a cell in the result set.
        r   r   )ry   parse_scalar_typesr=   rb   scalar_typer   r   s        r   rj   zQueryResult.parse_scalar+  s8     $q'llQ5(5e<<r   c                 (    d |D             | _         d S )Nc                 l    g | ]1}|d |                     d                                                   2S )r   ,)indexstrip)ra   xs     r   rc   z-QueryResult.parse_profile.<locals>.<listcomp>6  s8    III11Q-.4466IIIr   )r6   )r=   r>   s     r   r:   zQueryResult.parse_profile5  s    IIIIIr   c                 2    t          | j                  dk    S Nr   )r8   r6   r=   s    r   is_emptyzQueryResult.is_empty8  s    4?##q((r   c                     |D ]I}| |v rCt          |                    d          d                             d          d                   c S Jd S )Nz: r    r   )r   split)rn   rN   rV   s      r   rS   zQueryResult._get_value;  s`     	@ 	@Dt||TZZ--a066s;;A>?????  tr   c                 2    || j         v r| j         |         ndS r   )rN   )r=   rV   s     r   	_get_statzQueryResult._get_statC  s     (,(?(?tt$$QFr   c                 6    |                      t                    S )z/Returns the number of labels added in the query)r   LABELS_ADDEDr   s    r   labels_addedzQueryResult.labels_addedF  s     ~~l+++r   c                 6    |                      t                    S )z1Returns the number of labels removed in the query)r   LABELS_REMOVEDr   s    r   labels_removedzQueryResult.labels_removedK       ~~n---r   c                 6    |                      t                    S )z0Returns the number of nodes created in the query)r   NODES_CREATEDr   s    r   nodes_createdzQueryResult.nodes_createdP       ~~m,,,r   c                 6    |                      t                    S )z0Returns the number of nodes deleted in the query)r   NODES_DELETEDr   s    r   nodes_deletedzQueryResult.nodes_deletedU  r   r   c                 6    |                      t                    S )z1Returns the number of properties set in the query)r   PROPERTIES_SETr   s    r   properties_setzQueryResult.properties_setZ  r   r   c                 6    |                      t                    S )z5Returns the number of properties removed in the query)r   PROPERTIES_REMOVEDr   s    r   properties_removedzQueryResult.properties_removed_  s     ~~0111r   c                 6    |                      t                    S )z8Returns the number of relationships created in the query)r   RELATIONSHIPS_CREATEDr   s    r   relationships_createdz!QueryResult.relationships_createdd       ~~3444r   c                 6    |                      t                    S )z8Returns the number of relationships deleted in the query)r   RELATIONSHIPS_DELETEDr   s    r   relationships_deletedz!QueryResult.relationships_deletedi  r   r   c                 6    |                      t                    S )z2Returns the number of indices created in the query)r   INDICES_CREATEDr   s    r   indices_createdzQueryResult.indices_createdn       ~~o...r   c                 6    |                      t                    S )z2Returns the number of indices deleted in the query)r   INDICES_DELETEDr   s    r   indices_deletedzQueryResult.indices_deleteds  r   r   c                 >    |                      t                    dk    S )z:Returns whether or not the query execution plan was cachedr   )r   CACHED_EXECUTIONr   s    r   cached_executionzQueryResult.cached_executionx  s     ~~.//144r   c                 6    |                      t                    S )z.Returns the server execution time of the query)r   INTERNAL_EXECUTION_TIMEr   s    r   run_time_mszQueryResult.run_time_ms}  s     ~~5666r   c                    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%   rt   r&   r   r'   r   r(   r   r)   r   r+   r~   r*   r   r,   r   r-   r   r.   r   r#   r   r   s    r   r   zQueryResult.parse_scalar_types  s     !+T_ -t/@ .0B .0B -t/@ ,d.> +T_ +T_ +T_ *DN ,d.> .0B
 	
r   c                     t           j        | j        t           j        | j        t           j        | j        t           j        | j        iS r   )	r   r   rj   r   r~   r   r   r   r   r   s    r   r_   zQueryResult.parse_record_types  s8     !.0A ,do 0$/ /1C	
 	
r   NF)+r   r   r   r@   r7   r;   r9   rI   rJ   rq   rt   r~   r   r   r   r   r   r   r   r   r   r   rj   r:   r   staticmethodrS   r   propertyr   r   r   r   r   r   r   r   r   r   r   r   r   r_   r   r   r   r0   r0   @   s       ) ) ) ):   
= 
= 
=' ' '       	 	 	J J J"
 
 
&" " "   	 	 	    
 
 
        J J J) ) )   \G G G , , X, . . X. - - X- - - X- . . X. 2 2 X2 5 5 X5 5 5 X5 / / X/ / / X/ 5 5 X5 7 7 X7 
 
 X
  
 
 X
 
 
r   r0   c                   V    e Zd ZdZd ZddZd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd ZdS )AsyncQueryResultzp
    Async version for the QueryResult class - a class that
    represents a result of the query operation.
    c                     dS )zC
        To init the class you must call self.initialize()
        Nr   r   s    r   r@   zAsyncQueryResult.__init__  s	     	r   Fc                 X  K   || _         g | _        g | _        |                     |           t	          |          dk    r|                     |d                    nN|r|                     |           n6|                     |d                    |                     |           d{V  | 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   r2   Nr3   r<   s       r   
initializezAsyncQueryResult.initialize  s       
 	x(((x==A!!(1+.... 	/x(((( !!(2,///$$X.........r   c                 L  K   d}t          |d                   dk    r@g }|d         D ]5}|                    | j                            |           d{V            6|                     |d                    d{V }t          |d                   }t          |||          S )z.
        Parses a node from the cell.
        Nr   r   r   rv   )r8   rz   r4   r{   rq   ry   r	   )r=   rb   r|   r}   rm   rw   s         r   r~   zAsyncQueryResult.parse_node  s       tAw<<!F#Aw G GDJ$8$8$E$EEEEEEEFFFF77Q@@@@@@@@
d1g,,G6jIIIIr   c                    K   t          |d                   }|d         }	  | j        |         |           d{V }n&# t          $ r  | j        |         |          }Y nw xY w|S )zA
        Parses a scalar value from the server response.
        r   r   N)ry   r   	TypeErrorr   s        r   rj   zAsyncQueryResult.parse_scalar  s       $q'llQ	A?42;?FFFFFFFFFF 	A 	A 	A9T,[9%@@FFF	A s   >  A! A!c                     K   g }|d         D ]8} fdt          |          D              d{V }|                    |           9|S )r[   r   c                 t   K   g | ]2\  }} j         j        |         d                   |           d{V 3S )r   Nr^   r`   s      r   rc   z2AsyncQueryResult.parse_records.<locals>.<listcomp>  sb         C Cd-dk#.>q.AB4HHHHHHHH  r   N)rO   rz   )r=   rL   re   rd   records   `    r   rJ   zAsyncQueryResult.parse_records  s       !!$ 	# 	#C   !*3        F NN6""""r   c                    K   |                      |          | _        t          | j                  dk    rdS |                     |           d{V | _        dS rG   rH   rK   s     r   r;   zAsyncQueryResult.parse_results  sd       ''77 t{q  F $ 2 2> B BBBBBBBr   c                    K   i }|D ]P}| j                             |d                    d{V }|                     |dd                    d{V }|||<   Q|S )rg   r   Nr   rh   rk   s         r   rq   z(AsyncQueryResult.parse_entity_properties   s      
 
 	/ 	/D"j55d1g>>>>>>>>I#00abb::::::::J$.Jy!!r   c                 :  K   t          |d                   }| j                            |d                    d{V }t          |d                   }t          |d                   }|                     |d                    d{V }t	          |||||          S )r   r   r   Nr   r   r   r   r   r   s          r   r   zAsyncQueryResult.parse_edge  s       d1g,,00a99999999$q'll47||77Q@@@@@@@@
<Z
 
 
 	
r   c                    K   |                      |d                    d{V }|                      |d                    d{V }t          ||          S )r   r   Nr   r   r   s       r   r   zAsyncQueryResult.parse_path   sj       ''Q00000000''Q00000000E5!!!r   c                    K   t                      }t          |          }t          d|d          D ]D}|                     ||                   }|                     ||dz                       d{V ||<   E|S )z*
        Parse the cell to a map.
        r   r   r   Nr   r   s         r   r   zAsyncQueryResult.parse_map(  s       MMII	
 q)Q'' 	: 	:A##DG,,C,,T!a%[99999999AcFFr   c                 l    K    fdt          t                              D              d{V }|S )z$
        Parse array value.
        c                 V   K   g | ]#}                     |                    d {V $S r   r   r   s     r   rc   z0AsyncQueryResult.parse_array.<locals>.<listcomp><  sA      OOO))%(33333333OOOr   Nr   r   s   `` r   r   zAsyncQueryResult.parse_array8  sL       POOOOU3u::=N=NOOOOOOOOOr   Nr   )r   r   r   __doc__r@   r   r~   rj   rJ   r;   rq   r   r   r   r   r   r   r   r   r     s         
     <J J J"    
C 
C 
C  
 
 
&" " "       r   r   ) r   collectionsr   distutils.utilr   redisr   edger   
exceptionsr   noder	   pathr
   r   r   r   r   r   r   r   r   r   r   r   r   rR   r   r   r0   r   r   r   r   <module>r     s   



 # # # # # # $ $ $ $ $ $              0 0 0 0 0 0            !/ !) / ##% 3  	               Z
 Z
 Z
 Z
 Z
 Z
 Z
 Z
z
` ` ` ` `{ ` ` ` ` `r   