o
    `f(                     @   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   V/home/ubuntu/webapp/venv/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/>`_. # noqa

        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