3 $c<@s|ddlZddlmZejdejejBZGdddZGd d d ZGd d d eeZ Gd ddeZ Gdddee Z dS)N)errz'\s*((?:INSERT|REPLACE)\b.+\bVALUES?\s*)z6(\(\s*(?:%s|%\(.+\)s)\s*(?:,\s*(?:%s|%\(.+\)s)\s*)*\))z(\s*(?:ON DUPLICATE.*)?);?\s*\Zc@s0eZdZdZdZddZddZddZd d Zd d Z d dZ ddZ ddZ ddZ d:ddZddZd;ddZddZdd+d,Zd-d.Zd?d0d1Zd2d3Zd4d5Zd6d7Zd8d9Zej Z ej!Z!ej"Z"ej#Z#ej$Z$ej%Z%ej&Z&ej'Z'ej(Z(ej)Z)dS)@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 `_ in the specification. icCs4||_d|_d|_d|_d|_d|_d|_d|_dS)Nrr) connection description rownumberrowcount arraysize _executed_result_rows)selfrr6/tmp/pip-target-ebskqzh7/lib/python/pymysql/cursors.py__init__!szCursor.__init__c Cs2|j}|dkrdSzx |jr qWWdd|_XdS)zD Closing a cursor just exhausts all remaining data. N)rnextset)rconnrrrclose+s z Cursor.closecCs|S)Nr)rrrr __enter__8szCursor.__enter__cGs~|jdS)N)r)rexc_inforrr__exit__;szCursor.__exit__cCs|jstjd|jS)Nz Cursor closed)rrProgrammingError)rrrr_get_db?s zCursor._get_dbcCs|jstjddS)Nzexecute() first)r rr)rrrr_check_executedDszCursor._check_executedcCs|S)Nr)rrowrrr _conv_rowHszCursor._conv_rowcGsdS)z!Does nothing, required by DB API.Nr)rargsrrr setinputsizesKszCursor.setinputsizescGsdS)z!Does nothing, required by DB API.Nr)rrrrrsetoutputsizesNszCursor.setoutputsizesFcCsT|j}|j}|dks ||jk r$dS|js.dSd|_|j|j|d|jdS)zGet the next query setN) unbufferedT)rr Zhas_next _clear_resultZ next_result_do_get_result)rr rZcurrent_resultrrr_nextsetQs zCursor._nextsetcCs |jdS)NF)r#)rrrrr_szCursor.nextsetNcsDt|tr|j}n*t|ttfr@t|fdd|D}|S)Nc3s|]}j|dVqdS))encodingN) _ensure_bytes).0v)r$rrr fsz'Cursor._ensure_bytes..) isinstancestrencodetuplelisttype)rxr$r)r$rrr%bs   zCursor._ensure_bytescsRt|ttfr$tfdd|DSt|trDfdd|jDSj|SdS)Nc3s|]}j|VqdS)N)literal)r&arg)rrrr(ksz&Cursor._escape_args..csi|]\}}j||qSr)r0)r&keyval)rrr msz'Cursor._escape_args..)r)r,r-dictitemsescape)rrrr)rr _escape_argsis  zCursor._escape_argscCs$|j}|dk r ||j||}|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)rr8)rqueryrrrrrmogrifysszCursor.mogrifycCs.x |jr qW|j||}|j|}||_|S)aExecute 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. )rr:_queryr )rr9rresultrrrexecutes    zCursor.executecs|sdStj}|rx|jdf}|jdj}|jdp>d}|ddkrX|d dks\tj||||jjjSt fd d |D_ j S) aRun 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(). Nrr()c3s|]}j|VqdS)N)r=)r&r1)r9rrrr(sz%Cursor.executemany..r) RE_INSERT_VALUESmatchgrouprstripAssertionError_do_execute_manymax_stmt_lengthrr$sumr )rr9rmZq_prefixZq_valuesZ q_postfixr)r9rr executemanys"   zCursor.executemanyc Cs|j}|j}t|tr"|j|}t|tr6|j|}t|} t|}||t||} t| trn| j|d} | | 7} d} x||D]t} ||| |} t| tr| j|d} t| t| t|d|kr| |j | |7} t|} n| d7} | | 7} qW| |j | |7} | |_ | S)Nsurrogateescaperr,) rr8r)r*r+ bytearrayiternextlenr=r ) rprefixvaluesZpostfixrrIr$rr7Zsqlr'rowsr1rrrrHs2            zCursor._do_execute_manycs|j|rHdd|jddjfddt|D|jddjfdd tt|Df}|j|||_|S) aExecute 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=%szSET %s,c3s$|]\}}|j|fVqdS)N)r7)r&indexr1)rfmtrrr(sz"Cursor.callproc..z CALL %s(%s)csg|]}d|fqS)z@_%s_%dr)r&i)procnamerr sz#Cursor.callproc..)rr;join enumeraterrangerRr )rrZrqr)rrXrZrcallprocs $ zCursor.callproccCsD|j|jdks"|jt|jkr&dS|j|j}|jd7_|S)zFetch the next rowNr)rr rrR)rr<rrrfetchones  zCursor.fetchonecCsL|j|jdkrfS|j|p"|j}|j|j|}t|t|j|_|S)zFetch several rowsN)rr rr minrR)rsizeendr<rrr fetchmanys zCursor.fetchmanycCsD|j|jdkrfS|jr.|j|jd}n|j}t|j|_|S)zFetch all the rowsN)rr rrR)rr<rrrfetchalls  zCursor.fetchallrelativecCsh|j|dkr|j|}n|dkr*|}ntjd|d|koPt|jkns^td||_dS)Nrgabsolutezunknown scroll mode %srz out of range)rrrrrRr IndexError)rvaluemoderrrrscroll%s z Cursor.scrollcCs.|j}||_|j|j||j|jS)N)r_last_executedr!r9r"r )rr_rrrrr;2s  z Cursor._querycCs(d|_d|_d|_d|_d|_d|_dS)Nr)rr r r lastrowidr )rrrrr!:s zCursor._clear_resultcCs8|j}|j|_}|j|_|j|_|j|_|j|_dS)N) rr Z affected_rowsr rZ insert_idrorUr )rrr<rrrr"Cs  zCursor._do_get_resultcCs t|jdS)N)rPra)rrrr__iter__MszCursor.__iter__)F)N)N)N)N)rg)*__name__ __module__ __qualname____doc__rIrrrrrrrrrr#rr%r8r:r=rLrHr`rarerfrmr;r!r"rprWarningErrorZInterfaceErrorZ DatabaseErrorZ DataErrorZOperationalErrorZIntegrityErrorZ InternalErrorrNotSupportedErrorrrrrrsL       ! /    rcs(eZdZeZfddZddZZS)DictCursorMixincszttjg}jrVx6jjD]*}|j}||krB|jd|}|j|q"W|_ |rvj rvfddj D_ dS)N.csg|]}j|qSr)r)r&rl)rrrr[lsz2DictCursorMixin._do_get_result..) superrxr"rr fieldsnameZ table_nameappend_fieldsr )rr{fr|) __class__)rrr"`s zDictCursorMixin._do_get_resultcCs|dkr dS|jt|j|S)N) dict_typezipr~)rrrrrrnszDictCursorMixin._conv_row)rqrrrsr5rr"r __classcell__rr)rrrx\s rxc@seZdZdZdS) DictCursorz.A cursor which returns results as a dictionaryN)rqrrrsrtrrrrrtsrc@speZdZdZddZddZeZddZdd Zd d Z d d Z ddZ ddZ ddZ dddZdddZdS)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. cCs|S)Nr)rrrrrrszSSCursor._conv_rowc CsR|j}|dkrdS|jdk r2|j|jkr2|jjzx |jr@q6WWdd|_XdS)N)rr Z_finish_unbuffered_queryr)rrrrrrs  zSSCursor.closecCs2|j}||_|j|j|dd|j|jS)NT)r )rrnr!r9r"r )rr_rrrrr;s zSSCursor._querycCs |jddS)NT)r )r#)rrrrrszSSCursor.nextsetcCs|j|jjS)z Read next row)rr Z_read_rowdata_packet_unbuffered)rrrr read_nextszSSCursor.read_nextcCs.|j|j}|dkrdS|jd7_|S)zFetch next rowNr)rrr)rrrrrras zSSCursor.fetchonecCs t|jS)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_unbuffered)rrrrrfszSSCursor.fetchallcCs t|jdS)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. N)rPra)rrrrrszSSCursor.fetchall_unbufferedcCs|jS)N)r)rrrrrpszSSCursor.__iter__NcCsZ|j|dkr|j}g}x:t|D].}|j}|dkr:P|j||jd7_q$W|S)z Fetch manyNr)rr r^rr}r)rrcrUrYrrrrres zSSCursor.fetchmanyrgcCs|j|dkrL|dkr"tjdxt|D] }|jq,W|j|7_nV|dkr||jkrhtjd||j}xt|D] }|jq|W||_ntjd|dS)Nrgrz0Backwards scrolling not supported by this cursorrhzunknown scroll mode %s)rrrwr^rrr)rrjrk_rdrrrrms"    zSSCursor.scroll)N)rg)rqrrrsrtrr__del__r;rrrarfrrprermrrrrrxs  rc@seZdZdZdS) SSDictCursorz;An unbuffered cursor, which returns results as a dictionaryN)rqrrrsrtrrrrrsrz]\s*((?:INSERT|REPLACE)\b.+\bVALUES?\s*)(\(\s*(?:%s|%\(.+\)s)\s*(?:,\s*(?:%s|%\(.+\)s)\s*)*\))z|\s*((?:INSERT|REPLACE)\b.+\bVALUES?\s*)(\(\s*(?:%s|%\(.+\)s)\s*(?:,\s*(?:%s|%\(.+\)s)\s*)*\))(\s*(?:ON DUPLICATE.*)?);?\s*\Z) rer@rcompile IGNORECASEDOTALLrCrrxrrrrrrrs Nw