a
    Sic<                     @   s   d Z ddlZddlmZ edg dejejB ZG dd dZ	G d	d
 d
Z
G dd dZG dd dZG dd dZG dd de
ee	ZG dd de
ee	ZG dd deee	ZG dd deee	ZdS )zyMySQLdb Cursors

This module implements Cursors of various types for MySQLdb. By
default, MySQLdb uses the Cursor class.
    N   )ProgrammingError )z'\s*((?:INSERT|REPLACE)\b.+\bVALUES?\s*)z6(\(\s*(?:%s|%\(.+\)s)\s*(?:,\s*(?:%s|%\(.+\)s)\s*)*\))z(\s*(?:ON DUPLICATE.*)?);?\s*\Zc                   @   s  e Zd ZdZdZddlmZmZmZm	Z	m
Z
mZmZmZmZmZmZ dZdd Zdd	 Zd
d Zdd Zdd Z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# Z d.d%d&Z!d'd( Z"d/d)d*Z#d+d, Z$eZeZe	Z	e
Z
eZeZeZeZeZeZdS )0
BaseCursora  A base for Cursor classes. Useful attributes:

    description
        A tuple of DB API 7-tuples describing the columns in
        the last executed query; see PEP-249 for details.

    description_flags
        Tuple of column flags for last query, one entry per column
        in the result set. Values correspond to those in
        MySQLdb.constants.FLAG. See MySQL documentation (C API)
        for more information. Non-standard extension.

    arraysize
        default number of rows fetchmany() will fetch
    i   r   )
MySQLErrorWarningErrorInterfaceErrorDatabaseError	DataErrorOperationalErrorIntegrityErrorInternalErrorr   NotSupportedErrorNc                 C   s@   || _ d | _d | _d| _d| _d | _d | _d | _d | _d | _	d S )Nr   )

connectiondescriptiondescription_flagsrowcount	arraysize	_executed	lastrowid_result	rownumber_rows)selfr    r   K/var/www/html/django/DPS/env/lib/python3.9/site-packages/MySQLdb/cursors.py__init__A   s    zBaseCursor.__init__c                 C   sJ   z6| j du rW d| _ d| _dS |  r(qW d| _ d| _nd| _ d| _0 dS )z6Close the cursor. No further queries will be possible.N)r   r   nextsetr   r   r   r   closeN   s    
zBaseCursor.closec                 C   s   | S Nr   r    r   r   r   	__enter__Y   s    zBaseCursor.__enter__c                 G   s   ~|    d S r"   )r!   )r   exc_infor   r   r   __exit__\   s    zBaseCursor.__exit__c                    sz   |j  |j fddt|ttfrBtfdd|D }n0t|trffdd| D }n|}d |S )Nc                    sH   t | tr|  S t | tr,tt| S t | trDtt| S | S r"   )
isinstancestrencodetuplemaplist)x)encodingensure_bytesr   r   r.   d   s    



z-BaseCursor._escape_args.<locals>.ensure_bytesc                 3   s   | ]} |V  qd S r"   r   .0argr.   literalr   r   	<genexpr>n       z*BaseCursor._escape_args.<locals>.<genexpr>c                    s"   i | ]\}} | |qS r   r   )r0   keyvalr2   r   r   
<dictcomp>p   s   z+BaseCursor._escape_args.<locals>.<dictcomp>)r-   r3   r&   r)   r+   dictitems)r   argsconnretr   )r-   r.   r3   r   _escape_args`   s    	
zBaseCursor._escape_argsc                 C   s   | j stdd S )Nzexecute() first)r   r   r    r   r   r   _check_executed|   s    zBaseCursor._check_executedc                 C   s@   | j r|   |  }| }|dkr*dS | | |   dS )z`Advance to the next result set.

        Returns None if there are no more result sets.
        r   Nr   )r   fetchall_get_dbnext_result_do_get_result_post_get_result)r   dbnrr   r   r   r      s    
zBaseCursor.nextsetc                 C   sV   |    | _}|d u r$d  | _| _n| | _| | _| | _d| _|	 | _
d S Nr   )_get_resultr   r   r   describefield_flagsaffected_rowsr   r   	insert_idr   )r   rE   resultr   r   r   rC      s    


zBaseCursor._do_get_resultc                 C   s   d S r"   r   r    r   r   r   rD      s    zBaseCursor._post_get_resultc                 G   s   dS z!Does nothing, required by DB API.Nr   r   r;   r   r   r   setinputsizes   s    zBaseCursor.setinputsizesc                 G   s   dS rN   r   rO   r   r   r   setoutputsizes   s    zBaseCursor.setoutputsizesc                 C   s   | j }|d u rtd|S )Nzcursor closed)r   r   )r   conr   r   r   rA      s    zBaseCursor._get_dbc           	   
   C   s   |   r
