o
    Š¨Êh†(  ã                   @   s‚   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	m
Z
 dZdZd	Zd
ZdZdZdZdZG dd„ dƒZG dd„ deƒZdS )é    )Ú	DataError)ÚResponseErroré   )ÚVersionMismatchException)ÚExecutionPlan)ÚAsyncQueryResultÚQueryResultzGRAPH.PROFILEzGRAPH.RO_QUERYzGRAPH.QUERYzGRAPH.DELETEzGRAPH.SLOWLOGzGRAPH.CONFIGz
GRAPH.LISTzGRAPH.EXPLAINc                   @   sx   e Zd ZdZdd„ Zddd„Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zddd„Zdd„ Zddd„Zddd„ZdS )ÚGraphCommandszRedisGraph Commandsc                 C   s„   t | jƒdkrt | jƒdkrdS d}| j ¡ D ]\}}|t|ƒd 7 }q|d dd„ | jD ƒ¡7 }|d dkr=|dd… }|  |¡S )z&
        Create entire graph.
        r   NzCREATE ú,c                 S   s   g | ]}t |ƒ‘qS © )Ústr)Ú.0Úedger   r   úO/var/www/html/env/lib/python3.10/site-packages/redis/commands/graph/commands.pyÚ
<listcomp>    ó    z(GraphCommands.commit.<locals>.<listcomp>éÿÿÿÿ)ÚlenÚnodesÚedgesÚitemsr   ÚjoinÚquery)Úselfr   Ú_Únoder   r   r   Úcommit   s   
zGraphCommands.commitNFc              
   C   s  |}|   |¡| }|rt}n|rtnt}|| j|dg}t|tƒr(| d|g¡ n|dur0tdƒ‚z| j	|Ž }	t
| |	|ƒW S  tya }
 zdt|
ƒv r[|r[| j|||dd�W  Y d}
~
S |
‚d}
~
w ty‚ }
 z|
j| _|  ¡  |  ||||¡W  Y d}
~
S d}
~
ww )a  
        Executes a query against the graph.
        For more information see `GRAPH.QUERY <https://redis.io/commands/graph.query>`_. # noqa

        Args:

        q : str
            The query.
        params : dict
            Query parameters.
        timeout : int
            Maximum runtime for read queries in milliseconds.
        read_only : bool
            Executes a readonly query if set to True.
        profile : bool
            Return details on results produced by and time
            spent in each operation.
        ú	--compactÚtimeoutNú+Timeout argument must be a positive integerúunknown commandF©Ú	read_only)Ú_build_params_headerÚPROFILE_CMDÚRO_QUERY_CMDÚ	QUERY_CMDÚnameÚ
isinstanceÚintÚextendÚ	ExceptionÚexecute_commandr   r   r   r   r   ÚversionÚ_refresh_schema©r   ÚqÚparamsr   r"   Úprofiler   ÚcmdÚcommandÚresponseÚer   r   r   r   (   s0   

€€úzGraphCommands.queryc                 C   s   d}|t |ƒ7 }|  |¡S )z 
        Merge pattern.
        zMERGE )r   r   )r   Úpatternr   r   r   r   Úmergeb   s   
zGraphCommands.mergec                 C   s   |   ¡  |  t| j¡S )z|
        Deletes graph.
        For more information see `DELETE <https://redis.io/commands/graph.delete>`_. # noqa
        )Ú_clear_schemar,   Ú
DELETE_CMDr'   ©r   r   r   r   Údeletek   s   zGraphCommands.deletec                 C   s   |   ¡  i | _g | _dS ©zT
        Commit the graph and reset the edges and the nodes to zero length.
        N©r   r   r   r;   r   r   r   Úflusht   s   
zGraphCommands.flushc                 K   s   t dƒ‚)zInternal only. Not supported.zVGRAPH.BULK is internal only. Use https://github.com/redisgraph/redisgraph-bulk-loader.)ÚNotImplementedError)r   Úkwargsr   r   r   Úbulk|   s   ÿzGraphCommands.bulkc                 C   s   | j |dd�S )ar  
        Execute a query and produce an execution plan augmented with metrics
        for each operation's execution. Return a string representation of a
        query execution plan, with details on results produced by and time
        spent in each operation.
        For more information see `GRAPH.PROFILE <https://redis.io/commands/graph.profile>`_. # noqa
        T)r2   )r   )r   r   r   r   r   r2   ƒ   s   zGraphCommands.profilec                 C   s   |   t| j¡S )aÙ  
        Get a list containing up to 10 of the slowest queries issued
        against the given graph ID.
        For more information see `GRAPH.SLOWLOG <https://redis.io/commands/graph.slowlog>`_. # noqa

        Each item in the list has the following structure:
        1. A unix timestamp at which the log entry was processed.
        2. The issued command.
        3. The issued query.
        4. The amount of time needed for its execution, in milliseconds.
        )r,   ÚSLOWLOG_CMDr'   r;   r   r   r   Úslowlog�   s   zGraphCommands.slowlogc                 C   sB   |rdnd|g}|dur|r|  |¡ ntdƒ‚| jtg|¢R Ž S )a’  
        Retrieve or update a RedisGraph configuration.
        For more information see `<https://redis.io/commands/graph.config-get/>`__.

        Args:

        name : str
            The name of the configuration
        value :
            The value we want to set (can be used only when `set` is on)
        set : bool
            Turn on to set a configuration. Default behavior is get.
        ÚSETÚGETNz3``value`` can be provided only when ``set`` is True)Úappendr   r,   Ú
