U
    d(                     @   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rz|dd }| |S )z&
        Create entire graph.
        r   NzCREATE ,c                 S   s   g | ]}t |qS  )str).0Zedger   r   A/tmp/pip-unpacked-wheel-f5h5_hbx/redis/commands/graph/commands.py
<listcomp>    s     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rP|d|g n|dk	r`tdz| j	| }	t
| |	|W S  tk
r }
 z2dt|
kr|r| j|||dd W Y 
S |
W 5 d}
~
X Y nH tk
r }
 z(|
j| _|   | |||| W Y S d}
~
X Y nX dS )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   (   s*    

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
        )Z_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   r8   r   r   r   flusht   s    zGraphCommands.flushc                 K   s   t ddS )zInternal only. Not supported.zVGRAPH.BULK is internal only. Use https://github.com/redisgraph/redisgraph-bulk-loader.N)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)r0   )r   )r   r   r   r   r   r0      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%   r8   r   r   r   slowlog   s    zGraphCommands.slowlogc                 C   s@   |rdnd|g}|dk	r0|r(| | ntd| jtf| 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.
        ZSETGETNz3``value`` can be provided only when ``set`` is True)appendr   r*   
CONFIG_CMD)r   r%   valuesetr/   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_CMDr8   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   s   g | ]}|  qS r   decoder   br   r   r   r      s     z0GraphCommands.execution_plan.<locals>.<listcomp>
r!   r*   EXPLAIN_CMDr%   r&   bytesr   r   r   r/   Z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*   rQ   r%   r   rS   r   r   r   explain   s    
zGraphCommands.explain)NNFF)NF)N)N)__name__
__module____qualname____doc__r   r   r6   r9   r<   r?   r0   rA   rG   rI   rT   rV   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              
      s0  |}|  || }|rt}n|r$tnt}|| j|dg}t|trP|d|g n|dk	r`tdz(| j	| I dH }	t
 | |	|I dH W S  tk
r }
 z8dt|
kr|r| j|||ddI dH  W Y 
S |
W 5 d}
~
X Y nN tk
r* }
 z.|
j| _|   | ||||I dH  W Y S d}
~
X Y nX dS )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   Z
initializer   r   r   r   r+   r,   r-   r   r   r   r      s*    
 zAsyncGraphCommands.queryc                    sJ   |  || }| t| j|I dH }t|d tr@dd |D }d|S )rJ   Nr   c                 S   s   g | ]}|  qS r   rK   rM   r   r   r   r   "  s     z5AsyncGraphCommands.execution_plan.<locals>.<listcomp>rO   rP   rS   r   r   r   rT     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
        NrU   rS   r   r   r   rV   %  s    	zAsyncGraphCommands.explainc                    s   |   I dH  i | _g | _dS r:   r;   r8   r   r   r   r<   3  s    zAsyncGraphCommands.flush)NNFF)N)N)rW   rX   rY   r   rT   rV   r<   r   r   r   r   r[      s   
:

r[   N)Zredisr   Zredis.exceptionsr   
exceptionsr   rT   r   Zquery_resultr   r   r"   r#   r$   r7   r@   rD   rH   rQ   r	   r[   r   r   r   r   <module>   s    I