
    2i=                        d dl Z d dlmZ d dlmZ d dlmZ d dlmZmZ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e      Z#d Z$d1dZ%de&fdZ'de(fdZ)de*fd Z+de,fd!Z-defd"Z.defd#Z/defd$Z0defd%Z1de	fd&Z2defd'Z3d( Z4defd)Z5defd*Z6defd+Z7defd,Z8d- Z9d. Z:e$e%e'e)e+e-e.e6e5e7e8e9e/e0e1e2e3gZ; G d/ d0      Z<y)2    N)Enum)List)OrderedDict)datetimedatetime)relativedelta)ResponseError   )Edge)Node)Path)SchemaVersionMismatchExceptionzLabels addedzLabels removedzNodes createdzNodes deletedzProperties setzIndices createdzIndices deletedzCached executionzProperties removedzRelationships deletedzRelationships createdzinternal execution timec                   T    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ZdZdZdZdZdZy)ResultSetScalarTypesa"  
    Enumeration representing different scalar types in the query result set.

    Attributes:
        VALUE_UNKNOWN   (int): Unknown scalar type  (0)
        VALUE_NULL      (int): Null scalar type     (1)
        VALUE_STRING    (int): String scalar type   (2)
        VALUE_INTEGER   (int): Integer scalar type  (3)
        VALUE_BOOLEAN   (int): Boolean scalar type  (4)
        VALUE_DOUBLE    (int): Double scalar type   (5)
        VALUE_ARRAY     (int): Array scalar type    (6)
        VALUE_EDGE      (int): Edge scalar type     (7)
        VALUE_NODE      (int): Node scalar type     (8)
        VALUE_PATH      (int): Path scalar type     (9)
        VALUE_MAP       (int): Map scalar type      (10)
        VALUE_POINT     (int): Point scalar type    (11)
        VALUE_VECTORF32 (int): Vector scalar type   (12)
        VALUE_DATETIME  (int): DateTime scalar type (13)
        VALUE_DATE      (int): Date scalar type     (14)
        VALUE_TIME      (int): Time scalar type     (15)
        VALUE_DURATION  (int): Duration scalar type (16)
    r   r                        	   
                     N)__name__
__module____qualname____doc__VALUE_UNKNOWN
VALUE_NULLVALUE_STRINGVALUE_INTEGERVALUE_BOOLEANVALUE_DOUBLEVALUE_ARRAY
VALUE_EDGE
VALUE_NODE
VALUE_PATH	VALUE_MAPVALUE_POINTVALUE_VECTORF32VALUE_DATETIME
VALUE_DATE
VALUE_TIMEVALUE_DURATION     _/mnt/e/genesis-system/.venvs/voice-bridge/lib/python3.12/site-packages/falkordb/query_result.pyr   r   ,   s^    . MJLMMLKJJJIKONJJNr7   r   c                 B    t         j                  j                  d       y)z
    Parse a value of unknown type.

    Args:
        value: The value to parse.
        graph: The graph instance.

    Returns:
        None
    zUnknown type
N)sysstderrwritevaluegraphs     r8   __parse_unknownr@   V   s     JJ%&r7   returnc                      y)z
    Parse a null value.

    Args:
        value: The null value.
        graph: The graph instance.

    Returns:
        None: Always returns None.
    Nr6   r=   s     r8   __parse_nullrC   c   s     r7   c                 |    t        | t              r| j                         S t        | t              st        |       S | S )z
    Parse the value as a string.

    Args:
        value: The value to parse.
        graph: The graph instance.

    Returns:
        str: The parsed string value.
    )
