o
    c_<                     @   s|   d dl Z ddlmZ e de je jB ZG dd dZG dd dZG d	d
 d
eeZ	G dd deZ
G dd dee
ZdS )    N   )errz|\s*((?:INSERT|REPLACE)\b.+\bVALUES?\s*)(\(\s*(?:%s|%\(.+\)s)\s*(?:,\s*(?:%s|%\(.+\)s)\s*)*\))(\s*(?:ON DUPLICATE.*)?);?\s*\Zc                   @   s.  e Zd Zd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dZdd Zd<ddZdd Zd<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d>d1d2Zd3d4 Zd5d6 Zd7d8 Zd9d: Zej Z ej!Z!ej"Z"ej#Z#ej$Z$ej%Z%ej&Z&ej'Z'ej(Z(ej)Z)dS )?Cursora
  
    This is the object you use to interact with the database.

    Do not create an instance of a Cursor yourself. Call
    connections.Connection.cursor().

    See `Cursor <https://www.python.org/dev/peps/pep-0249/#cursor-objects>`_ in
    the specification.
    i  c                 C   s4   || _ d | _d| _d| _d| _d | _d | _d | _d S )Nr   r   )
connectiondescription	rownumberrowcount	arraysize	_executed_result_rows)selfr    r   1/usr/lib/python3/dist-packages/pymysql/cursors.py__init__!   s   
zCursor.__init__c                 C   sF   | j }|du r	dS z|  r	 |  sW d| _ dS W d| _ dS d| _ w )zD
        Closing a cursor just exhausts all remaining data.
        N)r   nextsetr   connr   r   r   close+   s   

zCursor.closec                 C   s   | S Nr   r   r   r   r   	__enter__8      zCursor.__enter__c                 G   s   ~|    d S r   )r   )r   exc_infor   r   r   __exit__;   s   zCursor.__exit__c                 C   s   | j std| j S )NzCursor closed)r   r   ProgrammingErrorr   r   r   r   _get_db?   s   
zCursor._get_dbc                 C   s   | j stdd S )Nzexecute() first)r   r   r   r   r   r   r   _check_executedD   s   
zCursor._check_executedc                 C      |S r   r   r   rowr   r   r   	_conv_rowH   r   zCursor._conv_rowc                 G      dS z!Does nothing, required by DB API.Nr   r   argsr   r   r   setinputsizesK       zCursor.setinputsizesc                 G   r#   r$   r   r%   r   r   r   setoutputsizesN   r(   zCursor.setoutputsizesFc                 C   sT   |   }| j}|du s||jurdS |jsdS d| _|   |j|d |   dS )zGet the next query setN
unbufferedT)r   r   has_next_clear_resultnext_result_do_get_result)r   r+   r   current_resultr   r   r   _nextsetQ   s   zCursor._nextsetc                 C   s
   |  dS )NFr1   r   r   r   r   r   _   s   
zCursor.nextsetNc                    sF   t |tr| }|S t |ttfr!t| fdd|D }|S )Nc                 3   s    | ]
}j | d V  qdS ))encodingN)_ensure_bytes).0vr3   r   r   r   	<genexpr>f   s    z'Cursor._ensure_bytes.<locals>.<genexpr>)
isinstancestrencodetuplelisttype)r   xr3   r   r7   r   r4   b   s   

zCursor._ensure_bytesc                    sN   t |ttfrt fdd|D S t |tr" fdd| D S  |S )Nc                 3   s    | ]}  |V  qd S r   literalr5   argr   r   r   r8   k   s    z&Cursor._escape_args.<locals>.<genexpr>c                    s   i | ]
\}}|  |qS r   r@   )r5   keyvalrD   r   r   
<dictcomp>m   s    z'Cursor._escape_args.<locals>.<dictcomp>)r9   r<   r=   dictitemsescape)r   r&   r   r   rD   r   _escape_argsi   s
   

zCursor._escape_argsc                 C   s$   |   }|dur|| || }|S )z
        Returns the exact string that is sent to the database by calling the
        execute() method.

        This method follows the extension to the DB API 2.0 followed by Psycopg.
        N)r   rK   )r   queryr&   r   r   r   r   mogrifys   s   zCursor.mogrifyc                 C   s2   |   r		 |   s| ||}| |}|| _|S )a  Execute a query

        :param str query: Query to execute.

        :param args: parameters used with query. (optional)
        :type args: tuple, list or dict

        :return: Number of affected rows
        :rtype: int

        If args is a list or tuple, %s can be used as a placeholder in the query.
        If args is a dict, %(name)s can be used as a placeholder in the query.
        )r   rM   _queryr   )r   rL   r&   resultr   r   r   execute   s   