q |  }t|tr(||j}|durt|trzi }| D ],\}}t|trd||j}||||< qF|}nt	t
|j|}z|| }W n0 ty } ztt|W Y d}~n
d}~0 0 t|ttfsJ | |}|S )a  Execute a query.

        query -- string, query to execute on server
        args -- optional sequence or mapping, parameters to use with query.

        Note: If args is a sequence, then %s must be used as the
        parameter placeholder in the query. If a mapping is used,
        %(key)s must be used as the placeholder.

        Returns integer represents rows affected, if any
        N)r   rA   r&   r'   r(   r-   r9   r:   r3   r)   r*   	TypeErrorr   bytes	bytearray_query)	r   queryr;   rE   nargsr6   itemmresr   r   r   execute   s*    


"
zBaseCursor.executec              	      s   |sdS t  }|rx|dd }|d }|dp>d}|d dkrX|d	 d
ks\J ||||j jS t fdd|D _	j	S )ax  Execute a multi-row query.

        :param query: query to execute on server
        :param args:  Sequence of sequences or mappings.  It is used as parameter.
        :return: Number of rows affected, if any.

        This method improves performance on multiple-row INSERT and
        REPLACE. Otherwise it is equivalent to looping over args with
        execute().
        Nr   r         r   r   (r   )c                 3   s   | ]}  |V  qd S r"   )r\   r/   rW   r   r   r   r4      r5   z)BaseCursor.executemany.<locals>.<genexpr>)
RE_INSERT_VALUESmatchgrouprstrip_do_execute_manymax_stmt_lengthrA   r-   sumr   )r   rW   r;   rZ   Zq_prefixZq_valuesZ	q_postfixr   ra   r   executemany   s$    
	zBaseCursor.executemanyc                 C   s   |   }| j}t|tr"||}t|tr6||}t|trJ||}t|}	t|}||t|| }
|	|
7 }	d}|D ]^}|||| }
t|	t|
 t| d |kr|| 	|	| 7 }t|}	n|	d7 }	|	|
7 }	q||| 	|	| 7 }|| _
|S )Nr   r      ,)rA   r>   r&   r'   r(   rU   iternextlenr\   r   )r   prefixvaluespostfixr;   rg   r-   r<   escapesqlvrowsr1   r   r   r   rf      s.    





 

zBaseCursor._do_execute_manyr   c                    s   |    ttr j|rbd d dd fddt|D  }| | |   ddfdd	t	t
|D f }| | |S )
a  Execute stored procedure procname with args

        procname -- string, name of procedure to execute on server

        args -- Sequence of parameters to use with procedure

        Returns the original args.

        Compatibility warning: PEP-249 specifies that any modified
        parameters must be returned. This is currently impossible
        as they are only available by storing them in a server
        variable and then retrieved by a query. Since stored
        procedures return zero or more result sets, there is no
        reliable way to get at OUT or INOUT parameters via callproc.
        The server variables are named @_procname_n, where procname
        is the parameter above and n is the position of the parameter
        (from zero). Once all result sets generated by the procedure
        have been fetched, you can issue a SELECT @_procname_0, ...
        query using .execute() to get any OUT or INOUT values.

        Compatibility warning: The act of calling a stored procedure
        itself creates an empty result set. This appears after any
        result sets generated by the procedure. This is non-standard
        behavior with respect to the DB-API. Be sure to use nextset()
        to advance through all result sets; otherwise you may get
        disconnected.
        s   @_s   _%d=%ss   SET %srj   c                 3   s$   | ]\}}|  |f V  qd S r"   )r3   )r0   indexr1   )rE   fmtr   r   r4   /  s   z&BaseCursor.callproc.<locals>.<genexpr>s   CALL %s(%s)c                    s   g | ]}d  |f qS )s   @_%s_%dr   )r0   i)procnamer   r   
<listcomp>7  r5   z'BaseCursor.callproc.<locals>.<listcomp>)rA   r&   r'   r(   r-   join	enumeraterV   r   rangerm   )r   rx   r;   qr   )rE   rv   rx   r   callproc  s     



zBaseCursor.callprocc                 C   s6   |   }d | _|| | | |   || _| jS r"   )rA   r   rW   rC   rD   r   r   )r   r}   rE   r   r   r   rV   <  s    

zBaseCursor._queryc                 C   s   | j s
dS | j || jS )Nr   )r   	fetch_row_fetch_type)r   sizer   r   r   
_fetch_rowE  s    zBaseCursor._fetch_rowc                 C   s   t | jd S r"   )rk   fetchoner    r   r   r   __iter__J  s    zBaseCursor.__iter__)N)r   )r   )%__name__
__module____qualname____doc__rg   _exceptionsr   r   r   r	   r
   r   r   r   r   r   r   r   r   r!   r#   r%   r>   r?   r   rC   rD   rP   rQ   rA   r\   ri   rf   r~   rV   r   r   r   r   r   r   r      sB   4