isinstancebytesdecodestrr=   s     r8   __parse_stringrI   p   s3     %||~eS!5zLr7   c                     t        |       S )z
    Parse the integer value from the value.

    Args:
        value: The value to parse.
        graph: The graph instance.

    Returns:
        int: The parsed integer value.
    )intr=   s     r8   __parse_integerrL      s     u:r7   c                 P    t        | t              r| j                         n| } | dk(  S )z
    Parse the value as a boolean.

    Args:
        value: The value to parse.
        graph: The graph instance.

    Returns:
        bool: The parsed boolean value.
    true)rE   rF   rG   r=   s     r8   __parse_booleanrO      s$     )6ELLNEEF?r7   c                     t        |       S )z
    Parse the value as a double.

    Args:
        value: The value to parse.
        graph: The graph instance.

    Returns:
        float: The parsed double value.
    floatr=   s     r8   __parse_doublerS      s     <r7   c                 l    t        t        |             D cg c]  }t        | |   |       }}|S c c}w )z
    Parse an array of values.

    Args:
        value: The array value to parse.
        graph: The graph instance.

    Returns:
        list: The parsed list of values.
    )rangelenparse_scalar)r>   r?   iscalars       r8   __parse_arrayrZ      s6     6;3u:5FGl58U+GFGM Hs   1c                 >    | D cg c]  }t        |       c}S c c}w )z
    Parse a vector32f.

    Args:
        value: The vector to parse.
        graph: The graph instance.

    Returns:
        list: The parsed vector.
    rQ   )r>   r?   vs      r8   __parse_vectorf32r]      s     $$E!H$$$s   c                 ,    t        j                  |       S N)r   utcfromtimestampr=   s     r8   __parse_datetimera      s    $$U++r7   c                 H    t        j                  |       j                         S r_   )r   r`   r   r=   s     r8   __parse_daterc          $$U+0022r7   c                 H    t        j                  |       j                         S r_   )r   r`   r   r=   s     r8   __parse_timerf      rd   r7   c                 ^    t        j                  |       }t        ddd      }t        ||      S )Ni  r   )r   r`   r	   )r>   r?   	timestampepochs       r8   __parse_durationrj      s-    ))%0IT1a EE**r7   c                 |    i }| D ]4  }|j                   j                  |d         }t        |dd |      }|||<   6 |S )z
    Parse node/edge properties.

    Args:
        props (List): List of properties.
        graph: The graph instance.

    Returns:
        dict: Dictionary containing parsed properties.
    r   r   N)schemaget_propertyrW   )propsr?   
propertiesprop	prop_name
prop_values         r8   __parse_entity_propertiesrs      sT     J +LL--d1g6	!$qr(E2
 *
9+
 r7   c                     t        | d         }d}t        | d         dkD  r+| d   D cg c]  }|j                  j                  |       }}t	        | d   |      }t        |d||      S c c}w )z
    Parse the value to a node.

    Args:
        value: The value to parse.
        graph: The graph instance.

    Returns:
        Node: The parsed Node instance.
    r   Nr   r    )node_idaliaslabelsro   )rK   rV   rl   	get_labelrs   r   )r>   r?   rv   rx   inner_labelro   s         r8   __parse_noder{      ss     %(mGF
58}qINqR+%,,((5RR*58U;Jr&ZPP Ss   "A+c                     t        | d         }|j                  j                  | d         }t        | d         }t        | d         }t        | d   |      }t	        |||||      S )z
    Parse the value to an edge.

    Args:
        value: The value to parse.
        graph: The graph instance.

    Returns:
        Edge: The parsed Edge instance.
    r   r   r   r   r   )edge_idro   )rK   rl   get_relationrs   r   )r>   r?   r}   relationsrc_node_iddest_node_idro   s          r8   __parse_edger      sg     %(mG||((q2HeAh-KuQx=L*58U;JX|WQ[\\r7   c                 V    t        | d   |      }t        | d   |      }t        ||      S )z
    Parse the value to a path.

    Args:
        value: The value to parse.
        graph: The graph instance.

    Returns:
        Path: The parsed Path instance.
    r   r   )rW   r   )r>   r?   nodesedgess       r8   __parse_pathr     s1     q5)Eq5)Eur7   c                     t               }t        |       }t        d|d      D ]&  }t        | |   |      }t	        | |dz      |      ||<   ( |S )z
    Parse the value as a map.

    Args:
        value: The value to parse.
        graph: The graph instance.

    Returns:
        OrderedDict: The parsed OrderedDict.
    r   r   r   )r   rV   rU   rI   rW   )r>   r?   m	n_entriesrX   keys         r8   __parse_mapr     s[     	AE
I1i# 3U1Xu-eAElE2#3 Hr7   c                 @    t        | d         t        | d         d}|S )z
    Parse the value to point.

    Args:
        value: The value to parse.
        graph: The graph instance.

    Returns:
        dict: The parsed dictionary representing a point.
    r   r   )latitude	longituderQ   )r>   r?   ps      r8   __parse_pointr   /  s#     58_5q?CAHr7   c                 J    t        | d         }| d   } t        |   | |      }|S )z
    Parse a scalar value from a value in the result set.

    Args:
        value: The value to parse.
        graph: The graph instance.

    Returns:
        Any: The parsed scalar value.
    r   r   )rK   PARSE_SCALAR_TYPES)r>   r?   scalar_typerY   s       r8   rW   rW   =  s0     eAh-K!HE,UE:FMr7   c                   h   e Zd ZdZd Zd Zd Zd Zd Zd Z	e
defd	       Ze
defd
       Ze
defd       Ze
defd       Ze
defd       Ze
defd       Ze
defd       Ze
defd       Ze
defd       Ze
defd       Ze
defd       Ze
defd       Ze
defd       Ze
defd       Zy)QueryResultzD
        Represents the result of a query operation on a graph.
    c                     || _         g | _        g | _        g | _        | j	                  |       t        |      dk(  r|d   | _        y|d   | _        | j                  |       y)z
        Initializes a QueryResult instance.

        Args:
            graph: The graph on which the query was executed.
            response: The response from the server.
        r   r   N)_graph_header_result_set
_raw_stats_QueryResult__check_for_errorsrV   _QueryResult__parse_results)selfr?   responses      r8   __init__zQueryResult.__init__g  sc     ! 	)x=A&qkDO 'rlDO  *r7   c                     t        |d   t              r%|d   }t        |      dk(  r|d   }t        |      }|t        |d   t              r|d   y)z
        Checks if the response contains an error.

        Args:
            response: The response from the server.

        Raises:
            ResponseError: If an error is encountered.
        r   zversion mismatchr   r   N)rE   r
   rH   r   )r   r   errorversions       r8   __check_for_errorszQueryResult.__check_for_errors~  s_     hqk=1QKE5z//"1+6w?K hrlM22, 3r7   c                     | j                  |      | _        t        | j                        dk(  ry| j                  |      | _        y)z
        Parse the query execution result returned from the server.

        Args:
            raw_result_set: The raw result set from the server.
        r   N)_QueryResult__parse_headerr   rV   _QueryResult__parse_recordsr   )r   raw_result_sets     r8   __parse_resultszQueryResult.__parse_results  s?     **>: t||!//?r7   c                     | j                   D ]7  }||v st        |j                  d      d   j                  d      d         c S  y)a  
        Get the value of a specific statistical metric.

        Args:
            s (str): The statistical metric to retrieve.

        Returns:
            float: The value of the specified statistical metric. Returns 0 if the metric is not found.
        z: r    r   )r   rR   split)r   sstats      r8   __get_statisticszQueryResult.__get_statistics  sN     OO 	@DDyTZZ-a066s;A>??	@ r7   c                     |d   }|S )z
        Parse the header of the result.

        Args:
            raw_result_set: The raw result set from the server.

        Returns:
            list: An array of column name/column type pairs.
        r   r6   )r   r   headers      r8   __parse_headerzQueryResult.__parse_header  s      "r7   c                     |d   D cg c]&  }|D cg c]  }t        || j                         c}( }}}|S c c}w c c}}w )z
        Parses the result set and returns a list of records.

        Args:
            raw_result_set: The raw result set from the server.

        Returns:
            list: A list of records.
        r   )rW   r   )r   r   rowcellrecordss        r8   __parse_recordszQueryResult.__parse_records  sM     &a(
 :==\$,=
 

 	 >
s   	>9>>rA   c                     | j                   S )z~
        Get the header of the result.

        Returns:
            list: An array of column name/column type pairs.
        )r   r   s    r8   r   zQueryResult.header  s     ||r7   c                     | j                   S )z
        Get a list of the results from a query.

        Returns:
            list: A list of each row returned from a query.
        )r   r   s    r8   
result_setzQueryResult.result_set  s     r7   c                 ,    | j                  t              S )zy
        Get the number of labels added in the query.

        Returns:
        int: The number of labels added.
        )_QueryResult__get_statisticsLABELS_ADDEDr   s    r8   labels_addedzQueryResult.labels_added  s     $$\22r7   c                 ,    | j                  t              S )z
        Get the number of labels removed in the query.

        Returns:
            int: The number of labels removed.
        )r   LABELS_REMOVEDr   s    r8   labels_removedzQueryResult.labels_removed       $$^44r7   c                 ,    | j                  t              S )z
        Get the number of nodes created in the query.

        Returns:
            int: The number of nodes created.
        )r   NODES_CREATEDr   s    r8   nodes_createdzQueryResult.nodes_created       $$]33r7   c                 ,    | j                  t              S )z
        Get the number of nodes deleted in the query.

        Returns:
            int: The number of nodes deleted.
        )r   NODES_DELETEDr   s    r8   nodes_deletedzQueryResult.nodes_deleted  r   r7   c                 ,    | j                  t              S )z
        Get the number of properties set in the query.

        Returns:
            int: The number of properties set.
        )r   PROPERTIES_SETr   s    r8   properties_setzQueryResult.properties_set  r   r7   c                 ,    | j                  t              S )z
        Get the number of properties removed in the query.

        Returns:
            int: The number of properties removed.
        )r   PROPERTIES_REMOVEDr   s    r8   properties_removedzQueryResult.properties_removed  s     $$%788r7   c                 ,    | j                  t              S )z
        Get the number of relationships created in the query.

        Returns:
            int: The number of relationships created.
        )r   RELATIONSHIPS_CREATEDr   s    r8   relationships_createdz!QueryResult.relationships_created#       $$%:;;r7   c                 ,    | j                  t              S )z
        Get the number of relationships deleted in the query.

        Returns:
            int: The number of relationships deleted.
        )r   RELATIONSHIPS_DELETEDr   s    r8   relationships_deletedz!QueryResult.relationships_deleted-  r   r7   c                 ,    | j                  t              S )z
        Get the number of indices created in the query.

        Returns:
            int: The number of indices created.
        )r   INDICES_CREATEDr   s    r8   indices_createdzQueryResult.indices_created7       $$_55r7   c                 ,    | j                  t              S )z
        Get the number of indices deleted in the query.

        Returns:
            int: The number of indices deleted.
        )r   INDICES_DELETEDr   s    r8   indices_deletedzQueryResult.indices_deletedA  r   r7   c                 2    | j                  t              dk(  S )z
        Check if the query execution plan was cached.

        Returns:
            bool: True if the query execution plan was cached, False otherwise.
        r   )r   CACHED_EXECUTIONr   s    r8   cached_executionzQueryResult.cached_executionK  s     $$%56!;;r7   c                 ,    | j                  t              S )z
        Get the server execution time of the query.

        Returns:
            float: The server execution time of the query in milliseconds.
        )r   INTERNAL_EXECUTION_TIMEr   s    r8   run_time_mszQueryResult.run_time_msU  s     $$%<==r7   N)r!   r"   r#   r$   r   r   r   r   r   r   propertylistr   r   rK   r   r   r   r   r   r   r   r   r   r   boolr   rR   r   r6   r7   r8   r   r   c  s   +.,@ "     D     3c 3 3 5 5 5 4s 4 4 4s 4 4 5 5 5 9C 9 9 <s < < <s < < 6 6 6 6 6 6 <$ < < >U > >r7   r   )rA   N)=r:   enumr   typingr   collectionsr   r   r   r   dateutil.relativedeltar	   redisr
   edger   noder   pathr   
exceptionsr   r   r   r   r   r   r   r   r   r   r   r   r   STATSr   r@   rC   rH   rI   rK   rL   r   rO   rR   rS   rZ   r]   ra   rc   rf   rj   rs   r{   r   r   r   r   rW   r   r   r6   r7   r8   <module>r      s   
   # ) ) 0     6 )*))*++, . 1 1 3  	(4 (T'C &S T E 4 %t %,h ,3$ 33$ 3+m +
&Q$ Q$]$ ]$$  (& # (z> z>r7   