U Dx`<@s|ddlZddlmZedejejBZGdddZGdddZGd d d eeZ Gd d d eZ Gd ddee Z dS)N)errz|\s*((?:INSERT|REPLACE)\b.+\bVALUES?\s*)(\(\s*(?:%s|%\(.+\)s)\s*(?:,\s*(?:%s|%\(.+\)s)\s*)*\))(\s*(?:ON DUPLICATE.*)?);?\s*\Zc@s.eZdZdZdZddZddZddZd d Zd d Z d dZ ddZ ddZ ddZ d;ddZddZdd!d"Zd#d$Zd%d&Zd?d(d)Zd*d+Zd@d,d-Zd.d/ZdAd1d2Zd3d4Zd5d6Zd7d8Zd9d:Zej Z ej!Z!ej"Z"ej#Z#ej$Z$ej%Z%ej&Z&ej'Z'ej(Z(ej)Z)dS)BCursora  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-zr53vnty/lib/python/pymysql/cursors.py__init__!szCursor.__init__cCs.|j}|dkrdSz|rqW5d|_XdS)zD Closing a cursor just exhausts all remaining data. N)rnextsetrconnrrrclose+sz Cursor.closecCs|SNrrrrr __enter__8szCursor.__enter__cGs~|dSr)r)rexc_inforrr__exit__;szCursor.__exit__cCs|jstd|jS)Nz Cursor closed)rrProgrammingErrorrrrr_get_db?s zCursor._get_dbcCs|jstddS)Nzexecute() first)r rrrrrr_check_executedDszCursor._check_executedcCs|Srrrrowrrr _conv_rowHszCursor._conv_rowcGsdSz!Does nothing, required by DB API.Nrrargsrrr setinputsizesKszCursor.setinputsizescGsdSr!rr"rrrsetoutputsizesNszCursor.setoutputsizesFcCsT|}|j}|dks ||jk r$dS|js.dSd|_||j|d|dS)zGet the next query setN unbufferedT)rr Zhas_next _clear_resultZ next_result_do_get_result)rr'rZcurrent_resultrrr_nextsetQs zCursor._nextsetcCs |dS)NFr*rrrrr_szCursor.nextsetNcsDt|tr|}n*t|ttfr@t|fdd|D}|S)Nc3s|]}j|dVqdS))encodingN) _ensure_bytes).0vr,rrr fsz'Cursor._ensure_bytes..) isinstancestrencodetuplelisttype)rxr,rr0rr-bs   zCursor._ensure_bytescsRt|ttfr$tfdd|DSt|trDfdd|DS|SdS)Nc3s|]}|VqdSrliteralr.argrrrr1ksz&Cursor._escape_args..csi|]\}}||qSrr9)r.keyvalr=rr msz'Cursor._escape_args..)r2r5r6dictitemsescape)rr#rrr=r _escape_argsis  zCursor._escape_argscCs$|}|dk 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)rrD)rqueryr#rrrrmogrifysszCursor.mogrifycCs*|r q|||}||}||_|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. )rrF_queryr )rrEr#resultrrrexecutes   zCursor.executec s|sdSt}|rx|dd}|d}|dp>d}|ddkrX|d d ks\t||||jjSt 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(). Nrrr(r)c3s|]}|VqdSr)rIr;rErrrr1sz%Cursor.executemany..) RE_INSERT_VALUESmatchgrouprstripAssertionError_do_execute_manymax_stmt_lengthrr,sumr )rrEr#mZq_prefixZq_valuesZ q_postfixrrOr executemanys$   zCursor.executemanyc Cs|}|j}t|tr"||}t|tr6||}t|} t|}||t||} t| trn| |d} | | 7} d} |D]t} ||| |} t| tr| |d} t| t| t|d|kr| | | |7} t|} n| d7} | | 7} q~| | | |7} | |_ | S)Nsurrogateescaperr,) rrDr2r3r4 bytearrayiternextlenrIr ) rprefixvaluesZpostfixr#rVr,rrCZsqlr/rowsr<rrrrUs2           zCursor._do_execute_manyrcs||rHdd|ddfddt|D|ddfdd tt|Df}||||_|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$|]\}}||fVqdSr)rC)r.indexr<)rfmtrrr1sz"Cursor.callproc..z CALL %s(%s)csg|]}d|fqS)z@_%s_%dr)r.i)procnamerr sz#Cursor.callproc..)rrGjoin enumeraterranger_r )rrgr#qr)rrergrcallprocs$  zCursor.callproccCsD||jdks"|jt|jkr&dS|j|j}|jd7_|S)zFetch the next rowNrrr rr_rrHrrrfetchones  zCursor.fetchonecCsL||jdkrdS|j|p"|j}|j|j|}t|t|j|_|S)zFetch several rowsNr)rr rr minr_)rsizeendrHrrr fetchmanys zCursor.fetchmanycCsD||jdkrdS|jr.|j|jd}n|j}t|j|_|S)zFetch all the rowsNrrnrorrrfetchalls  zCursor.fetchallrelativecCsf||dkr|j|}n|dkr*|}ntd|d|krRt|jks\ntd||_dS)Nrvabsoluteunknown scroll mode %srz out of range)rrrrr_r IndexError)rvaluemoderrrrscroll%s z Cursor.scrollcCs.|}||_|||||jSrrZ_last_executedr(rEr)r rrlrrrrrG2s  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|_dSr) rr Z affected_rowsr rZ insert_idrrbr )rrrHrrrr)Cs  zCursor._do_get_resultcCs t|jdSrr]rprrrr__iter__MszCursor.__iter__)F)N)N)N)r)N)rv)*__name__ __module__ __qualname____doc__rVrrrrrrr r$r%r*rr-rDrFrIrYrUrmrprtrur}rGr(r)rrWarningErrorZInterfaceErrorZ DatabaseErrorZ DataErrorZOperationalErrorZIntegrityError InternalErrorrNotSupportedErrorrrrrrsL      ! /    rcs(eZdZeZfddZddZZS)DictCursorMixincsvttg}jrRjjD]*}|j}||kr@|jd|}||q |_ |rrj rrfddj D_ dS)N.csg|]}|qSr)r )r.r|rrrrhlsz2DictCursorMixin._do_get_result..) superrr)rr fieldsnameZ table_nameappend_fieldsr )rrfr __class__rrr)`s   zDictCursorMixin._do_get_resultcCs|dkr dS|t|j|Sr) dict_typeziprrrrrr nszDictCursorMixin._conv_row)rrrrArr)r __classcell__rrrrr\s rc@seZdZdZdS) DictCursorz.A cursor which returns results as a dictionaryNrrrrrrrrrtsrc@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|Srrrrrrr szSSCursor._conv_rowcCsN|j}|dkrdS|jdk r2|j|jkr2|jz|r>q4W5d|_XdSr)rr Z_finish_unbuffered_queryrrrrrrs zSSCursor.closecCs2|}||_||j|dd||jSNTr&r~rrrrrGs zSSCursor._querycCs |jddSrr+rrrrrszSSCursor.nextsetcCs||jS)z Read next row)r r Z_read_rowdata_packet_unbufferedrrrr read_nextszSSCursor.read_nextcCs.||}|dkrdS|jd7_|S)zFetch next rowNr)rrrrrrrrps zSSCursor.fetchonecCs 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. )r6fetchall_unbufferedrrrrruszSSCursor.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. NrrrrrrszSSCursor.fetchall_unbufferedcCs|Sr)rrrrrrszSSCursor.__iter__NcCsX||dkr|j}g}t|D]0}|}|dkr:qT|||jd7_q"|S)z Fetch manyNr)rr rkrrr)rrrrbrfrrrrrts  zSSCursor.fetchmanyrvcCs||dkrH|dkr"tdt|D] }|q*|j|7_nR|dkr||jkrdtd||j}t|D] }|qv||_ntd|dS)Nrvrz0Backwards scrolling not supported by this cursorrwrx)rrrrkrrr)rrzr{_rsrrrr}s&      zSSCursor.scroll)N)rv)rrrrr r__del__rGrrrprurrrtr}rrrrrxs  rc@seZdZdZdS) SSDictCursorz;An unbuffered cursor, which returns results as a dictionaryNrrrrrrsr) rerLrcompile IGNORECASEDOTALLrPrrrrrrrrrs  Nw