zCursor.executec              	      s   |sdS t  }|r<|dd }|d }|dpd}|d dkr,|d	 d
ks.J ||||j jS t fdd|D _	j	S )a  Run several data against one 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   )c                 3   s    | ]	}  |V  qd S r   )rP   rB   rL   r   r   r   r8      s    z%Cursor.executemany.<locals>.<genexpr>)
RE_INSERT_VALUESmatchgrouprstrip_do_execute_manymax_stmt_lengthr   r3   sumr	   )r   rL   r&   mq_prefixq_values	q_postfixr   rV   r   executemany   s$   
	zCursor.executemanyc                 C   s  |   }| j}t|tr||}t|tr||}t|}	t|}||t|| }
t|
tr7|
|d}
|	|
7 }	d}|D ]:}|||| }
t|
trS|
|d}
t|	t|
 t| d |krq|| 	|	| 7 }t|}	n|	d7 }	|	|
7 }	q?|| 	|	| 7 }|| _
|S )Nsurrogateescaper   r      ,)r   rK   r9   r:   r;   	bytearrayiternextlenrP   r	   )r   prefixvaluespostfixr&   r\   r3   r   rJ   sqlr6   rowsrC   r   r   r   r[      s2   





 

zCursor._do_execute_manyr   c                    s   |    |r$d 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.
        z@_z_%d=%szSET %s,c                 3   s&    | ]\}}|  |f V  qd S r   )rJ   )r5   indexrC   )r   fmtr   r   r8      s    
z"Cursor.callproc.<locals>.<genexpr>zCALL %s(%s)c                    s   g | ]}d  |f qS )z@_%s_%dr   )r5   i)procnamer   r   
<listcomp>   s    z#Cursor.callproc.<locals>.<listcomp>)r   rN   join	enumerater   rangerh   r   )r   rr   r&   qr   )r   rp   rr   r   callproc   s$   
zCursor.callprocc                 C   sD   |    | jdu s| jt| jkrdS | j| j }|  jd7  _|S )zFetch the next rowNr   r   r   r   rh   r   rO   r   r   r   fetchone  s   zCursor.fetchonec                 C   sL   |    | jdu rdS | j|p| j }| j| j| }t|t| j| _|S )zFetch several rowsNr   )r   r   r   r
   minrh   )r   sizeendrO   r   r   r   	fetchmany  s   
zCursor.fetchmanyc                 C   sD   |    | jdu rdS | jr| j| jd }n| j}t| j| _|S )zFetch all the rowsNr   ry   rz   r   r   r   fetchall  s   
zCursor.fetchallrelativec                 C   sl   |    |dkr| j| }n|dkr|}ntd| d|  kr,t| jk s1td td|| _d S )Nr   absoluteunknown scroll mode %sr   zout of range)r   r   r   r   rh   r   
IndexError)r   valuemoderr   r   r   scroll%  s   
zCursor.scrollc                 C   s.   |   }|| _|   || |   | jS r   r   _last_executedr-   rL   r/   r	   r   rw   r   r   r   r   rN   2  s   
zCursor._queryc                 C   s(   d| _ d | _d| _d | _d | _d | _d S )Nr   )r   r   r	   r   	lastrowidr   r   r   r   r   r-   :  s   
zCursor._clear_resultc                 C   s8   |   }|j | _}|j| _|j| _|j| _|j| _d S r   )	r   r   affected_rowsr	   r   	insert_idr   rm   r   )r   r   rO   r   r   r   r/   C  s   zCursor._do_get_resultc                 C   s   t | jd S r   rf   r{   r   r   r   r   __iter__M     zCursor.__iter__)Fr   )r   r   )*__name__
__module____qualname____doc__r\   r   r   r   r   r   r   r"   r'   r)   r1   r   r4   rK   rM   rP   rb   r[   rx   r{   r   r   r   rN   r-   r/   r   r   WarningErrorInterfaceErrorDatabaseError	DataErrorOperationalErrorIntegrityErrorInternalErrorr   NotSupportedErrorr   r   r   r   r      sN    





