
    j,gqD                         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 G d d      Z G d d      Z G d d      Z G d de      Zd 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 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     {/var/www/trellinator.diamondhoofcare.com/public_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   0   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selfr3   responseprofiles       r   __init__zQueryResult.__init__@   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   r1   N)
isinstancer   strr   )r<   r=   errorversions       r   r6   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_headerr4   r7   parse_recordsr5   r<   raw_result_sets     r   r:   zQueryResult.parse_resultsm   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	enumeraterA   bytesdecodeSTATS
_get_value)r<   raw_statisticsidxstatsvs         r   r8   zQueryResult.parse_statisticsy   ss      #>2 	4IC$&&*kkms#	4  	'A>2A}%&"	'r   c                     |d   }|S )z1
        Parse the header of the result.
        r   r   )r<   rK   r4   s      r   rH   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   )rN   parse_record_typesr4   )r<   rK   rowrT   cellrecordss         r   rI   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r3   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.
        )rA   rO   rP   rB   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labelrf   )intr7   appendr3   	get_labelrj   r   )r<   r]   rp   labelsinner_labelrf   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_idrf   rr   r3   get_relationrj   r   r<   r]   r{   relationsrc_node_iddest_node_idrf   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   rc   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   r7   rangerm   rc   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   rl   s     r   
parse_nullzQueryResult.parse_null   s     r   c                     t        |      S )z8
        Parse the integer value from the cell.
        )rr   rl   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)rA   rO   rP   	strtobool
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   rl   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   r7   rc   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   rl   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   )rr   parse_scalar_typesr<   r]   scalar_typer   r   s        r   rc   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stripr5   )r<   r=   xs      r   r9   zQueryResult.parse_profile4  s/    @HI11Q.446IIs   )7c                 2    t        | j                        dk(  S Nr   )r7   r5   r<   s    r   is_emptyzQueryResult.is_empty7  s    4??#q((r   c                 |    |D ]7  }| |v st        |j                  d      d   j                  d      d         c S  y )Nz: r    r   )r   split)rg   rM   rU   s      r   rR   zQueryResult._get_value:  sH     	@Dt|TZZ-a066s;A>??	@ r   c                 @    || j                   v r| j                   |   S dS r   )rM   )r<   rU   s     r   	_get_statzQueryResult._get_statB  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_addedE  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_removedJ       ~~n--r   c                 ,    | j                  t              S )z0Returns the number of nodes created in the query)r   NODES_CREATEDr   s    r   nodes_createdzQueryResult.nodes_createdO       ~~m,,r   c                 ,    | j                  t              S )z0Returns the number of nodes deleted in the query)r   NODES_DELETEDr   s    r   nodes_deletedzQueryResult.nodes_deletedT  r   r   c                 ,    | j                  t              S )z1Returns the number of properties set in the query)r   PROPERTIES_SETr   s    r   properties_setzQueryResult.properties_setY  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_createdc       ~~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_deletedh  r   r   c                 ,    | j                  t              S )z2Returns the number of indices created in the query)r   INDICES_CREATEDr   s    r   indices_createdzQueryResult.indices_createdm       ~~o..r   c                 ,    | j                  t              S )z2Returns the number of indices deleted in the query)r   INDICES_DELETEDr   s    r   indices_deletedzQueryResult.indices_deletedr  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_executionw  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$   rm   r%   r   r&   r   r'   r   r(   r   r*   rw   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   rc   r   rw   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?   r6   r:   r8   rH   rI   rj   rm   rw   r   r   r   r   r   r   r   r   r   r   rc   r9   r   staticmethodrR   r   propertyr   r   r   r   r   r   r   r   r   r   r   r   r   r[   r   r   r   r/   r/   ?   s   ): 
=' 	J"
&" 	
J)  G , , . . - - - - . . 2 2 5 5 5 5 / / / / 5 5 7 7 
 
  
 
r   r/   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   r1   Nr2   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   ro   )r7   rs   r3   rt   rj   rr   r   )r<   r]   ru   rv   rf   rp   s         r   rw   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)rr   r   	TypeErrorr   s        r   rc   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)rZ   r   r   N)rN   r[   r4   rs   )r<   rK   r^   r\   rT   r]   records          r   rI   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rF   rG   rJ   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)r`   r   Nr   ra   rd   s         r   rj   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)ry   r   r   Nr   r   r   rz   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_array7  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   rw   rc   rI   r:   rj   r   r   r   r   r   r   r   r   r     s@    
<J"
C
&" r   r   c                 R    | j                         } | dv ry| dv ryt        d|       )z
    Convert a string representation of truth to true (1) or false (0).
    True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
    are 'n', 'no', 'f', 'false', 'off', and '0'.  Raises ValueError if
    'val' is anything else.
    )yyesttrueon1T)nnoffalseoff0Fzinvalid truth value )lowerr   )vals    r   r   r   ?  s:     ))+C
22	5	5/w788r   )r   collectionsr   redisr   edger   
exceptionsr   noder   pathr	   r   r   r   r   r   r   r   r   r   r   r   r   rQ   r   r   r/   r   r   r   r   r   <module>r     s    
 #    0  !/ !) / ##% 3  	   Z
 Z
z
`{ `F9r   