U
    cc                     @   sN   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	 G dd dZ
d	S )
    )	DataError)ResponseError   )VersionMismatchException)ExecutionPlan)QueryResultc                   @   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).0edger
   r
   W/var/www/html/project/venv/lib/python3.8/site-packages/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(  |}|dk	r|  || }|r$d}n|r,dnd}|| j|dg}|r`t|tsTtd|d|g7 }z| j| }	t| |	|W S  tk
r }
 zFdt|
krt	d	 d
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.
        NzGRAPH.PROFILEzGRAPH.RO_QUERYzGRAPH.QUERYz	--compactz+Timeout argument must be a positive integertimeoutzwrong number of argumentsz>Note: RedisGraph Python requires server version 2.2.8 or abovezunknown commandF)	read_only)_build_params_headername
isinstanceint	Exceptionexecute_commandr   r   r   printr   r   versionZ_refresh_schema)r   qparamsr   r   profiler   cmdcommandresponseer
   r
   r   r      s4    

zGraphCommands.queryc                 C   s   d}|t |7 }| |S )z 
        Merge pattern.
        zMERGE )r   r   )r   patternr   r
   r
   r   merge^   s    zGraphCommands.mergec                 C   s   |    | d| jS )z|
        Deletes graph.
        For more information see `DELETE <https://redis.io/commands/graph.delete>`_. # noqa
        zGRAPH.DELETE)Z_clear_schemar"   r   r   r
   r
   r   deleteg   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   flushp   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   bulkx   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)r'   )r   )r   r   r
   r
   r   r'      s    zGraphCommands.profilec                 C   s   |  d| 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.
        zGRAPH.SLOWLOG)r"   r   r.   r
   r
   r   slowlog   s    zGraphCommands.slowlogc                 C   s>   |rdnd|g}|dk	r0|r(| | ntd| jd| 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GRAPH.CONFIG)r6   )appendr   r"   )r   r   valuesetr&   r
   r
   r   config   s    zGraphCommands.configc                 C   s
   |  dS )z
        Lists all graph keys in the keyspace.
        For more information see `GRAPH.LIST <https://redis.io/commands/graph.list>`_. # noqa
        z
GRAPH.LIST)r"   r.   r
   r
   r   	list_keys   s    zGraphCommands.list_keysc                 C   sL   |dk	r|  || }| d| j|}t|d trBdd |D }d|S )z
        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
        NGRAPH.EXPLAINr   c                 S   s   g | ]}|  qS r
   )decode)r   br
   r
   r   r      s     z0GraphCommands.execution_plan.<locals>.<listcomp>
)r   r"   r   r   bytesr   r   r   r&   Zplanr
   r
   r   execution_plan   s    	zGraphCommands.execution_planc                 C   s.   |dk	r|  || }| d| 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
        Nr<   )r   r"   r   r   rA   r
   r
   r   explain   s    
zGraphCommands.explain)NNFF)NF)N)N)__name__
__module____qualname____doc__r   r   r-   r/   r0   r3   r'   r4   r:   r;   rB   rC   r
   r
   r
   r   r   	   s   
?		


r   N)Zredisr   Zredis.exceptionsr   
exceptionsr   rB   r   Zquery_resultr   r   r
   r
   r
   r   <module>   s
   