!
/
	

	

r   c                       s(   e Zd ZeZ fddZdd Z  ZS )DictCursorMixinc                    s~   t t   g } jr) jjD ]}|j}||v r |jd | }|| q| _	|r; j
r= fdd j
D  _
d S d S d S )N.c                    s   g | ]}  |qS r   )r"   )r5   r   r   r   r   rs   l  s    z2DictCursorMixin._do_get_result.<locals>.<listcomp>)superr   r/   r   r   fieldsname
table_nameappend_fieldsr   )r   r   fr   	__class__r   r   r/   `  s   
zDictCursorMixin._do_get_resultc                 C   s   |d u rd S |  t| j|S r   )	dict_typezipr   r    r   r   r   r"   n  s   zDictCursorMixin._conv_row)r   r   r   rH   r   r/   r"   __classcell__r   r   r   r   r   \  s    r   c                   @      e Zd ZdZdS )
DictCursorz.A cursor which returns results as a dictionaryNr   r   r   r   r   r   r   r   r   t      r   c                   @   sp   e Zd ZdZdd Zdd Ze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dZdS )SSCursora  
    Unbuffered Cursor, mainly useful for queries that return a lot of data,
    or for connections to remote servers over a slow network.

    Instead of copying every row of data into a buffer, this will fetch
    rows as needed. The upside of this is the client uses much less memory,
    and rows are returned much faster when traveling over a slow network
    or if the result set is very big.

    There are limitations, though. The MySQL protocol doesn't support
    returning the total number of rows, so the only way to tell how many rows
    there are is to iterate over every row returned. Also, it currently isn't
    possible to scroll backwards, as only the current row is held in memory.
    c                 C   r   r   r   r    r   r   r   r"     r   zSSCursor._conv_rowc                 C   sf   | j }|d u r	d S | jd ur| j|ju r| j  z|  r)	 |  sW d | _ d S W d | _ d S d | _ w r   )r   r   _finish_unbuffered_queryr   r   r   r   r   r     s   


zSSCursor.closec                 C   s2   |   }|| _|   |j|dd |   | jS NTr*   r   r   r   r   r   rN     s   zSSCursor._queryc                 C   s   | j ddS r   r2   r   r   r   r   r     r   zSSCursor.nextsetc                 C   s   |  | j S )zRead next row)r"   r   _read_rowdata_packet_unbufferedr   r   r   r   	read_next  s   zSSCursor.read_nextc                 C   s.   |    |  }|du rdS |  jd7  _|S )zFetch next rowNr   )r   r   r   r    r   r   r   r{     s   zSSCursor.fetchonec                 C   s   t |  S )z
        Fetch all, as per MySQLdb. Pretty useless for large queries, as
        it is buffered. See fetchall_unbuffered(), if you want an unbuffered
        generator version of this method.
        )r=   fetchall_unbufferedr   r   r   r   r        zSSCursor.fetchallc                 C   s   t | jdS )z
        Fetch all, implemented as a generator, which isn't to standard,
        however, it doesn't make sense to return everything in a list, as that
        would use ridiculous memory for large result sets.
        Nr   r   r   r   r   r     r   zSSCursor.fetchall_unbufferedc                 C   s   |   S r   )r   r   r   r   r   r     s   zSSCursor.__iter__Nc                 C   sZ   |    |du r| j}g }t|D ]}|  }|du r |S || |  jd7  _q|S )z
Fetch manyNr   )r   r
   rv   r   r   r   )r   r}   rm   rq   r!   r   r   r   r     s   
zSSCursor.fetchmanyr   c                 C   s   |    |dkr%|dk rtdt|D ]}|   q|  j|7  _d S |dkrH|| jk r3td|| j }t|D ]}|   q<|| _d S td| )Nr   r   z0Backwards scrolling not supported by this cursorr   r   )r   r   r   rv   r   r   r   )r   r   r   _r~   r   r   r   r     s&   




zSSCursor.scrollr   r   )r   r   r   r   r"   r   __del__rN   r   r   r{   r   r   r   r   r   r   r   r   r   r   x  s    	
r   c                   @   r   )SSDictCursorz;An unbuffered cursor, which returns results as a dictionaryNr   r   r   r   r   r     r   r   )rerS   r   compile
IGNORECASEDOTALLrW   r   r   r   r   r   r   r   r   r   <module>   s    
  Nw