
    ipD                        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	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dedefdZ G d d      Z G d d      Z G d d      Z G d de      Z y)     N)OrderedDict)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 timevaluereturnc                 R    | j                         } | dv ry| dv ryt        d|        )z
    Convert a string representation of truth to True or False.
    True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
    are 'n', 'no', 'f', 'false', 'off', and '0'.  Raises ValueError if
    'value' is anything else.
    )yyesttrueon1T)nnoffalseoff0Fzinvalid truth value )lower
ValueError)r
   s    U/root/searxng-venv/lib/python3.12/site-packages/valkey/commands/graph/query_result.py	strtoboolr   )   s:     KKME44	7	7/w788    c                       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 r   r   r   r   9   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+   @   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selfrE   responseprofiles       r   __init__zQueryResult.__init__P   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   rC   N)
isinstancer   strr   )rN   rO   errorversions       r   rH   zQueryResult._check_for_errorsm   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_headerrF   rI   parse_recordsrG   rN   raw_result_sets     r   rL   zQueryResult.parse_results}   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	enumeraterS   bytesdecodeSTATS
_get_value)rN   raw_statisticsidxstatsvs         r   rJ   zQueryResult.parse_statistics   ss      #>2 	4IC$&&*kkms#	4  	'A>2A}%&"	'r   c                     |d   }|S )z1
        Parse the header of the result.
        r   r)   )rN   r]   rF   s      r   rZ   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   )r`   parse_record_typesrF   )rN   r]   rowrf   cellrecordss         r   r[   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rE   get_propertyparse_scalarrN   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.
        )rS   ra   rb   rT   rN   ro   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labelrx   )intrI   appendrE   	get_labelr|   r   )rN   ro   r   labelsinner_labelrx   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_idrx   r   rE   get_relationr|   r   rN   ro   r   relationsrc_node_iddest_node_idrx   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   ru   r	   rN   ro   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   rI   ranger   ru   rN   ro   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)rN   ro   ps      r   parse_pointzQueryResult.parse_point  s2      d1g*tAw+r   c                      y)z%
        Parse a null value.
        Nr)   r~   s     r   
parse_nullzQueryResult.parse_null  s     r   c                     t        |      S )z8
        Parse the integer value from the cell.
        )r   r~   s     r   parse_integerzQueryResult.parse_integer  s     4yr   c                     t        |t              r|j                         n|}	 t        |      }|S # t        $ r% t
        j                  j                  d       d}Y |S w xY w)z4
        Parse the cell value as a boolean.
        zunknown boolean type
N)rS   ra   rb   r   r   sysstderrwrite)rN   r
   scalars      r   parse_booleanzQueryResult.parse_boolean  s^     #-UE":	u%F   	JJ56F	s   1 *AAc                     t        |      S )z-
        Parse the cell as a double.
        r   r~   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   rI   ru   rN   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   r~   s     r   parse_unknownzQueryResult.parse_unknown3  s     	

)*r   c                 V    t        |d         }|d   } | j                  |   |      }|S )zE
        Parse a scalar value from a cell in the result set.
        r   r   )r   parse_scalar_typesrN   ro   scalar_typer
   r   s        r   ru   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striprG   )rN   rO   xs      r   rK   zQueryResult.parse_profileD  s/    @HI11Q.446IIs   )7c                 2    t        | j                        dk(  S Nr   )rI   rG   rN   s    r   is_emptyzQueryResult.is_emptyG  s    4??#q((r   c                 |    |D ]7  }| |v st        |j                  d      d   j                  d      d         c S  y )Nz: r    r   )r   split)ry   r_   rg   s      r   rd   zQueryResult._get_valueJ  sH     	@Dt|TZZ-a066s;A>??	@ r   c                 @    || j                   v r| j                   |   S dS r   )r_   )rN   rg   s     r   	_get_statzQueryResult._get_statR  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_addedU  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_removedZ       ~~n--r   c                 ,    | j                  t              S )z0Returns the number of nodes created in the query)r   NODES_CREATEDr   s    r   nodes_createdzQueryResult.nodes_created_       ~~m,,r   c                 ,    | j                  t              S )z0Returns the number of nodes deleted in the query)r   NODES_DELETEDr   s    r   nodes_deletedzQueryResult.nodes_deletedd  r   r   c                 ,    | j                  t              S )z1Returns the number of properties set in the query)r   PROPERTIES_SETr   s    r   properties_setzQueryResult.properties_seti  r   r   c                 ,    | j                  t              S )z5Returns the number of properties removed in the query)r   PROPERTIES_REMOVEDr   s    r   properties_removedzQueryResult.properties_removedn  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_createds       ~~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_deletedx  r   r   c                 ,    | j                  t              S )z2Returns the number of indices created in the query)r   INDICES_CREATEDr   s    r   indices_createdzQueryResult.indices_created}       ~~o..r   c                 ,    | j                  t              S )z2Returns the number of indices deleted in the query)r   INDICES_DELETEDr   s    r   indices_deletedzQueryResult.indices_deleted  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_execution  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+   r5   r   r6   r   r7   r   r8   r   r9   r   r:   r   r<   r   r;   r   r=   r   r>   r   r?   r   r4   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&   ru   r'   r   r(   r   r%   r   r   s    r   rm   zQueryResult.parse_record_types  sN     !..0A0A ,,doo 00$// //1C1C	
 	
r   NF)+r"   r#   r$   rQ   rH   rL   rJ   rZ   r[   r|   r   r   r   r   r   r   r   r   r   r   r   r   ru   rK   r   staticmethodrd   r   propertyr   r   r   r   r   r   r   r   r   r   r   r   r   rm   r)   r   r   rA   rA   O   s   ): 
=' 	J"
&" 	
J)  G , , . . - - - - . . 2 2 5 5 5 5 / / / / 5 5 7 7 
 
  
 
r   rA   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   rQ   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   rC   NrD   rM   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    r   )rI   r   rE   r   r|   r   r   )rN   ro   r   r   rx   r   s         r   r   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)r   r   	TypeErrorr   s        r   ru   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)rl   r   r   N)r`   rm   rF   r   )rN   r]   rp   rn   rf   ro   records          r   r[   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rX   rY   r\   s     r   rL   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)rr   r   Nr   rs   rv   s         r   r|   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)r   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_map7  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_arrayG  sG      >C3u:=NO))%(333OO 4Os%   AAA 
AA AANr   )r"   r#   r$   __doc__rQ   r  r   ru   r[   rL   r|   r   r   r   r   r)   r   r   r   r     s@    
<J"
C
&" r   r   )!r   collectionsr   valkeyr   edger   
exceptionsr   noder   pathr	   r   r   r   r   r   r   r   r   r   r   r   r   rc   rT   boolr   r   r+   rA   r   r)   r   r   <module>r     s    
 # !  0  !/ !) / ##% 3  	 9S 9T 9   Z
 Z
z
`{ `r   