
    +hB                         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y)    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y)ResultSetColumnTypesr   r         N)__name__
__module____qualname__COLUMN_UNKNOWNCOLUMN_SCALARCOLUMN_NODECOLUMN_RELATION     T/var/www/html/venv/lib/python3.12/site-packages/redis/commands/graph/query_result.pyr   r   *   s    NMKOr   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y)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>    MJLMMLKJJJIKr   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*y'))QueryResultc                    || _         g | _        g | _        | j                  |       t	        |      dk(  r| j                  |d          y|r| j                  |       y| j                  |d          | j                  |       y)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   sz     
 	x(x=A!!(1+.x( !!(2,/x(r   c                     t        |d   t              r%|d   }t        |      dk(  r|d   }t        |      }|t        |d   t              r|d   y)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QKE5z//"1+09K hrlM22, 3r   c                     | j                  |      | _        t        | j                        dk(  ry| j                  |      | _        y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   s>     ''7 t{{q ,,^<r   c                     i | _         t        |      D ])  \  }}t        |t              s|j	                         ||<   + t
        D ]&  }| j                  ||      }||| j                   |<   ( y)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   ss      #>2 	4IC$&&*kkms#	4  	'A>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                     |d   D cg c]C  }t        |      D cg c]*  \  }} | j                  | j                  |   d      |      , c}}E }}}}|S c c}}w c c}}}w )F
        Parses the result set and returns a list of records.
        r   r   )rO   parse_record_typesr5   )r=   rL   rowrU   cellrecordss         r   rJ   zQueryResult.parse_records   sy     &a(
 

  "+3C =''C(8(;<TB
 
 
s   A/AAAc                     i }|D ]9  }| j                   j                  |d         }| j                  |dd       }|||<   ; |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   sX    
 
 	/D

//Q8I**484J$.Jy!	/
 r   c                 |    t        |t              r|j                         S t        |t              st        |      S |S )z-
        Parse the cell as a string.
        )rB   rP   rQ   rC   r=   r^   s     r   parse_stringzQueryResult.parse_string   s3     dE";;= D#&t9Kr   c                     t        |d         }d}t        |d         dkD  r6g }|d   D ],  }|j                  | j                  j	                  |             . | j                  |d         }t        |||      S )z+
        Parse the cell to a node.
        r   Nr   r   node_idlabelrg   )intr8   appendr4   	get_labelrk   r	   )r=   r^   rq   labelsinner_labelrg   s         r   
parse_nodezQueryResult.parse_node   s~     d1g,tAw<!F#Aw Adjj22;?@A11$q':
G6jIIr   c                     t        |d         }| j                  j                  |d         }t        |d         }t        |d         }| j                  |d         }t	        |||||      S ),
        Parse the cell to an edge.
        r   r   r   r   r   edge_idrg   rs   r4   get_relationrk   r   r=   r^   r|   relationsrc_node_iddest_node_idrg   s          r   
parse_edgezQueryResult.parse_edge   so     d1g,::**473$q'l47|11$q':
<Z
 	
r   c                 j    | j                  |d         }| j                  |d         }t        ||      S )+
        Parse the cell to a path.
        r   r   rd   r
   r=   r^   nodesedgess       r   
parse_pathzQueryResult.parse_path   s9     !!$q'*!!$q'*E5!!r   c                     t               }t        |      }t        d|d      D ]0  }| j                  ||         }| j	                  ||dz            ||<   2 |S )z*
        Parse the cell as a map.
        r   r   r   r   r8   rangern   rd   r=   r^   m	n_entriesikeys         r   	parse_mapzQueryResult.parse_map   se     MI	
 q)Q' 	4A##DG,C&&tAE{3AcF	4 r   c                 N    i }t        |d         |d<   t        |d         |d<   |S )z*
        Parse the cell to point.
        r   latituder   	longitudefloat)r=   r^   ps      r   parse_pointzQueryResult.parse_point   s2      d1g*tAw+r   c                      y)z%
        Parse a null value.
        Nr   rm   s     r   