CONFIG_CMD)r   r'   ÚvalueÚsetr1   r   r   r   Úconfig›   s   ÿzGraphCommands.configc                 C   s
   |   t¡S )z•
        Lists all graph keys in the keyspace.
        For more information see `GRAPH.LIST <https://redis.io/commands/graph.list>`_. # noqa
        )r,   ÚLIST_CMDr;   r   r   r   Ú	list_keys³   s   
zGraphCommands.list_keysc                 C   sD   |   |¡| }|  t| j|¡}t|d tƒrdd„ |D ƒ}d |¡S )úÖ
        Get the execution plan for given query,
        GRAPH.EXPLAIN returns an array of operations.

        Args:
            query: the query that will be executed
            params: query parameters
        r   c                 S   ó   g | ]}|  ¡ ‘qS r   ©Údecode©r   Úbr   r   r   r   Ç   r   z0GraphCommands.execution_plan.<locals>.<listcomp>Ú
©r#   r,   ÚEXPLAIN_CMDr'   r(   Úbytesr   ©r   r   r1   Úplanr   r   r   Úexecution_planº   s
   	
zGraphCommands.execution_planc                 C   s&   |   |¡| }|  t| j|¡}t|ƒS )a8  
        Get the execution plan for given query,
        GRAPH.EXPLAIN returns ExecutionPlan object.
        For more information see `GRAPH.EXPLAIN <https://redis.io/commands/graph.explain>`_. # noqa

        Args:
            query: the query that will be executed
            params: query parameters
        ©r#   r,   rV   r'   r   rX   r   r   r   ÚexplainÊ   s   
zGraphCommands.explain©NNFF)NF©N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r8   r<   r?   rB   r2   rD   rK   rM   rZ   r\   r   r   r   r   r	      s    
:		


r	   c                   @   s2   e Zd Zddd„Zddd„Zddd„Zd	d
„ ZdS )ÚAsyncGraphCommandsNFc              
   Ã   s$  �|}|   |¡| }|rt}n|rtnt}|| j|dg}t|tƒr)| d|g¡ n|dur1tdƒ‚z| j	|Ž I dH }	t
ƒ  | |	|¡I dH W S  tym }
 zdt|
ƒv rg|rg| j|||dd�I dH W  Y d}
~
S |
‚d}
~
w ty‘ }
 z|
j| _|  ¡  |  ||||¡I dH W  Y d}
~
S d}
~
ww )a1  
        Executes a query against the graph.
        For more information see `GRAPH.QUERY <https://oss.redis.com/redisgraph/master/commands/#graphquery>`_. # noqa

        Args:

        q : str
            The query.
        params : dict
            Query parameters.
        timeout : int
            Maximum runtime for read queries in milliseconds.
        read_only : bool
            Executes a readonly query if set to True.
        profile : bool
            Return details on results produced by and time
            spent in each operation.
        r   r   Nr   r    Fr!   )r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r   Ú
initializer   r   r   r   r-   r.   r/   r   r   r   r   Û   s2   €
$€"€úzAsyncGraphCommands.queryc                 Ã   sL   �|   |¡| }|  t| j|¡I dH }t|d tƒr!dd„ |D ƒ}d |¡S )rN   Nr   c                 S   rO   r   rP   rR   r   r   r   r   "  r   z5AsyncGraphCommands.execution_plan.<locals>.<listcomp>rT   rU   rX   r   r   r   rZ     s   €	
z!AsyncGraphCommands.execution_planc                 Ã   s.   �|   |¡| }|  t| j|¡I dH }t|ƒS )zÔ
        Get the execution plan for given query,
        GRAPH.EXPLAIN returns ExecutionPlan object.

        Args:
            query: the query that will be executed
            params: query parameters
        Nr[   rX   r   r   r   r\   %  s   €	zAsyncGraphCommands.explainc                 Ã   s    �|   ¡ I dH  i | _g | _dS r=   r>   r;   r   r   r   r?   3  s   €
zAsyncGraphCommands.flushr]   r^   )r_   r`   ra   r   rZ   r\   r?   r   r   r   r   rc   Ú   s
    

:
rc   N)Úredisr   Úredis.exceptionsr   Ú
exceptionsr   rZ   r   Úquery_resultr   r   r$   r%   r&   r:   rC   rH   rL   rV   r	   rc   r   r   r   r   Ú<module>   s      I