&!
.	
r   c                   @   sL   e Zd ZdZdd Zdd Zdd Zdd	d
Zdd ZdddZ	dd Z
dS )CursorStoreResultMixIna  This is a MixIn class which causes the entire result set to be
    stored on the client side, i.e. it uses mysql_store_result(). If the
    result set can be very large, consider adding a LIMIT clause to your
    query, or using CursorUseResultMixIn instead.c                 C   s   |    S r"   )rA   store_resultr    r   r   r   rH   _  s    z"CursorStoreResultMixIn._get_resultc                 C   s   |  d| _d | _d S rG   )r   r   r   r    r   r   r   rD   b  s    z'CursorStoreResultMixIn._post_get_resultc                 C   s8   |    | jt| jkrdS | j| j }| jd | _|S )z]Fetches a single row from the cursor. None indicates that
        no more rows are available.Nr   )r?   r   rm   r   r   rM   r   r   r   r   f  s    zCursorStoreResultMixIn.fetchoneNc                 C   s>   |    | j|p| j }| j| j| }t|t| j| _|S zFetch up to size rows from the cursor. Result set may be smaller
        than size. If size is not defined, cursor.arraysize is used.)r?   r   r   r   minrm   )r   r   endrM   r   r   r   	fetchmanyp  s
    z CursorStoreResultMixIn.fetchmanyc                 C   s6   |    | jr | j| jd }n| j}t| j| _|S )+Fetches all available rows from the cursor.N)r?   r   r   rm   r   r   r   r   r@   y  s    zCursorStoreResultMixIn.fetchallrelativec                 C   sb   |    |dkr| j| }n|dkr*|}ntdt| |dk sP|t| jkrXtd|| _dS )a  Scroll the cursor in the result set to a new position according
        to mode.

        If mode is 'relative' (default), value is taken as offset to
        the current position in the result set, if set to 'absolute',
        value states an absolute target position.r   absolutezunknown scroll mode %sr   zout of rangeN)r?   r   r   reprrm   r   
IndexError)r   valuemoderr   r   r   scroll  s    zCursorStoreResultMixIn.scrollc                 C   s,   |    | jr| j| jd  p"| j}t|S r"   )r?   r   r   rk   r   r   r   r   r     s    zCursorStoreResultMixIn.__iter__)N)r   )r   r   r   r   rH   rD   r   r   r@   r   r   r   r   r   r   r   Y  s   

	

r   c                   @   sF   e Zd ZdZdd Zdd ZdddZd	d
 Zdd Zdd Z	e	Z
dS )CursorUseResultMixIna  This is a MixIn class which causes the result set to be stored
    in the server and sent row-by-row to client side, i.e. it uses
    mysql_use_result(). You MUST retrieve the entire result set and
    close() the cursor before additional queries can be performed on
    the connection.c                 C   s   |    S r"   )rA   
use_resultr    r   r   r   rH     s    z CursorUseResultMixIn._get_resultc                 C   s.   |    | d}|sdS | jd | _|d S )z%Fetches a single row from the cursor.r   Nr   )r?   r   r   r   r   r   r   r   r     s    
zCursorUseResultMixIn.fetchoneNc                 C   s,   |    | |p| j}| jt| | _|S r   )r?   r   r   r   rm   )r   r   r   r   r   r   r     s    zCursorUseResultMixIn.fetchmanyc                 C   s&   |    | d}| jt| | _|S )r   r   )r?   r   r   rm   r   r   r   r   r@     s    
zCursorUseResultMixIn.fetchallc                 C   s   | S r"   r   r    r   r   r   r     s    zCursorUseResultMixIn.__iter__c                 C   s   |   }|d u rt|S r"   )r   StopIteration)r   rowr   r   r   rl     s    zCursorUseResultMixIn.next)N)r   r   r   r   rH   r   r   r@   r   rl   __next__r   r   r   r   r     s   	
r   c                   @   s   e Zd ZdZdZdS )CursorTupleRowsMixInzwThis is a MixIn class that causes all rows to be returned as tuples,
    which is the standard form required by DB API.r   Nr   r   r   r   r   r   r   r   r   r     s   r   c                   @   s   e Zd ZdZdZdS )CursorDictRowsMixInznThis is a MixIn class that causes all rows to be returned as
    dictionaries. This is a non-standard feature.r   Nr   r   r   r   r   r     s   r   c                   @   s   e Zd ZdZdS )CursorzjThis is the standard Cursor class that returns rows as tuples
    and stores the result set in the client.Nr   r   r   r   r   r   r   r   r     s   r   c                   @   s   e Zd ZdZdS )
DictCursorzeThis is a Cursor class that returns rows as dictionaries and
    stores the result set in the client.Nr   r   r   r   r   r     s   r   c                   @   s   e Zd ZdZdS )SSCursorz_This is a Cursor class that returns rows as tuples and stores
    the result set in the server.Nr   r   r   r   r   r     s   r   c                   @   s   e Zd ZdZdS )SSDictCursorzeThis is a Cursor class that returns rows as dictionaries and
    stores the result set in the server.Nr   r   r   r   r   r     s   r   )r   rer   r   compilerz   
IGNORECASEDOTALLrb   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s&   
  AB/