parse_nullzQueryResult.parse_null   s     r   c                     t        |      S )z8
        Parse the integer value from the cell.
        )rs   rm   s     r   parse_integerzQueryResult.parse_integer  s     4yr   c                     t        |t              r|j                         n|}	 t        |      rd}|S d}|S # t        $ r% t
        j                  j                  d       d}Y |S w xY w)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  sn     #-UE":	&u-TF 	 49F   	JJ56F	s   7 7 *A%$A%c                     t        |      S )z-
        Parse the cell as a double.
        r   rm   s     r   parse_doublezQueryResult.parse_double  s     T{r   c                 v    t        t        |            D cg c]  }| j                  ||          }}|S c c}w )z+
        Parse an array of values.
        r   r8   rd   r=   r   r   r   s       r   parse_arrayzQueryResult.parse_array  s:     8=SZ7HI!$##E!H-II Js   6c                 B    t         j                  j                  d       y)z/
        Parse a cell of unknown type.
        zUnknown type
N)r   r   r   rm   s     r   parse_unknownzQueryResult.parse_unknown$  s     	

)*r   c                 V    t        |d         }|d   } | j                  |   |      }|S )zE
        Parse a scalar value from a cell in the result set.
        r   r   )rs   parse_scalar_typesr=   r^   scalar_typer   r   s        r   rd   zQueryResult.parse_scalar+  s6     $q'lQ5((5e<r   c                 x    |D cg c]$  }|d|j                  d       j                         & c}| _        y c c}w )Nr   ,)indexstripr6   )r=   r>   xs      r   r:   zQueryResult.parse_profile5  s/    @HI11Q.446IIs   )7c                 2    t        | j                        dk(  S Nr   )r8   r6   r=   s    r   is_emptyzQueryResult.is_empty8  s    4??#q((r   c                 |    |D ]7  }| |v st        |j                  d      d   j                  d      d         c S  y )Nz: r    r   )r   split)rh   rN   rV   s      r   rS   zQueryResult._get_value;  sH     	@Dt|TZZ-a066s;A>??	@ r   c                 @    || j                   v r| j                   |   S dS r   )rN   )r=   rV   s     r   	_get_statzQueryResult._get_statC  s!    (,(?tt$FQFr   c                 ,    | j                  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                 ,    | j                  t              S )z1Returns the number of labels removed in the query)r   LABELS_REMOVEDr   s    r   labels_removedzQueryResult.labels_removedK       ~~n--r   c                 ,    | j                  t              S )z0Returns the number of nodes created in the query)r   NODES_CREATEDr   s    r   nodes_createdzQueryResult.nodes_createdP       ~~m,,r   c                 ,    | j                  t              S )z0Returns the number of nodes deleted in the query)r   NODES_DELETEDr   s    r   nodes_deletedzQueryResult.nodes_deletedU  r   r   c                 ,    | j                  t              S )z1Returns the number of properties set in the query)r   PROPERTIES_SETr   s    r   properties_setzQueryResult.properties_setZ  r   r   c                 ,    | j                  t              S )z5Returns the number of properties removed in the query)r   PROPERTIES_REMOVEDr   s    r   properties_removedzQueryResult.properties_removed_  s     ~~011r   c                 ,    | j                  t              S )z8Returns the number of relationships created in the query)r   RELATIONSHIPS_CREATEDr   s    r   relationships_createdz!QueryResult.relationships_createdd       ~~344r   c                 ,    | j                  t              S )z8Returns the number of relationships deleted in the query)r   RELATIONSHIPS_DELETEDr   s    r   relationships_deletedz!QueryResult.relationships_deletedi  r   r   c                 ,    | j                  t              S )z2Returns the number of indices created in the query)r   INDICES_CREATEDr   s    r   indices_createdzQueryResult.indices_createdn       ~~o..r   c                 ,    | j                  t              S )z2Returns the number of indices deleted in the query)r   INDICES_DELETEDr   s    r   indices_deletedzQueryResult.indices_deleteds  r   r   c                 2    | j                  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                 ,    | j                  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6r   c                 v   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.                  | j0                  iS N)r   r$   r   r%   rn   r&   r   r'   r   r(   r   r)   r   r+   rx   r*   r   r,   r   r-   r   r.   r   r#   r   r   s    r   r   zQueryResult.parse_scalar_types  s     !++T__ --t/@/@ ..0B0B ..0B0B --t/@/@ ,,d.>.> ++T__ ++T__ ++T__ **DNN ,,d.>.> ..0B0B
 	
r   c                     t         j                  | j                  t         j                  | j                  t         j
                  | j                  t         j                  | j                  iS r   )	r   r   rd   r   rx   r   r   r   r   r   s    r   r\   zQueryResult.parse_record_types  sN     !..0A0A ,,doo 00$// //1C1C	
 	
r   NF)+r   r   r   r@   r7   r;   r9   rI   rJ   rk   rn   rx   r   r   r   r   r   r   r   r   r   r   rd   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)  G , , . . - - - - . . 2 2 5 5 5 5 / / / / 5 5 7 7 
 
  
 
r   r0   c                   T    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y)AsyncQueryResultzp
    Async version for the QueryResult class - a class that
    represents a result of the query operation.
    c                      y)zC
        To init the class you must call self.initialize()
        Nr   r   s    r   r@   zAsyncQueryResult.__init__  s     	r   c                 *  K   || _         g | _        g | _        | j                  |       t	        |      dk(  r| j                  |d          | S |r| j                  |       | S | j                  |d          | j                  |       d{    | S 7 w)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... /s   BB
BBc                 $  K   d}t        |d         dkD  r>g }|d   D ]4  }|j                  | j                  j                  |       d{          6 | j	                  |d          d{   }t        |d         }t        |||      S 7 B7 "w)z.
        Parses a node from the cell.
        Nr   r   r   rp   )r8   rt   r4   ru   rk   rs   r	   )r=   r^   rv   rw   rg   rq   s         r   rx   zAsyncQueryResult.parse_node  s      tAw<!F#Aw GDJJ$8$8$EEFG77Q@@
d1g,G6jII F@s$   AB	B

!B+B,!BBc                    K   t        |d         }|d   }	  | j                  |   |       d{   }|S 7 # t        $ r  | j                  |   |      }Y |S w xY ww)zA
        Parses a scalar value from the server response.
        r   r   N)rs   r   	TypeErrorr   s        r   rd   zAsyncQueryResult.parse_scalar  sw      $q'lQ	A?422;?FFF
  G 	A9T,,[9%@F		As1   A8 68 A8 AAAAc           	         K   g }|d   D ][  }t        |      D cg c]2  \  }} | j                  | j                  |   d      |       d{   4 }}}|j                  |       ] |S 7 c c}}w w)r[   r   r   N)rO   r\   r5   rt   )r=   rL   r_   r]   rU   r^   records          r   rJ   zAsyncQueryResult.parse_records  s      !!$ 	#C "+3C Cd--dkk#.>q.AB4HHHF  NN6"	#  Is(   A20A,A*A,A2*A,,A2c                    K   | j                  |      | _        t        | j                        dk(  ry| j                  |       d{   | _        y7 
wrG   rH   rK   s     r   r;   zAsyncQueryResult.parse_results  sH      ''7 t{{q  $ 2 2> BBBs   AAAAc                    K   i }|D ]I  }| j                   j                  |d          d{   }| j                  |dd        d{   }|||<   K |S 7 )7 w)ra   r   Nr   rb   re   s         r   rk   z(AsyncQueryResult.parse_entity_properties   sk     
 
 	/D"jj55d1g>>I#00ab::J$.Jy!	/
 	 ?:s!   )AAAAAAc                 
  K   t        |d         }| j                  j                  |d          d{   }t        |d         }t        |d         }| j                  |d          d{   }t	        |||||      S 7 L7 w)rz   r   r   Nr   r   r   r{   r}   r   s          r   r   zAsyncQueryResult.parse_edge  s      d1g,00a99$q'l47|77Q@@
<Z
 	
	 : As!   0BA?7B*B+BBc                    K   | j                  |d          d{   }| j                  |d          d{   }t        ||      S 7 ,7 w)r   r   Nr   r   r   s       r   r   zAsyncQueryResult.parse_path   sL      ''Q00''Q00E5!! 10s   AAAA	A	Ac                    K   t               }t        |      }t        d|d      D ]8  }| j                  ||         }| j	                  ||dz             d{   ||<   : |S 7 w)z*
        Parse the cell to a map.
        r   r   r   Nr   r   s         r   r   zAsyncQueryResult.parse_map(  sq      MI	
 q)Q' 	:A##DG,C,,T!a%[99AcF	:  :s   AA$A"A$c                    K   t        t        |            D cg c]  }| j                  ||          d{     }}|S 7 
c c}w w)z$
        Parse array value.
        Nr   r   s       r   r   zAsyncQueryResult.parse_array8  sG      >C3u:=NO))%(333OO 4Os%   AAA 
AA AANr   )r   r   r   __doc__r@   r   rx   rd   rJ   r;   rk   r   r   r   r   r   r   r   r   r     s@    
<J"
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  !/ !) / ##% 3  	   Z
 Z
z
`{ `r   